Created
July 8, 2021 13:26
-
-
Save JEnoch/3ed44b4b182b36e347d06661c5ae6d2e to your computer and use it in GitHub Desktop.
CycloneDDS TRANSIENT_LOCAL helloworld
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NOTE: modified version of | |
// https://github.com/eclipse-cyclonedds/cyclonedds/tree/c261053186c455abc63ca5ac7d56c0808a59c364/examples/helloworld | |
#include "dds/dds.h" | |
#include "HelloWorldData.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int main(int argc, char **argv) | |
{ | |
dds_entity_t participant; | |
dds_entity_t topic; | |
dds_entity_t writer; | |
dds_return_t rc; | |
dds_qos_t *qos; | |
HelloWorldData_Msg msg; | |
(void)argc; | |
(void)argv; | |
/* Create a Participant. */ | |
participant = dds_create_participant(1, NULL, NULL); | |
if (participant < 0) | |
DDS_FATAL("dds_create_participant: %s\n", dds_strretcode(-participant)); | |
/* Create a Topic. */ | |
topic = dds_create_topic( | |
participant, &HelloWorldData_Msg_desc, "HelloWorldData_Msg", NULL, NULL); | |
if (topic < 0) | |
DDS_FATAL("dds_create_topic: %s\n", dds_strretcode(-topic)); | |
/* Create a Writer. */ | |
qos = dds_create_qos(); | |
dds_qset_reliability(qos, DDS_RELIABILITY_RELIABLE, DDS_SECS(10)); | |
dds_qset_durability(qos, DDS_DURABILITY_TRANSIENT_LOCAL); | |
dds_qset_history(qos, DDS_HISTORY_KEEP_LAST, 10); | |
dds_qset_durability_service(qos, DDS_SECS(60), DDS_HISTORY_KEEP_LAST, 10, 100, 100, 100); | |
writer = dds_create_writer(participant, topic, qos, NULL); | |
if (writer < 0) | |
DDS_FATAL("dds_create_writer: %s\n", dds_strretcode(-writer)); | |
for (int i = 1; i < 100; i++) | |
{ | |
char str[20]; | |
sprintf(str, "Hello World #%d", i); | |
/* Create a message to write. */ | |
msg.userID = 1; | |
msg.message = str; | |
printf("=== [Publisher] Writing : "); | |
printf("Message (%" PRId32 ", %s)\n", msg.userID, msg.message); | |
fflush(stdout); | |
rc = dds_write(writer, &msg); | |
if (rc != DDS_RETCODE_OK) | |
DDS_FATAL("dds_write: %s\n", dds_strretcode(-rc)); | |
dds_sleepfor(DDS_SECS(1)); | |
} | |
/* Deleting the participant will delete all its children recursively as well. */ | |
rc = dds_delete(participant); | |
if (rc != DDS_RETCODE_OK) | |
DDS_FATAL("dds_delete: %s\n", dds_strretcode(-rc)); | |
return EXIT_SUCCESS; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NOTE: modified version of | |
// https://github.com/eclipse-cyclonedds/cyclonedds/tree/c261053186c455abc63ca5ac7d56c0808a59c364/examples/helloworld | |
#include "dds/dds.h" | |
#include "HelloWorldData.h" | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
/* An array of one message (aka sample in dds terms) will be used. */ | |
#define MAX_SAMPLES 10 | |
int main(int argc, char **argv) | |
{ | |
dds_entity_t participant; | |
dds_entity_t topic; | |
dds_entity_t reader; | |
HelloWorldData_Msg *msg; | |
void *samples[MAX_SAMPLES]; | |
dds_sample_info_t infos[MAX_SAMPLES]; | |
dds_return_t rc; | |
dds_qos_t *qos; | |
(void)argc; | |
(void)argv; | |
/* Create a Participant. */ | |
participant = dds_create_participant(2, NULL, NULL); | |
if (participant < 0) | |
DDS_FATAL("dds_create_participant: %s\n", dds_strretcode(-participant)); | |
/* Create a Topic. */ | |
topic = dds_create_topic( | |
participant, &HelloWorldData_Msg_desc, "HelloWorldData_Msg", NULL, NULL); | |
if (topic < 0) | |
DDS_FATAL("dds_create_topic: %s\n", dds_strretcode(-topic)); | |
/* Create a reliable Reader. */ | |
qos = dds_create_qos(); | |
dds_qset_reliability(qos, DDS_RELIABILITY_RELIABLE, DDS_SECS(10)); | |
dds_qset_durability(qos, DDS_DURABILITY_TRANSIENT_LOCAL); | |
dds_qset_history(qos, DDS_HISTORY_KEEP_LAST, 10); | |
reader = dds_create_reader(participant, topic, qos, NULL); | |
if (reader < 0) | |
DDS_FATAL("dds_create_reader: %s\n", dds_strretcode(-reader)); | |
dds_delete_qos(qos); | |
rc = dds_reader_wait_for_historical_data(reader, DDS_MSECS(1000)); | |
if (rc < 0) | |
DDS_FATAL("dds_reader_wait_for_historical_data: %s\n", dds_strretcode(-rc)); | |
printf("\n=== [Subscriber] Waiting for a sample ...\n"); | |
fflush(stdout); | |
/* Initialize sample buffer, by pointing the void pointer within | |
* the buffer array to a valid sample memory location. */ | |
for (int i = 0; i < MAX_SAMPLES; i++) | |
{ | |
samples[i] = HelloWorldData_Msg__alloc(); | |
} | |
/* Poll until data has been read. */ | |
while (true) | |
{ | |
/* Do the actual read. | |
* The return value contains the number of read samples. */ | |
rc = dds_take(reader, samples, infos, MAX_SAMPLES, MAX_SAMPLES); | |
if (rc < 0) | |
DDS_FATAL("dds_read: %s\n", dds_strretcode(-rc)); | |
/* Check if we read some data and it is valid. */ | |
if ((rc > 0) && (infos[0].valid_data)) | |
{ | |
printf("= %d samples received\n", rc); | |
/* Print Messages. */ | |
for (int i = 0; i < rc; i++) | |
{ | |
msg = (HelloWorldData_Msg *)samples[i]; | |
printf("=== [Subscriber] Received : "); | |
printf("Message (%" PRId32 ", %s)\n", msg->userID, msg->message); | |
fflush(stdout); | |
} | |
// break; | |
} | |
else | |
{ | |
/* Polling sleep. */ | |
dds_sleepfor(DDS_MSECS(20)); | |
} | |
} | |
/* Free the data location. */ | |
HelloWorldData_Msg_free(samples[0], DDS_FREE_ALL); | |
/* Deleting the participant will delete all its children recursively as well. */ | |
rc = dds_delete(participant); | |
if (rc != DDS_RETCODE_OK) | |
DDS_FATAL("dds_delete: %s\n", dds_strretcode(-rc)); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment