From 0775f3d0c6ea9b4d6bd66ac6fb49062060843eec Mon Sep 17 00:00:00 2001 From: Arksine Date: Sat, 8 Aug 2020 11:28:05 -0400 Subject: [PATCH] moonraker: Connect to Klippy as a client Signed-off-by: Eric Callahan --- moonraker/confighelper.py | 4 +--- moonraker/moonraker.py | 45 ++++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/moonraker/confighelper.py b/moonraker/confighelper.py index 319bc17..e1f2d44 100644 --- a/moonraker/confighelper.py +++ b/moonraker/confighelper.py @@ -83,7 +83,5 @@ def get_configuration(server, cmd_line_args): if server_cfg.get('enable_debug_logging', True): logging.getLogger().setLevel(logging.DEBUG) - config['cmd_args'] = { - 'logfile': cmd_line_args.logfile, - 'socketfile': cmd_line_args.socketfile} + config['cmd_args'] = {'logfile': cmd_line_args.logfile} return ConfigHelper(server, config, 'server') diff --git a/moonraker/moonraker.py b/moonraker/moonraker.py index ab624da..4ab50bc 100644 --- a/moonraker/moonraker.py +++ b/moonraker/moonraker.py @@ -11,9 +11,6 @@ import time import socket import logging import json -import errno -import tornado -import tornado.netutil import confighelper from tornado import gen, iostream from tornado.ioloop import IOLoop, PeriodicCallback @@ -42,12 +39,8 @@ class Server: self.events = {} # Klippy Connection Handling - socketfile = config['cmd_args'].get('socketfile', "/tmp/moonraker") - socketfile = os.path.normpath(os.path.expanduser(socketfile)) - self.klippy_server_sock = tornado.netutil.bind_unix_socket( - socketfile, backlog=1) - self.remove_server_sock = tornado.netutil.add_accept_handler( - self.klippy_server_sock, self._handle_klippy_connection) + self.klippy_address = config.get( + 'klippy_uds_address', "/tmp/klippy_uds") self.klippy_iostream = None self.is_klippy_ready = False self.moonraker_available = False @@ -85,6 +78,7 @@ class Server: (self.host, self.port)) self.moonraker_app.listen(self.host, self.port) self.server_running = True + self.ioloop.spawn_callback(self._connect_klippy) # ***** Plugin Management ***** def _load_plugins(self, config): @@ -147,13 +141,22 @@ class Server: self.remote_methods[method_name] = cb # ***** Klippy Connection ***** - def _handle_klippy_connection(self, conn, addr): - if self.klippy_iostream is not None and \ - not self.klippy_iostream.closed(): - logging.info("New Connection received while Klippy Connected") - self.klippy_iostream.close() + async def _connect_klippy(self): + ksock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + kstream = iostream.IOStream(ksock) + try: + await kstream.connect(self.klippy_address) + except iostream.StreamClosedError: + # Klippy Socket Server not available + self.ioloop.call_later(1., self._connect_klippy) + return + await gen.sleep(0.5) + if kstream.closed(): + # Klippy Connection was rejected + self.ioloop.call_later(1., self._connect_klippy) + return logging.info("Klippy Connection Established") - self.klippy_iostream = iostream.IOStream(conn) + self.klippy_iostream = kstream self.klippy_iostream.set_close_callback( self._handle_stream_closed) self.ioloop.spawn_callback( @@ -178,23 +181,28 @@ class Server: if cb is not None: cb(**params) else: - logging.info("Unknown command received %s" % cmd.decode()) + logging.info("Unknown command received %s" % data.decode()) except Exception: logging.exception( "Error processing Klippy Host Response: %s" - % (cmd.decode())) + % (data.decode())) def _handle_stream_closed(self): self.is_klippy_ready = False self.moonraker_available = False + self.klippy_iostream = None self.init_cb.stop() for request in self.pending_requests.values(): request.notify(ServerError("Klippy Disconnected", 503)) self.pending_requests = {} logging.info("Klippy Connection Removed") self.send_event("server:klippy_state_changed", "disconnect") + self.ioloop.call_later(1., self._connect_klippy) async def send_klippy_request(self, request): + if self.klippy_iostream is None: + request.notify(ServerError("Klippy Host not connected", 503)) + return data = json.dumps(request.to_dict()).encode() + b"\x03" try: await self.klippy_iostream.write(data) @@ -356,9 +364,6 @@ def main(): "-c", "--configfile", default="~/moonraker.conf", metavar='', help="Location of moonraker configuration file") - parser.add_argument( - "-s", "--socketfile", default="/tmp/moonraker", metavar='', - help="file name and location for the Unix Domain Socket") parser.add_argument( "-l", "--logfile", default="/tmp/moonraker.log", metavar='', help="log file name and location")