async def main(): p = argparse.ArgumentParser() p.add_argument( 'mode', help='Whether the socket should "listen" or "dial"', choices=['listen', 'dial'], ) p.add_argument( 'addr', help='Address to listen or dial; e.g. tcp://127.0.0.1:13134', ) args = p.parse_args() with pynng.Pair1(polyamorous=True) as sock: async with trio.open_nursery() as n: if args.mode == 'listen': # the listening socket can get dialled by any number of dialers. # add a couple callbacks to see when the socket is receiving # connections. def pre_connect_cb(pipe): addr = str(pipe.remote_address) print('~~~~got connection from {}'.format(addr)) def post_remove_cb(pipe): addr = str(pipe.remote_address) print('~~~~goodbye for now from {}'.format(addr)) sock.add_pre_pipe_connect_cb(pre_connect_cb) sock.add_post_pipe_remove_cb(post_remove_cb) sock.listen(args.addr) else: sock.dial(args.addr) n.start_soon(recv_eternally, sock) n.start_soon(send_eternally, sock)
def test_pair1_polyamorousness(): with pynng.Pair1(listen=addr, polyamorous=True, recv_timeout=500) as s0, \ pynng.Pair1(dial=addr, polyamorous=True, recv_timeout=500) as s1: wait_pipe_len(s0, 1) # pipe for s1 . p1 = s0.pipes[0] with pynng.Pair1(dial=addr, polyamorous=True, recv_timeout=500) as s2: wait_pipe_len(s0, 2) # pipes is backed by a dict, so we can't rely on order in # Python 3.5. pipes = s0.pipes p2 = pipes[1] if p2 is p1: p2 = pipes[0] p1.send(b'hello s1') assert s1.recv() == b'hello s1' p2.send(b'hello there s2') assert s2.recv() == b'hello there s2'
def test_pair1(): with pynng.Pair1(listen=addr, recv_timeout=100) as s0, \ pynng.Pair1(dial=addr, recv_timeout=100) as s1: val = b'oaisdjfa' s1.send(val) assert s0.recv() == val
def test_pair1(): with pynng.Pair1(listen=addr, recv_timeout=100) as s0, \ pynng.Pair1(dial=addr, recv_timeout=100) as s1: s1.send(b'beep boop beep') assert s0.recv() == b'beep boop beep'