diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2019-05-01 17:59:33 +0200 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2019-05-01 17:59:33 +0200 |
commit | d1bffce0242f721a4bd3a61e88aaff6f6fa49248 (patch) | |
tree | 318898c7f8322827442c1fdcfcfbe6b2afcb46f0 | |
parent | 030d6818c0f6d2d330414feea6fd28b1b60869e7 (diff) |
Start listening for future commands on a socket.
-rw-r--r-- | pyapd/app.py | 3 | ||||
-rw-r--r-- | pyapd/ctl.py | 40 | ||||
-rw-r--r-- | pyapd_config.yaml | 2 | ||||
-rw-r--r-- | tests/data/test_config.yaml | 1 | ||||
-rw-r--r-- | tests/test_ctl.py | 18 |
5 files changed, 63 insertions, 1 deletions
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 |