Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-assign ports for the host server by default #64

Merged
merged 2 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 19 additions & 1 deletion lglpy/comms/service_gpu_timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ def get_service_name(self) -> str:
'''
return 'GPUTimeline'

def handle_device(self, msg: Any) -> None:
'''
Handle a device config packet.

Args:
msg: The Python decode of a JSON payload.
'''
# Reset the local frame state for the next frame
major = msg["driverMajor"]
minor = msg["driverMinor"]
patch = msg["driverPatch"]

print(f'Device: {msg["deviceName"]}')
print(f'Driver: r{major}p{minor} ({patch})')

def handle_frame(self, msg: Any) -> None:
'''
Handle a frame boundary workload.
Expand Down Expand Up @@ -187,7 +202,10 @@ def handle_message(self, message: Message) -> None:

payload_type = payload['type']

if payload_type == 'frame':
if payload_type == 'device':
self.handle_device(payload)

elif payload_type == 'frame':
self.handle_frame(payload)

elif payload_type == 'submit':
Expand Down