Skip to content

Commit

Permalink
Use port auto-assignment by default
Browse files Browse the repository at this point in the history
  • Loading branch information
solidpixel committed Jan 30, 2025
1 parent 7277c85 commit a7444ee
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
31 changes: 15 additions & 16 deletions lgl_host_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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:')
Expand All @@ -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')
Expand Down
22 changes: 11 additions & 11 deletions lglpy/comms/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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')
Expand All @@ -250,7 +251,6 @@ def run(self) -> None:
continue

self.sockl.close()
self.sockl = None

def run_client(self) -> None:
'''
Expand Down

0 comments on commit a7444ee

Please sign in to comment.