diff --git a/lgl_host_server.py b/lgl_host_server.py index ed6dd9b..4798236 100755 --- a/lgl_host_server.py +++ b/lgl_host_server.py @@ -28,9 +28,7 @@ Android devices using layers can tunnel their connection using adb reverse to forward a Unix domain socket on the device to a TCP socket on the host. - - adb reverse localabstract:lglcomms tcp:63412 - +This is done automatically by the script. ''' import argparse @@ -60,7 +58,7 @@ def parse_cli() -> argparse.Namespace: help='enable the communications unit test helper service') parser.add_argument( - '--android-port', '-A', type=int, default=62142, + '--android-port', '-A', type=int, default=0, help='enable adb reverse on the specified port for network comms') parser.add_argument( @@ -79,20 +77,9 @@ def main() -> int: ''' args = parse_cli() - # Configure Android adb reverse on the specified port - conn = ADBConnect() - try: - conn.adb( - 'reverse', - 'localabstract:lglcomms', - f'tcp:{args.android_port}') - - except sp.CalledProcessError: - print('ERROR: Could not setup Android network comms') - return 1 - # Create a server instance svr = server.CommsServer(args.android_port) + print(f'Server using host port {svr.port}\n') # Register all the services with it print('Registering host services:') @@ -119,6 +106,18 @@ def main() -> int: svr_thread = threading.Thread(target=svr.run, daemon=True) svr_thread.start() + # Configure Android adb reverse on the specified port + conn = ADBConnect() + try: + conn.adb( + 'reverse', + 'localabstract:lglcomms', + f'tcp:{svr.port}') + + except sp.CalledProcessError: + print('ERROR: Could not setup Android network comms') + return 1 + # Press to exit try: input('Press any key to exit ...\n\n') diff --git a/lglpy/comms/server.py b/lglpy/comms/server.py index 5e0ffb2..ecb35d0 100644 --- a/lglpy/comms/server.py +++ b/lglpy/comms/server.py @@ -153,7 +153,7 @@ class CommsServer: next message. Attributes: - port: The port the server listens on. + port: The port the server listens on, or 0 for auto-assign. endpoints: The mapping of addresses to registered microservices. sockl: The listener socket, or None if not listening. sockd: The client data socket, or None if no client connected. @@ -167,14 +167,22 @@ def __init__(self, port: int): Args: port: The local TCP/IP port to listen on. ''' - self.port = port self.endpoints = {} # type: dict[int, Any] self.register_endpoint(self) self.shutdown = False - self.sockl = None # type: Optional[socket.socket] self.sockd = None # type: Optional[socket.socket] + self.sockl = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.sockl.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + + # Set up the listening socket + self.sockl.bind(('localhost', port)) + self.sockl.listen(1) + + # Work out which port was assigned if not user-defined + self.port = self.sockl.getsockname()[1] + def register_endpoint(self, endpoint: Any) -> int: ''' Register a new service endpoint with the server. @@ -220,13 +228,6 @@ def run(self) -> None: ''' Enter server connection handler run loop. ''' - self.sockl = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.sockl.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - - # Set up the listening socket - self.sockl.bind(('localhost', self.port)) - self.sockl.listen(1) - # Accept a new client connection and assign a data socket while not self.shutdown: print('Waiting for client connection') @@ -250,7 +251,6 @@ def run(self) -> None: continue self.sockl.close() - self.sockl = None def run_client(self) -> None: '''