Skip to content

Commit

Permalink
Fix segmentation fault and use accept_ocket() when connecting to trab…
Browse files Browse the repository at this point in the history
…sient federates
  • Loading branch information
ChadliaJerad committed Jan 3, 2025
1 parent 09d31fe commit 6893de6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 33 deletions.
39 changes: 7 additions & 32 deletions core/federated/RTI/rti_remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ static pqueue_delayed_grant_element_t* pqueue_delayed_grants_find_by_fed_id(pque
return NULL;
for (int i = 1; i <= q->size; i++) {
dge = (pqueue_delayed_grant_element_t*)q->d[i];
if (dge->fed_id == fed_id) {
return dge;
if (dge) {
if (dge->fed_id == fed_id) {
return dge;
}
}
}
return NULL;
Expand All @@ -168,7 +170,6 @@ static pqueue_delayed_grant_element_t* pqueue_delayed_grants_find_by_fed_id(pque
/**
* @brief Insert the delayed grant into the delayed_grants queue and notify.
*
*
* This function assumes the caller holds the rti_mutex.
* @param fed The federate.
* @param tag The tag to grant.
Expand Down Expand Up @@ -2078,35 +2079,11 @@ void send_stop(federate_info_t* fed) {
}

void* lf_connect_to_transient_federates_thread(void* nothing) {
// This loop will continue to accept connections of transient federates, as
// soon as there is room, or enable hot swap
// This loop will continue to accept connections of transient federates, as soon as there is room, or enable hot swap
while (!rti_remote->all_persistent_federates_exited) {
// Continue waiting for an incoming connection requests from transients
// to join, or for hot swap.
// Continue waiting for an incoming connection requests from transients to join, or for hot swap.
// Wait for an incoming connection request.
// struct sockaddr client_fd;
// uint32_t client_length = sizeof(client_fd);
// // // The following blocks until a federate connects.
// int socket_id = -1;
// while (1) {
// if (!rti_remote->all_persistent_federates_exited) {
// return NULL;
// }
// socket_id = accept(rti_remote->socket_descriptor_TCP, &client_fd, &client_length);
// if (socket_id >= 0) {
// // Got a socket
// break;
// } else if (socket_id < 0 && (errno != EAGAIN || errno != EWOULDBLOCK)) {
// lf_print_error_system_failure("RTI failed to accept the socket.");
// } else {
// // Try again
// lf_print_warning("RTI failed to accept the socket. %s. Trying again.", strerror(errno));
// continue;
// }
// }

int socket_id = accept_socket(rti_remote->socket_descriptor_TCP, 1);
// lf_print(">>>>>>>>>>>>>>>>>>>>>>>>>> socket_id %d in 2105 \n", socket_id);
int socket_id = accept_socket(rti_remote->socket_descriptor_TCP, -1);

// Wait for the first message from the federate when RTI -a option is on.
#ifdef __RTI_AUTH__
Expand All @@ -2126,8 +2103,6 @@ void* lf_connect_to_transient_federates_thread(void* nothing) {
// The function also detects if a hot swap request is initiated.
int32_t fed_id = receive_and_check_fed_id_message(&socket_id);

// lf_print(">>>>>>>>>>>>>>>>>>>>>>>>>> socket_id %d in 2125 \n", socket_id);

if (fed_id >= 0 && receive_connection_information(&socket_id, (uint16_t)fed_id) &&
receive_udp_message_and_set_up_clock_sync(&socket_id, (uint16_t)fed_id)) {
LF_MUTEX_LOCK(&rti_mutex);
Expand Down
6 changes: 5 additions & 1 deletion core/federated/federate.c
Original file line number Diff line number Diff line change
Expand Up @@ -2164,7 +2164,11 @@ void* lf_handle_p2p_connections_from_federates(void* env_arg) {
// Extract the ID of the sending federate.
uint16_t remote_fed_id = extract_uint16((unsigned char*)&(buffer[1]));
bool remote_fed_is_transient = buffer[1 + sizeof(uint16_t)];
LF_PRINT_DEBUG("Received sending federate ID %d.", remote_fed_id);
if (remote_fed_is_transient) {
LF_PRINT_DEBUG("Received sending federate ID %d, which is transient.", remote_fed_id);
} else {
LF_PRINT_DEBUG("Received sending federate ID %d, which is persistent.", remote_fed_id);
}

// Trace the event when tracing is enabled
tracepoint_federate_to_federate(receive_FED_ID, _lf_my_fed_id, remote_fed_id, NULL);
Expand Down

0 comments on commit 6893de6

Please sign in to comment.