mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-09-21 14:57:42 +00:00
Autogenerated type safety parameterisation of all methods
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import threading
|
||||
@@ -44,15 +46,15 @@ MOTD = (
|
||||
class TelnetServer:
|
||||
"""A telnet server designed to provide spots in the same format as DXSpider, for compatibility with desktop loggers."""
|
||||
|
||||
def __init__(self):
|
||||
self._port = None
|
||||
self._running = False
|
||||
self._clients = set()
|
||||
self._loop = None
|
||||
self._thread = None
|
||||
def __init__(self) -> None:
|
||||
self._port: int | None = None
|
||||
self._running: bool = False
|
||||
self._clients: set[asyncio.StreamWriter] = set()
|
||||
self._loop: asyncio.AbstractEventLoop | None = None
|
||||
self._thread: threading.Thread | None = None
|
||||
self._shutdown_event = asyncio.Event()
|
||||
|
||||
def start(self, port=7373):
|
||||
def start(self, port: int = 7373) -> None:
|
||||
"""Starts the telnet server"""
|
||||
|
||||
self._port = port
|
||||
@@ -69,7 +71,7 @@ class TelnetServer:
|
||||
DATA_STORE.spots.add_listener(self.publish)
|
||||
self._running = True
|
||||
|
||||
async def _start_internal(self):
|
||||
async def _start_internal(self) -> None:
|
||||
"""Start method (async). Sets up the telnet server and waits for shutdown."""
|
||||
|
||||
self._loop = asyncio.get_running_loop()
|
||||
@@ -81,7 +83,7 @@ class TelnetServer:
|
||||
|
||||
await self._stop_internal()
|
||||
|
||||
async def _handle_client(self, reader, writer):
|
||||
async def _handle_client(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
|
||||
"""Handles a new client connection"""
|
||||
|
||||
logger.debug("Telnet client connected")
|
||||
@@ -131,7 +133,7 @@ class TelnetServer:
|
||||
writer.close()
|
||||
await writer.wait_closed()
|
||||
|
||||
def stop(self):
|
||||
def stop(self) -> None:
|
||||
"""Stops the telnet server"""
|
||||
|
||||
self._running = False
|
||||
@@ -147,7 +149,7 @@ class TelnetServer:
|
||||
def client_count(self) -> int:
|
||||
return len(self._clients)
|
||||
|
||||
async def _stop_internal(self):
|
||||
async def _stop_internal(self) -> None:
|
||||
"""Stops the telnet server"""
|
||||
|
||||
for writer in list(self._clients):
|
||||
@@ -159,13 +161,13 @@ class TelnetServer:
|
||||
logger.debug("Exception while closing telnet client connection", exc_info=True)
|
||||
self._clients.clear()
|
||||
|
||||
def publish(self, spot: Spot):
|
||||
def publish(self, spot: Spot) -> None:
|
||||
"""Callback from the data store when a spot is added"""
|
||||
|
||||
if self._running and self._clients and self._loop and self._loop.is_running():
|
||||
asyncio.run_coroutine_threadsafe(self._broadcast_spot_internal(spot), self._loop)
|
||||
|
||||
async def _broadcast_spot_internal(self, spot: Spot):
|
||||
async def _broadcast_spot_internal(self, spot: Spot) -> None:
|
||||
"""Internal version, run on async loop for thread safety?"""
|
||||
|
||||
# Ensure ASCII formatting for telnet clients
|
||||
@@ -173,7 +175,7 @@ class TelnetServer:
|
||||
|
||||
# Try to write to all clients, and in the process find the ones that are disconnected. Iterate over a copy
|
||||
# since a new client can connect (mutating self._clients) while we're awaiting a write below.
|
||||
disconnected_clients = set()
|
||||
disconnected_clients: set[asyncio.StreamWriter] = set()
|
||||
for writer in list(self._clients):
|
||||
try:
|
||||
writer.write(encoded_line)
|
||||
|
||||
Reference in New Issue
Block a user