class JSONRPCServer(BaseService): name = 'jsonrpc' def __init__(self, app): log.debug('initializing JSONRPCServer') BaseService.__init__(self, app) self.app = app self.dispatcher = RPCDispatcher() transport = WsgiServerTransport(queue_class=gevent.queue.Queue) # start wsgi server as a background-greenlet self.wsgi_server = gevent.wsgi.WSGIServer(('127.0.0.1', 5000), transport.handle) self.rpc_server = RPCServerGreenlets( transport, JSONRPCProtocol(), self.dispatcher ) def _run(self): log.info('starting JSONRPCServer') # in the main greenlet, run our rpc_server self.wsgi_thread = gevent.spawn(self.wsgi_server.serve_forever) self.rpc_server.serve_forever() def add_method(self, func, name=None): self.dispatcher.add_method(func, name) def stop(self): log.info('stopping JSONRPCServer') self.wsgi_thread.kill()
def __init__(self, ws, rpc_callback): dispatcher = RPCDispatcher() dispatcher.register_instance(rpc_callback) super(WebSocketRPCServer, self).__init__( WebSocketServerTransport(ws), JSONRPCProtocol(), dispatcher, )
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()
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)
def __init__(self, app): log.debug('initializing JSONRPCServer') BaseService.__init__(self, app) self.app = app self.dispatcher = RPCDispatcher() transport = WsgiServerTransport(queue_class=gevent.queue.Queue) # start wsgi server as a background-greenlet self.wsgi_server = gevent.wsgi.WSGIServer(('127.0.0.1', 5000), transport.handle) self.rpc_server = RPCServerGreenlets( transport, JSONRPCProtocol(), self.dispatcher )
if __name__=="__main__": 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 )
parser.add_argument("--device-address","-i",dest="deviceAddress",type=int,default=253) parser.add_argument("--baud-rate","-b",dest="baudrate",type=int,default=9600) parser.add_argument("--service-port","-p",dest="servicePort",type=int,default=5005) parser.add_argument("--service-ip",dest="serviceIP",type=str,default="127.0.0.1") args = parser.parse_args() serial = serial.Serial(port=portNameToInt(args.serialPortName), baudrate=args.baudrate, parity=serial.PARITY_NONE, timeout=0.1) pirani = KJLC925PiraniSensor(serial,deviceAddress=args.deviceAddress) dispatcher = RPCDispatcher() dispatcher.register_instance(pirani) 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 )
parser.add_argument("--device-port","-d",dest="serialPortName",type=str,default="COM1") parser.add_argument("--baud-rate","-b",dest="baudrate",type=int,default=9600) parser.add_argument("--service-port","-p",dest="servicePort",type=int,default=5050) parser.add_argument("--service-ip",dest="serviceIP",type=str,default="127.0.0.1") args = parser.parse_args() serial = serial.Serial(port=portNameToInt(args.serialPortName), baudrate=args.baudrate, parity=serial.PARITY_NONE, timeout=0.1) vfc = VFRackUnitController(serial) dispatcher = RPCDispatcher() dispatcher.register_instance(vfc) 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 )
from tinyrpc.dispatch import RPCDispatcher from tinyrpc.protocols.jsonrpc import JSONRPCProtocol from werkzeug.wrappers import Response, Request try: import config except ImportError: import default_config as config dispatcher = RPCDispatcher() protocol = JSONRPCProtocol() import api from api import api_methods, create_instruments create_instruments() for method in api_methods: dispatcher.add_method(getattr(api, method), method) def application(environ, start_response): request = Request(environ) access_control_headers = { 'Access-Control-Allow-Methods': 'POST', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'Content-Type, X-Requested-With, Accept, Origin' } if request.method == 'OPTIONS': response = Response(headers=access_control_headers) elif request.method == 'POST': # message is encoded in POST, read it... message = request.stream.read() request = protocol.parse_request(message) result = dispatcher.dispatch(request)
if __name__=="__main__": system("Title "+ "Device Service: Temperature Controller") parser = argparse.ArgumentParser(description="Temperature controller unit") parser.add_argument("--modbus-service",dest="modbusService",type=str) parser.add_argument("--device-id",dest="deviceId",type=str) parser.add_argument("--service-port","-p",dest="servicePort",type=int,default=5070) parser.add_argument("--service-ip",dest="serviceIP",type=str,default="127.0.0.1") args = parser.parse_args() ctx = zmq.Context() tcService = TemperatureControllerService(args.modbusService, args.deviceId, ctx) dispatcher = RPCDispatcher() dispatcher.register_instance(tcService) 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()
def dispatch(): return RPCDispatcher()
import os import threading import queue import minitask from handofcats import as_subcommand from minitask.executor.namedpipe import Executor from minitask.port import namedpipe from tinyrpc.dispatch import RPCDispatcher executor = Executor() dispatcher = RPCDispatcher() @dispatcher.public("add") def _add(x: int, y: int) -> int: return x + y @as_subcommand def run(): with executor: input_endpoint = executor.create_endpoint() output_endpoint = executor.create_endpoint() executor.spawn(worker, input_endpoint=input_endpoint, output_endpoint=output_endpoint) ipc = minitask.IPC(port=namedpipe) def notify(): with ipc.connect(output_endpoint) as x:
def server(*, endpoint: str, callback_endpoint: str): q = queue.Queue() ev = threading.Event() ctx = zmq.Context() dispatcher = RPCDispatcher() rpc_server = RPCServer(ZmqServerTransport.create(ctx, endpoint), JSONRPCProtocol(), dispatcher) rpc_server.trace = print # client callback_client = RPCClient( JSONRPCProtocol(), ZmqClientTransport.create(ctx, callback_endpoint)) callback_remote_server = callback_client.get_proxy(one_way=True) running = True @dispatcher.public def act(uid: int, s: str) -> str: q.put(("act", (uid, s), {})) ev.set() return "ok" @dispatcher.public def shutdown() -> None: nonlocal running running = False def do_act(uid: int, s: str) -> Future: fut = Future() def do(): for i in range(5): callback_remote_server.notify(f"{uid:02d}: {i} {s}") print(f"{uid:02d}: {i} {s}") time.sleep(0.1) fut.set_result(("ok", uid)) threading.Thread(target=do, daemon=True).start() return fut def do_server(): nonlocal running while running: rpc_server.receive_one_message() def do_loop(*, ns): nonlocal running while running: fn, args, kwargs = q.get() fut = ns[f"do_{fn}"](*args, **kwargs) def cont(fut): q.task_done() fut.add_done_callback(cont) ns = locals() th = threading.Thread(target=do_loop, kwargs={"ns": ns}, daemon=True) th.start() th2 = threading.Thread(target=do_server, daemon=True) th2.start() ev.wait() q.join()