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

Gamecontroller can Bind to a Different Port Based on Command Line Argument #3207

Merged
merged 33 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
bacfacf
chnage port depending on argument
Mr-Anyone May 20, 2024
d620120
added docs
Mr-Anyone May 20, 2024
1557448
remove stuff
Mr-Anyone May 20, 2024
5478f15
updated entry points
Mr-Anyone May 20, 2024
8d394cd
fixed stuff
Mr-Anyone May 21, 2024
0b5d2a6
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] May 21, 2024
5d5e130
copied code from nima
Mr-Anyone May 22, 2024
53b3120
added some new port to fix
Mr-Anyone May 22, 2024
c052a97
change bind address and stuff like that
Mr-Anyone May 23, 2024
ace67d6
added new stuff to robot communication
Mr-Anyone May 23, 2024
730753a
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] May 23, 2024
9b8d59d
added new stuff for robot communication
Mr-Anyone May 23, 2024
9453411
Merge branch 'gamecontroller_port' of github.com:Mr-Anyone/Thunderbot…
Mr-Anyone May 23, 2024
ebcc8d6
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] May 23, 2024
6ad2b47
merged arune's changes
Mr-Anyone May 31, 2024
e730860
added new feature that arune requested
Mr-Anyone Jun 11, 2024
60dad81
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] Jun 11, 2024
b44cdcf
added requested feature
Mr-Anyone Jun 26, 2024
0c984eb
added comments and remove necessary print statements
Mr-Anyone Jun 26, 2024
749fb3f
remove code that are non necessary
Mr-Anyone Jun 26, 2024
f3a42e7
removed one line of legacy code
Mr-Anyone Jun 26, 2024
bed3109
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] Jun 26, 2024
51f42e2
Merge branch 'gamecontroller_port' of github.com:Mr-Anyone/Thunderbot…
Mr-Anyone Jun 26, 2024
1e8fcba
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] Jun 26, 2024
3218a32
refactored function
Mr-Anyone Jun 28, 2024
b147096
Merge branch 'gamecontroller_port' of github.com:Mr-Anyone/Thunderbot…
Mr-Anyone Jun 28, 2024
cf668bf
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] Jun 28, 2024
036da38
added comments to make things make more sense
Mr-Anyone Jul 2, 2024
4f1fbe5
Merge branch 'gamecontroller_port' of github.com:Mr-Anyone/Thunderbot…
Mr-Anyone Jul 2, 2024
9a3ef5f
address some comments
Mr-Anyone Jul 3, 2024
82e6f87
resolved some comments
Mr-Anyone Jul 5, 2024
c51c412
made new changes
Mr-Anyone Jul 5, 2024
e616430
[pre-commit.ci lite] apply automatic fixes
pre-commit-ci-lite[bot] Jul 5, 2024
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
10 changes: 9 additions & 1 deletion src/software/field_tests/field_test_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,13 @@ def load_command_line_arguments():
help="Estop Baudrate",
)

parser.add_argument(
"--gamecontroller_port",
type=int,
default=None,
help="The port number for the game controller.",
)

parser.add_argument(
"--run_yellow",
action="store_true",
Expand Down Expand Up @@ -382,7 +389,8 @@ def field_test_runner():
enable_radio=args.enable_radio,
) as rc_friendly:
with Gamecontroller(
supress_logs=(not args.show_gamecontroller_logs)
supress_logs=(not args.show_gamecontroller_logs),
gamecontroller_port=args.gamecontroller_port,
) as gamecontroller:
friendly_fs.setup_proto_unix_io(friendly_proto_unix_io)
rc_friendly.setup_for_fullsystem()
Expand Down
10 changes: 9 additions & 1 deletion src/software/simulated_tests/simulated_test_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,13 @@ def load_command_line_arguments():
default=False,
help="Use realism in the simulator",
)

parser.add_argument(
"--gamecontroller_port",
type=int,
default=None,
help="The port number for the game controller.",
)
return parser.parse_args()


Expand Down Expand Up @@ -542,7 +549,8 @@ def simulated_test_runner():
should_restart_on_crash=False,
) as yellow_fs:
with Gamecontroller(
supress_logs=(not args.show_gamecontroller_logs)
supress_logs=(not args.show_gamecontroller_logs),
gamecontroller_port=args.gamecontroller_port,
) as gamecontroller:

blue_fs.setup_proto_unix_io(blue_full_system_proto_unix_io)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Gamecontroller(object):
REFEREE_IP = "224.5.23.1"
CI_MODE_OUTPUT_RECEIVE_BUFFER_SIZE = 9000

def __init__(self, supress_logs: bool = False) -> None:
def __init__(self, supress_logs: bool = False, gamecontroller_port=None) -> None:
"""Run Gamecontroller

:param supress_logs: Whether to suppress the logs
Expand All @@ -37,7 +37,12 @@ def __init__(self, supress_logs: bool = False) -> None:
# We need to find 2 free ports to use for the gamecontroller
# so that we can run multiple gamecontroller instances in parallel
self.referee_port = self.next_free_port()
self.ci_port = self.next_free_port()

self.ci_port = gamecontroller_port
if self.ci_port is None:
self.ci_port = self.next_free_port(40000)
else:
self.ci_port = self.next_free_port(self.ci_port)

# this allows gamecontroller to listen to override commands
self.command_override_buffer = ThreadSafeBuffer(
Expand Down Expand Up @@ -102,7 +107,23 @@ def refresh(self):
)
manual_command = self.command_override_buffer.get(return_cached=False)

def next_free_port(self, port: int = 40000, max_port: int = 65535) -> None:
def is_valid_port(self, port):
Mr-Anyone marked this conversation as resolved.
Show resolved Hide resolved
"""
Check if a port is available

:param port: the port we are checking
:return: True if the port is available, False otherwise
"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
sock.bind(("", port))
sock.close()
return True
except OSError:
return False

def next_free_port(self, start_port: int = 40000, max_port: int = 65535) -> None:
Mr-Anyone marked this conversation as resolved.
Show resolved Hide resolved
"""Find the next free port. We need to find 2 free ports to use for the gamecontroller
so that we can run multiple gamecontroller instances in parallel.

Expand All @@ -111,15 +132,10 @@ def next_free_port(self, port: int = 40000, max_port: int = 65535) -> None:
:return: The next free port

"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

while port <= max_port:
try:
sock.bind(("", port))
sock.close()
return port
except OSError:
port += 1
while start_port <= max_port:
if self.is_valid_port(start_port):
return start_port
start_port += 1

raise IOError("no free ports")

Expand Down
10 changes: 9 additions & 1 deletion src/software/thunderscope/thunderscope_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@
help="Whether to populate with default robot positions (False) or start with an empty field (True) for AI vs AI",
)

parser.add_argument(
"--gamecontroller_port",
type=int,
default=None,
help="The port number for the game controller.",
)

args = parser.parse_args()

# Sanity check that an interface was provided
Expand Down Expand Up @@ -428,7 +435,8 @@ def __ticker(tick_rate_ms: int) -> None:
should_restart_on_crash=False,
run_sudo=args.sudo,
) as yellow_fs, Gamecontroller(
supress_logs=(not args.verbose)
supress_logs=(not args.verbose),
gamecontroller_port=args.gamecontroller_port,
) as gamecontroller, (
# Here we only initialize autoref if the --enable_autoref flag is requested.
# To avoid nested Python withs, the autoref is initialized as None when this flag doesn't exist.
Expand Down
Loading