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 __init__(self, service, name, data): self.service = service self.name = name try: self.args = channel.fromJSONSequence(data) except Exception as x: protocol.log("Error decoding event data", x)
def event(self, name, data): try: args = channel.fromJSONSequence(data) if name == "contextSuspended": assert len(args) == 4 self.listener.contextSuspended(args[0], args[1], args[2], args[3]) elif name == "contextResumed": assert len(args) == 1 self.listener.contextResumed(args[0]) elif name == "contextAdded": assert len(args) == 1 self.listener.contextAdded(_toContextArray(self.service,args[0])) elif name == "contextChanged": assert len(args) == 1 self.listener.contextChanged(_toContextArray(self.service,args[0])) elif name == "contextRemoved": assert len(args) == 1 self.listener.contextRemoved(args[0]) elif name == "contextException": assert len(args) == 2 self.listener.contextException(args[0], args[1]) elif name == "containerSuspended": assert len(args) == 5 self.listener.containerSuspended(args[0], args[1], args[2], args[3], args[4]) elif name == "containerResumed": assert len(args) == 1 self.listener.containerResumed(args[0]) else: raise IOError("RunControl service: unknown event: " + name); except Exception as x: self.service.channel.terminate(x)
def event(self, name, data): try: args = channel.fromJSONSequence(data) if name == "changed": assert len(args) == 1 self.listener.changed(args[0]) else: raise IOError("MemoryMap service: unknown event: " + name); except Exception as x: self.service.channel.terminate(x)
def event(self, name, data): try: args = channel.fromJSONSequence(data) if name == "exited": assert len(args) == 2 self.listener.exited(args[0], args[1]) else: raise IOError("Processes service: unknown event: " + name); except Exception as x: self.service.channel.terminate(x)
def result(self, token, data): assert self.token is token error = None args = None try: args = fromJSONSequence(data) except Exception as e: error = e assert not self.__done self.__done = True self.done(error, args)
def event(self, name, data): try: args = channel.fromJSONSequence(data) if name == "peerAdded": assert len(args) == 1 _peer = Peer(self.channel.getRemotePeer(), args[0]) if self.proxy.peers.get(_peer.getID()): protocol.log("Invalid peerAdded event", Exception()) return self.proxy.peers[_peer.getID()] = _peer for l in self.proxy.listeners: try: l.peerAdded(_peer) except Exception as x: protocol.log("Unhandled exception in Locator listener", x) elif name == "peerChanged": assert len(args) == 1 m = args[0] if not m: raise Exception("Locator service: invalid peerChanged event - no peer ID") _peer = self.proxy.peers.get(m.get(peer.ATTR_ID)) if not _peer: return self.proxy.peers[_peer.getID()] = _peer for l in self.proxy.listeners: try: l.peerChanged(_peer) except Exception as x: protocol.log("Unhandled exception in Locator listener", x) elif name == "peerRemoved": assert len(args) == 1 id = args[0] _peer = self.proxy.peers.get(id) if not _peer: return del self.proxy.peers[id] for l in self.proxy.listeners: try: l.peerRemoved(id) except Exception as x: protocol.log("Unhandled exception in Locator listener", x) elif name == "peerHeartBeat": assert len(args) == 1 id = args[0] _peer = self.proxy.peers.get(id) if not _peer: return for l in self.proxy.listeners: try: l.peerHeartBeat(id) except Exception as x: protocol.log("Unhandled exception in Locator listener", x) else: raise IOError("Locator service: unknown event: " + name) except Exception as x: self.channel.terminate(x)
def handleEOS(self): if not self.channel.out_tokens and not self.eos_err_report: self.channel.close() else: x = IOError("Communication channel is closed by remote peer") if self.eos_err_report: try: args = fromJSONSequence(self.eos_err_report) if len(args) > 0 and args[0] is not None: x.caused_by = Exception(errors.toErrorString(args[0])) except IOError: pass self.channel.terminate(x)
def event(self, name, data): try: args = channel.fromJSONSequence(data) if name == "created": if len(args) == 3: self.listener.created(args[0], args[1], args[2]) else: assert len(args) == 2 self.listener.created(args[0], args[1], None) elif name == "disposed": assert len(args) == 2 self.listener.disposed(args[0], args[1]) else: raise IOError("Streams service: unknown event: " + name); except Exception as x: self.service.channel.terminate(x)
def event(self, name, data): try: args = channel.fromJSONSequence(data) if name == "status": assert len(args) == 2 self.listener.breakpointStatusChanged(args[0], args[1]) elif name == "contextAdded": assert len(args) == 1 self.listener.contextAdded(args[0]) elif name == "contextChanged": assert len(args) == 1 self.listener.contextChanged(args[0]) elif name == "contextRemoved": assert len(args) == 1 self.listener.contextRemoved(args[0]) else: raise IOError("Breakpoints service: unknown event: " + name); except Exception as x: self.service.channel.terminate(x)
def event(self, name, data): try: args = channel.fromJSONSequence(data) if name == "contextAdded": assert len(args) == 1 self.listener.contextAdded(_toContextArray(args[0])) elif name == "contextChanged": assert len(args) == 1 self.listener.contextChanged(_toContextArray(args[0])) elif name == "contextRemoved": assert len(args) == 1 self.listener.contextRemoved(args[0]) elif name == "memoryChanged": assert len(args) == 2 self.listener.memoryChanged(args[0], _toAddrArray(args[1]), _toSizeArray(args[1])) else: raise IOError("Memory service: unknown event: " + name); except Exception as x: self.service.channel.terminate(x)
def event(self, name, data): try: args = channel.fromJSONSequence(data) self._callable(self.svc_name, name, *args) except Exception as x: protocol.log("Error decoding event data", x)
def __handleInput(self, msg): assert protocol.isDispatchThread() if self.state == STATE_CLOSED: return if self.trace_listeners: self.__traceMessageReceived(msg) try: token = None typeCode = msg.type if typeCode in 'PRN': token_id = msg.token.getID() cmd = self.out_tokens.get(token_id) if cmd is None: raise Exception("Invalid token received: " + token_id) if typeCode != 'P': del self.out_tokens[token_id] token = cmd.token if typeCode == 'C': if self.state == STATE_OPENING: raise IOError("Received command " + msg.service + "." + msg.name + " before Hello message") if self.proxy: self.proxy.onCommand(msg.token, msg.service, msg.name, msg.data) else: token = msg.token cmds = self.command_servers.get(msg.service) if cmds: cmds.command(token, msg.name, msg.data) else: self.rejectCommand(token) elif typeCode == 'P': token.getListener().progress(token, msg.data) self.__sendCongestionLevel() elif typeCode == 'R': token.getListener().result(token, msg.data) self.__sendCongestionLevel() elif typeCode == 'N': token.getListener().terminated(token, errors.ErrorReport( "Command is not recognized", errors.TCF_ERROR_INV_COMMAND)) elif typeCode == 'E': hello = msg.service == locator.NAME and msg.name == "Hello" if hello: self.remote_service_by_name.clear() self.remote_service_by_class.clear() data = fromJSONSequence(msg.data)[0] services.onChannelOpened(self, data, self.remote_service_by_name) self.__makeServiceByClassMap(self.remote_service_by_name, self.remote_service_by_class) self.zero_copy = "ZeroCopy" in self.remote_service_by_name if self.proxy and self.state == STATE_OPEN: self.proxy.onEvent(msg.service, msg.name, msg.data) elif hello: assert self.state == STATE_OPENING self.state = STATE_OPEN assert self.redirect_command is None if self.redirect_queue: self.redirect(self.redirect_queue.pop(0)) else: self.notifying_channel_opened = True if not self.registered_with_trasport: transport.channelOpened(self) self.registered_with_trasport = True for l in self.channel_listeners: if not l: break try: l.onChannelOpened() except Exception as x: protocol.log("Exception in channel listener", x) self.notifying_channel_opened = False else: list = self.event_listeners.get(msg.service) if list: for l in list: l.event(msg.name, msg.data) self.__sendCongestionLevel() elif typeCode == 'F': len = len(msg.data) if len > 0 and msg.data[len - 1] == '\0': len -= 1 self.remote_congestion_level = int(msg.data) else: assert False except Exception as x: x.tb = sys.exc_info()[2] self.terminate(x)