Beispiel #1
0
def interface_r(cfg, logger=None):
    connect_interfaces(SocketChannel(cfg["src_host"],
                                     cfg["src_port"],
                                     logger=logger),
                       SocketChannel(cfg["dst_host"],
                                     cfg["dst_port"],
                                     logger=logger),
                       encoder=compose(partial(DataAggregator, size=5),
                                       REncoder,
                                       partial(DataLogger, fpath="r_path")))
Beispiel #2
0
    def setUp(self):
        self.sock = socket.socket(socket.AF_INET)
        self.sock.bind(('0.0.0.0', 0))
        self.addr = self.sock.getsockname()
        self.sock.listen()

        self.fb = FakeBrain()
        sock = socket.create_connection(self.fb.addr)
        self.fb.work(0.01)
        self.be = SocketChannel(sock, buffering='line')

        self.router = Router(self.sock, self.be)
def main(sockname, host, port):
    serv = socket.socket()
    serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
    serv.bind((host, port))
    addr, port = serv.getsockname()
    serv.listen(0)

    if ':' in sockname:
        rhost, rport = sockname.split(':')
        while True:
            try:
                sock = socket.create_connection((rhost, int(rport)))
                break
            except ConnectionRefusedError:
                time.sleep(0.1)
    else:
        sock = socket.socket(socket.AF_UNIX)
        sock.connect(sockname)

    endpoint = TcpEndpoint(serv, SocketChannel(sock))
    endpoint.send_name()

    print("Server started on", addr+':'+str(port))
    sys.stdout.flush()
    try:
        while True:
            endpoint.poll(1)
    except KeyboardInterrupt:
        return
Beispiel #4
0
def interface_g(cfg, logger=None):
    connect_interfaces(FileChannel("g.bin",
                                   "rb",
                                   reader=TestReader,
                                   logger=logger),
                       SocketChannel("localhost", 1234, logger=logger),
                       encoder=TestParse)
Beispiel #5
0
def main(sockname, config_file_name, brain_sockname):
    brain_serv, brain_serv_addr = start_server(brain_sockname)

    be = SocketChannel(brain_serv.accept()[0])

    sock, router_addr = start_server(sockname)
    router = Router(sock, be)

    while True:
        router.tick()
def main(sockname, pipename):
    sock = socket.socket(socket.AF_UNIX)
    sock.connect(sockname)

    endpoint = IncomingEndpoint(pipename, SocketChannel(sock))
    atexit.register(endpoint.shutdown)

    try:
        while True:
            endpoint.poll(1)
    except KeyboardInterrupt:
        return
Beispiel #7
0
def main(sockname):
    sock = socket.socket(socket.AF_UNIX)
    sock.connect(sockname)
    set_router(SocketChannel(sock, buffering='line'))
    set_stdio(PipeChannel(sys.stdin.fileno(),
                          sys.stdout.fileno(),
                          buffering='line'))
    send_name()
    _ROUTER.write(json.dumps({"event": "presence",
                              "from": {"user": _USER,
                                       "channel": _CHANNEL},
                              "to": {"user": "******",
                                     "channel": "brain"}}).encode(), b'\n')

    try:
        while True:
            poll(1)
    except KeyboardInterrupt:
        return
Beispiel #8
0
def interface_a(cfg, logger=None):
    connect_interfaces(SocketChannel("localhost", 1234, logger=logger),
                       SocketChannel("localhost", 5432, logger=logger),
                       duplex=True)
Beispiel #9
0
 def setUp(self):
     self._server, self._client = socket.socketpair()
     self.addCleanup(self._server.close)
     self.addCleanup(self._client.close)
     self._chan = SocketChannel(self._client, buffering='line')
Beispiel #10
0
class RouterTest(unittest.TestCase):
    def setUp(self):
        self.sock = socket.socket(socket.AF_INET)
        self.sock.bind(('0.0.0.0', 0))
        self.addr = self.sock.getsockname()
        self.sock.listen()

        self.fb = FakeBrain()
        sock = socket.create_connection(self.fb.addr)
        self.fb.work(0.01)
        self.be = SocketChannel(sock, buffering='line')

        self.router = Router(self.sock, self.be)

    def tearDown(self):
        self.fb.shutdown()
        self.be.close()
        self.sock.close()

    def _connect(self, channel_name="channel"):
        client = socket.create_connection(self.addr)
        self.addCleanup(client.close)
        self.router.tick(0.01)
        client.send(channel_name.encode() + b'\n')
        self.router.tick(0.01)
        return client

    def _feed(self, client, msg):
        client.send(json.dumps(msg).encode() + b'\n')
        self.router.tick(0.01)
        self.fb.work(0.01)
        self.router.tick(0.01)

    def test_route_to_brain(self):
        client = self._connect()

        msg = {"command": "nop", "to": {"user": "******", "channel": "brain"}}
        self._feed(client, msg)

        self.assertEqual(self.fb.messages, [msg])

    def test_discard_if_not_to_brain(self):
        client = self._connect()

        msg = {"command": "nop", "to": {"user": "******", "channel": "channel"}}
        self._feed(client, msg)

        self.assertEqual(self.fb.messages, [])

    def test_switch_user(self):
        client = self._connect()

        msg = {
            "message": "test",
            "from": {
                "user": "******",
                "channel": "channel"
            },
            "to": {
                "user": "******",
                "channel": "brain"
            }
        }
        self._feed(client, msg)

        self.assertEqual(self.fb.messages, [{
            "command": "switch-user",
            "user": "******"
        }, msg])

    def test_keep_same_user(self):
        client = self._connect()

        msg = {
            "message": "test",
            "from": {
                "user": "******",
                "channel": "channel"
            },
            "to": {
                "user": "******",
                "channel": "brain"
            }
        }
        self._feed(client, msg)
        self._feed(client, msg)

        self.assertEqual(self.fb.messages, [{
            "command": "switch-user",
            "user": "******"
        }, msg, msg])

    def test_route_from_brain(self):
        client1 = self._connect("channel1")
        client2 = self._connect("channel2")
        poll = select.poll()
        poll.register(client1, select.POLLIN)
        poll.register(client2, select.POLLIN)

        self.fb.send_message_to("hello", "user", "channel1:abcd")
        self.router.tick(0.01)

        res = poll.poll(0.01)
        self.assertEqual(res, [(client1.fileno(), select.POLLIN)])
        data = client1.recv(1024)
        msg = json.loads(data.decode())
        self.assertEqual(
            msg, {
                "message": "hello",
                "from": {
                    "user": "******",
                    "channel": "brain"
                },
                "to": {
                    "user": "******",
                    "channel": "channel1:abcd"
                }
            })

    def test_client_disconnect(self):
        client = self._connect()
        client.close()

        self.fb.send_message_to("hello", "user", "channel:abcd")
        self.router.tick(0.01)

        self.assertEqual(self.fb.messages, [])

    def test_client_gone(self):
        client = self._connect()
        msg = {
            "message": "test",
            "from": {
                "user": "******",
                "channel": "channel"
            },
            "to": {
                "user": "******",
                "channel": "brain"
            }
        }
        self._feed(client, msg)
        self.assertEqual(self.fb.messages.pop(0), {
            "command": "switch-user",
            "user": "******"
        })
        self.assertEqual(self.fb.messages.pop(0), msg)
        self.assertEqual(self.fb.messages, [])

        client.close()
        self.router.tick(0.01)
        self.fb.work(0.01)

        self.assertEqual(self.fb.messages, [{
            "event": "gone",
            "from": {
                "user": "******",
                "channel": "channel"
            },
            "to": {
                "user": "******",
                "channel": "brain"
            }
        }])

    def test_brain_gone(self):
        self.fb.shutdown()

        with self.assertRaises(BrainDisconnectedException):
            self.router.tick(0.01)
def connect(addr, poller):
    sock = socket.create_connection(addr)
    up = SocketChannel(sock)
    poller.register(up)
    up.write(getpass.getuser().encode() + b'\n')
    return up
Beispiel #12
0
def step_impl(context):
    with timeout(1):
        client, _ = context.router.accept()
    context.channel = SocketChannel(client, buffering='line')
class SocketChannelTest(unittest.TestCase):

    _server = None
    _client = None
    _channel = None

    def setUp(self):
        self._server, self._client = socket.socketpair()
        self.addCleanup(self._server.close)
        self.addCleanup(self._client.close)
        self._channel = SocketChannel(self._client)

    def test_read(self):
        self._server.send(b'hello, world')

        self.assertEqual(self._channel.read(), b'hello, world')
        self.assertEqual(self._channel.read(), b'')
        self.assertEqual(self._channel.read(), b'')

    def test_write(self):
        self._channel.write(b'hello, world')

        self.assertEqual(self._server.recv(4096), b'hello, world')

    def test_write_list(self):
        self._channel.write(b'hello, ', b'world')

        self.assertEqual(self._server.recv(4096), b'hello, world')

    def test_closed_read(self):
        self._client.close()

        with self.assertRaises(EndpointClosedException):
            self._channel.read()

    def test_close_read(self):
        self._channel.close()

        with self.assertRaises(OSError) as ose:
            self._client.recv(1)
            self.assertEqual(ose.exception.error_code, 9)  # EBADF

    def test_closed_write(self):
        self._client.close()

        with self.assertRaises(EndpointClosedException):
            self._channel.write(b' ')

    def test_close_write(self):
        self._channel.close()

        with self.assertRaises(OSError) as ose:
            self._client.send(b' ')
            self.assertEqual(ose.exception.error_code, 9)  # EBADF

    def test_write_close_read(self):
        self._server.send(b'hello, world')
        self._server.close()

        self.assertEqual(self._channel.read(), b'hello, world')
        with self.assertRaises(EndpointClosedException):
            self._channel.read()

    def test_get_fd(self):
        self.assertEqual(self._channel.get_fd(), self._client.fileno())
 def setUp(self):
     self._server, self._client = socket.socketpair()
     self.addCleanup(self._server.close)
     self.addCleanup(self._client.close)
     self._channel = SocketChannel(self._client)