Skip to content

Instantly share code, notes, and snippets.

@wjwwood wjwwood/TestMessage.idl Secret
Created May 20, 2016

Embed
What would you like to do?
Example of DataWriter discovery with a local DataWriter.
// Copyright William Woodall 2016
// Licensed under the MIT License:
// https://opensource.org/licenses/MIT
#include <cstdio>
#ifndef _WIN32
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
# pragma GCC diagnostic ignored "-Wunused-parameter"
# ifdef __clang__
# pragma clang diagnostic ignored "-Wdeprecated-register"
# pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
# endif
#endif
#include <ndds/ndds_cpp.h>
#include <ndds/ndds_requestreply_cpp.h>
#include "connext/TestMessageSupport.h"
#ifndef _WIN32
# pragma GCC diagnostic pop
#endif
class MyListener : public DDSDataReaderListener
{
public:
virtual void on_data_available(DDSDataReader * reader)
{
printf("enter on_data_available\n");
DDSPublicationBuiltinTopicDataDataReader * builtin_reader =
static_cast<DDSPublicationBuiltinTopicDataDataReader *>(reader);
DDS_PublicationBuiltinTopicDataSeq data_seq;
DDS_SampleInfoSeq info_seq;
DDS_ReturnCode_t retcode = builtin_reader->take(
data_seq, info_seq, DDS_LENGTH_UNLIMITED,
DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE);
if (retcode == DDS_RETCODE_NO_DATA) {
printf("exit on_data_available: no data\n");
return;
}
if (retcode != DDS_RETCODE_OK) {
fprintf(stderr, "failed to access data from the built-in reader\n");
printf("exit on_data_available: error\n");
return;
}
for (auto i = 0; i < data_seq.length(); ++i) {
if (info_seq[i].valid_data) {
printf("new( ");
for (int j = 0; j < info_seq[i].instance_handle.keyHash.length; ++j) {
printf(" %02x", info_seq[i].instance_handle.keyHash.value[j]);
}
printf("): %s <%s>\n", data_seq[i].topic_name, data_seq[i].type_name);
} else {
printf("removed(");
for (int j = 0; j < info_seq[i].instance_handle.keyHash.length; ++j) {
printf(" %02x", info_seq[i].instance_handle.keyHash.value[j]);
}
printf(")\n");
}
}
builtin_reader->return_loan(data_seq, info_seq);
printf("exit on_data_available\n");
}
};
int main()
{
DDS_ReturnCode_t ret;
/* Create the participant */
DDSDomainParticipantFactory * dpf_ = DDSDomainParticipantFactory::get_instance();
if (!dpf_) {
fprintf(stderr, "failed to get participant factory\n");
return 1;
}
// create the participant in a disabled state to avoid loss of discovery data
DDS_DomainParticipantFactoryQos factory_qos;
ret = DDSTheParticipantFactory->get_qos(factory_qos);
if (ret != DDS_RETCODE_OK) {
fprintf(stderr, "failed to get factory qos\n");
return 1;
}
factory_qos.entity_factory.autoenable_created_entities = DDS_BOOLEAN_FALSE;
switch(DDSTheParticipantFactory->set_qos(factory_qos)) {
case DDS_RETCODE_OK:
break;
case DDS_RETCODE_IMMUTABLE_POLICY:
fprintf(stderr, "faild to set factory qos due to IMMUTABLE_POLICY\n");
return 1;
case DDS_RETCODE_INCONSISTENT_POLICY:
fprintf(stderr, "faild to set factory qos due to INCONSISTENT_POLICY\n");
return 1;
default:
fprintf(stderr, "faild to set factory qos for unknown reason\n");
return 1;
}
DDSDomainParticipant * participant = dpf_->create_participant(
42,
DDS_PARTICIPANT_QOS_DEFAULT, NULL, DDS_STATUS_MASK_NONE
);
if (!participant) {
fprintf(stderr, "failed to create participant\n");
return 1;
}
/* Create the custom listener */
DDSSubscriber * builtin_subscriber = participant->get_builtin_subscriber();
if (!builtin_subscriber) {
fprintf(stderr, "builtin subscriber handle is null\n");
return 1;
}
DDSDataReader * data_reader = builtin_subscriber->lookup_datareader(DDS_PUBLICATION_TOPIC_NAME);
DDSPublicationBuiltinTopicDataDataReader * builtin_publication_datareader =
static_cast<DDSPublicationBuiltinTopicDataDataReader *>(data_reader);
if (!builtin_publication_datareader) {
fprintf(stderr, "builtin publication datareader handle is null\n");
return 1;
}
MyListener my_listener;
builtin_publication_datareader->set_listener(&my_listener, DDS_DATA_AVAILABLE_STATUS);
// now that the listener is installed, enable the participant.
if (participant->enable() != DDS_RETCODE_OK) {
fprintf(stderr, "failed to enable participant\n");
return 1;
}
printf("Sleeping before creating publisher... ");
fflush(stdout);
sleep(5);
printf("done.\n");
/* Create a publisher so our listener finds something */
DDSPublisher * publisher = participant->create_publisher(
DDS_PUBLISHER_QOS_DEFAULT, nullptr, DDS_STATUS_MASK_NONE);
if (!publisher) {
fprintf(stderr, "faield to create publisher\n");
return 1;
}
ret = TestMessageTypeSupport::register_type(participant);
if (ret != DDS_RETCODE_OK) {
fprintf(stderr, "registering type failed\n");
return 1;
}
DDSTopic * topic = participant->create_topic(
"test", TestMessageTypeSupport::get_type_name(),
DDS_TOPIC_QOS_DEFAULT, nullptr, DDS_STATUS_MASK_NONE
);
if (!topic) {
fprintf(stderr, "failed to create topic\n");
return 1;
}
DDSDataWriter * data_writer = publisher->create_datawriter(
topic, DDS_DATAWRITER_QOS_DEFAULT, nullptr, DDS_STATUS_MASK_NONE);
if (!data_writer) {
fprintf(stderr, "failed to create DataWriter\n");
return 1;
}
TestMessageDataWriter * tm_dw = TestMessageDataWriter::narrow(data_writer);
if (!tm_dw) {
fprintf(stderr, "failed to narrow DataWriter\n");
return 1;
}
while(true) {
sleep(1);
}
return 0;
}
struct TestMessage {
string<64> message;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.