Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Federated Hello Protocol Buffers Example #117

Open
burakakca opened this issue Dec 3, 2024 · 1 comment
Open

Federated Hello Protocol Buffers Example #117

burakakca opened this issue Dec 3, 2024 · 1 comment

Comments

@burakakca
Copy link

This example works fine when i change the federated to main. Is there way to use as a federated?

target C {
  timeout: 10 secs,
  protobufs: hello_string.proto
}

/**
 * Reactor that generates serialized Protocol Buffers messages.
 * @param prefix The prefix string.
 * @output message The serialized message (Protocol Buffers format).
 */
reactor MessageGenerator(prefix: string = "") {
  output message: uint8_t[]
  state count: int = 1
  timer t(1 sec, 1 sec)

  reaction(t) -> message {=
    // Create and populate the Protocol Buffers message
    HelloString pack_msg = HELLO_STRING__INIT;
    char buffer[256];
    snprintf(buffer, sizeof(buffer), "%s %d", self->prefix, self->count++);
    pack_msg.message = buffer;

    // Serialize the message
    unsigned len = hello_string__get_packed_size(&pack_msg);
    uint8_t* buf = malloc(len);
    hello_string__pack(&pack_msg, buf);

    // Set the output
    lf_set_array(message, buf, len);

    tag_t tag = lf_tag();
    lf_print("At (elapsed) logical tag (%lld, %u), source sends message: %s",
      tag.time - lf_time_start(), tag.microstep,
      buffer
    );
  =}
}

/**
 * Reactor that deserializes and prints Protocol Buffers messages.
 * @input message The serialized message (Protocol Buffers format).
 */
reactor PrintMessage {
  input message: uint8_t[]

  reaction(message) {=
    // Deserialize the incoming message
    unsigned len = message->length;
    HelloString* unpacked_msg = hello_string__unpack(NULL, len, message->value);

    // Print the unpacked message
    if (unpacked_msg != NULL) {
      tag_t tag = lf_tag();
      lf_print("At (elapsed) logical tag (%lld, %u), print receives: %s",
        tag.time - lf_time_start(), tag.microstep,
        unpacked_msg->message
      );
      hello_string__free_unpacked(unpacked_msg, NULL);
    } else {
      lf_print("Failed to deserialize message.");
    }
  =}
}

federated reactor HelloWorld {
  source = new MessageGenerator(prefix = "Hello Protocol Buffers")
  print = new PrintMessage()
  source.message -> print.message
}

@burakakca
Copy link
Author

@erlingrj Can you help me?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant