HelloWorld_publisher.cpp
The publication example generated by
rtiddsgen. The example has been modified slightly to update the sample value.
[$(NDDSHOME)/example/CPPCLI/helloWorld/HelloWorld_publisher.cpp]
#ifndef IMPORT_HelloWorld
#include "HelloWorldSupport.h"
#endif
using namespace System;
public ref class HelloWorldPublisher {
public:
static void publish(int domain_id, int sample_count);
private:
static void shutdown(
DDS::DomainParticipant^ participant);
};
int main(array<System::String^>^ argv) {
int domain_id = 0;
if (argv->Length >= 1) {
domain_id = Int32::Parse(argv[0]);
}
int sample_count = 0;
if (argv->Length >= 2) {
sample_count = Int32::Parse(argv[1]);
}
try {
HelloWorldPublisher::publish(
domain_id, sample_count);
}
catch(DDS::Exception^) {
return -1;
}
return 0;
}
void HelloWorldPublisher::publish(int domain_id, int sample_count) {
DDS::DomainParticipant^ participant =
DDS::DomainParticipantFactory::get_instance()->create_participant(
domain_id,
DDS::DomainParticipantFactory::PARTICIPANT_QOS_DEFAULT,
nullptr ,
DDS::StatusMask::STATUS_MASK_NONE);
if (participant == nullptr) {
shutdown(participant);
throw gcnew ApplicationException("create_participant error");
}
DDS::Publisher^ publisher = participant->create_publisher(
DDS::DomainParticipant::PUBLISHER_QOS_DEFAULT,
nullptr ,
DDS::StatusMask::STATUS_MASK_NONE);
if (publisher == nullptr) {
shutdown(participant);
throw gcnew ApplicationException("create_publisher error");
}
System::String^ type_name = HelloWorldTypeSupport::get_type_name();
try {
HelloWorldTypeSupport::register_type(
participant, type_name);
} catch (DDS::Exception^ e) {
shutdown(participant);
throw e;
}
DDS::Topic^ topic = participant->create_topic(
"Example HelloWorld",
type_name,
DDS::DomainParticipant::TOPIC_QOS_DEFAULT,
nullptr ,
DDS::StatusMask::STATUS_MASK_NONE);
if (topic == nullptr) {
shutdown(participant);
throw gcnew ApplicationException("create_topic error");
}
DDS::DataWriter^ writer = publisher->create_datawriter(
topic,
DDS::Publisher::DATAWRITER_QOS_DEFAULT,
nullptr ,
DDS::StatusMask::STATUS_MASK_NONE);
if (writer == nullptr) {
shutdown(participant);
throw gcnew ApplicationException("create_datawriter error");
}
HelloWorldDataWriter^ HelloWorld_writer =
safe_cast<HelloWorldDataWriter^>(writer);
HelloWorld^ instance = HelloWorldTypeSupport::create_data();
if (instance == nullptr) {
shutdown(participant);
throw gcnew ApplicationException(
"HelloWorldTypeSupport::create_data error");
}
DDS::InstanceHandle_t instance_handle = DDS::InstanceHandle_t::HANDLE_NIL;
const System::Int32 send_period = 4000;
for (int count=0; (sample_count == 0) || (count < sample_count); ++count) {
Console::WriteLine("Writing HelloWorld, count {0}", count);
instance->msg = "Hello World! (" + count + ")";
try {
HelloWorld_writer->write(instance, instance_handle);
}
catch(DDS::Exception ^e) {
Console::WriteLine("write error: {0}", e);
}
System::Threading::Thread::Sleep(send_period);
}
try {
HelloWorldTypeSupport::delete_data(instance);
}
catch(DDS::Exception ^e) {
Console::WriteLine("HelloWorldTypeSupport::delete_data error: {0}", e);
}
shutdown(participant);
}
void HelloWorldPublisher::shutdown(
DDS::DomainParticipant^ participant) {
if (participant != nullptr) {
participant->delete_contained_entities();
DDS::DomainParticipantFactory::get_instance()->delete_participant(participant);
}
}