Skip to content

Commit

Permalink
Merge pull request #453 from lf-lang/fed_id_patch
Browse files Browse the repository at this point in the history
Usage of 'fixed-size' integer types in RTI code
  • Loading branch information
Jakio815 authored Dec 20, 2024
2 parents 6253d02 + c1c1500 commit 4cad9fb
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 19 deletions.
10 changes: 4 additions & 6 deletions core/federated/RTI/rti_remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions core/federated/clock-sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
9 changes: 4 additions & 5 deletions core/federated/federate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions core/utils/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions include/core/utils/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 4cad9fb

Please sign in to comment.