Modify SignalReceiver to only start the XMLRPC server once the thread is

started.
Allow components to directly connect to signals in Signals.
This commit is contained in:
Andrew Resch 2008-02-17 18:56:39 +00:00
parent 2eec78f9fb
commit 89bea0a40f
2 changed files with 45 additions and 34 deletions

View File

@ -39,12 +39,12 @@ from deluge.log import LOG as log
class Signals(component.Component):
def __init__(self):
component.Component.__init__(self, "Signals")
self.receiver = SignalReceiver()
def start(self):
remote = False
if not client.is_localhost():
remote = True
self.receiver = SignalReceiver(remote)
self.receiver.set_remote(True)
self.receiver.start()
self.receiver.connect_to_signal("torrent_added",
self.torrent_added_signal)
@ -66,6 +66,10 @@ class Signals(component.Component):
except:
pass
def connect_to_signal(self, signal, callback):
"""Connects a callback to a signal"""
self.receiver.connect_to_signal(signal, callback)
def torrent_added_signal(self, torrent_id):
log.debug("torrent_added signal received..")
log.debug("torrent id: %s", torrent_id)

View File

@ -50,7 +50,7 @@ class SignalReceiver(
ThreadingMixIn,
SimpleXMLRPCServer.SimpleXMLRPCServer):
def __init__(self, remote=False):
def __init__(self):
log.debug("SignalReceiver init..")
gobject.threads_init()
threading.Thread.__init__(self)
@ -61,33 +61,10 @@ class SignalReceiver(
# Daemonize the thread so it exits when the main program does
self.setDaemon(True)
host = "localhost"
if remote == True:
host = ""
# Setup the xmlrpc server
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 = {}
# Register the emit_signal function
self.register_function(self.emit_signal)
self.remote = False
# Register the signal receiver with the core
client.register_client(str(self.port))
def shutdown(self):
"""Shutdowns receiver thread"""
@ -106,8 +83,37 @@ class SignalReceiver(
# We don't care about errors at this point
pass
def set_remote(self, remote):
self.remote = remote
def run(self):
"""This gets called when we start the thread"""
host = "localhost"
if self.remote == True:
host = ""
# Setup the xmlrpc server
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
# Register the emit_signal function
self.register_function(self.emit_signal)
# Register the signal receiver with the core
client.register_client(str(self.port))
t = threading.Thread(target=self.handle_thread)
t.start()
@ -140,7 +146,8 @@ class SignalReceiver(
def connect_to_signal(self, signal, callback):
"""Connect to a signal"""
try:
self.signals[signal].append(callback)
if callback not in self.signals[signal]:
self.signals[signal].append(callback)
except KeyError:
self.signals[signal] = [callback]