Esempio n. 1
0
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()
Esempio n. 2
0
def client(*, endpoint: str, callback_endpoint: str):
    ctx = zmq.Context()

    dispatcher = RPCDispatcher()

    @dispatcher.public
    def notify(s: str):
        print("** {s} **")

    callback_server = RPCServer(
        ZmqServerTransport.create(ctx, callback_endpoint), JSONRPCProtocol(),
        dispatcher)
    callback_server.trace = print
    threading.Thread(target=callback_server.serve_forever, daemon=True).start()

    rpc_client = RPCClient(JSONRPCProtocol(),
                           ZmqClientTransport.create(ctx, endpoint))
    remote_server = rpc_client.get_proxy()

    for i in range(7):
        result = remote_server.act(i, "Hello, World!")
        print("Server answered:", result)

    result = remote_server.shutdown()
    print("Send shutdown:", result)
Esempio n. 3
0
 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()
Esempio n. 4
0
def manager():
    ctx = zmq.Context()
    transport = ZmqServerTransport.create(ctx, "tcp://127.0.0.1:5002")

    rpc_server = RPCServer(transport, JSONRPCProtocol(), dispatcher)
    # rpc_server.trace = print
    th = threading.Thread(daemon=True, target=rpc_server.serve_forever)
    th.start()

    cmd = [sys.executable, __file__, "worker"]
    p = subprocess.Popen(cmd, stdin=subprocess.PIPE, text=True)
    for i in range(10):
        send(f"world{i}", port=p.stdin)
    p.stdin.close()

    th.join()
    p.terminate()
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()
Esempio n. 6
0
def transport(request, zmq_context, zmq_green_context):
    if request.param == 'dummy':
        server = DummyServerTransport()
        client = DummyClientTransport(server)
    elif request.param in ('zmq', 'zmq.green'):
        ctx = zmq_context if request.param == 'zmq' else zmq_green_context

        server = ZmqServerTransport.create(ctx, ZMQ_ENDPOINT_SERVER)
        client = ZmqClientTransport.create(ctx, ZMQ_ENDPOINT_CLIENT)

        def fin():
            server.shutdown()
            client.shutdown()

        request.addfinalizer(fin)
    else:
        raise ValueError('Invalid transport.')
    return (client, server)
Esempio n. 7
0
def transport(request, zmq_context, zmq_green_context):
    if request.param == 'dummy':
        server = DummyServerTransport()
        client = DummyClientTransport(server)
    elif request.param in ('zmq', 'zmq.green'):
        ctx = zmq_context if request.param == 'zmq' else zmq_green_context

        server = ZmqServerTransport.create(ctx, ZMQ_ENDPOINT)
        client = ZmqClientTransport.create(ctx, ZMQ_ENDPOINT)

        def fin():
            server.socket.close()
            client.socket.close()

        request.addfinalizer(fin)
    else:
        raise ValueError('Invalid transport.')
    return (client, server)
Esempio n. 8
0
 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()
Esempio n. 9
0
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()
Esempio n. 10
0
    def init_server(self, transport, logger):
        '''
        Internal function that should not be called explicitly.
        '''
        if isinstance(transport, ZmqServerTransport):
            self.transport = transport
        else:
            # dict like {'receiver': transport, 'receiver': replier_endpoint}
            for key in transport:
                if 'tcp' not in str(transport[key]):
                    transport[key] = "tcp://" + str(transport[key])
            self.transport = ZmqServerTransport.create(self.ctx, transport)

        self.transport.publisher = self.publisher

        self.rpc_server = RPCServer(self.transport, self.protocol,
                                    self.dispatcher)
        self.rpc_server.set_logger(logger)
        self.register_instance({'server': self})
        self.rpc_server.start()
        self.logger.info('rpc server {} started.'.format(self.endpoint))
Esempio n. 11
0
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()
Esempio n. 12
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

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:5001")

rpc_server = RPCServer(transport, JSONRPCProtocol(), dispatcher)


@dispatcher.public
def reverse_string(s):
    return s[::-1]


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()
Esempio n. 14
0
def server(*, endpoint: str):
    import queue
    import threading
    import time
    from concurrent.futures import Future
    from tinyrpc.transports.zmq import ZmqServerTransport
    from tinyrpc.server import RPCServer
    from tinyrpc.dispatch import RPCDispatcher

    q = queue.Queue()
    ev = threading.Event()

    ctx = zmq.Context()
    dispatcher = RPCDispatcher()
    transport = ZmqServerTransport.create(ctx, endpoint)

    rpc_server = RPCServer(transport, JSONRPCProtocol(), dispatcher)
    rpc_server.trace = print

    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):
                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()
Esempio n. 15
0
                    help='The port on which to bind to service.')
parser.add_argument(
    '--persist',
    dest='persist',
    type=bool,
    default=False,
    help='set whether or not to persist the connection to the daq after a call'
)

args = parser.parse_args()

ctx = zmq.Context()
dispatcher = RPCDispatcher()

endpoint = 'tcp://127.0.0.1:%i' % args.port
transport = ZmqServerTransport.create(ctx, endpoint)
print "serving requests at %s" % endpoint
system("Title " + "NI DAQ Device Service")

rpc_server = RPCServer(transport, JSONRPCProtocol(), dispatcher)

channelTasks = dict()


def initializeDigitalOutput(channel):
    digital_output = daq.Task()
    digital_output.CreateDOChan(channel, "", daq.DAQmx_Val_ChanPerLine)
    return digital_output


def initializeDigitalInput(channel):
Esempio n. 16
0
def manager():
    @dataclasses.dataclass
    class Handler:
        uid: int
        process: subprocess.Popen
        remote_server: RPCProxy

    ctx = zmq.Context()
    dispatcher = RPCDispatcher()
    living_process = {None}  # None is semaphore

    @dispatcher.public
    def notify(msg: t.Any):
        print("*", msg, "*")

    @dispatcher.public
    def exit(uid: int):
        print("EXIT", living_process.remove(uid))

    # use tmpfs?
    with tempfile.TemporaryDirectory() as d:
        endpoint = f"ipc://{d}/worker.manager"
        transport = ZmqServerTransport.create(ctx, endpoint)
        rpc_server = RPCServer(transport, JSONRPCProtocol(), dispatcher)

        def server_loop():
            while len(living_process) > 0:
                rpc_server.receive_one_message()

        th = threading.Thread(target=server_loop, daemon=True)
        th.start()

        handlers = []
        n = 3
        protocol = JSONRPCProtocol()

        for uid in range(n):
            living_process.add(uid)
            cmd = [
                sys.executable,
                "-u",
                __file__,
                "worker",
                "--uid",
                str(uid),
                "--endpoint",
                endpoint,
            ]
            p = subprocess.Popen(
                cmd,
                # stdout=subprocess.PIPE,
                # stderr=subprocess.PIPE,
                # text=False,  # for read1
            )
            rpc_client = RPCClient(
                protocol,
                ZmqClientTransport.create(
                    ctx, endpoint.replace(".manager", f".{uid}")),
            )
            remote_server = rpc_client.get_proxy()
            handlers.append(
                Handler(uid=uid, process=p, remote_server=remote_server))

        # 本当はguardなどを使う
        living_process.remove(None)  # None is semaphore

        futs = []
        from concurrent.futures import ThreadPoolExecutor

        with ThreadPoolExecutor() as ex:
            for h in handlers:
                futs.append(ex.submit(h.remote_server.do_task))
                futs.append(ex.submit(h.remote_server.do_task))

        for fut in futs:
            print(fut.result())

        for h in handlers:
            h.remote_server.exit()
        print("END")
        th.join()
Esempio n. 17
0
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()