SignalReceiver now chooses a random port to listen on.

This commit is contained in:
Andrew Resch 2008-01-20 23:47:57 +00:00
parent 80514ad829
commit bcd2bcd2d4
2 changed files with 18 additions and 10 deletions

View File

@ -44,7 +44,7 @@ class Signals(component.Component):
remote = False remote = False
if not client.is_localhost(): if not client.is_localhost():
remote = True remote = True
self.receiver = SignalReceiver(6667, remote) self.receiver = SignalReceiver(remote)
self.receiver.start() self.receiver.start()
self.receiver.connect_to_signal("torrent_added", self.receiver.connect_to_signal("torrent_added",
self.torrent_added_signal) self.torrent_added_signal)

View File

@ -33,6 +33,7 @@
import sys import sys
import socket import socket
import random
import gobject import gobject
@ -49,14 +50,13 @@ class SignalReceiver(
ThreadingMixIn, ThreadingMixIn,
SimpleXMLRPCServer.SimpleXMLRPCServer): SimpleXMLRPCServer.SimpleXMLRPCServer):
def __init__(self, port, remote=False): def __init__(self, remote=False):
log.debug("SignalReceiver init..") log.debug("SignalReceiver init..")
gobject.threads_init() gobject.threads_init()
threading.Thread.__init__(self) threading.Thread.__init__(self)
# Set to true so that the receiver thread will exit # Set to true so that the receiver thread will exit
self._shutdown = False self._shutdown = False
self.port = port
# Daemonize the thread so it exits when the main program does # Daemonize the thread so it exits when the main program does
self.setDaemon(True) self.setDaemon(True)
@ -66,12 +66,20 @@ class SignalReceiver(
host = "" host = ""
# Setup the xmlrpc server # Setup the xmlrpc server
server_ready = False
while not server_ready:
port = random.randint(40000, 65535)
try: try:
SimpleXMLRPCServer.SimpleXMLRPCServer.__init__( SimpleXMLRPCServer.SimpleXMLRPCServer.__init__(
self, (host, port), logRequests=False, allow_none=True) self, (host, port), logRequests=False, allow_none=True)
except socket.error, e:
log.debug("Trying again with another port: %s", e)
except: except:
log.info("SignalReceiver already running or port not available..") log.error("Could not start SignalReceiver XMLRPC server: %s", e)
sys.exit(0) sys.exit(0)
else:
self.port = port
server_ready = True
self.signals = {} self.signals = {}
@ -79,7 +87,7 @@ class SignalReceiver(
self.register_function(self.emit_signal) self.register_function(self.emit_signal)
# Register the signal receiver with the core # Register the signal receiver with the core
client.register_client(str(port)) client.register_client(str(self.port))
def shutdown(self): def shutdown(self):
"""Shutdowns receiver thread""" """Shutdowns receiver thread"""