Skip to content

Commit

Permalink
Merge in port-search-removal
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardalee committed Dec 18, 2023
2 parents 4192bd6 + ff7aa08 commit e86b44f
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 296 deletions.
2 changes: 1 addition & 1 deletion core/federated/RTI/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void usage(int argc, const char* argv[]) {
lf_print(" -n, --number_of_federates <n>");
lf_print(" The number of federates in the federation that this RTI will control.\n");
lf_print(" -p, --port <n>");
lf_print(" The port number to use for the RTI. Must be larger than 0 and smaller than %d. Default is %d.\n", UINT16_MAX, STARTING_PORT);
lf_print(" The port number to use for the RTI. Must be larger than 0 and smaller than %d. Default is %d.\n", UINT16_MAX, DEFAULT_PORT);
lf_print(" -c, --clock_sync [off|init|on] [period <n>] [exchanges-per-interval <n>]");
lf_print(" The status of clock synchronization for this federate.");
lf_print(" - off: Clock synchronization is off.");
Expand Down
31 changes: 11 additions & 20 deletions core/federated/RTI/rti_remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ int create_server(int32_t specified_port, uint16_t port, socket_type_t socket_ty

/*
* The following used to permit reuse of a port that an RTI has previously
* used that has not been released. We no longer do this, but instead
* increment the port number until an available port is found.
* used that has not been released. We no longer do this, and instead retry
* some number of times after waiting.
// SO_REUSEPORT (since Linux 3.9)
// Permits multiple AF_INET or AF_INET6 sockets to be bound to an
Expand Down Expand Up @@ -127,28 +127,19 @@ int create_server(int32_t specified_port, uint16_t port, socket_type_t socket_ty
(struct sockaddr *) &server_fd,
sizeof(server_fd));

// If the binding fails with this port and no particular port was specified
// in the LF program, then try the next few ports in sequence.
while (result != 0
&& specified_port == 0
&& port >= STARTING_PORT
&& port <= STARTING_PORT + PORT_RANGE_LIMIT) {
lf_print("RTI failed to get port %d. Trying %d.", port, port + 1);
port++;
// Try repeatedly to bind to the specified port.
int count = 1;
while (result != 0 && count++ < PORT_BIND_RETRY_LIMIT) {
lf_print("RTI failed to get port %d. Will try again.", port);
lf_sleep(PORT_BIND_RETRY_INTERVAL);
server_fd.sin_port = htons(port);
result = bind(
socket_descriptor,
(struct sockaddr *) &server_fd,
sizeof(server_fd));
}
if (result != 0) {
if (specified_port == 0) {
lf_print_error_system_failure("Failed to bind the RTI socket. Cannot find a usable port. "
"Consider increasing PORT_RANGE_LIMIT in net_common.h.");
} else {
lf_print_error_system_failure("Failed to bind the RTI socket. Specified port is not available. "
"Consider leaving the port unspecified");
}
lf_print_error_and_exit("Failed to bind the RTI socket. Port %d is not available. ", port);
}
char* type = "TCP";
if (socket_type == UDP) {
Expand Down Expand Up @@ -1019,7 +1010,7 @@ void handle_federate_resign(federate_info_t *my_fed) {
// an orderly shutdown.
// close(my_fed->socket); // from unistd.h

lf_print("Federate %d has resigned.", my_fed->enclave.id);
lf_print("RTI: Federate %d has resigned.", my_fed->enclave.id);

// Check downstream federates to see whether they should now be granted a TAG.
// To handle cycles, need to create a boolean array to keep
Expand Down Expand Up @@ -1563,8 +1554,8 @@ void initialize_federate(federate_info_t* fed, uint16_t id) {
int32_t start_rti_server(uint16_t port) {
int32_t specified_port = port;
if (port == 0) {
// Use the default starting port.
port = STARTING_PORT;
// Use the default port.
port = DEFAULT_PORT;
}
_lf_initialize_clock();
// Create the TCP socket server
Expand Down
Loading

0 comments on commit e86b44f

Please sign in to comment.