def manage(self, peer_id, sock): if peer_id not in self.managed_sockets: self.managed_sockets[peer_id] = sock self.peers_by_socket[sock] = peer_id self.notify_peer(peer_id) if self.status_cb: cbthread = QuittableThread(target=self.status_cb, args=(peer_id, "manage")) cbthread.start()
def start(self): from mfp import log import subprocess arglist = [self.exec_file] + self.exec_args log.debug("RPCExecRemote: starting as ", arglist) self.process = subprocess.Popen([str(a) for a in arglist], bufsize=0, stderr=subprocess.STDOUT, stdout=subprocess.PIPE) QuittableThread.start(self)
def start(self): from mfp import log import subprocess arglist = [self.exec_file] + self.exec_args log.debug("RPCExecRemote: starting as ", arglist) self.process = subprocess.Popen([str(a) for a in arglist], bufsize=0, stderr=subprocess.STDOUT, stdout=subprocess.PIPE) QuittableThread.start(self)
def unmanage(self, peer_id): if peer_id in self.managed_sockets: # remove this peer as a publisher for any classes for clsname, cls in RPCWrapper.rpctype.items(): if peer_id in cls.publishers: cls.publishers.remove(peer_id) oldsock = self.managed_sockets[peer_id] del self.managed_sockets[peer_id] del self.peers_by_socket[oldsock] if oldsock.fileno() in self.fdsockets: del self.fdsockets[oldsock.fileno()] if self.status_cb: cbthread = QuittableThread(target=self.status_cb, args=(peer_id, "unmanage")) cbthread.start()
import traceback einfo = ("Method call failed rpcid=%s node=%s\nobj=%s data=%s\n" % (rpcid, peer_id, obj, rpcdata)) req.result = (RPCWrapper.METHOD_FAILED, einfo + traceback.format_exc()) elif method == 'publish': for clsname in req.params.get("classes"): cls = RPCWrapper.rpctype.get(clsname) if cls is not None: cls.publishers.append(peer_id) if self.status_cb: cbthread = QuittableThread(target=self.status_cb, args=(peer_id, "publish", req.params.get("classes"))) cbthread.start() req.result = (True, None) elif method == "ready": req.result = (True, peer_id) elif method == "exit_request": if not self.join_req: self.finish() req.request_id = None elif method == "exit_notify": self.unmanage(peer_id) # FIXME: exit_notify should cause patches to be closed req.request_id = None
def handle_request(self, req, peer_id): from datetime import datetime method = req.method rpcdata = req.params rpcid = rpcdata.get('rpcid') args = rpcdata.get('args') or [] kwargs = rpcdata.get('kwargs') or {} req.state = Request.RESPONSE_DONE req.diagnostic['local_call_started'] = str(datetime.now()) if method == 'create': factory = RPCWrapper.rpctype.get(rpcdata.get('type')) if factory: obj = factory(*args, **kwargs) req.result = (True, obj.rpcid) else: req.result = (RPCWrapper.NO_CLASS, None) elif method == 'delete': del RPCWrapper.objects[rpcid] req.result = (True, None) elif method == 'call': obj = RPCWrapper.rpcobj.get(rpcid) try: retval = obj.call_locally(rpcdata) req.result = (RPCWrapper.METHOD_OK, retval) except RPCWrapper.MethodNotFound as e: req.result = (RPCWrapper.NO_METHOD, None) except RPCWrapper.MethodFailed as e: req.result = (RPCWrapper.METHOD_FAILED, e.traceback) except Exception as e: import traceback einfo = ("Method call failed rpcid=%s node=%s\nobj=%s data=%s\n" % (rpcid, peer_id, obj, rpcdata)) req.result = (RPCWrapper.METHOD_FAILED, einfo + traceback.format_exc()) elif method == 'publish': for clsname in req.params.get("classes"): cls = RPCWrapper.rpctype.get(clsname) if cls is not None: cls.publishers.append(peer_id) if self.status_cb: cbthread = QuittableThread(target=self.status_cb, args=(peer_id, "publish", req.params.get("classes"), req.params.get("pubdata"))) cbthread.start() req.result = (True, None) elif method == "ready": req.result = (True, peer_id) elif method == "exit_request": if not self.join_req: self.finish() req.request_id = None elif method == "exit_notify": self.unmanage(peer_id) # FIXME: exit_notify should cause patches to be closed req.request_id = None elif method == "node_status": pass else: print("rpc_wrapper: WARNING: no handler for method '%s'" % method) print("call data:", rpcid, method, rpcdata) req.method = None req.params = None req.diagnostic['local_call_complete'] = str(datetime.now())
def handle_request(self, req, peer_id): from datetime import datetime method = req.method rpcdata = req.params rpcid = rpcdata.get('rpcid') args = rpcdata.get('args') or [] kwargs = rpcdata.get('kwargs') or {} req.state = Request.RESPONSE_DONE req.diagnostic['local_call_started'] = str(datetime.now()) if method == 'create': factory = RPCWrapper.rpctype.get(rpcdata.get('type')) if factory: obj = factory(*args, **kwargs) req.result = (True, obj.rpcid) else: req.result = (RPCWrapper.NO_CLASS, None) elif method == 'delete': del RPCWrapper.objects[rpcid] req.result = (True, None) elif method == 'call': obj = RPCWrapper.rpcobj.get(rpcid) try: retval = obj.call_locally(rpcdata) req.result = (RPCWrapper.METHOD_OK, retval) except RPCWrapper.MethodNotFound as e: req.result = (RPCWrapper.NO_METHOD, None) except RPCWrapper.MethodFailed as e: req.result = (RPCWrapper.METHOD_FAILED, e.traceback) except Exception as e: import traceback einfo = ( "Method call failed rpcid=%s node=%s\nobj=%s data=%s\n" % (rpcid, peer_id, obj, rpcdata)) req.result = (RPCWrapper.METHOD_FAILED, einfo + traceback.format_exc()) elif method == 'publish': for clsname in req.params.get("classes"): cls = RPCWrapper.rpctype.get(clsname) if cls is not None: cls.publishers.append(peer_id) if self.status_cb: cbthread = QuittableThread(target=self.status_cb, args=(peer_id, "publish", req.params.get("classes"), req.params.get("pubdata"))) cbthread.start() req.result = (True, None) elif method == "ready": req.result = (True, peer_id) elif method == "exit_request": if not self.join_req: self.finish() req.request_id = None elif method == "exit_notify": self.unmanage(peer_id) # FIXME: exit_notify should cause patches to be closed req.request_id = None elif method == "node_status": pass else: print("rpc_wrapper: WARNING: no handler for method '%s'" % method) print("call data:", rpcid, method, rpcdata) req.method = None req.params = None req.diagnostic['local_call_complete'] = str(datetime.now())