def initTorrentClient(self): self.handler = TorrentClient() self.addChildren(self.handler) self.link((self, "outbox"), (self.handler, "inbox")) self.link((self, "signal"), (self.handler, "control")) self.link((self.handler, "outbox"), (self, "inbox")) self.link((self.handler, "signal"), (self, "_torrentcontrol")) self.handler.activate()
def __init__(self, torrent_id: str, torrent_client: TorrentClient): super().__init__(torrent_id, torrent_client) if isinstance(torrent_id, str): copy = torrent_client.get_torrent_status(torrent_id) self.__dict__ = deepcopy(copy.__dict__) elif isinstance(torrent_id, Torrent): self._torrent = torrent_id self._torrent_client = torrent_client self._torrent_to_prop()
class TorrentService(AdaptiveCommsComponent): #Axon.AdaptiveCommsComponent.AdaptiveCommsComponent): # SmokeTests_Selector.test_SmokeTest """\ TorrentService() -> new TorrentService component Use TorrentService.getTorrentService(...) in preference as it returns an existing instance, or automatically creates a new one. """ Inboxes = { "control" : "Recieving a Axon.Ipc.shutdown() message here causes shutdown", "inbox" : "Connects to TorrentClient (the BitTorrent code)", "notify" : "Used to be notified about things to select", "_torrentcontrol" : "Notice that TorrentClient has shutdown", } Outboxes = { "signal" : "Not used", "outbox" : "Connects to TorrentClient (the BitTorrent code)" } def __init__(self): print "Torrent service init" super(TorrentService, self).__init__() self.outboxFor = {} self.torrentBelongsTo = {} self.pendingAdd = [] self.initTorrentClient() self.myclients = 0 def initTorrentClient(self): self.handler = TorrentClient() self.addChildren(self.handler) self.link((self, "outbox"), (self.handler, "inbox")) self.link((self, "signal"), (self.handler, "control")) self.link((self.handler, "outbox"), (self, "inbox")) self.link((self.handler, "signal"), (self, "_torrentcontrol")) self.handler.activate() def addClient(self, replyService): """Registers a TorrentPatron with this service, creating an outbox connected to it""" print "Adding client!" print replyService self.myclients += 1 particularOutbox = self.addOutbox("clientoutbox") self.link((self, particularOutbox), replyService) self.outboxFor[replyService] = particularOutbox; def removeClient(self, replyService): """Deregisters a TorrentPatron with this service, deleting its outbox""" self.myclients -= 1 particularOutbox = self.outboxFor[replyService] self.unlink((self, particularOutbox), replyService) #self.deleteOutbox(particularOutbox) WHY NOT def sendToClient(self, msg, replyService): """Send a message to a TorrentPatron""" self.send(msg, self.outboxFor[replyService]) def main(self): """Main loop""" while 1: #print "TorrentService main loop" yield 1 while self.dataReady("notify"): message = self.recv("notify") print "NOTIFY" print message if isinstance(message, TIPCServiceAdd): self.addClient(message.replyService) elif isinstance(message, TIPCServiceRemove): self.removeClient(message.replyService) elif isinstance(message, TIPCServicePassOn): replyService = message.replyService message = message.message #Requests to TorrentClient if isinstance(message, TIPCCreateNewTorrent) or isinstance(message, str): self.pendingAdd.append(replyService) self.send(message, "outbox") else: self.send(message, "outbox") while self.dataReady("inbox"): message = self.recv("inbox") #print "INBOX" #print message if isinstance(message, TIPCNewTorrentCreated): replyService = self.pendingAdd.pop(0) self.torrentBelongsTo[message.torrentid] = replyService self.sendToClient(message, replyService) elif isinstance(message, TIPCTorrentAlreadyDownloading) or isinstance(message, TIPCTorrentStartFail): replyService = self.pendingAdd.pop(0) self.sendToClient(message, replyService) elif isinstance(message, TIPCTorrentStatusUpdate): replyService = self.torrentBelongsTo[message.torrentid] self.sendToClient(message, replyService) else: print "Unknown message to TorrentService from TorrentClient!\n" print message while self.dataReady("control"): message = self.recv("control") print "CONTROL" print message if isinstance(message, shutdown): return if self.myclients == 0: self.send(shutdown(), "signal") torrentclientfinished = False while not torrentclientfinished: yield 1 while self.dataReady("_torrentcontrol"): msg = self.recv("_torrentcontrol") if isinstance(msg, producerFinished): torrentclientfinished = True yield 1 break self.pause() if not self.dataReady("notify"): TorrentService.endTorrentServices() else: #whoops we need to start again doh! initTorrentClient() self.pause() def endTorrentServices(tracker = None): if not tracker: tracker = cat.coordinatingassistanttracker.getcat() tracker.deRegisterService("torrentsrv") tracker.deRegisterService("torrentsrvshutdown") endTorrentServices = staticmethod(endTorrentServices) def setTorrentServices(torrentsrv, tracker = None): """\ Sets the given TorrentService as the service for the selected tracker or the default one. (static method) """ if not tracker: tracker = cat.coordinatingassistanttracker.getcat() tracker.registerService("torrentsrv", torrentsrv, "notify") tracker.registerService("torrentsrvshutdown", torrentsrv, "control") setTorrentServices = staticmethod(setTorrentServices) def getTorrentServices(tracker = None): # STATIC METHOD """\ Returns any live TorrentService registered with the specified (or default) tracker, or creates one for the system to use. (static method) """ if tracker is None: tracker = cat.coordinatingassistanttracker.getcat() try: service = tracker.retrieveService("torrentsrv") shutdownservice = tracker.retrieveService("torrentsrvshutdown") return service, shutdownservice, None except KeyError: torrentsrv = TorrentService() torrentsrv.setTorrentServices(torrentsrv, tracker) service = (torrentsrv, "notify") shutdownservice = (torrentsrv, "control") print "Gonna return" print (service, shutdownservice, torrentsrv) return service, shutdownservice, torrentsrv getTorrentServices = staticmethod(getTorrentServices)
class TorrentService( AdaptiveCommsComponent ): # Axon.AdaptiveCommsComponent.AdaptiveCommsComponent): # SmokeTests_Selector.test_SmokeTest """\ TorrentService() -> new TorrentService component Use TorrentService.getTorrentService(...) in preference as it returns an existing instance, or automatically creates a new one. """ Inboxes = { "control": "Recieving a Axon.Ipc.shutdown() message here causes shutdown", "inbox": "Connects to TorrentClient (the BitTorrent code)", "notify": "Used to be notified about things to select", "_torrentcontrol": "Notice that TorrentClient has shutdown", } Outboxes = {"signal": "Not used", "outbox": "Connects to TorrentClient (the BitTorrent code)"} def __init__(self): print "Torrent service init" super(TorrentService, self).__init__() self.outboxFor = {} self.torrentBelongsTo = {} self.pendingAdd = [] self.initTorrentClient() self.myclients = 0 def initTorrentClient(self): self.handler = TorrentClient() self.addChildren(self.handler) self.link((self, "outbox"), (self.handler, "inbox")) self.link((self, "signal"), (self.handler, "control")) self.link((self.handler, "outbox"), (self, "inbox")) self.link((self.handler, "signal"), (self, "_torrentcontrol")) self.handler.activate() def addClient(self, replyService): """Registers a TorrentPatron with this service, creating an outbox connected to it""" print "Adding client!" print replyService self.myclients += 1 particularOutbox = self.addOutbox("clientoutbox") self.link((self, particularOutbox), replyService) self.outboxFor[replyService] = particularOutbox def removeClient(self, replyService): """Deregisters a TorrentPatron with this service, deleting its outbox""" self.myclients -= 1 particularOutbox = self.outboxFor[replyService] self.unlink((self, particularOutbox), replyService) # self.deleteOutbox(particularOutbox) WHY NOT def sendToClient(self, msg, replyService): """Send a message to a TorrentPatron""" self.send(msg, self.outboxFor[replyService]) def main(self): """Main loop""" while 1: # print "TorrentService main loop" yield 1 while self.dataReady("notify"): message = self.recv("notify") print "NOTIFY" print message if isinstance(message, TIPCServiceAdd): self.addClient(message.replyService) elif isinstance(message, TIPCServiceRemove): self.removeClient(message.replyService) elif isinstance(message, TIPCServicePassOn): replyService = message.replyService message = message.message # Requests to TorrentClient if isinstance(message, TIPCCreateNewTorrent) or isinstance(message, str): self.pendingAdd.append(replyService) self.send(message, "outbox") else: self.send(message, "outbox") while self.dataReady("inbox"): message = self.recv("inbox") # print "INBOX" # print message if isinstance(message, TIPCNewTorrentCreated): replyService = self.pendingAdd.pop(0) self.torrentBelongsTo[message.torrentid] = replyService self.sendToClient(message, replyService) elif isinstance(message, TIPCTorrentAlreadyDownloading) or isinstance(message, TIPCTorrentStartFail): replyService = self.pendingAdd.pop(0) self.sendToClient(message, replyService) elif isinstance(message, TIPCTorrentStatusUpdate): replyService = self.torrentBelongsTo[message.torrentid] self.sendToClient(message, replyService) else: print "Unknown message to TorrentService from TorrentClient!\n" print message while self.dataReady("control"): message = self.recv("control") print "CONTROL" print message if isinstance(message, shutdown): return if self.myclients == 0: self.send(shutdown(), "signal") torrentclientfinished = False while not torrentclientfinished: yield 1 while self.dataReady("_torrentcontrol"): msg = self.recv("_torrentcontrol") if isinstance(msg, producerFinished): torrentclientfinished = True yield 1 break self.pause() if not self.dataReady("notify"): TorrentService.endTorrentServices() else: # whoops we need to start again doh! initTorrentClient() self.pause() def endTorrentServices(tracker=None): if not tracker: tracker = cat.coordinatingassistanttracker.getcat() tracker.deRegisterService("torrentsrv") tracker.deRegisterService("torrentsrvshutdown") endTorrentServices = staticmethod(endTorrentServices) def setTorrentServices(torrentsrv, tracker=None): """\ Sets the given TorrentService as the service for the selected tracker or the default one. (static method) """ if not tracker: tracker = cat.coordinatingassistanttracker.getcat() tracker.registerService("torrentsrv", torrentsrv, "notify") tracker.registerService("torrentsrvshutdown", torrentsrv, "control") setTorrentServices = staticmethod(setTorrentServices) def getTorrentServices(tracker=None): # STATIC METHOD """\ Returns any live TorrentService registered with the specified (or default) tracker, or creates one for the system to use. (static method) """ if tracker is None: tracker = cat.coordinatingassistanttracker.getcat() try: service = tracker.retrieveService("torrentsrv") shutdownservice = tracker.retrieveService("torrentsrvshutdown") return service, shutdownservice, None except KeyError: torrentsrv = TorrentService() torrentsrv.setTorrentServices(torrentsrv, tracker) service = (torrentsrv, "notify") shutdownservice = (torrentsrv, "control") print "Gonna return" print (service, shutdownservice, torrentsrv) return service, shutdownservice, torrentsrv getTorrentServices = staticmethod(getTorrentServices)