def worker(*, uid: int, endpoint: str): ctx = zmq.Context() dispatcher = RPCDispatcher() client = RPCClient( JSONRPCProtocol(), ZmqClientTransport.create(ctx, endpoint), ) proxy = client.get_proxy() @dispatcher.public def do_task(): from time import sleep for i in range(5): proxy.notify(f"{uid} y{i} ") sleep(0.1) return f"OK {uid}" @dispatcher.public def exit(): proxy.exit(uid) transport = ZmqServerTransport.create( ctx, endpoint.replace(".manager", f".{uid}")) rpc_server = RPCServer(transport, JSONRPCProtocol(), dispatcher) rpc_server.trace = partial(print, file=sys.stderr) rpc_server.serve_forever()
class Server(object): def __init__(self, req_callback): # print 'initializing Rpc' self.ctx = zmq.Context() self.dispatcher = RPCDispatcher() self.transport = ZmqServerTransport.create(self.ctx, 'tcp://127.0.0.1:8000') self.req_callback = req_callback self.rpc_server = RPCServer( self.transport, JSONRPCProtocol(), self.dispatcher ) self.dispatcher.public(self.request) # register this function (replacing the decorator) # print 'READYc: '+str(time.clock()) # sys.exit(0) self.rpc_server.serve_forever() # def start(self): # self.rpc_server.serve_forever() def request(self, req): return self.req_callback(req)
def worker(): rpc_server = RPCServer( SubprocessServerTransport( input_port=sys.stdin.buffer, output_port=sys.stdout.buffer ), JSONRPCProtocol(), dispatcher, ) rpc_server.trace = partial(print, file=sys.stderr) rpc_server.serve_forever()
def main(args): cmd = ["clef", "--stdio-ui"] if len(args) > 0 and args[0] == "test": cmd.extend(["--stdio-ui-test"]) print("cmd: {}".format(" ".join(cmd))) dispatcher = RPCDispatcher() dispatcher.register_instance(StdIOHandler(), '') # line buffered p = subprocess.Popen(cmd, bufsize=1, universal_newlines=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) rpc_server = RPCServer( PipeTransport(p.stdout, p.stdin), JSONRPCProtocol(), dispatcher ) rpc_server.serve_forever()
def main(args): cmd = ["./clef", "--stdio-ui"] if len(args) > 0 and args[0] == "test": cmd.extend(["--stdio-ui-test"]) print("cmd: {}".format(" ".join(cmd))) dispatcher = RPCDispatcher() dispatcher.register_instance(StdIOHandler(), '') # line buffered p = subprocess.Popen(cmd, bufsize=1, universal_newlines=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) rpc_server = RPCServer( PipeTransport(p.stdout, p.stdin), JSONRPCProtocol(), dispatcher ) rpc_server.serve_forever()
def network_server(statusQueue): cec_power_control=CecPowerControl(statusQueue) ctx = zmq.Context() dispatcher = RPCDispatcher() transport = ZmqServerTransport.create(ctx, SERVIER_ADDR) rpc_server = RPCServer( transport, JSONRPCProtocol(), dispatcher ) dispatcher.register_instance(cec_power_control, 'tv_controller.') logger.debug("Starting RPC Server") rpc_server.serve_forever()
def run(*, port: int = 8888, sentinel: str = "") -> None: if sentinel: p = pathlib.Path(sentinel) ok = False for wait_time in [0.1, 0.2, 0.2, 0.4, 0.8, 1.6, 3.2, 6.4]: if not p.exists(): print(f" wait ... {wait_time}", file=sys.stderr) time.sleep(wait_time) continue print(f"ack {sentinel}", file=sys.stderr) p.unlink() ok = True break if not ok: raise RuntimeError(f"timeout, f{sentinel}, {time.time()-st}") print(f"listen ... {port}", file=sys.stderr) transport = ZmqServerTransport.create(ctx, f"tcp://127.0.0.1:{port}") rpc_server = RPCServer(transport, JSONRPCProtocol(), dispatcher) rpc_server.serve_forever()
class Server(object): def __init__(self, req_callback): print 'initializing Rpc' self.ctx = zmq.Context() self.dispatcher = RPCDispatcher() self.transport = ZmqServerTransport.create(self.ctx, 'tcp://127.0.0.1:8000') self.req_callback = req_callback self.rpc_server = RPCServer( self.transport, JSONRPCProtocol(), self.dispatcher ) self.dispatcher.public(self.request) # register this function (replacing the decorator) self.rpc_server.serve_forever() # def start(self): # self.rpc_server.serve_forever() def request(self, req): return self.req_callback(req)
import json import zmq from tinyrpc.protocols.jsonrpc import JSONRPCProtocol from tinyrpc.transports.zmq import ZmqServerTransport from tinyrpc.server import RPCServer from tinyrpc.dispatch import RPCDispatcher ctx = zmq.Context() dispatcher = RPCDispatcher() transport = ZmqServerTransport.create(ctx, 'tcp://127.0.0.1:8000') rpc_server = RPCServer(transport, JSONRPCProtocol(), dispatcher) @dispatcher.public def request(s): # return s[::-1] rq = json.loads(s) print 'REQUEST: ' print rq['meta'] print rq['body'] if 'OFPFlowMod' in rq['body']: print rq['body']['OFPFlowMod'] return 'PERMIT' rpc_server.serve_forever()
parser = argparse.ArgumentParser(description="Device service for the Quad temperature controller unit") parser.add_argument("--device-port","-d",dest="serialPortName",type=str,default="COM5") parser.add_argument("--baud-rate","-b",dest="baudrate",type=int,default=9600) parser.add_argument("--service-port","-p",dest="servicePort",type=int,default=5060) parser.add_argument("--service-ip",dest="serviceIP",type=str,default="127.0.0.1") parser.add_argument("--fake",dest="fake",type=bool,default=False) args = parser.parse_args() system("Title "+ "%sDevice Service: Quad Temperature Controller" % ("" if not args.fake else "FAKE ")) qtc = QuadTemperatureController(args.serialPortName,args.baudrate,fake=args.fake) dispatcher = RPCDispatcher() dispatcher.register_instance(qtc) ctx = zmq.Context() endpoint = 'tcp://%s:%i' % (args.serviceIP,args.servicePort) transport = ZmqServerTransport.create(ctx, endpoint) print "serving requests at %s" % endpoint rpc_server = RPCServer( transport, JSONRPCProtocol(), dispatcher ) rpc_server.serve_forever()