diff options
| -rw-r--r-- | pyapd/ctl.py | 29 | ||||
| -rw-r--r-- | tests/test_ctl.py | 8 | 
2 files changed, 32 insertions, 5 deletions
diff --git a/pyapd/ctl.py b/pyapd/ctl.py index d134e08..4f5270f 100644 --- a/pyapd/ctl.py +++ b/pyapd/ctl.py @@ -2,13 +2,25 @@ import errno  import functools  import socket +import ruamel.yaml +  import tornado.netutil  import tornado.ioloop  import tornado.iostream +class Commands(): +    def __init__(self): +        pass + +    def ping(self): +        return "pong" + +  class CtlServer():      def __init__(self, config): +        self.yaml = ruamel.yaml.YAML(typ='safe') +        self.commands = Commands()          self.config = config          self.ctl_socket = tornado.netutil.bind_unix_socket(              self.config.ctl_socket, @@ -34,7 +46,20 @@ class CtlServer():      async def handle_connection(self, conn, addr):          stream = tornado.iostream.IOStream(conn)          message = await stream.read_until_close() -        await self.get_command(message) +        res = await self.get_command(message) +        # TODO: we want to send back a result instead of waiting for the +        # connection to close +        # stream.write(res.encode()) +        print("command result was:", res)      async def get_command(self, message): -        print("got command: ", message) +        args = self.yaml.load(message) +        cmd = args.pop('command') +        try: +            res = getattr(self.commands, cmd)(*args) +        except AttributeError: +            res = "no such command" +        except TypeError: +            res = "syntax error" +        # TODO: wrap res in a sensible yaml +        return res diff --git a/tests/test_ctl.py b/tests/test_ctl.py index 404a430..ddac775 100644 --- a/tests/test_ctl.py +++ b/tests/test_ctl.py @@ -1,7 +1,7 @@  import os  import tempfile -from tornado.testing import AsyncTestCase +from tornado.testing import AsyncTestCase, gen_test  from pyapd import ctl, config @@ -14,5 +14,7 @@ class TestCtlServer(AsyncTestCase):          conf.ctl_socket = os.path.join(self.tempdir.name, 'pyapd.sock')          self.ctl_server = ctl.CtlServer(conf) -    def test_nothing(self): -        pass +    @gen_test +    def test_ping_command(self): +        res = yield self.ctl_server.get_command('{"command": "ping"}') +        self.assertIn('pong', res)  | 
