diff --git a/core/federated/RTI/rti_remote.c b/core/federated/RTI/rti_remote.c index 3daaeff7e..4cfe83630 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])); + 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. @@ -1423,14 +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])); - assert(fed_id > -1); - assert(fed_id < 65536); + uint16_t fed_id = extract_uint16(&(buffer[1])); LF_PRINT_DEBUG("RTI received T3 clock sync message from federate %d.", fed_id); handle_physical_clock_sync_message(fed, TCP); } else { 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/federated/federate.c b/core/federated/federate.c index 2bfc3656a..c98b5d0bc 100644 --- a/core/federated/federate.c +++ b/core/federated/federate.c @@ -1851,9 +1851,8 @@ 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); + 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); @@ -1974,8 +1973,8 @@ 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); + 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. diff --git a/core/utils/util.c b/core/utils/util.c index 4fb956b3a..f7c260af8 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; +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 @@ -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; } +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' @@ -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 == UINT16_MAX) { size_t length = strlen(prefix) + strlen(format) + 32; message = (char*)malloc(length + 1); snprintf(message, length, "%s%s\n", prefix, format); diff --git a/include/core/utils/util.h b/include/core/utils/util.h index 2d9998a72..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 int _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. */ -int lf_fed_id(void); +uint16_t lf_fed_id(void); /** * varargs alternative of "lf_print"