From 9bbe6990ed9ed291a1348c97e2613e6f57dc0897 Mon Sep 17 00:00:00 2001 From: Dongha Kim Date: Mon, 24 Jun 2024 15:31:24 -0700 Subject: [PATCH 01/14] Clarify int types && Change clock-sync to send uint16_t types not int32_t types. --- core/federated/RTI/rti_remote.c | 4 ++-- core/federated/clock-sync.c | 6 +++--- core/utils/util.c | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/federated/RTI/rti_remote.c b/core/federated/RTI/rti_remote.c index 3daaeff7e..58dda8a35 100644 --- a/core/federated/RTI/rti_remote.c +++ b/core/federated/RTI/rti_remote.c @@ -923,7 +923,7 @@ void* clock_synchronization_thread(void* noargs) { send_physical_clock(MSG_TYPE_CLOCK_SYNC_T1, fed, UDP); // Listen for reply message, which should be T3. - size_t message_size = 1 + sizeof(int32_t); + size_t message_size = 1 + sizeof(uint16_t); unsigned char buffer[message_size]; // Maximum number of messages that we discard before giving up on this cycle. // If the T3 message from this federate does not arrive and we keep receiving @@ -935,7 +935,7 @@ void* clock_synchronization_thread(void* noargs) { // If any errors occur, either discard the message or the clock sync round. if (!read_failed) { if (buffer[0] == MSG_TYPE_CLOCK_SYNC_T3) { - int32_t fed_id_2 = extract_int32(&(buffer[1])); + int32_t fed_id_2 = (int32_t) extract_uint16(&(buffer[1])); // Check that this message came from the correct federate. if (fed_id_2 != fed->enclave.id) { // Message is from the wrong federate. Discard the message. diff --git a/core/federated/clock-sync.c b/core/federated/clock-sync.c index b18efb650..ef5403c89 100644 --- a/core/federated/clock-sync.c +++ b/core/federated/clock-sync.c @@ -275,13 +275,13 @@ int handle_T1_clock_sync_message(unsigned char* buffer, int socket, instant_t t2 // T3-T2 between receiving the T1 message and replying. // Reply will have the federate ID as a payload. - unsigned char reply_buffer[1 + sizeof(int)]; + unsigned char reply_buffer[1 + sizeof(uint16_t)]; reply_buffer[0] = MSG_TYPE_CLOCK_SYNC_T3; - encode_int32(_lf_my_fed_id, &(reply_buffer[1])); + encode_uint16(_lf_my_fed_id, &(reply_buffer[1])); // Write the reply to the socket. LF_PRINT_DEBUG("Sending T3 message to RTI."); - if (write_to_socket(socket, 1 + sizeof(int), reply_buffer)) { + if (write_to_socket(socket, 1 + sizeof(uint16_t), reply_buffer)) { lf_print_error("Clock sync: Failed to send T3 message to RTI."); return -1; } diff --git a/core/utils/util.c b/core/utils/util.c index 62de9fd27..721c2d977 100644 --- a/core/utils/util.c +++ b/core/utils/util.c @@ -58,7 +58,7 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * The ID of this federate. For a non-federated execution, this will be -1. * For a federated execution, it will be assigned in the generated code. */ -int _lf_my_fed_id = -1; +int32_t _lf_my_fed_id = -1; /** * If non-null, this function will be used instead of the printf to From d44afaf832244395259ba583a900ee250290cfe7 Mon Sep 17 00:00:00 2001 From: Dongha Kim Date: Mon, 24 Jun 2024 15:37:08 -0700 Subject: [PATCH 02/14] Run formatting. --- core/federated/RTI/rti_remote.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/federated/RTI/rti_remote.c b/core/federated/RTI/rti_remote.c index 58dda8a35..bed58203b 100644 --- a/core/federated/RTI/rti_remote.c +++ b/core/federated/RTI/rti_remote.c @@ -935,7 +935,7 @@ void* clock_synchronization_thread(void* noargs) { // If any errors occur, either discard the message or the clock sync round. if (!read_failed) { if (buffer[0] == MSG_TYPE_CLOCK_SYNC_T3) { - int32_t fed_id_2 = (int32_t) extract_uint16(&(buffer[1])); + int32_t fed_id_2 = (int32_t)extract_uint16(&(buffer[1])); // Check that this message came from the correct federate. if (fed_id_2 != fed->enclave.id) { // Message is from the wrong federate. Discard the message. From 58199bcfc5ef16b086dd08704a752f3c9c612624 Mon Sep 17 00:00:00 2001 From: Dongha Kim Date: Mon, 24 Jun 2024 17:18:32 -0700 Subject: [PATCH 03/14] Fix more types. --- core/utils/util.c | 2 +- include/core/utils/util.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/utils/util.c b/core/utils/util.c index 721c2d977..2bf165301 100644 --- a/core/utils/util.c +++ b/core/utils/util.c @@ -69,7 +69,7 @@ print_message_function_t* print_message_function = NULL; /** The level of messages to redirect to print_message_function. */ int print_message_level = -1; -int lf_fed_id() { return _lf_my_fed_id; } +int32_t lf_fed_id() { return _lf_my_fed_id; } // Declaration needed to attach attributes to suppress warnings of the form: // "warning: function '_lf_message_print' might be a candidate for 'gnu_printf' diff --git a/include/core/utils/util.h b/include/core/utils/util.h index 2d9998a72..9ff7ec182 100644 --- a/include/core/utils/util.h +++ b/include/core/utils/util.h @@ -86,12 +86,12 @@ typedef struct lf_stat_ll { * _lf_initialize_trigger_objects() is called. * @see xtext/org.icyphy.linguafranca/src/org/icyphy/generator/CGenerator.xtend. */ -extern int _lf_my_fed_id; +extern int32_t _lf_my_fed_id; /** * Return the federate ID or -1 if this program is not part of a federation. */ -int lf_fed_id(void); +int32_t lf_fed_id(void); /** * varargs alternative of "lf_print" From 24fdde9cb8bc355a56f4f805d574d7dddf66bd1d Mon Sep 17 00:00:00 2001 From: Dongha Kim Date: Mon, 24 Jun 2024 18:23:52 -0700 Subject: [PATCH 04/14] Fix to RTI receive uint16_t --- core/federated/RTI/rti_remote.c | 4 ++-- core/federated/clock-sync.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/federated/RTI/rti_remote.c b/core/federated/RTI/rti_remote.c index bed58203b..55a4f04b8 100644 --- a/core/federated/RTI/rti_remote.c +++ b/core/federated/RTI/rti_remote.c @@ -1423,12 +1423,12 @@ static int receive_udp_message_and_set_up_clock_sync(int* socket_id, uint16_t fe send_physical_clock(MSG_TYPE_CLOCK_SYNC_T1, fed, TCP); // Listen for reply message, which should be T3. - size_t message_size = 1 + sizeof(int32_t); + size_t message_size = 1 + sizeof(uint16_t); unsigned char buffer[message_size]; read_from_socket_fail_on_error(socket_id, message_size, buffer, NULL, "Socket to federate %d unexpectedly closed.", fed_id); if (buffer[0] == MSG_TYPE_CLOCK_SYNC_T3) { - int32_t fed_id = extract_int32(&(buffer[1])); + uint16_t fed_id = extract_uint16(&(buffer[1])); assert(fed_id > -1); assert(fed_id < 65536); LF_PRINT_DEBUG("RTI received T3 clock sync message from federate %d.", fed_id); diff --git a/core/federated/clock-sync.c b/core/federated/clock-sync.c index ef5403c89..0abfccbb9 100644 --- a/core/federated/clock-sync.c +++ b/core/federated/clock-sync.c @@ -277,7 +277,7 @@ int handle_T1_clock_sync_message(unsigned char* buffer, int socket, instant_t t2 // Reply will have the federate ID as a payload. unsigned char reply_buffer[1 + sizeof(uint16_t)]; reply_buffer[0] = MSG_TYPE_CLOCK_SYNC_T3; - encode_uint16(_lf_my_fed_id, &(reply_buffer[1])); + encode_uint16((uint16_t)_lf_my_fed_id, &(reply_buffer[1])); // Write the reply to the socket. LF_PRINT_DEBUG("Sending T3 message to RTI."); From 6a30310d3d2bb8e46744215807a32bec26c11b18 Mon Sep 17 00:00:00 2001 From: Dongha Kim Date: Mon, 24 Jun 2024 19:57:45 -0700 Subject: [PATCH 05/14] Remove assert to pass mac-tests. --- core/federated/RTI/rti_remote.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/federated/RTI/rti_remote.c b/core/federated/RTI/rti_remote.c index 55a4f04b8..bfcebad14 100644 --- a/core/federated/RTI/rti_remote.c +++ b/core/federated/RTI/rti_remote.c @@ -1429,8 +1429,6 @@ static int receive_udp_message_and_set_up_clock_sync(int* socket_id, uint16_t fe "Socket to federate %d unexpectedly closed.", fed_id); if (buffer[0] == MSG_TYPE_CLOCK_SYNC_T3) { uint16_t fed_id = extract_uint16(&(buffer[1])); - assert(fed_id > -1); - assert(fed_id < 65536); LF_PRINT_DEBUG("RTI received T3 clock sync message from federate %d.", fed_id); handle_physical_clock_sync_message(fed, TCP); } else { From fb25cf729ba956d4d68ca6b6704a3c8067e4aee2 Mon Sep 17 00:00:00 2001 From: Dongha Kim <74869052+Jakio815@users.noreply.github.com> Date: Mon, 1 Jul 2024 14:57:38 -0700 Subject: [PATCH 06/14] Apply suggestions from code review Co-authored-by: Christian Menard --- core/federated/RTI/rti_remote.c | 2 +- core/federated/clock-sync.c | 2 +- core/utils/util.c | 4 ++-- include/core/utils/util.h | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/federated/RTI/rti_remote.c b/core/federated/RTI/rti_remote.c index bfcebad14..4cfe83630 100644 --- a/core/federated/RTI/rti_remote.c +++ b/core/federated/RTI/rti_remote.c @@ -935,7 +935,7 @@ void* clock_synchronization_thread(void* noargs) { // If any errors occur, either discard the message or the clock sync round. if (!read_failed) { if (buffer[0] == MSG_TYPE_CLOCK_SYNC_T3) { - int32_t fed_id_2 = (int32_t)extract_uint16(&(buffer[1])); + uint16_t fed_id_2 = extract_uint16(&(buffer[1])); // Check that this message came from the correct federate. if (fed_id_2 != fed->enclave.id) { // Message is from the wrong federate. Discard the message. diff --git a/core/federated/clock-sync.c b/core/federated/clock-sync.c index 0abfccbb9..ef5403c89 100644 --- a/core/federated/clock-sync.c +++ b/core/federated/clock-sync.c @@ -277,7 +277,7 @@ int handle_T1_clock_sync_message(unsigned char* buffer, int socket, instant_t t2 // Reply will have the federate ID as a payload. unsigned char reply_buffer[1 + sizeof(uint16_t)]; reply_buffer[0] = MSG_TYPE_CLOCK_SYNC_T3; - encode_uint16((uint16_t)_lf_my_fed_id, &(reply_buffer[1])); + encode_uint16(_lf_my_fed_id, &(reply_buffer[1])); // Write the reply to the socket. LF_PRINT_DEBUG("Sending T3 message to RTI."); diff --git a/core/utils/util.c b/core/utils/util.c index 2bf165301..3928570f9 100644 --- a/core/utils/util.c +++ b/core/utils/util.c @@ -58,7 +58,7 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * The ID of this federate. For a non-federated execution, this will be -1. * For a federated execution, it will be assigned in the generated code. */ -int32_t _lf_my_fed_id = -1; +uint16_t _lf_my_fed_id; /** * If non-null, this function will be used instead of the printf to @@ -69,7 +69,7 @@ print_message_function_t* print_message_function = NULL; /** The level of messages to redirect to print_message_function. */ int print_message_level = -1; -int32_t lf_fed_id() { return _lf_my_fed_id; } +uint16_t lf_fed_id() { return _lf_my_fed_id; } // Declaration needed to attach attributes to suppress warnings of the form: // "warning: function '_lf_message_print' might be a candidate for 'gnu_printf' diff --git a/include/core/utils/util.h b/include/core/utils/util.h index 9ff7ec182..77b7b767d 100644 --- a/include/core/utils/util.h +++ b/include/core/utils/util.h @@ -86,12 +86,12 @@ typedef struct lf_stat_ll { * _lf_initialize_trigger_objects() is called. * @see xtext/org.icyphy.linguafranca/src/org/icyphy/generator/CGenerator.xtend. */ -extern int32_t _lf_my_fed_id; +extern uint16_t _lf_my_fed_id; /** * Return the federate ID or -1 if this program is not part of a federation. */ -int32_t lf_fed_id(void); +uint16_t lf_fed_id(void); /** * varargs alternative of "lf_print" From 56f4b0a5c0b0b4ab23880cc1af794fcde755cdbb Mon Sep 17 00:00:00 2001 From: Dongha Kim Date: Mon, 1 Jul 2024 15:07:27 -0700 Subject: [PATCH 07/14] Remove _lf_my_fed_id checks. --- core/utils/util.c | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/core/utils/util.c b/core/utils/util.c index 3928570f9..6408efcc3 100644 --- a/core/utils/util.c +++ b/core/utils/util.c @@ -112,29 +112,23 @@ void _lf_message_print(const char* prefix, const char* format, va_list args, // interleaved between threads. // vprintf() is a version that takes an arg list rather than multiple args. char* message; - if (_lf_my_fed_id < 0) { - size_t length = strlen(prefix) + strlen(format) + 32; - message = (char*)malloc(length + 1); - snprintf(message, length, "%s%s\n", prefix, format); - } else { #if defined STANDALONE_RTI - size_t length = strlen(prefix) + strlen(format) + 37; - message = (char*)malloc(length + 1); - snprintf(message, length, "RTI: %s%s\n", prefix, format); + size_t length = strlen(prefix) + strlen(format) + 37; + message = (char*)malloc(length + 1); + snprintf(message, length, "RTI: %s%s\n", prefix, format); #else - // Get the federate name from the top-level environment, which by convention is the first. - environment_t* envs; - _lf_get_environments(&envs); - char* name = envs->name; - size_t length = strlen(prefix) + strlen(format) + +strlen(name) + 32; - message = (char*)malloc(length + 1); - // If the name has prefix "federate__", strip that out. - if (strncmp(name, "federate__", 10) == 0) - name += 10; - - snprintf(message, length, "Fed %d (%s): %s%s\n", _lf_my_fed_id, name, prefix, format); + // Get the federate name from the top-level environment, which by convention is the first. + environment_t* envs; + _lf_get_environments(&envs); + char* name = envs->name; + size_t length = strlen(prefix) + strlen(format) + +strlen(name) + 32; + message = (char*)malloc(length + 1); + // If the name has prefix "federate__", strip that out. + if (strncmp(name, "federate__", 10) == 0) + name += 10; + + snprintf(message, length, "Fed %d (%s): %s%s\n", _lf_my_fed_id, name, prefix, format); #endif // STANDALONE_RTI - } if (print_message_function == NULL) { // NOTE: Send all messages to stdout, not to stderr, so that ordering makes sense. vfprintf(stdout, message, args); From 7ad2454519532153dbf26ed8e8a894e743d20f9c Mon Sep 17 00:00:00 2001 From: Dongha Kim Date: Mon, 1 Jul 2024 15:15:25 -0700 Subject: [PATCH 08/14] Remove _lf_my_fed_id > UINT16_MAX checks. --- core/federated/federate.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/core/federated/federate.c b/core/federated/federate.c index c1bd4cbea..32aaa9485 100644 --- a/core/federated/federate.c +++ b/core/federated/federate.c @@ -1795,10 +1795,6 @@ void lf_connect_to_federate(uint16_t remote_federate_id) { size_t buffer_length = 1 + sizeof(uint16_t) + 1; unsigned char buffer[buffer_length]; buffer[0] = MSG_TYPE_P2P_SENDING_FED_ID; - if (_lf_my_fed_id > UINT16_MAX) { - // This error is very unlikely to occur. - lf_print_error_and_exit("Too many federates! More than %d.", UINT16_MAX); - } encode_uint16((uint16_t)_lf_my_fed_id, (unsigned char*)&(buffer[1])); unsigned char federation_id_length = (unsigned char)strnlen(federation_metadata.federation_id, 255); buffer[sizeof(uint16_t) + 1] = federation_id_length; @@ -1918,9 +1914,6 @@ void lf_connect_to_rti(const char* hostname, int port) { unsigned char buffer[4]; buffer[0] = MSG_TYPE_FED_IDS; // Next send the federate ID. - if (_lf_my_fed_id > UINT16_MAX) { - lf_print_error_and_exit("Too many federates! More than %d.", UINT16_MAX); - } encode_uint16((uint16_t)_lf_my_fed_id, &buffer[1]); // Next send the federation ID length. // The federation ID is limited to 255 bytes. From b14696853ca04cfa48e7a51b72400e4d93e011ab Mon Sep 17 00:00:00 2001 From: Dongha Kim Date: Mon, 1 Jul 2024 15:44:01 -0700 Subject: [PATCH 09/14] Revert "Remove _lf_my_fed_id checks." This reverts commit 9b5e198f388c59be646e33058a64fbe93fe82408. --- core/utils/util.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/core/utils/util.c b/core/utils/util.c index 6408efcc3..3928570f9 100644 --- a/core/utils/util.c +++ b/core/utils/util.c @@ -112,23 +112,29 @@ void _lf_message_print(const char* prefix, const char* format, va_list args, // interleaved between threads. // vprintf() is a version that takes an arg list rather than multiple args. char* message; + if (_lf_my_fed_id < 0) { + size_t length = strlen(prefix) + strlen(format) + 32; + message = (char*)malloc(length + 1); + snprintf(message, length, "%s%s\n", prefix, format); + } else { #if defined STANDALONE_RTI - size_t length = strlen(prefix) + strlen(format) + 37; - message = (char*)malloc(length + 1); - snprintf(message, length, "RTI: %s%s\n", prefix, format); + size_t length = strlen(prefix) + strlen(format) + 37; + message = (char*)malloc(length + 1); + snprintf(message, length, "RTI: %s%s\n", prefix, format); #else - // Get the federate name from the top-level environment, which by convention is the first. - environment_t* envs; - _lf_get_environments(&envs); - char* name = envs->name; - size_t length = strlen(prefix) + strlen(format) + +strlen(name) + 32; - message = (char*)malloc(length + 1); - // If the name has prefix "federate__", strip that out. - if (strncmp(name, "federate__", 10) == 0) - name += 10; - - snprintf(message, length, "Fed %d (%s): %s%s\n", _lf_my_fed_id, name, prefix, format); + // Get the federate name from the top-level environment, which by convention is the first. + environment_t* envs; + _lf_get_environments(&envs); + char* name = envs->name; + size_t length = strlen(prefix) + strlen(format) + +strlen(name) + 32; + message = (char*)malloc(length + 1); + // If the name has prefix "federate__", strip that out. + if (strncmp(name, "federate__", 10) == 0) + name += 10; + + snprintf(message, length, "Fed %d (%s): %s%s\n", _lf_my_fed_id, name, prefix, format); #endif // STANDALONE_RTI + } if (print_message_function == NULL) { // NOTE: Send all messages to stdout, not to stderr, so that ordering makes sense. vfprintf(stdout, message, args); From 148ac63ed8a379f97bbcf84cb5a83486f0358ce5 Mon Sep 17 00:00:00 2001 From: Dongha Kim Date: Tue, 2 Jul 2024 17:13:05 -0700 Subject: [PATCH 10/14] Initialized fed id to 0xffff --- core/utils/util.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/utils/util.c b/core/utils/util.c index 3928570f9..787641a0f 100644 --- a/core/utils/util.c +++ b/core/utils/util.c @@ -58,7 +58,7 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * The ID of this federate. For a non-federated execution, this will be -1. * For a federated execution, it will be assigned in the generated code. */ -uint16_t _lf_my_fed_id; +uint16_t _lf_my_fed_id = 0xffff; /** * If non-null, this function will be used instead of the printf to @@ -112,7 +112,7 @@ void _lf_message_print(const char* prefix, const char* format, va_list args, // interleaved between threads. // vprintf() is a version that takes an arg list rather than multiple args. char* message; - if (_lf_my_fed_id < 0) { + if (_lf_my_fed_id == 0xffff) { size_t length = strlen(prefix) + strlen(format) + 32; message = (char*)malloc(length + 1); snprintf(message, length, "%s%s\n", prefix, format); From 3eacb1d9979c5e0fb35c0df594986063435fca5d Mon Sep 17 00:00:00 2001 From: Dongha Kim <74869052+Jakio815@users.noreply.github.com> Date: Mon, 8 Jul 2024 13:28:54 -0700 Subject: [PATCH 11/14] Update core/utils/util.c Co-authored-by: Edward A. Lee --- core/utils/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/utils/util.c b/core/utils/util.c index 787641a0f..28b610dc9 100644 --- a/core/utils/util.c +++ b/core/utils/util.c @@ -58,7 +58,7 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * The ID of this federate. For a non-federated execution, this will be -1. * For a federated execution, it will be assigned in the generated code. */ -uint16_t _lf_my_fed_id = 0xffff; +uint16_t _lf_my_fed_id = UINT16_MAX; // Federate IDs are counted up from 0. If we hit this, we have too many. /** * If non-null, this function will be used instead of the printf to From c9153cdff81cc8c20631203fc8d7c58a59ff6d8e Mon Sep 17 00:00:00 2001 From: Dongha Kim Date: Mon, 8 Jul 2024 13:32:46 -0700 Subject: [PATCH 12/14] Minor fix --- core/utils/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/utils/util.c b/core/utils/util.c index 28b610dc9..a1e39a96c 100644 --- a/core/utils/util.c +++ b/core/utils/util.c @@ -112,7 +112,7 @@ void _lf_message_print(const char* prefix, const char* format, va_list args, // interleaved between threads. // vprintf() is a version that takes an arg list rather than multiple args. char* message; - if (_lf_my_fed_id == 0xffff) { + if (_lf_my_fed_id == UINT16_MAX) { size_t length = strlen(prefix) + strlen(format) + 32; message = (char*)malloc(length + 1); snprintf(message, length, "%s%s\n", prefix, format); From e201df6c958fca57ee136fa342a99ad2d6ae2993 Mon Sep 17 00:00:00 2001 From: Dongha Kim Date: Mon, 8 Jul 2024 13:34:36 -0700 Subject: [PATCH 13/14] Fix formatting --- core/utils/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/utils/util.c b/core/utils/util.c index a1e39a96c..cc2529a59 100644 --- a/core/utils/util.c +++ b/core/utils/util.c @@ -58,7 +58,7 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * The ID of this federate. For a non-federated execution, this will be -1. * For a federated execution, it will be assigned in the generated code. */ -uint16_t _lf_my_fed_id = UINT16_MAX; // Federate IDs are counted up from 0. If we hit this, we have too many. +uint16_t _lf_my_fed_id = UINT16_MAX; // Federate IDs are counted up from 0. If we hit this, we have too many. /** * If non-null, this function will be used instead of the printf to From 02d474940cb19d508c553a08e05936e8fc37a62d Mon Sep 17 00:00:00 2001 From: Dongha Kim Date: Fri, 20 Dec 2024 14:17:03 -0700 Subject: [PATCH 14/14] Add error messages. --- core/federated/federate.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/federated/federate.c b/core/federated/federate.c index 32aaa9485..395d89b18 100644 --- a/core/federated/federate.c +++ b/core/federated/federate.c @@ -1795,6 +1795,9 @@ void lf_connect_to_federate(uint16_t remote_federate_id) { size_t buffer_length = 1 + sizeof(uint16_t) + 1; unsigned char buffer[buffer_length]; buffer[0] = MSG_TYPE_P2P_SENDING_FED_ID; + if (_lf_my_fed_id == UINT16_MAX) { + lf_print_error_and_exit("Too many federates! More than %d.", UINT16_MAX - 1); + } encode_uint16((uint16_t)_lf_my_fed_id, (unsigned char*)&(buffer[1])); unsigned char federation_id_length = (unsigned char)strnlen(federation_metadata.federation_id, 255); buffer[sizeof(uint16_t) + 1] = federation_id_length; @@ -1914,6 +1917,9 @@ void lf_connect_to_rti(const char* hostname, int port) { unsigned char buffer[4]; buffer[0] = MSG_TYPE_FED_IDS; // Next send the federate ID. + if (_lf_my_fed_id == UINT16_MAX) { + lf_print_error_and_exit("Too many federates! More than %d.", UINT16_MAX - 1); + } encode_uint16((uint16_t)_lf_my_fed_id, &buffer[1]); // Next send the federation ID length. // The federation ID is limited to 255 bytes.