From fc72642193f663642293455961d98ae9b8a9ac98 Mon Sep 17 00:00:00 2001 From: Anirudh Rengarajan <44007330+arengarajan99@users.noreply.github.com> Date: Mon, 13 Feb 2023 13:52:19 -0800 Subject: [PATCH 01/12] Remove includes of .c files within RTI --- core/federated/RTI/CMakeLists.txt | 3 +++ core/federated/RTI/rti.c | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/core/federated/RTI/CMakeLists.txt b/core/federated/RTI/CMakeLists.txt index e1d1baf41..e9dae002d 100644 --- a/core/federated/RTI/CMakeLists.txt +++ b/core/federated/RTI/CMakeLists.txt @@ -70,6 +70,9 @@ add_executable( rti.c ${LF_PLATFORM_FILE} ${CoreLib}/platform/lf_unix_clock_support.c + ${CoreLib}/utils/util.c + ${CoreLib}/tag.c + ${CoreLib}/federated/net_util.c ${CoreLib}/utils/pqueue.c message_record/message_record.c ) diff --git a/core/federated/RTI/rti.c b/core/federated/RTI/rti.c index a2ce7d553..b40c07bc6 100644 --- a/core/federated/RTI/rti.c +++ b/core/federated/RTI/rti.c @@ -60,10 +60,10 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include // Defines wait() for process to change state. #include "platform.h" // Platform-specific types and functions -#include "util.c" // Defines print functions (e.g., lf_print). -#include "net_util.c" // Defines network functions. +#include "util.h" // Defines print functions (e.g., lf_print). +#include "net_util.h" // Defines network functions. #include "net_common.h" // Defines message types, etc. Includes and "reactor.h". -#include "tag.c" // Time-related types and functions. +#include "tag.h" // Time-related types and functions. #include "rti.h" #ifdef __RTI_AUTH__ #include // For secure random number generation. From 0fa8f1e2784728d549b5bb064c925a01fa769e44 Mon Sep 17 00:00:00 2001 From: Anirudh Rengarajan <44007330+arengarajan99@users.noreply.github.com> Date: Mon, 20 Feb 2023 23:41:57 -0800 Subject: [PATCH 02/12] Clean up CMakeLists.txt --- core/CMakeLists.txt | 5 +++++ core/federated/CMakeLists.txt | 5 +++++ core/federated/RTI/CMakeLists.txt | 14 +++++++------- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index df701f8a1..1785eac96 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -15,6 +15,11 @@ list(APPEND INFO_SOURCES ${GENERAL_SOURCES}) # Create the core library add_library(core ${GENERAL_SOURCES}) +# Add sources for either threaded or unthreaded runtime +if(DEFINED FEDERATED) + include(federated/CMakeLists.txt) +endif() + # Add sources for either threaded or unthreaded runtime if(DEFINED LF_THREADED) message(STATUS "Including sources for threaded runtime with \ diff --git a/core/federated/CMakeLists.txt b/core/federated/CMakeLists.txt index e69de29bb..cd80582ee 100644 --- a/core/federated/CMakeLists.txt +++ b/core/federated/CMakeLists.txt @@ -0,0 +1,5 @@ +set(FEDERATED_SOURCES clock-sync.c federate.c net_util.c) +list(APPEND INFO_SOURCES ${FEDERATED_SOURCES}) + +list(TRANSFORM FEDERATED_SOURCES PREPEND federated/) +target_sources(core PRIVATE ${FEDERATED_SOURCES}) \ No newline at end of file diff --git a/core/federated/RTI/CMakeLists.txt b/core/federated/RTI/CMakeLists.txt index e9dae002d..3f727d81b 100644 --- a/core/federated/RTI/CMakeLists.txt +++ b/core/federated/RTI/CMakeLists.txt @@ -50,11 +50,11 @@ else() endif() # The following should not be done -include_directories(${CoreLib}/..) -include_directories(${CoreLib}) -include_directories(${CoreLib}/federated) -include_directories(${CoreLib}/platform) -include_directories(${CoreLib}/utils) +# include_directories(${CoreLib}/..) +# include_directories(${CoreLib}) +# include_directories(${CoreLib}/federated) +# include_directories(${CoreLib}/platform) +# include_directories(${CoreLib}/utils) set(IncludeDir ../../../include/core) include_directories(${IncludeDir}) @@ -82,8 +82,8 @@ IF(CMAKE_BUILD_TYPE MATCHES DEBUG) target_compile_definitions(RTI PUBLIC LOG_LEVEL=4) ENDIF(CMAKE_BUILD_TYPE MATCHES DEBUG) -# Set LF_THREADING to get the threaded support -target_compile_definitions(RTI PUBLIC LF_THREADED=1) +# Set LF_THREADING to get the threaded support and FEDERATED to get federated compilation support +target_compile_definitions(RTI PUBLIC LF_THREADED=1 FEDERATED=1) # Find threads and link to it find_package(Threads REQUIRED) From 133354ac495189d6c7dbdd26430b52771f551fc6 Mon Sep 17 00:00:00 2001 From: Anirudh Rengarajan <44007330+arengarajan99@users.noreply.github.com> Date: Mon, 20 Feb 2023 23:42:30 -0800 Subject: [PATCH 03/12] Fix includes to allow for federated runtime to no longer need .c includes --- core/federated/clock-sync.c | 7 +++++ core/federated/federate.c | 41 +++++++++-------------------- core/federated/net_util.c | 3 +++ core/threaded/reactor_threaded.c | 1 - include/core/federated/clock-sync.h | 2 ++ include/core/federated/federate.h | 41 +++++++++++++++++++++++++++++ include/core/federated/net_common.h | 2 ++ include/core/federated/net_util.h | 5 +++- 8 files changed, 72 insertions(+), 30 deletions(-) diff --git a/core/federated/clock-sync.c b/core/federated/clock-sync.c index 0419743f4..feeb2f070 100644 --- a/core/federated/clock-sync.c +++ b/core/federated/clock-sync.c @@ -30,12 +30,18 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * Utility functions for clock synchronization. */ +#ifdef FEDERATED #include #include +#include +#include +#include +#include #include "clock-sync.h" #include "net_common.h" #include "net_util.h" +#include "util.h" /** * Keep a record of connection statistics @@ -567,3 +573,4 @@ int create_clock_sync_thread(lf_thread_t* thread_id) { #endif // _LF_CLOCK_SYNC_ON return 0; } +#endif diff --git a/core/federated/federate.c b/core/federated/federate.c index 9ceda2591..3f314f36a 100644 --- a/core/federated/federate.c +++ b/core/federated/federate.c @@ -30,25 +30,31 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * The main entry point is synchronize_with_other_federates(). */ +#ifdef FEDERATED +#ifndef PLATFORM_ARDUINO #include // inet_ntop & inet_pton -#include -#include // Defined perror(), errno #include // Defines gethostbyname(). #include // Defines struct sockaddr_in #include +#include // Defines bzero(). +#include +#endif + +#include +#include // Defined perror(), errno #include // Defines sigaction. #include #include -#include // Defines bzero(). -#include #include // Defines read(), write(), and close() -#include "clock-sync.c" +//clock-sync.c, net_util.c +#include "clock-sync.h" #include "federate.h" #include "lf_types.h" #include "net_common.h" -#include "net_util.c" +#include "net_util.h" #include "platform.h" +#include "reactor.h" #include "reactor_common.h" #include "reactor_threaded.h" #include "scheduler.h" @@ -101,28 +107,6 @@ federation_metadata_t federation_metadata = { }; -/** - * Thread that listens for inputs from other federates. - * This thread listens for messages of type MSG_TYPE_P2P_TAGGED_MESSAGE - * from the specified peer federate and calls schedule to - * schedule an event. If an error occurs or an EOF is received - * from the peer, then this procedure returns, terminating the - * thread. - * @param fed_id_ptr A pointer to a uint16_t containing federate ID being listened to. - * This procedure frees the memory pointed to before returning. - */ -void* listen_to_federates(void* args); - - -/** - * Generated function that sends information about connections between this federate and - * other federates where messages are routed through the RTI. Currently, this - * only includes logical connections when the coordination is centralized. This - * information is needed for the RTI to perform the centralized coordination. - * @see MSG_TYPE_NEIGHBOR_STRUCTURE in net_common.h - */ -void send_neighbor_structure_to_RTI(int rti_socket); - /** * Create a server to listen to incoming physical * connections from remote federates. This function @@ -2857,3 +2841,4 @@ parse_rti_code_t parse_rti_addr(const char* rti_addr) { void set_federation_id(const char* fid) { federation_metadata.federation_id = fid; } +#endif \ No newline at end of file diff --git a/core/federated/net_util.c b/core/federated/net_util.c index 326ce050c..d41fed714 100644 --- a/core/federated/net_util.c +++ b/core/federated/net_util.c @@ -29,6 +29,8 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * @section DESCRIPTION * Utility functions for a federate in a federated execution. */ + +#ifdef FEDERATED #include #include #include @@ -705,3 +707,4 @@ void extract_rti_addr_info(const char* rti_addr, rti_addr_info_t* rti_addr_info) } regfree(®ex_compiled); } +#endif diff --git a/core/threaded/reactor_threaded.c b/core/threaded/reactor_threaded.c index 5f6be3d75..ffa1073b6 100644 --- a/core/threaded/reactor_threaded.c +++ b/core/threaded/reactor_threaded.c @@ -1,4 +1,3 @@ -#if defined(LF_THREADED) /* Runtime infrastructure for the threaded version of the C target of Lingua Franca. */ /************* diff --git a/include/core/federated/clock-sync.h b/include/core/federated/clock-sync.h index a083c1e4e..eb3e4c341 100644 --- a/include/core/federated/clock-sync.h +++ b/include/core/federated/clock-sync.h @@ -33,6 +33,8 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef CLOCK_SYNC_H #define CLOCK_SYNC_H +#include "platform.h" + /** * Number of required clock sync T4 messages per synchronization * interval. The offset to the clock will not be adjusted until diff --git a/include/core/federated/federate.h b/include/core/federated/federate.h index 7a7f2abd8..006a40725 100644 --- a/include/core/federated/federate.h +++ b/include/core/federated/federate.h @@ -32,6 +32,10 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef FEDERATE_H #define FEDERATE_H +#include "tag.h" +#include "lf_types.h" +#include + #ifndef ADVANCE_MESSAGE_INTERVAL #define ADVANCE_MESSAGE_INTERVAL MSEC(10) #endif @@ -239,6 +243,43 @@ typedef struct federation_metadata_t { char* rti_user; } federation_metadata_t; +/** +* Generated function that sends information about connections between this federate and +* other federates where messages are routed through the RTI. Currently, this +* only includes logical connections when the coordination is centralized. This +* information is needed for the RTI to perform the centralized coordination. +* @see MSG_TYPE_NEIGHBOR_STRUCTURE in net_common.h +*/ +void send_neighbor_structure_to_RTI(int); + +/** + * Thread that listens for inputs from other federates. + * This thread listens for messages of type MSG_TYPE_P2P_MESSAGE, + * MSG_TYPE_P2P_TAGGED_MESSAGE, or MSG_TYPE_PORT_ABSENT (@see net_common.h) from the specified + * peer federate and calls the appropriate handling function for + * each message type. If an error occurs or an EOF is received + * from the peer, then this procedure sets the corresponding + * socket in _fed.sockets_for_inbound_p2p_connections + * to -1 and returns, terminating the thread. + * @param fed_id_ptr A pointer to a uint16_t containing federate ID being listened to. + * This procedure frees the memory pointed to before returning. + */ +void* listen_to_federates(void*); + +/** + * Send a port absent message to federate with fed_ID, informing the + * remote federate that the current federate will not produce an event + * on this network port at the current logical time. + * + * @param additional_delay The offset applied to the timestamp + * using after. The additional delay will be greater or equal to zero + * if an after is used on the connection. If no after is given in the + * program, -1 is passed. + * @param port_ID The ID of the receiving port. + * @param fed_ID The fed ID of the receiving federate. + */ +void send_port_absent_to_federate(interval_t, unsigned short, unsigned short); + /** * Synchronize the start with other federates via the RTI. * This assumes that a connection to the RTI is already made diff --git a/include/core/federated/net_common.h b/include/core/federated/net_common.h index 6618d9c20..5e9740e84 100644 --- a/include/core/federated/net_common.h +++ b/include/core/federated/net_common.h @@ -196,7 +196,9 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef NET_COMMON_H #define NET_COMMON_H +#ifndef PLATFORM_ARDUINO #include +#endif /** * The timeout time in ns for TCP operations. diff --git a/include/core/federated/net_util.h b/include/core/federated/net_util.h index e4eb840d6..ddf5cd5e1 100644 --- a/include/core/federated/net_util.h +++ b/include/core/federated/net_util.h @@ -38,9 +38,12 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef NET_UTIL_H #define NET_UTIL_H +#ifndef PLATFORM_ARDUINO #include -#include #include +#endif + +#include #include #include "../platform.h" From 90ba811f204e7c2b91dbdcd81ff7e569259c25a9 Mon Sep 17 00:00:00 2001 From: Anirudh Rengarajan <44007330+arengarajan99@users.noreply.github.com> Date: Wed, 22 Feb 2023 12:00:13 -0800 Subject: [PATCH 04/12] Add additional function signatures in header to enforce .h and .c structures --- core/federated/federate.c | 4 -- include/core/federated/federate.h | 67 +++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/core/federated/federate.c b/core/federated/federate.c index 3f314f36a..b9f34d12c 100644 --- a/core/federated/federate.c +++ b/core/federated/federate.c @@ -67,10 +67,6 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. char* ERROR_SENDING_HEADER = "ERROR sending header information to federate via RTI"; char* ERROR_SENDING_MESSAGE = "ERROR sending message to federate via RTI"; -// Mutex lock held while performing socket write and close operations. -lf_mutex_t outbound_socket_mutex; -lf_cond_t port_status_changed; - /** * The state of this federate instance. */ diff --git a/include/core/federated/federate.h b/include/core/federated/federate.h index 006a40725..70c5fa560 100644 --- a/include/core/federated/federate.h +++ b/include/core/federated/federate.h @@ -252,6 +252,31 @@ typedef struct federation_metadata_t { */ void send_neighbor_structure_to_RTI(int); +// Mutex lock held while performing socket write and close operations. +lf_mutex_t outbound_socket_mutex; +lf_cond_t port_status_changed; + +/** + * Send a logical tag complete (LTC) message to the RTI + * unless an equal or later LTC has previously been sent. + * This function assumes the caller holds the mutex lock. + * + * @param tag_to_send The tag to send. +*/ +void _lf_logical_tag_complete(tag_t); + +void initialize_triggers_for_federate(); + +/** + * Connect to the RTI at the specified host and port and return + * the socket descriptor for the connection. If this fails, the + * program exits. If it succeeds, it sets the _fed.socket_TCP_RTI global + * variable to refer to the socket for communicating with the RTI. + * @param hostname A hostname, such as "localhost". + * @param port_number A port number. + */ +void connect_to_rti(const char*, int); + /** * Thread that listens for inputs from other federates. * This thread listens for messages of type MSG_TYPE_P2P_MESSAGE, @@ -280,6 +305,48 @@ void* listen_to_federates(void*); */ void send_port_absent_to_federate(interval_t, unsigned short, unsigned short); +/** + * Send the specified timestamped message to the specified port in the + * specified federate via the RTI or directly to a federate depending on + * the given socket. The timestamp is calculated as current_logical_time + + * additional delay which is greater than or equal to zero. + * The port should be an input port of a reactor in + * the destination federate. This version does include the timestamp + * in the message. The caller can reuse or free the memory after this returns. + * + * If the socket connection to the remote federate or the RTI has been broken, + * then this returns 0 without sending. Otherwise, it returns 1. + * + * This method assumes that the caller does not hold the outbound_socket_mutex lock, + * which it acquires to perform the send. + * + * @note This function is similar to send_message() except that it + * sends timed messages and also contains logics related to time. + * + * @param additional_delay The offset applied to the timestamp + * using after. The additional delay will be greater or equal to zero + * if an after is used on the connection. If no after is given in the + * program, -1 is passed. + * @param message_type The type of the message being sent. + * Currently can be MSG_TYPE_TAGGED_MESSAGE for messages sent via + * RTI or MSG_TYPE_P2P_TAGGED_MESSAGE for messages sent between + * federates. + * @param port The ID of the destination port. + * @param federate The ID of the destination federate. + * @param next_destination_str The next destination in string format (RTI or federate) + * (used for reporting errors). + * @param length The message length. + * @param message The message. + * @return 1 if the message has been sent, 0 otherwise. + */ +int send_timed_message(interval_t, + int, + unsigned short, + unsigned short, + const char*, + size_t, + unsigned char*); + /** * Synchronize the start with other federates via the RTI. * This assumes that a connection to the RTI is already made From 4adf95ab59984b2b68e6170289a97a218288a86d Mon Sep 17 00:00:00 2001 From: Anirudh Rengarajan <44007330+arengarajan99@users.noreply.github.com> Date: Wed, 22 Feb 2023 14:03:37 -0800 Subject: [PATCH 05/12] Remove unused function --- include/core/federated/federate.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/include/core/federated/federate.h b/include/core/federated/federate.h index 70c5fa560..ff84c2149 100644 --- a/include/core/federated/federate.h +++ b/include/core/federated/federate.h @@ -265,8 +265,6 @@ lf_cond_t port_status_changed; */ void _lf_logical_tag_complete(tag_t); -void initialize_triggers_for_federate(); - /** * Connect to the RTI at the specified host and port and return * the socket descriptor for the connection. If this fails, the From 77c2a79e1e0344cfa737af7eca3261843ffe0261 Mon Sep 17 00:00:00 2001 From: Anirudh Rengarajan <44007330+arengarajan99@users.noreply.github.com> Date: Fri, 24 Feb 2023 08:19:55 -0800 Subject: [PATCH 06/12] Attempted fix at multiple definition error --- core/federated/federate.c | 4 ++++ include/core/federated/federate.h | 7 +++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/core/federated/federate.c b/core/federated/federate.c index 4b56153f5..8373f26df 100644 --- a/core/federated/federate.c +++ b/core/federated/federate.c @@ -67,6 +67,10 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. char* ERROR_SENDING_HEADER = "ERROR sending header information to federate via RTI"; char* ERROR_SENDING_MESSAGE = "ERROR sending message to federate via RTI"; +// Mutex lock held while performing socket write and close operations. +lf_mutex_t outbound_socket_mutex; +lf_cond_t port_status_changed; + /** * The state of this federate instance. */ diff --git a/include/core/federated/federate.h b/include/core/federated/federate.h index ff84c2149..fee4708fc 100644 --- a/include/core/federated/federate.h +++ b/include/core/federated/federate.h @@ -243,6 +243,9 @@ typedef struct federation_metadata_t { char* rti_user; } federation_metadata_t; +extern lf_mutex_t outbound_socket_mutex; +extern lf_cond_t port_status_changed; + /** * Generated function that sends information about connections between this federate and * other federates where messages are routed through the RTI. Currently, this @@ -252,10 +255,6 @@ typedef struct federation_metadata_t { */ void send_neighbor_structure_to_RTI(int); -// Mutex lock held while performing socket write and close operations. -lf_mutex_t outbound_socket_mutex; -lf_cond_t port_status_changed; - /** * Send a logical tag complete (LTC) message to the RTI * unless an equal or later LTC has previously been sent. From b28a442bc6ae42d11579e919835430938ad49b09 Mon Sep 17 00:00:00 2001 From: Anirudh Rengarajan <44007330+arengarajan99@users.noreply.github.com> Date: Fri, 24 Feb 2023 08:48:50 -0800 Subject: [PATCH 07/12] add function definition --- include/core/federated/federate.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/include/core/federated/federate.h b/include/core/federated/federate.h index fee4708fc..09310e1e1 100644 --- a/include/core/federated/federate.h +++ b/include/core/federated/federate.h @@ -288,6 +288,15 @@ void connect_to_rti(const char*, int); */ void* listen_to_federates(void*); +/** + * Thread to accept connections from other federates that send this federate + * messages directly (not through the RTI). This thread starts a thread for + * each accepted socket connection and, once it has opened all expected + * sockets, exits. + * @param ignored No argument needed for this thread. + */ +void* handle_p2p_connections_from_federates(void* ignored); + /** * Send a port absent message to federate with fed_ID, informing the * remote federate that the current federate will not produce an event From 9b6d606976f9e5bdc4545345c0ebbd7e5e3e95b4 Mon Sep 17 00:00:00 2001 From: Anirudh Rengarajan <44007330+arengarajan99@users.noreply.github.com> Date: Fri, 24 Feb 2023 11:41:06 -0800 Subject: [PATCH 08/12] Additional function definitions --- include/core/federated/federate.h | 71 ++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/include/core/federated/federate.h b/include/core/federated/federate.h index 09310e1e1..d15a0e920 100644 --- a/include/core/federated/federate.h +++ b/include/core/federated/federate.h @@ -255,6 +255,20 @@ extern lf_cond_t port_status_changed; */ void send_neighbor_structure_to_RTI(int); +/** + * Connect to the federate with the specified id. This established + * connection will then be used in functions such as send_timed_message() + * to send messages directly to the specified federate. + * This function first sends an MSG_TYPE_ADDRESS_QUERY message to the RTI to obtain + * the IP address and port number of the specified federate. It then attempts + * to establish a socket connection to the specified federate. + * If this fails, the program exits. If it succeeds, it sets element [id] of + * the _fed.sockets_for_outbound_p2p_connections global array to + * refer to the socket for communicating directly with the federate. + * @param remote_federate_id The ID of the remote federate. + */ +void connect_to_federate(uint16_t); + /** * Send a logical tag complete (LTC) message to the RTI * unless an equal or later LTC has previously been sent. @@ -288,6 +302,32 @@ void connect_to_rti(const char*, int); */ void* listen_to_federates(void*); +/** + * Create a server to listen to incoming physical + * connections from remote federates. This function + * only handles the creation of the server socket. + * The reserved port for the server socket is then + * sent to the RTI by sending an MSG_TYPE_ADDRESS_ADVERTISEMENT message + * (@see net_common.h). This function expects no response + * from the RTI. + * + * If a port is specified by the user, that will be used + * as the only possibility for the server. This function + * will fail if that port is not available. If a port is not + * specified, the STARTING_PORT (@see net_common.h) will be used. + * The function will keep incrementing the port in this case + * until the number of tries reaches PORT_RANGE_LIMIT. + * + * @note This function is similar to create_server(...) in rti.c. + * However, it contains specific log messages for the peer to + * peer connections between federates. It also additionally + * sends an address advertisement (MSG_TYPE_ADDRESS_ADVERTISEMENT) message to the + * RTI informing it of the port. + * + * @param specified_port The specified port by the user. + */ +void create_server(int specified_port); + /** * Thread to accept connections from other federates that send this federate * messages directly (not through the RTI). This thread starts a thread for @@ -295,7 +335,7 @@ void* listen_to_federates(void*); * sockets, exits. * @param ignored No argument needed for this thread. */ -void* handle_p2p_connections_from_federates(void* ignored); +void* handle_p2p_connections_from_federates(void*); /** * Send a port absent message to federate with fed_ID, informing the @@ -311,6 +351,35 @@ void* handle_p2p_connections_from_federates(void* ignored); */ void send_port_absent_to_federate(interval_t, unsigned short, unsigned short); +/** + * Send a message to another federate directly or via the RTI. + * This method assumes that the caller does not hold the outbound_socket_mutex lock, + * which it acquires to perform the send. + * + * If the socket connection to the remote federate or the RTI has been broken, + * then this returns 0 without sending. Otherwise, it returns 1. + * + * @note This function is similar to send_timed_message() except that it + * does not deal with time and timed_messages. + * + * @param message_type The type of the message being sent. + * Currently can be MSG_TYPE_TAGGED_MESSAGE for messages sent via + * RTI or MSG_TYPE_P2P_TAGGED_MESSAGE for messages sent between + * federates. + * @param port The ID of the destination port. + * @param federate The ID of the destination federate. + * @param next_destination_str The name of the next destination in string format + * @param length The message length. + * @param message The message. + * @return 1 if the message has been sent, 0 otherwise. + */ +int send_message(int message_type, + unsigned short port, + unsigned short federate, + const char* next_destination_str, + size_t length, + unsigned char* message); + /** * Send the specified timestamped message to the specified port in the * specified federate via the RTI or directly to a federate depending on From 0dc967336cbaa1b9bb459bb1c77e447b9d5378c6 Mon Sep 17 00:00:00 2001 From: Anirudh Rengarajan <44007330+arengarajan99@users.noreply.github.com> Date: Fri, 24 Feb 2023 12:04:43 -0800 Subject: [PATCH 09/12] Remove unneeded include of core directories --- core/federated/RTI/CMakeLists.txt | 7 ------- 1 file changed, 7 deletions(-) diff --git a/core/federated/RTI/CMakeLists.txt b/core/federated/RTI/CMakeLists.txt index c3482dc5e..43e916c29 100644 --- a/core/federated/RTI/CMakeLists.txt +++ b/core/federated/RTI/CMakeLists.txt @@ -49,13 +49,6 @@ else() message(FATAL_ERROR "Your platform is not supported! RTI supports Linux and MacOS.") endif() -# The following should not be done -# include_directories(${CoreLib}/..) -# include_directories(${CoreLib}) -# include_directories(${CoreLib}/federated) -# include_directories(${CoreLib}/platform) -# include_directories(${CoreLib}/utils) - set(IncludeDir ../../../include/core) include_directories(${IncludeDir}) include_directories(${IncludeDir}/federated) From f5d714c945792ddcd3ddac05fd25466e462770a9 Mon Sep 17 00:00:00 2001 From: Anirudh Rengarajan <44007330+arengarajan99@users.noreply.github.com> Date: Fri, 24 Feb 2023 12:05:06 -0800 Subject: [PATCH 10/12] Apply suggestions from code review Co-authored-by: Marten Lohstroh --- core/CMakeLists.txt | 2 +- core/federated/CMakeLists.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index b0ba14f2d..2ff9d6095 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -16,7 +16,7 @@ list(APPEND INFO_SOURCES ${GENERAL_SOURCES}) add_library(core ${GENERAL_SOURCES}) # Add sources for either threaded or unthreaded runtime -if(DEFINED FEDERATED) +if (DEFINED FEDERATED) include(federated/CMakeLists.txt) endif() diff --git a/core/federated/CMakeLists.txt b/core/federated/CMakeLists.txt index cd80582ee..d5af17c2b 100644 --- a/core/federated/CMakeLists.txt +++ b/core/federated/CMakeLists.txt @@ -2,4 +2,4 @@ set(FEDERATED_SOURCES clock-sync.c federate.c net_util.c) list(APPEND INFO_SOURCES ${FEDERATED_SOURCES}) list(TRANSFORM FEDERATED_SOURCES PREPEND federated/) -target_sources(core PRIVATE ${FEDERATED_SOURCES}) \ No newline at end of file +target_sources(core PRIVATE ${FEDERATED_SOURCES}) From 361e59d30a49238342b841333f01854a2938d471 Mon Sep 17 00:00:00 2001 From: Anirudh Rengarajan <44007330+arengarajan99@users.noreply.github.com> Date: Fri, 24 Feb 2023 14:29:41 -0800 Subject: [PATCH 11/12] Clean up unneeded code --- core/federated/federate.c | 1 - include/core/federated/net_common.h | 4 ---- 2 files changed, 5 deletions(-) diff --git a/core/federated/federate.c b/core/federated/federate.c index 8373f26df..312848d42 100644 --- a/core/federated/federate.c +++ b/core/federated/federate.c @@ -47,7 +47,6 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include // Defines read(), write(), and close() -//clock-sync.c, net_util.c #include "clock-sync.h" #include "federate.h" #include "lf_types.h" diff --git a/include/core/federated/net_common.h b/include/core/federated/net_common.h index 5e9740e84..8ace6d9b0 100644 --- a/include/core/federated/net_common.h +++ b/include/core/federated/net_common.h @@ -196,10 +196,6 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef NET_COMMON_H #define NET_COMMON_H -#ifndef PLATFORM_ARDUINO -#include -#endif - /** * The timeout time in ns for TCP operations. * Default value is 10 secs. From 3c137cd691d5c4e8daabc462bde76501b1657323 Mon Sep 17 00:00:00 2001 From: Anirudh Rengarajan <44007330+arengarajan99@users.noreply.github.com> Date: Fri, 24 Feb 2023 14:36:07 -0800 Subject: [PATCH 12/12] Added error checking for federation on Arduino. --- core/federated/federate.c | 4 +++- include/core/federated/net_util.h | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/core/federated/federate.c b/core/federated/federate.c index 312848d42..baee3e783 100644 --- a/core/federated/federate.c +++ b/core/federated/federate.c @@ -31,7 +31,9 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifdef FEDERATED -#ifndef PLATFORM_ARDUINO +#ifdef PLATFORM_ARDUINO +#error To be implemented. No support for federation on Arduino yet. +#else #include // inet_ntop & inet_pton #include // Defines gethostbyname(). #include // Defines struct sockaddr_in diff --git a/include/core/federated/net_util.h b/include/core/federated/net_util.h index ddf5cd5e1..666eac653 100644 --- a/include/core/federated/net_util.h +++ b/include/core/federated/net_util.h @@ -38,7 +38,9 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef NET_UTIL_H #define NET_UTIL_H -#ifndef PLATFORM_ARDUINO +#ifdef PLATFORM_ARDUINO +#error To be implemented. No support for federation on Arduino yet. +#else #include #include #endif