Skip to content

Commit 6b1b364

Browse files
committed
socket_comms: add helpers for server-port support
Add a argparse helper to add a `--server-port` argument to tools that need it. This can be used to specify a different UDP port to allow for multiple gateway scripts and corresponding connections to run simultaneously. Signed-off-by: Aeyohan Furtado <aeyohan@embeint.com>
1 parent 11c4108 commit 6b1b364

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

src/infuse_iot/socket_comms.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python3
22

3+
import argparse
34
import enum
45
import json
56
import socket
@@ -21,6 +22,23 @@
2122
def default_multicast_address(port: int = 8751) -> tuple[str, int]:
2223
return ("224.1.1.1", port)
2324

25+
def server_port_parser(arg_port: str) -> tuple[str, int]:
26+
port = int(arg_port)
27+
if not (0 < port < 65536):
28+
raise argparse.ArgumentError(None, f"Invalid `--server-port`: {port}")
29+
if port % 2 == 0:
30+
raise argparse.ArgumentError(None, f"`--server-port` must be odd: {port}")
31+
return default_multicast_address(port)
32+
33+
def add_server_port_parser(parser: argparse.ArgumentParser, multi_port: bool = False):
34+
parser.add_argument(
35+
'--server-port',
36+
dest='server_sock',
37+
default=[default_multicast_address()] if multi_port else default_multicast_address(),
38+
type=server_port_parser,
39+
nargs= '+' if multi_port else None,
40+
help="Alternate port to use for Gateway connections (default 8751)"
41+
)
2442

2543
class ClientNotification:
2644
class Type(enum.IntEnum):

0 commit comments

Comments
 (0)