diff --git a/coinparty/client.py b/coinparty/client.py new file mode 100644 index 0000000..aacf247 --- /dev/null +++ b/coinparty/client.py @@ -0,0 +1,26 @@ +import asyncio +from aiozmq import rpc + +# debugging: +# sudo tcpdump -i lo -f -X -S -s 0 'tcp port 2205' + +async def run(server_addr): + print('connecting to rpc server...') + client = await rpc.connect_rpc(connect=server_addr, timeout=5) + + for i in range(10): + res = await client.call.add(123, 456) + print('add_server.add(4, 2) = %d' % res) + await asyncio.sleep(3) + + print('closing client') + client.close() + await client.wait_closed() + +# server_addr = 'ipc://add_server.ipc' +server_addr = 'tcp://127.0.0.1:2205' + +loop = asyncio.get_event_loop() +loop.run_until_complete(run(server_addr)) +print('closing event loop') +loop.close() diff --git a/coinparty/server.py b/coinparty/server.py new file mode 100644 index 0000000..c970fa0 --- /dev/null +++ b/coinparty/server.py @@ -0,0 +1,50 @@ +import asyncio +from aiozmq import rpc +import signal +import os + +class Handler(rpc.AttrHandler): + + @rpc.method + async def add(self, a: int, b: int) -> int: + print('add %d, %d' % (a, b)) + await asyncio.sleep(3) + print('finished sleeping') + return a + b + + def hidden(self): + return 'wtf' + +def close(loop, server, signame): + print('close(): got signal %s: closing' % signame) + print('close(): closing server') + server.close() + print('close(): closing event loop') + loop.stop() + +async def run(loop, server_addr): + print('rpc starting server...') + server = await rpc.serve_rpc( + Handler(), + bind=server_addr, + log_exceptions=True + ) + + for signame in ('SIGINT', 'SIGTERM'): + loop.add_signal_handler( + getattr(signal, signame), + lambda: close(loop, server, signame) + ) + +# server_addr = 'ipc://add_server.ipc' +server_addr = 'tcp://127.0.0.1:2205' +print('server running at "%s", pid: %s' % (server_addr, os.getpid())) + +loop = asyncio.get_event_loop() +loop.create_task(run(loop, server_addr)) + +try: + loop.run_forever() +finally: + print('closing event loop') + loop.close()