예제 #1
0
def test_bus():
    with pynng.Bus0(recv_timeout=100) as s0, \
            pynng.Bus0(recv_timeout=100) as s1, \
            pynng.Bus0(recv_timeout=100) as s2:
        s0.listen(addr)
        s1.dial(addr)
        s2.dial(addr)
        wait_pipe_len(s0, 2)
        s0.send(b's1 and s2 get this')
        assert s1.recv() == b's1 and s2 get this'
        assert s2.recv() == b's1 and s2 get this'
        s1.send(b'only s0 gets this')
        assert s0.recv() == b'only s0 gets this'
        s2.recv_timeout = 0
        with pytest.raises(pynng.Timeout):
            s2.recv()
예제 #2
0
 def __init__(self,
              slave_id: str,
              super_node_ip: str,
              super_node_port: int = 40000) -> None:
     self.on_message = None
     self._slave_id = slave_id
     self._node = pynng.Bus0()
     self._node.dial(f'tcp://{super_node_ip}:{super_node_port}')
예제 #3
0
async def node(name, listen_address, contacts):
    with pynng.Bus0() as sock:
        sock.listen(listen_address)
        await curio.sleep(1)  #  wait for peers to bind
        for contact in contacts:
            sock.dial(contact)

        await curio.sleep(1)
        # wait for connects to establish
        print(f"{name}: SENDING '{listen_address}' ONTO BUS")
        await sock.asend(listen_address.encode())

        while True:
            try:
                msg = await sock.arecv_msg()
                print(f'{name}: RECEIVED "{msg.bytes.decode()}" FROM BUS')
            except pynng.Timeout:
                print(f"{name}: Timeout")
                break
예제 #4
0
    def __init__(self, nano: bool = False, base_port: int = 50000) -> None:
        self._on_message = None
        self._topics = list()

        self._node = pynng.Bus0()
        port: int = base_port
        for port in range(base_port, 65535):
            try:
                self._node.listen(f'tcp://0.0.0.0:{port}')
                break
            except pynng.exceptions.AddressInUse:
                # 端口已被占用
                pass

        for target_port in range(base_port, port):
            self._node.dial(f'tcp://127.0.0.1:{target_port}')

        if nano is False:
            # 连接Nano
            self._node.dial('tcp://192.168.3.112:50000')
예제 #5
0
 def __init__(self, port: int = 40000) -> None:  # pylint: disable=super-init-not-called
     self.on_message = None
     self._node = pynng.Bus0()
     self._node.listen(f'tcp://0.0.0.0:{port}')