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

HITL - Add cross-process signal to toggle inflow of new connections. #1953

Merged
merged 1 commit into from
May 14, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

from multiprocessing import Queue
from multiprocessing import Queue, Value
from typing import Any, List

from habitat_hitl.core.types import (
Expand All @@ -27,6 +27,15 @@ def __init__(self, networking_config) -> None:
self._connection_record_queue: Queue[ConnectionRecord] = Queue()
self._disconnection_record_queue: Queue[DisconnectionRecord] = Queue()
self._kick_signal_queue: Queue[int] = Queue()
self._allow_new_connections = Value("b", False)

def enable_new_connections(self, enabled: bool):
"""Signal the networking process whether it should accept new connections."""
self._allow_new_connections.value = enabled # type: ignore

def new_connections_allowed(self) -> bool:
"""Get whether new connections are allowed."""
return self._allow_new_connections.value # type: ignore

def send_keyframe_to_networking_thread(
self, keyframe: KeyframeAndMessages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,28 @@ async def send_keyframes(self) -> None:
await asyncio.sleep(wait_time)

def has_connection(self) -> bool:
"""
Returns true if at least one user is connected.
"""
return len(self._connected_clients) > 0

def is_server_available(self) -> bool:
"""
Returns true if the server should advertise itself as available.
"""
return (
not self.has_connection()
and self._interprocess_record.new_connections_allowed()
)

def can_accept_connection(self) -> bool:
"""
Returns true if the server had the capacity for a new connection.
"""
return (
len(self._connected_clients)
< self._networking_config.max_client_count
and self._interprocess_record.new_connections_allowed()
)

def handle_disconnect(self, connection_id: int) -> None:
Expand Down Expand Up @@ -496,7 +512,7 @@ async def http_handler(request):
# return an HTTP code to indicate available or not
code = (
networking_config.http_availability_server.code_available
if not network_mgr.has_connection()
if network_mgr.is_server_available()
else networking_config.http_availability_server.code_unavailable
)
# print(f"Returned availability HTTP code {code}")
Expand Down