Fixed and enabled the SignalReceiver.

This commit is contained in:
Andrew Resch 2007-10-21 01:53:40 +00:00
parent c4d4e75667
commit 298fd1bc7f
5 changed files with 58 additions and 31 deletions

View File

@ -37,20 +37,20 @@ from deluge.log import LOG as log
class SignalManager:
def __init__(self):
self.clients = []
self.clients = {}
def deregister_client(self, uri):
"""Deregisters a client"""
log.debug("Deregistering %s as a signal reciever..", uri)
self.clients.remove(self.clients.index(uri))
del self.clients[uri]
def register_client(self, uri):
"""Registers a client to emit signals to."""
log.debug("Registering %s as a signal reciever..", uri)
self.clients.append(xmlrpclib.ServerProxy(uri))
self.clients[uri] = xmlrpclib.ServerProxy(uri)
def emit(self, signal, data):
for client in self.clients:
for client in self.clients.values():
try:
client.emit_signal(signal, data)
except:

View File

@ -62,14 +62,17 @@ class CoreProxy:
def set_core_uri(self, uri):
log.info("Setting core uri as %s", uri)
self._uri = uri
if self._uri == None:
if uri == None:
for callback in self._on_no_core_callbacks:
callback()
self._uri = None
self._core = None
else:
# Get a new core
self.get_core()
return
self._uri = uri
# Get a new core
self.get_core()
def get_core_uri(self):
"""Returns the URI of the core currently being used."""

View File

@ -115,7 +115,7 @@ class GtkUI:
self.connectionmanager = ConnectionManager()
# Start the signal receiver
#self.signal_receiver = Signals(self)
self.signal_receiver = Signals()
# Initalize the plugins
self.plugins = PluginManager(self)
@ -141,6 +141,6 @@ class GtkUI:
del self.torrentview
del self.torrentdetails
del self.preferences
# del self.signal_receiver
del self.signal_receiver
del self.plugins
del deluge.configmanager

View File

@ -31,13 +31,16 @@
# this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here.
import deluge.ui.component as component
from deluge.ui.signalreceiver import SignalReceiver
from deluge.log import LOG as log
class Signals:
def __init__(self, ui):
self.ui = ui
self.receiver = SignalReceiver(6667, "http://localhost:56684")
class Signals(component.Component):
def __init__(self):
component.Component.__init__(self, "Signals")
def start(self):
self.receiver = SignalReceiver(6667)
self.receiver.start()
self.receiver.connect_to_signal("torrent_added",
self.torrent_added_signal)
@ -50,31 +53,34 @@ class Signals:
self.receiver.connect_to_signal("torrent_all_resumed",
self.torrent_all_resumed)
def stop(self):
self.receiver.shutdown()
def torrent_added_signal(self, torrent_id):
log.debug("torrent_added signal received..")
log.debug("torrent id: %s", torrent_id)
# Add the torrent to the treeview
self.ui.mainwindow.torrentview.add_row(torrent_id)
component.get("TorrentView").add_row(torrent_id)
def torrent_removed_signal(self, torrent_id):
log.debug("torrent_remove signal received..")
log.debug("torrent id: %s", torrent_id)
# Remove the torrent from the treeview
self.ui.mainwindow.torrentview.remove_row(torrent_id)
self.ui.mainwindow.torrentdetails.clear()
component.get("TorrentView").remove_row(torrent_id)
component.get("TorrentDetails").clear()
def torrent_paused(self, torrent_id):
log.debug("torrent_paused signal received..")
self.ui.mainwindow.torrentview.update()
component.get("TorrentView").update()
def torrent_resumed(self, torrent_id):
log.debug("torrent_resumed signal received..")
self.ui.mainwindow.torrentview.update()
component.get("TorrentView").update()
def torrent_all_paused(self):
log.debug("torrent_all_paused signal received..")
self.ui.mainwindow.torrentview.update()
component.get("TorrentView").update()
def torrent_all_resumed(self):
log.debug("torrent_all_resumed signal received..")
self.ui.mainwindow.torrentview.update()
component.get("TorrentView").update()

View File

@ -32,6 +32,7 @@
# statement from all source files in the program, then also delete it here.
import sys
import deluge.ui.client as client
import deluge.SimpleXMLRPCServer as SimpleXMLRPCServer
from SocketServer import ThreadingMixIn
import deluge.xmlrpclib as xmlrpclib
@ -44,10 +45,12 @@ class SignalReceiver(
ThreadingMixIn,
SimpleXMLRPCServer.SimpleXMLRPCServer):
def __init__(self, port, core_uri):
def __init__(self, port):
log.debug("SignalReceiver 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
@ -68,18 +71,33 @@ class SignalReceiver(
# Register the signal receiver with the core
# FIXME: send actual URI not localhost
core = xmlrpclib.ServerProxy(core_uri)
core = client.get_core()
core.register_client("http://localhost:" + str(port))
def shutdown(self):
"""Shutdowns receiver thread"""
self._shutdown = True
# De-register with the daemon so it doesn't try to send us more signals
client.get_core().deregister_client(
"http://localhost:" + str(self.port))
def __del__(self):
core.deregister_client("http://localhost:" + str(self.port))
# Hacky.. sends a request to our local receiver to ensure that it
# shutdowns.. This is because handle_request() is a blocking call.
receiver = xmlrpclib.ServerProxy("http://localhost:" + str(self.port),
allow_none=True)
receiver.emit_signal("shutdown", None)
def run(self):
"""This gets called when we start the thread"""
t = threading.Thread(target=self.serve_forever)
t = threading.Thread(target=self.handle_thread)
t.start()
def handle_thread(self):
while not self._shutdown:
self.handle_request()
self._shutdown = False
self.server_close()
def emit_signal(self, signal, data):
"""Exported method used by the core to emit a signal to the client"""
log.debug("Received signal %s with data %s from core..", signal, data)