Beispiel #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()
Beispiel #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)
    def __init__(self,tc_service_endpoint,zmq_context):
        
        rpc_client = RPCClient(
            JSONRPCProtocol(),
            ZmqClientTransport.create(zmq_context, tc_service_endpoint)
        )

        self._remote = rpc_client.get_proxy()
 def __init__(self, modbus_service_endpoint, device_id, zmq_context=None):
     self.device_id = device_id
     if zmq_context is None:
         self.zmq_context = zmq.Context()
     else:
         self.zmq_context = zmq_context
     self._rpc_client = RPCClient(
         JSONRPCProtocol(),
         ZmqClientTransport.create(self.zmq_context, modbus_service_endpoint)
     )
     self.modbus_proxy = self._rpc_client.get_proxy()
Beispiel #5
0
def worker():
    ctx = zmq.Context()
    rpc_client = RPCClient(
        JSONRPCProtocol(),
        ZmqClientTransport.create(ctx, "tcp://127.0.0.1:5002"))
    proxy = rpc_client.get_proxy()

    for message in iter(partial(recv, port=sys.stdin), ""):
        proxy.post(f"hello {message}")
        import time

        time.sleep(0.4)
    sys.stdout.write("")
    sys.stdout.close()
Beispiel #6
0
    def __init__(self, transport, publisher=None, ctx=None, protocol=None):
        self.ctx = ctx if ctx else zmq.Context().instance()
        if isinstance(transport, ZmqClientTransport):
            self.transport = transport
        else:
            if 'tcp' not in str(transport):
                transport = "tcp://*:" + str(transport)
            self.transport = ZmqClientTransport.create(self.ctx, transport)

        self.protocol = protocol if protocol else JSONRPCProtocol()
        self.publisher = publisher if publisher else NoOpPublisher()
        self.transport.publisher = self.publisher

        self.rpc_client = RPCClient(self.protocol, self.transport,
                                    self.publisher)
        self.proxy = self.get_proxy()
Beispiel #7
0
def client(*, endpoint: str):
    from tinyrpc.transports.zmq import ZmqClientTransport
    from tinyrpc import RPCClient

    ctx = zmq.Context()

    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)
Beispiel #8
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)
Beispiel #9
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)
Beispiel #10
0
def run() -> None:
    ctx = zmq.Context()

    port = _find_free_port()
    sentinel = tempfile.mktemp()

    cmd = [
        sys.executable, "03server.py", "--port",
        str(port), "--sentinel", sentinel
    ]
    with connect_server(cmd, sentinel=sentinel, port=port):
        print(f"connect ... {port}", file=sys.stderr)
        rpc_client = RPCClient(
            JSONRPCProtocol(),
            ZmqClientTransport.create(ctx, f"tcp://127.0.0.1:{port}"))
        s = rpc_client.get_proxy()

        print("Returns 2**3 = 8", "->", s.pow(2, 3))
        print("Returns 5", "->", s.add(2, 3))
        print("Returns 5*2 = 10", "->", s.mul(5, 2))

        # Print list of available methods
        print(s.list_methods())
Beispiel #11
0
import zmq

from tinyrpc.protocols.jsonrpc import JSONRPCProtocol
from tinyrpc.transports.zmq import ZmqClientTransport
from tinyrpc import RPCClient

ctx = zmq.Context()

rpc_client = RPCClient(JSONRPCProtocol(),
                       ZmqClientTransport.create(ctx, 'tcp://127.0.0.1:5050'))

remote_server = rpc_client.get_proxy()

remote_server.gotoStopState()
print remote_server.getState()
import zmq
import time
from tinyrpc.protocols.jsonrpc import JSONRPCProtocol
from tinyrpc.transports.zmq import ZmqClientTransport
from tinyrpc import RPCClient

ctx = zmq.Context()

rpc_client = RPCClient(
    JSONRPCProtocol(),
    ZmqClientTransport.create(ctx, 'tcp://127.0.0.1:5062')
)


remote_server = rpc_client.get_proxy()

for i in range(1000):
    print remote_server.get_pv()
Beispiel #13
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import zmq

from tinyrpc.protocols.jsonrpc import JSONRPCProtocol
from tinyrpc.transports.zmq import ZmqClientTransport
from tinyrpc import RPCClient

ctx = zmq.Context()

rpc_client = RPCClient(JSONRPCProtocol(),
                       ZmqClientTransport.create(ctx, "tcp://127.0.0.1:5001"))

remote_server = rpc_client.get_proxy()

for i in range(10):
    # call a method called 'reverse_string' with a single string argument
    result = remote_server.reverse_string("Hello, World!")

    print("Server answered:", result)
Beispiel #14
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()
Beispiel #15
0
    def __init__(self,
                 transport=None,
                 publisher=None,
                 ctx=None,
                 protocol=None,
                 ip=None,
                 port=None,
                 receiver_port=None):
        self.ctx = ctx if ctx else zmq.Context().instance()
        msg = 'ip and port should be used together.'
        assert ([ip, port] == [None, None]) or (ip is not None
                                                and port is not None), msg
        if ip is not None and port is not None:
            msg = 'port {} invalid; please choose from (0, 65536)'.format(port)
            assert 0 < port < 65536, msg
            # user provide ip and port, assuming using tcp.
            # endpoint generated as 'tcp'
            # user should not pass another transport in.
            msg = 'transport cannot be used along with ip&port'
            assert transport is None, msg
            assert isinstance(ip, basestring)
            msg = 'invalid tcp port: {}'.format(port)
            assert isinstance(port, int) and 0 < port < 65536, msg
            # generate zmq endpoint from ip and port
            if receiver_port is None:
                receiver_port = port + 10000
                # ensure receiver_port is valid
                msg = ('Failed to generate receiver_port by 10000 + port {}; '
                       'please use port < 55536')
                assert receiver_port < 65536, msg.format(port)
            else:
                msg = 'receiver_port {} invalid; please choose from (0, 65536)'.format(
                    receiver_port)
                assert 0 < receiver_port < 65536, msg
            transport = {
                'requester': 'tcp://{}:{}'.format(ip, port),
                'receiver': 'tcp://{}:{}'.format(ip, receiver_port)
            }

        if isinstance(transport, ZmqClientTransport):
            self.transport = transport
        elif isinstance(transport, dict):
            # dictionary:
            if 'requester' in transport and 'receiver' in transport:
                self.transport = ZmqClientTransport.create(self.ctx, transport)
            else:
                msg = 'endpoint dictionary {} should contains requester and receiver'
                raise Exception(msg.format(transport))
        elif isinstance(transport, basestring):
            # only 1 endpoint is provided; create endpoint for receiver by adding port by 10000
            pattern = '(tcp://)?((?P<ip>[0-9.*]+):)?(?P<port>[0-9]+)'
            re_groups = re.match(pattern, transport.strip())
            if not re_groups:
                raise Exception('Invalid RPC client endpoint format {}; '
                                'expecting tcp://IP:PORT'.format(transport))
            requester_port = int(re_groups.group('port'))
            ip = re_groups.group('ip')
            endpoints = {
                'requester': 'tcp://{}:{}'.format(ip, requester_port),
                'receiver': 'tcp://{}:{}'.format(ip, requester_port + 10000)
            }
            self.transport = ZmqClientTransport.create(self.ctx, endpoints)
        else:
            msg = 'RPC client endpoint {} not supported; expecting dict or string or ip&port.'
            raise Exception(msg.format(transport))

        self.protocol = protocol if protocol else JSONRPCProtocol()
        self.publisher = publisher if publisher else NoOpPublisher()
        self.transport.publisher = self.publisher

        self.rpc_client = RPCClient(self.protocol, self.transport,
                                    self.publisher)
        self.proxy = self.rpc_client.get_proxy()
Beispiel #16
0
# interface with knowledge
import rosprolog_client
# Messages
import actionlib
import nlg_msgs.msg
# connection to nlg.py3 script
import zmq
from tinyrpc import RPCClient, RPCProxy
from tinyrpc.protocols.jsonrpc import JSONRPCProtocol
from tinyrpc.transports.zmq import ZmqClientTransport
from std_msgs.msg import String

# Start an RPC Client
rpc_client = RPCClient(
    JSONRPCProtocol(),
    ZmqClientTransport.create(zmq.Context(), 'tcp://127.0.0.1:5002'))
gen_server = None

prolog = rosprolog_client.Prolog()


class NlgAction:
    # create messages that are used to publish feedback/result
    _feedback = nlg_msgs.msg.LanguageGenerationFeedback()
    _result = nlg_msgs.msg.LanguageGenerationResult()

    def __init__(self, name):
        self._action_name = name
        self._as = actionlib.SimpleActionServer(
            self._action_name,
            nlg_msgs.msg.LanguageGenerationAction,
Beispiel #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()
import zmq

from tinyrpc.protocols.jsonrpc import JSONRPCProtocol
from tinyrpc.transports.zmq import ZmqClientTransport
from tinyrpc import RPCClient
import numpy as np
import time

ctx = zmq.Context()

rpc_client = RPCClient(
    JSONRPCProtocol(),
    ZmqClientTransport.create(ctx, 'tcp://127.0.0.1:10101')
)

remote_server = rpc_client.get_proxy()

i=0
while True:
    try:
        print remote_server.knaker(test=i)
    except:
        pass
# call a method called 'reverse_string' with a single string argument


                    type=str,
                    default=None,
                    help='A file to write the analog input values to (csv)')

parser.add_argument('--interval',
                    dest='interval',
                    type=int,
                    default=1,
                    help="The time between samples in seconds")

args = parser.parse_args()

ctx = zmq.Context()

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

remote_server = rpc_client.get_proxy()

# call a method called 'reverse_string' with a single string argument

channels = range(8)

if (args.outfile is not None):
    with file(args.outfile, 'w') as f:
        f.write(",".join(["datetime"] + ["ai%i" % ch
                                         for ch in channels]) + "\n")

while True:
    results = []
    now = datetime.datetime.now()
                   help='The server address')

parser.add_argument('--outfile', dest='outfile', type=str, default=None,
                    help='A file to write the analog input values to (csv)')

parser.add_argument('--interval', dest='interval', type=int, default=1,
                    help="The time between samples in seconds")

args = parser.parse_args()


ctx = zmq.Context()

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

remote_server = rpc_client.get_proxy()

# call a method called 'reverse_string' with a single string argument


channels=range(8)

if (args.outfile is not None):
    with file(args.outfile,'w') as f:
        f.write(",".join(["datetime"] + ["ai%i" % ch for ch in channels]) + "\n")

while True:
    results = []
def main():

    try:

        # s = socket.socket()         # Create a socket object
        # host = socket.gethostname() # Get local machine name
        # port = 55555                # Reserve a port for your service.
        # s.bind((host, port))        # Bind to the port
        #
        # s.listen(5)                 # Now wait for client connection.
        #
        # nt = Thread(target=network_server, args=(s,))
        # nt.daemon = True # thread dies with the program
        # nt.start()

        ctx = zmq.Context()

        rpc_client = RPCClient(
            JSONRPCProtocol(),
            ZmqClientTransport.create(ctx, SERVER_ADDR)
        )

        remote_server = rpc_client.get_proxy()
        tv_controller = rpc_client.get_proxy("tv_controller.")

        #############################
        # Runtime Loop

        global sleepActive
        sleepActive = False

        global timeout
        global lastNetwork
        timeout = 0
        lastNetwork = time.time()
        playstate = 'idle'
        prevplaystate = playstate

        lastStat = time.time()
        last_mythtv_status = False
        last_xbmc_status = False

        mythtvStatus = mythtv_status.mythtv_status()

        keyPressTimeout = time.time() - TIMEOUT + 90    # Start with 1.5 minute to sleep

        timeSinceComms = time.time() - RESEND_TIMEOUT + 20

        while True:
            if not resetQueue.empty():
                resetQueue.get(timeout=10)
                keyPressTimeout = time.time()

            if (time.time() - lastStat) > 60:
                logger.debug("idleTime: " + str((time.time() - keyPressTimeout)/60))
                lastStat = time.time()

            # Monitor MythTV
            current_mythtv_status = mythtvStatus.getStatus()
            if current_mythtv_status:
                if not last_mythtv_status:
                    logger.debug("mythtv start playing")
                keyPressTimeout = time.time()
            elif last_mythtv_status:
                logger.debug("mythtv stop playing")
            last_mythtv_status = current_mythtv_status

            # Monitor XBMC
            try:
                current_xbmc_status = xbmc_status.getStatus()
                if current_xbmc_status:
                    if not last_xbmc_status:
                        logger.debug("xbmc start playing")
                    keyPressTimeout = time.time()
                elif last_xbmc_status:
                    logger.debug("xbmc stop playing")
                last_xbmc_status = current_xbmc_status
            except xbmc_status.Timeout:
                pass

            now = time.time()

            currSleep = (now - keyPressTimeout) > TIMEOUT

            if (now - timeSinceComms > RESEND_TIMEOUT) or (currSleep != sleepActive):
                timeSinceComms = now
                sleepActive = currSleep
                logger.debug("Sending Status: " + ("off" if sleepActive else "on"))
                result = tv_controller.SetPowerStatus(not sleepActive)
                logger.debug(result)

            # if (time.time() - keyPressTimeout) > TIMEOUT:
            #     # Go To Sleep
            #     if not sleepActive:
            #         print "Sleep"
            #         logger.debug("timeout: sleep")
            #         sleepActive = True
            #         result = tv_controller.SetPowerStatus('on' if sleepActive else 'off')
            #         logger.debug(result)
            #
            #
            #     # Comms has dropped out, let system service manager restart
            #     # if time.time() - lastNetwork > TIMEOUT:
            #     #     exit()
            #
            # else:
            #     if sleepActive:
            #         print "Wake"
            #         logger.debug("timeout: wake")
            #         sleepActive = False
            #         result = tv_controller.SetPowerStatus(False if sleepActive else True)
            #         logger.debug(result)

            time.sleep(0.5)

    except Exception as e:
        logger.warn("exception in main()")
        logger.exception(e)