publisher.cpp
Go to the documentation of this file.
1 
23 #include <string>
24 #include <iostream>
25 #include <sstream>
26 #include <vector>
27 #include "ccpp_dds_dcps.h"
32 
33 using namespace DDS;
34 
35 namespace opensplice_dds_broker{
36 
37  Publisher::Publisher(const std::string& topic_name)
38  {
39  domain = 0;
40  topic_name_ = topic_name.data();
41  GSDFPacketTypeName = NULL;
42 
43  //Create a DomainParticipantFactory and a DomainParticipant, using Default QoS settings
44  dpf = DomainParticipantFactory::get_instance ();
45  checkHandle(dpf.in(), "DDS::DomainParticipantFactory::get_instance");
46  participant = dpf->create_participant(domain, PARTICIPANT_QOS_DEFAULT, NULL, STATUS_MASK_NONE);
47  checkHandle(participant.in(), "DDS::DomainParticipantFactory::create_participant");
48 
49  //Register the required datatype
50  GSDFPacketTS = new GSDFPacketTypeSupport();
51  checkHandle(GSDFPacketTS.in(), "new GSDFPacketTypeSupport");
52  GSDFPacketTypeName = GSDFPacketTS->get_type_name();
53  status = GSDFPacketTS->register_type(
54  participant.in(),
55  GSDFPacketTypeName);
56  checkStatus(status, "micros_swarm_framework::GSDFPacketTypeSupport::register_type");
57 
58  //Set the ReliabilityQosPolicy to BEST_EFFORT_RELIABILITY
59  status = participant->get_default_topic_qos(topic_qos);
60  checkStatus(status, "DDS::DomainParticipant::get_default_topic_qos");
61  topic_qos.reliability.kind = BEST_EFFORT_RELIABILITY_QOS;
62  //topic_qos.reliability.kind = RELIABLE_RELIABILITY_QOS;
63  topic_qos.durability_service.history_kind = KEEP_LAST_HISTORY_QOS;
64  //topic_qos.durability_service.history_depth = 4000;
65 
66  //Make the tailored QoS the new default
67  status = participant->set_default_topic_qos(topic_qos);
68  checkStatus(status, "DDS::DomainParticipant::set_default_topic_qos");
69 
70  //Use the changed policy when defining the GSDFPacket topic
71  GSDFPacketTopic = participant->create_topic(
72  topic_name_,
73  GSDFPacketTypeName,
74  topic_qos,
75  NULL,
76  STATUS_MASK_NONE);
77  checkHandle(GSDFPacketTopic.in(), "DDS::DomainParticipant::create_topic (GSDFPacket)");
78 
79  //Adapt the default PublisherQos to write into the "micros_swarm_framework_default_partion" Partition
80  status = participant->get_default_publisher_qos (pub_qos);
81  checkStatus(status, "DDS::DomainParticipant::get_default_publisher_qos");
82  pub_qos.partition.name.length(1);
83  pub_qos.partition.name[0] = "micros_swarm_framework_partion";
84 
85  //Create a Publisher for the application
86  publisher_ = participant->create_publisher(pub_qos, NULL, STATUS_MASK_NONE);
87  checkHandle(publisher_.in(), "DDS::DomainParticipant::create_publisher");
88 
89  status = publisher_->get_default_datawriter_qos(dw_qos);
90  dw_qos.destination_order.kind = BY_SOURCE_TIMESTAMP_DESTINATIONORDER_QOS;
91  dw_qos.history.kind = KEEP_ALL_HISTORY_QOS;
92  //dw_qos.history.kind = KEEP_LAST_HISTORY_QOS;
93  //dw_qos.durability.kind = TRANSIENT_LOCAL_DURABILITY_QOS;
94  dw_qos.durability.kind = VOLATILE_DURABILITY_QOS;
95 
96  //Create a DataWriter for the GSDFPacket Topic (using the appropriate QoS)
97  parentWriter = publisher_->create_datawriter(
98  GSDFPacketTopic.in(),
99  dw_qos,
100  NULL,
101  STATUS_MASK_NONE);
102  checkHandle(parentWriter, "DDS::Publisher::create_datawriter (GSDFPacket)");
103 
104  //Narrow the abstract parent into its typed representative
105  GSDFPacketDW = GSDFPacketDataWriter::_narrow(parentWriter);
106  checkHandle(GSDFPacketDW.in(), "micros_swarm_framework::GSDFPacketDataWriter::_narrow");
107  }
108 
109  void Publisher::publish(const GSDFPacket& packet)
110  {
111  status = GSDFPacketDW->write(packet, DDS::HANDLE_NIL);
112  checkStatus(status, "micros_swarm_framework::GSDFPacketDataWriter::write");
113  }
114 
115  Publisher::~Publisher()
116  {
117  //Remove the DataWriters
118  status = publisher_->delete_datawriter(GSDFPacketDW.in() );
119  checkStatus(status, "DDS::Publisher::delete_datawriter (GSDFPacketDW)");
120 
121  //Remove the Publisher
122  status = participant->delete_publisher(publisher_.in() );
123  checkStatus(status, "DDS::DomainParticipant::delete_publisher");
124 
125  status = participant->delete_topic( GSDFPacketTopic.in() );
126  checkStatus(status, "DDS::DomainParticipant::delete_topic (GSDFPacketTopic)");
127 
128  //Remove the type-names
129  string_free(GSDFPacketTypeName);
130 
131  //Remove the DomainParticipant
132  status = dpf->delete_participant( participant.in() );
133  checkStatus(status, "DDS::DomainParticipantFactory::delete_participant");
134 
135  //cout << "Completed publisher" << endl;
136  }
137 };
void checkStatus(DDS::ReturnCode_t status, const char *info)
void checkHandle(void *handle, const char *info)


opensplice_dds_broker
Author(s):
autogenerated on Mon Jun 10 2019 14:02:23