summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxim Devaev <[email protected]>2022-04-11 16:34:07 +0300
committerMaxim Devaev <[email protected]>2022-04-11 16:34:30 +0300
commit80b5546d21cb5c67aeb4f28f339fa4a029bc88c2 (patch)
tree5db9801bcf7697e540c12d6226bfdb192c026ab7
parent6bd2b9c6809294702320c0dd5dfdfb0fbef204d7 (diff)
refactoring
-rw-r--r--kvmd/apps/kvmd/__init__.py4
-rw-r--r--kvmd/apps/kvmd/server.py28
-rw-r--r--kvmd/htserver.py14
3 files changed, 25 insertions, 21 deletions
diff --git a/kvmd/apps/kvmd/__init__.py b/kvmd/apps/kvmd/__init__.py
index 3de764fd..f7536577 100644
--- a/kvmd/apps/kvmd/__init__.py
+++ b/kvmd/apps/kvmd/__init__.py
@@ -100,14 +100,12 @@ def main(argv: Optional[List[str]]=None) -> None:
**config.snapshot._unpack(),
),
- heartbeat=config.server.heartbeat,
-
keymap_path=config.hid.keymap,
ignore_keys=config.hid.ignore_keys,
mouse_x_range=(config.hid.mouse_x_range.min, config.hid.mouse_x_range.max),
mouse_y_range=(config.hid.mouse_y_range.min, config.hid.mouse_y_range.max),
stream_forever=config.streamer.forever,
- ).run(**config.server._unpack(ignore=["heartbeat"]))
+ ).run(**config.server._unpack())
get_logger(0).info("Bye-bye")
diff --git a/kvmd/apps/kvmd/server.py b/kvmd/apps/kvmd/server.py
index e7bc744d..f40381a9 100644
--- a/kvmd/apps/kvmd/server.py
+++ b/kvmd/apps/kvmd/server.py
@@ -157,8 +157,6 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins
streamer: Streamer,
snapshoter: Snapshoter,
- heartbeat: float,
-
keymap_path: str,
ignore_keys: List[str],
mouse_x_range: Tuple[int, int],
@@ -173,8 +171,6 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins
self.__snapshoter = snapshoter # Not a component: No state or cleanup
self.__user_gpio = user_gpio # Has extra state "gpio_scheme_state"
- self.__heartbeat = heartbeat
-
self.__stream_forever = stream_forever
self.__components = [
@@ -254,13 +250,9 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins
@exposed_http("GET", "/ws")
async def __ws_handler(self, request: aiohttp.web.Request) -> aiohttp.web.WebSocketResponse:
- logger = get_logger(0)
-
- client = _WsClient(
- ws=aiohttp.web.WebSocketResponse(heartbeat=self.__heartbeat),
- stream=valid_bool(request.query.get("stream", "true")),
- )
- await client.ws.prepare(request)
+ stream = valid_bool(request.query.get("stream", "true"))
+ ws = await self._make_ws_response(request)
+ client = _WsClient(ws, stream)
await self.__register_ws_client(client)
try:
@@ -281,27 +273,27 @@ class KvmdServer(HttpServer): # pylint: disable=too-many-arguments,too-many-ins
))
for stage in [stage1, stage2]:
await asyncio.gather(*[
- send_ws_event(client.ws, event_type, events.pop(event_type))
+ send_ws_event(ws, event_type, events.pop(event_type))
for (event_type, _) in stage
])
- await send_ws_event(client.ws, "loop", {})
+ await send_ws_event(ws, "loop", {})
- async for msg in client.ws:
+ async for msg in ws:
if msg.type != aiohttp.web.WSMsgType.TEXT:
break
try:
(event_type, event) = parse_ws_event(msg.data)
except Exception as err:
- logger.error("Can't parse JSON event from websocket: %r", err)
+ get_logger(0).error("Can't parse JSON event from websocket: %r", err)
else:
handler = self.__ws_handlers.get(event_type)
if handler:
- await handler(client.ws, event)
+ await handler(ws, event)
else:
- logger.error("Unknown websocket event: %r", msg.data)
+ get_logger(0).error("Unknown websocket event: %r", msg.data)
- return client.ws
+ return ws
finally:
await self.__remove_ws_client(client)
diff --git a/kvmd/htserver.py b/kvmd/htserver.py
index 6196b7a1..34b38846 100644
--- a/kvmd/htserver.py
+++ b/kvmd/htserver.py
@@ -255,9 +255,12 @@ class HttpServer:
unix_path: str,
unix_rm: bool,
unix_mode: int,
+ heartbeat: float,
access_log_format: str,
) -> None:
+ self.__heartbeat = heartbeat # pylint: disable=attribute-defined-outside-init
+
if unix_rm and os.path.exists(unix_path):
os.remove(unix_path)
server_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
@@ -274,6 +277,15 @@ class HttpServer:
loop=asyncio.get_event_loop(),
)
+ # =====
+
+ async def _make_ws_response(self, request: Request) -> WebSocketResponse:
+ ws = WebSocketResponse(heartbeat=self.__heartbeat)
+ await ws.prepare(request)
+ return ws
+
+ # =====
+
async def _init_app(self, app: Application) -> None:
raise NotImplementedError
@@ -283,6 +295,8 @@ class HttpServer:
async def _on_cleanup(self, app: Application) -> None:
_ = app
+ # =====
+
async def __make_app(self) -> Application:
app = Application(middlewares=[normalize_path_middleware(
append_slash=False,