From bcd2bcd2d45f1e695dfb1451552a52c806f52434 Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Sun, 20 Jan 2008 23:47:57 +0000 Subject: [PATCH] SignalReceiver now chooses a random port to listen on. --- deluge/ui/gtkui/signals.py | 2 +- deluge/ui/signalreceiver.py | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/deluge/ui/gtkui/signals.py b/deluge/ui/gtkui/signals.py index 286c93d27..ac7037f79 100644 --- a/deluge/ui/gtkui/signals.py +++ b/deluge/ui/gtkui/signals.py @@ -44,7 +44,7 @@ class Signals(component.Component): remote = False if not client.is_localhost(): remote = True - self.receiver = SignalReceiver(6667, remote) + self.receiver = SignalReceiver(remote) self.receiver.start() self.receiver.connect_to_signal("torrent_added", self.torrent_added_signal) diff --git a/deluge/ui/signalreceiver.py b/deluge/ui/signalreceiver.py index c3c600059..6486189c4 100644 --- a/deluge/ui/signalreceiver.py +++ b/deluge/ui/signalreceiver.py @@ -33,6 +33,7 @@ import sys import socket +import random import gobject @@ -49,14 +50,13 @@ class SignalReceiver( ThreadingMixIn, SimpleXMLRPCServer.SimpleXMLRPCServer): - def __init__(self, port, remote=False): + def __init__(self, remote=False): log.debug("SignalReceiver init..") gobject.threads_init() threading.Thread.__init__(self) # Set to true so that the receiver thread will exit self._shutdown = False - self.port = port # Daemonize the thread so it exits when the main program does self.setDaemon(True) @@ -66,12 +66,20 @@ class SignalReceiver( host = "" # Setup the xmlrpc server - try: - SimpleXMLRPCServer.SimpleXMLRPCServer.__init__( - self, (host, port), logRequests=False, allow_none=True) - except: - log.info("SignalReceiver already running or port not available..") - sys.exit(0) + server_ready = False + while not server_ready: + port = random.randint(40000, 65535) + try: + SimpleXMLRPCServer.SimpleXMLRPCServer.__init__( + self, (host, port), logRequests=False, allow_none=True) + except socket.error, e: + log.debug("Trying again with another port: %s", e) + except: + log.error("Could not start SignalReceiver XMLRPC server: %s", e) + sys.exit(0) + else: + self.port = port + server_ready = True self.signals = {} @@ -79,7 +87,7 @@ class SignalReceiver( self.register_function(self.emit_signal) # Register the signal receiver with the core - client.register_client(str(port)) + client.register_client(str(self.port)) def shutdown(self): """Shutdowns receiver thread"""