def __command(self, channel, token, name, data): try: if name == "redirect": peer_id = fromJSONSequence(data)[0] _peer = self.peers.get(peer_id) if _peer is None: error = self.__makeErrorReport(errors.TCF_ERROR_UNKNOWN_PEER, "Unknown peer ID") channel.sendResult(token, toJSONSequence((error,))) return channel.sendResult(token, toJSONSequence((None,))) if isinstance(_peer, peer.LocalPeer): channel.sendEvent(protocol.getLocator(), "Hello", toJSONSequence((channel.getLocalServices(),))) return ChannelProxy(channel, _peer.openChannel()) elif name == "sync": channel.sendResult(token, None) elif name == "getPeers": arr = [] for p in self.peers.values(): arr.append(p.getAttributes()) channel.sendResult(token, toJSONSequence((None, arr))) else: channel.rejectCommand(token) except Exception as x: channel.terminate(x)
def __initServices(self): try: if self.proxy: return if self.state == STATE_CLOSED: return services.onChannelCreated(self, self.local_service_by_name) self.__makeServiceByClassMap(self.local_service_by_name, self.local_service_by_class) args = self.local_service_by_name.keys() self.sendEvent(protocol.getLocator(), "Hello", toJSONSequence((args,))) except IOError as x: self.terminate(x)
def updateAttributes(self, attrs): equ = True assert attrs.get(ATTR_ID) == self.rw_attrs.get(ATTR_ID) for key in self.rw_attrs.keys(): if self.rw_attrs.get(key) != attrs.get(key): equ = False break for key in attrs.keys(): if attrs.get(key) != self.rw_attrs.get(key): equ = False break timeVal = int(time.time() * 1000) if not equ: self.rw_attrs.clear() self.rw_attrs.update(attrs) for l in protocol.getLocator().getListeners(): try: l.peerChanged(self) except Exception as x: protocol.log("Unhandled exception in Locator listener", x) try: args = [self.rw_attrs] protocol.sendEvent(locator.NAME, "peerChanged", channel.toJSONSequence(args)) except IOError as x: protocol.log("Locator: failed to send 'peerChanged' event", x) self.last_heart_beat_time = timeVal elif self.last_heart_beat_time + locator.DATA_RETENTION_PERIOD / 4 \ < timeVal: for l in protocol.getLocator().getListeners(): try: l.peerHeartBeat(attrs.get(ATTR_ID)) except Exception as x: protocol.log("Unhandled exception in Locator listener", x) try: args = [self.rw_attrs.get(ATTR_ID)] protocol.sendEvent(locator.NAME, "peerHeartBeat", channel.toJSONSequence(args)) except IOError as x: protocol.log( "Locator: failed to send 'peerHeartBeat' event", x) self.last_heart_beat_time = timeVal
def sendPeerRemovedEvent(self): for l in protocol.getLocator().getListeners(): try: l.peerRemoved(self.rw_attrs.get(ATTR_ID)) except Exception as x: protocol.log("Unhandled exception in Locator listener", x) try: args = [self.rw_attrs.get(ATTR_ID)] protocol.sendEvent(locator.NAME, "peerRemoved", channel.toJSONSequence(args)) except IOError as x: protocol.log("Locator: failed to send 'peerRemoved' event", x)
def sendPeerAddedEvent(self): for l in protocol.getLocator().getListeners(): try: l.peerAdded(self) except Exception as x: protocol.log("Unhandled exception in Locator listener", x) try: args = [self.rw_attrs] protocol.sendEvent(locator.NAME, "peerAdded", channel.toJSONSequence(args)) except IOError as x: protocol.log("Locator: failed to send 'peerAdded' event", x) self.last_heart_beat_time = int(time.time() * 1000)
def __init__(self, channel, service, command, args): if isinstance(service, services.Service): service = service.getName() self.service = service self.command = command self.args = args t = None try: # TODO zero_copy #zero_copy = channel.isZeroCopySupported() t = channel.sendCommand(service, command, toJSONSequence(args), self) except Exception as y: t = Token() protocol.invokeLater(self._error, y) self.token = t
def setProxy(self, proxy, services): self.proxy = proxy self.sendEvent(protocol.getLocator(), "Hello", toJSONSequence((services,))) self.local_service_by_class.clear() self.local_service_by_name.clear()