Skip to content

Commit

Permalink
Make create_server to one integrated function of TCP and UDP.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakio815 committed Dec 26, 2024
1 parent 86f8395 commit b2a5af8
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 31 deletions.
6 changes: 3 additions & 3 deletions core/federated/RTI/rti_remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -1508,15 +1508,15 @@ void initialize_federate(federate_info_t* fed, uint16_t id) {
int32_t start_rti_server(uint16_t port) {
_lf_initialize_clock();
// Create the TCP socket server
if (create_TCP_server(port, &rti_remote->socket_descriptor_TCP, &rti_remote->final_port_TCP, true)) {
if (create_server(port, &rti_remote->socket_descriptor_TCP, &rti_remote->final_port_TCP, TCP, true)) {
lf_print_error_system_failure("RTI failed to create TCP server: %s.", strerror(errno));
};
lf_print("RTI: Listening for federates.");
// Create the UDP socket server
// Try to get the rti_remote->final_port_TCP + 1 port
if (rti_remote->clock_sync_global_status >= clock_sync_on) {
if (create_UDP_server(rti_remote->final_port_TCP + 1, &rti_remote->socket_descriptor_UDP,
&rti_remote->final_port_UDP, true)) {
if (create_server(rti_remote->final_port_TCP + 1, &rti_remote->socket_descriptor_UDP,
&rti_remote->final_port_UDP, UDP, true)) {
lf_print_error_system_failure("RTI failed to create UDP server: %s.", strerror(errno));
}
}
Expand Down
3 changes: 1 addition & 2 deletions core/federated/RTI/rti_remote.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@

#include "lf_types.h"
#include "pqueue_tag.h"
#include "socket_common.h"

/** Time allowed for federates to reply to stop request. */
#define MAX_TIME_FOR_REPLY_TO_STOP_REQUEST SEC(30)

/////////////////////////////////////////////
//// Data structures

typedef enum socket_type_t { TCP, UDP } socket_type_t;

/**
* Information about a federate known to the RTI, including its runtime state,
* mode of execution, and connectivity with other federates.
Expand Down
2 changes: 1 addition & 1 deletion core/federated/federate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1942,7 +1942,7 @@ void lf_connect_to_rti(const char* hostname, int port) {

void lf_create_server(int specified_port) {
assert(specified_port <= UINT16_MAX && specified_port >= 0);
if (create_TCP_server(specified_port, &_fed.server_socket, (uint16_t*)&_fed.server_port, false)) {
if (create_server(specified_port, &_fed.server_socket, (uint16_t*)&_fed.server_port, TCP, false)) {
lf_print_error_system_failure("RTI failed to create TCP server: %s.", strerror(errno));
};
LF_PRINT_LOG("Server for communicating with other federates started using port %d.", _fed.server_port);
Expand Down
17 changes: 4 additions & 13 deletions core/federated/network/socket_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,10 @@ static int set_socket_bind_option(int socket_descriptor, uint16_t specified_port
return used_port;
}

static int create_server(uint16_t port, int* final_socket, uint16_t* final_port, int sock_type,
bool increment_port_on_retry) {
int create_server(uint16_t port, int* final_socket, uint16_t* final_port, socket_type_t sock_type, bool increment_port_on_retry) {
int socket_descriptor;
struct timeval timeout_time;
if (sock_type == 0) {
if (sock_type == TCP) {
// Create an IPv4 socket for TCP.
socket_descriptor = create_real_time_tcp_socket_errexit();
// Set the timeout time for the communications of the server
Expand All @@ -149,14 +148,14 @@ static int create_server(uint16_t port, int* final_socket, uint16_t* final_port,
timeout_time =
(struct timeval){.tv_sec = UDP_TIMEOUT_TIME / BILLION, .tv_usec = (UDP_TIMEOUT_TIME % BILLION) / 1000};
}
char* type = (sock_type == 0) ? "TCP" : "UDP";
char* type = (sock_type == TCP) ? "TCP" : "UDP";
if (socket_descriptor < 0) {
lf_print_error("Failed to create %s socket.", type);
return -1;
}
set_socket_timeout_option(socket_descriptor, &timeout_time);
int used_port = set_socket_bind_option(socket_descriptor, port, increment_port_on_retry);
if (sock_type == 0) {
if (sock_type == TCP) {
// Enable listening for socket connections.
// The second argument is the maximum number of queued socket requests,
// which according to the Mac man page is limited to 128.
Expand All @@ -170,14 +169,6 @@ static int create_server(uint16_t port, int* final_socket, uint16_t* final_port,
return 0;
}

int create_TCP_server(uint16_t port, int* final_socket, uint16_t* final_port, bool increment_port_on_retry) {
return create_server(port, final_socket, final_port, 0, increment_port_on_retry);
}

int create_UDP_server(uint16_t port, int* final_socket, uint16_t* final_port, bool increment_port_on_retry) {
return create_server(port, final_socket, final_port, 1, increment_port_on_retry);
}

/**
* Return true if either the socket to the RTI is broken or the socket is
* alive and the first unread byte on the socket's queue is MSG_TYPE_FAILED.
Expand Down
16 changes: 4 additions & 12 deletions include/core/federated/network/socket_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
*/
#define MSG_TYPE_FAILED 25

typedef enum socket_type_t { TCP, UDP } socket_type_t;

/**
* Mutex protecting socket close operations.
*/
Expand Down Expand Up @@ -93,21 +95,11 @@ int create_real_time_tcp_socket_errexit();
* @param port The port number to use or 0 to let the OS pick or 1 to start trying at DEFAULT_PORT.
* @param final_socket Pointer to the returned socket descriptor on which accepting connections will occur.
* @param final_port Pointer to the final port the server will use.
* @param sock_type Type of the socket wheter TCP or UDP.
* @param increment_port_on_retry Boolean to retry port increment.
* @return 0 for success, -1 for failure.
*/
int create_TCP_server(uint16_t port, int* final_socket, uint16_t* final_port, bool increment_port_on_retry);
/**
* @brief Create a UDP server that listens for socket connections.
*
* This function is just like create_TCP_server(), except that it creates a UDP server.
*
* @param port The port number to use or 0 to let the OS pick or 1 to start trying at DEFAULT_PORT.
* @param final_socket Pointer to the returned socket descriptor on which accepting connections will occur.
* @param final_port Pointer to the final port the server will use.
* @return 0 for success, -1 for failure.
*/
int create_UDP_server(uint16_t port, int* final_socket, uint16_t* final_port, bool increment_port_on_retry);
int create_server(uint16_t port, int* final_socket, uint16_t* final_port, socket_type_t sock_type, bool increment_port_on_retry);

/**
* These two functions waits for an incoming connection request on the specified server socket.
Expand Down

0 comments on commit b2a5af8

Please # to comment.