From d1bffce0242f721a4bd3a61e88aaff6f6fa49248 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Wed, 1 May 2019 17:59:33 +0200 Subject: Start listening for future commands on a socket. --- pyapd/app.py | 3 ++- pyapd/ctl.py | 40 ++++++++++++++++++++++++++++++++++++++++ pyapd_config.yaml | 2 ++ tests/data/test_config.yaml | 1 + tests/test_ctl.py | 18 ++++++++++++++++++ 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 pyapd/ctl.py create mode 100644 tests/test_ctl.py diff --git a/pyapd/app.py b/pyapd/app.py index ec8d523..39948ee 100644 --- a/pyapd/app.py +++ b/pyapd/app.py @@ -1,7 +1,7 @@ import tornado.web from tornado.routing import URLSpec -from . import stores +from . import stores, ctl class RootHandler(tornado.web.RequestHandler): @@ -25,6 +25,7 @@ class App(tornado.web.Application): def __init__(self, config, *args, **kw): self.config = config self.store = getattr(stores, self.config.backend).Store() + self.ctl = ctl.CtlServer(config) urls = [ (r'/', RootHandler), URLSpec(r'/object/(.*)', handler=ObjectHandler), diff --git a/pyapd/ctl.py b/pyapd/ctl.py new file mode 100644 index 0000000..d134e08 --- /dev/null +++ b/pyapd/ctl.py @@ -0,0 +1,40 @@ +import errno +import functools +import socket + +import tornado.netutil +import tornado.ioloop +import tornado.iostream + + +class CtlServer(): + def __init__(self, config): + self.config = config + self.ctl_socket = tornado.netutil.bind_unix_socket( + self.config.ctl_socket, + ) + io_loop = tornado.ioloop.IOLoop.current() + io_loop.add_handler( + self.ctl_socket.fileno(), + functools.partial(self.connection_ready, self.ctl_socket), + io_loop.READ, + ) + + def connection_ready(self, sock, fd, events): + try: + connection, address = sock.accept() + except socket.error as e: + if e.args[0] not in (errno.EWOULDBLOCK, errno.EAGAIN): + raise + return + connection.setblocking(0) + io_loop = tornado.ioloop.IOLoop.current() + io_loop.spawn_callback(self.handle_connection, connection, address) + + async def handle_connection(self, conn, addr): + stream = tornado.iostream.IOStream(conn) + message = await stream.read_until_close() + await self.get_command(message) + + async def get_command(self, message): + print("got command: ", message) diff --git a/pyapd_config.yaml b/pyapd_config.yaml index 4a485ff..dbfca28 100644 --- a/pyapd_config.yaml +++ b/pyapd_config.yaml @@ -1 +1,3 @@ backend: memory +#ctl_socket: /var/run/pyapd.sock +ctl_socket: ./pyapd.sock diff --git a/tests/data/test_config.yaml b/tests/data/test_config.yaml index 4a485ff..c85a935 100644 --- a/tests/data/test_config.yaml +++ b/tests/data/test_config.yaml @@ -1 +1,2 @@ backend: memory +ctl_socket: tempfile diff --git a/tests/test_ctl.py b/tests/test_ctl.py new file mode 100644 index 0000000..404a430 --- /dev/null +++ b/tests/test_ctl.py @@ -0,0 +1,18 @@ +import os +import tempfile + +from tornado.testing import AsyncTestCase + +from pyapd import ctl, config + + +class TestCtlServer(AsyncTestCase): + def setUp(self): + super().setUp() + self.tempdir = tempfile.TemporaryDirectory() + conf = config.Config('tests/data/test_config.yaml') + conf.ctl_socket = os.path.join(self.tempdir.name, 'pyapd.sock') + self.ctl_server = ctl.CtlServer(conf) + + def test_nothing(self): + pass -- cgit v1.2.3