Skip to content

Commit

Permalink
Fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
erlingrj committed Oct 21, 2024
1 parent 7a44b16 commit 93ca484
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
24 changes: 15 additions & 9 deletions examples/posix/testing_nanopb.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,37 @@

int main() {

TaggedMessage original_message;
TaggedMessage deserialized_message;
FederateMessage msg;
FederateMessage deserialized;

msg.type = MessageType_TAGGED_MESSAGE;
msg.which_message = FederateMessage_tagged_message_tag;

TaggedMessage *original_message = &msg.message.tagged_message;
unsigned char buffer[BUFFER_SIZE];
unsigned char *message = NULL;
int message_size = 0;

original_message.conn_id = MSG_ID;
original_message->conn_id = MSG_ID;
const char *text = "Hello World1234";
memcpy(original_message.payload.bytes, text, sizeof("Hello World1234")); // NOLINT
original_message.payload.size = sizeof("Hello World1234");
memcpy(original_message->payload.bytes, text, sizeof("Hello World1234")); // NOLINT
original_message->payload.size = sizeof("Hello World1234");

message = buffer;
message_size = encode_protobuf(&original_message, buffer, BUFFER_SIZE);
message_size = encode_protobuf(&msg, buffer, BUFFER_SIZE);
if (message_size < 0) {
printf("encoding failed!\n");
exit(1);
}

int remaining_bytes = decode_protobuf(&deserialized_message, message, message_size);
int remaining_bytes = decode_protobuf(&deserialized, message, message_size);

if (remaining_bytes < 0) {
printf("decoding failed!\n");
exit(1);
}

printf("o: %i d: %i\n", original_message.conn_id, deserialized_message.conn_id);
printf("o: %s d: %s\n", (char *)original_message.payload.bytes, (char *)deserialized_message.payload.bytes);
printf("o: %i d: %i\n", original_message->conn_id, deserialized.message.tagged_message.conn_id);
printf("o: %s d: %s\n", (char *)original_message->payload.bytes,
(char *)deserialized.message.tagged_message.payload.bytes);
}
22 changes: 13 additions & 9 deletions examples/posix/testing_posix_tcp_ip_channel_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ int main() {
unsigned short port = 8902; // NOLINT

// message for server
TaggedMessage port_message;
port_message.conn_id = 42; // NOLINT
FederateMessage msg;
msg.type = MessageType_TAGGED_MESSAGE;
msg.which_message = FederateMessage_tagged_message_tag;

TaggedMessage *port_message = &msg.message.tagged_message;
port_message->conn_id = 42; // NOLINT
const char *message = "Hello World1234";
memcpy(port_message.payload.bytes, message, sizeof("Hello World1234")); // NOLINT
port_message.payload.size = sizeof("Hello World1234");
memcpy(port_message->payload.bytes, message, sizeof("Hello World1234")); // NOLINT
port_message->payload.size = sizeof("Hello World1234");

// creating a server that listens on loopback device on port 8900
TcpIpChannel_ctor(&channel, host, port, AF_INET);
Expand All @@ -28,13 +32,13 @@ int main() {

for (int i = 0; i < NUM_ITER; i++) {
// sending message
channel.super.send(&channel.super, &port_message);

channel.super.send(&channel.super, &msg);
// waiting for reply
TaggedMessage *received_message = channel.super.receive(&channel.super);
const FederateMessage *received_message = channel.super.receive(&channel.super);
const TaggedMessage *received_tagged_message = &received_message->message.tagged_message;

printf("Received message with connection number %i and content %s\n", received_message->conn_id,
(char *)received_message->payload.bytes);
printf("Received message with connection number %i and content %s\n", received_tagged_message->conn_id,
(char *)received_tagged_message->payload.bytes);
}

channel.super.close(&channel.super);
Expand Down
7 changes: 4 additions & 3 deletions examples/posix/testing_posix_tcp_ip_channel_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ int main() {

for (int i = 0; i < NUM_ITER; i++) {
// waiting for messages from client
TaggedMessage *message = channel.super.receive(&channel.super);
printf("Received message with connection number %i and content %s\n", message->conn_id,
(char *)message->payload.bytes);
const FederateMessage *message = channel.super.receive(&channel.super);
const TaggedMessage *tagged_message = &message->message.tagged_message;
printf("Received message with connection number %i and content %s\n", tagged_message->conn_id,
(char *)tagged_message->payload.bytes);

channel.super.send(&channel.super, message);
}
Expand Down
5 changes: 3 additions & 2 deletions examples/posix/testing_tcp_ip_channel_server_callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
#include <unistd.h>
TcpIpChannel channel;

void callback_handler(FederatedConnectionBundle *self, TaggedMessage *msg) {
void callback_handler(FederatedConnectionBundle *self, const FederateMessage *_msg) {
const TaggedMessage *msg = &_msg->message.tagged_message;
printf("Received message with connection number %i and content %s\n", msg->conn_id, (char *)msg->payload.bytes);
channel.super.send(&channel.super, msg);
channel.super.send(&channel.super, _msg);
}

int main() {
Expand Down

0 comments on commit 93ca484

Please sign in to comment.