subscriber.cpp
Go to the documentation of this file.
1 
23 #include <iostream>
24 #include <string>
25 #include "ccpp_dds_dcps.h"
30 
32 
33 using namespace DDS;
34 
35 namespace opensplice_dds_broker{
36 
37  Subscriber::Subscriber(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 (
47  domain,
48  PARTICIPANT_QOS_DEFAULT,
49  NULL,
50  STATUS_MASK_NONE);
51  checkHandle(participant, "DDS::DomainParticipantFactory::create_participant");
52 
53  //Register the required datatype for GSDFPacket
54  GSDFPacketTS = new GSDFPacketTypeSupport();
55  checkHandle(GSDFPacketTS.in(), "new GSDFPacketTypeSupport");
56  GSDFPacketTypeName = GSDFPacketTS->get_type_name();
57  status = GSDFPacketTS->register_type(
58  participant.in(),
59  GSDFPacketTypeName);
60  checkStatus(status, "NetworkPartitionsData::GSDFPacketTypeSupport::register_type");
61 
62  //Set the ReliabilityQosPolicy to BEST_EFFORT_RELIABILITY
63  status = participant->get_default_topic_qos(topic_qos);
64  checkStatus(status, "DDS::DomainParticipant::get_default_topic_qos");
65 
66  topic_qos.reliability.kind = BEST_EFFORT_RELIABILITY_QOS;
67  //topic_qos.reliability.kind = RELIABLE_RELIABILITY_QOS;
68  topic_qos.durability_service.history_kind = KEEP_LAST_HISTORY_QOS;
69  //topic_qos.durability_service.history_depth = 4000;
70 
71  //Make the tailored QoS the new default
72  status = participant->set_default_topic_qos(topic_qos);
73  checkStatus(status, "DDS::DomainParticipant::set_default_topic_qos");
74 
75  //Use the changed policy when defining the GSDFPacket topic
76  GSDFPacketTopic = participant->create_topic(
77  topic_name_,
78  GSDFPacketTypeName,
79  topic_qos,
80  NULL,
81  STATUS_MASK_NONE);
82  checkHandle(GSDFPacketTopic.in(), "DDS::DomainParticipant::create_topic (GSDFPacket)");
83 
84  //Adapt the default SubscriberQos to read from the "micros_swarm_framework_partion" Partition
85  status = participant->get_default_subscriber_qos (sub_qos);
86  checkStatus(status, "DDS::DomainParticipant::get_default_subscriber_qos");
87  sub_qos.partition.name.length(1);
88  std::string partition_name="micros_swarm_framework_partion";
89  sub_qos.partition.name[0] = partition_name.data();
90 
91  //Create a Subscriber for the MessageBoard application
92  subscriber_ = participant->create_subscriber(sub_qos, NULL, STATUS_MASK_NONE);
93  checkHandle(subscriber_.in(), "DDS::DomainParticipant::create_subscriber");
94 
95  status = subscriber_->get_default_datareader_qos(dr_qos);
96  dr_qos.history.kind = KEEP_ALL_HISTORY_QOS;
97  //dr_qos.history.kind = KEEP_LAST_HISTORY_QOS;
98  dr_qos.destination_order.kind = BY_SOURCE_TIMESTAMP_DESTINATIONORDER_QOS;
99  //dr_qos.durability.kind = TRANSIENT_LOCAL_DURABILITY_QOS;
100  dr_qos.durability.kind = VOLATILE_DURABILITY_QOS;
101 
102  //Create a DataReader for the NamedMessage Topic (using the appropriate QoS)
103  parentReader = subscriber_->create_datareader(
104  GSDFPacketTopic.in(),
105  dr_qos,
106  NULL,
107  STATUS_MASK_NONE);
108  checkHandle(parentReader, "DDS::Subscriber::create_datareader");
109 
110  //Narrow the abstract parent into its typed representative
111  GSDFPacketDR = GSDFPacketDataReader::_narrow(parentReader);
112  checkHandle(GSDFPacketDR.in(), "NetworkPartitionsData::GSDFPacketDataReader::_narrow");
113  }
114 
115  void Subscriber::subscribe(void (*callBack)(const GSDFPacket& packet))
116  {
117  GSDFPacketListener *myListener = new GSDFPacketListener();
118  myListener->callBack_ = callBack; //set callBack function
119  //myListener->callBack_ = boost::bind(callBack, _1); //set callBack function
120  //myListener->GSDFPacketDR_ = GSDFPacketDataReader::_narrow(GSDFPacketDR.in());
121  //checkHandle(myListener->GSDFPacketDR_.in(), "GSDFPacketDataReader::_narrow");
122 
123  //DDS::StatusMask mask = DDS::DATA_AVAILABLE_STATUS | DDS::REQUESTED_DEADLINE_MISSED_STATUS;
124  DDS::StatusMask mask = DDS::DATA_AVAILABLE_STATUS;
125  //myListener->GSDFPacketDR_->set_listener(myListener, mask);
126  GSDFPacketDR->set_listener(myListener, mask);
127  }
128 
129  void Subscriber::subscribe(boost::function<void(const GSDFPacket&)> callBack)
130  {
131  GSDFPacketListener *myListener = new GSDFPacketListener();
132  myListener->callBack_ = callBack; //set callBack function
133  //myListener->GSDFPacketDR_ = GSDFPacketDataReader::_narrow(GSDFPacketDR.in());
134  //checkHandle(myListener->GSDFPacketDR_.in(), "GSDFPacketDataReader::_narrow");
135 
136  //DDS::StatusMask mask = DDS::DATA_AVAILABLE_STATUS | DDS::REQUESTED_DEADLINE_MISSED_STATUS;
137  DDS::StatusMask mask = DDS::DATA_AVAILABLE_STATUS;
138  //myListener->GSDFPacketDR_->set_listener(myListener, mask);
139  GSDFPacketDR->set_listener(myListener, mask);
140  }
141 
142  Subscriber::~Subscriber()
143  {
144  //Remove the DataReade
145  status = subscriber_->delete_datareader(GSDFPacketDR.in());
146  checkStatus(status, "DDS::Subscriber::delete_datareader");
147 
148  //Remove the Subscriber
149  status = participant->delete_subscriber(subscriber_.in());
150  checkStatus(status, "DDS::DomainParticipant::delete_subscriber");
151 
152  //Remove the Topic
153  status = participant->delete_topic(GSDFPacketTopic.in());
154  checkStatus(status, "DDS::DomainParticipant::delete_topic (GSDFPacketTopic)");
155 
156  //De-allocate the type-names
157  string_free(GSDFPacketTypeName);
158 
159  //Remove the DomainParticipant
160  status = dpf->delete_participant(participant.in());
161  checkStatus(status, "DDS::DomainParticipantFactory::delete_participant");
162 
163  //cout << "Completed subscriber" << endl;
164  }
165 };
166 
void checkStatus(DDS::ReturnCode_t status, const char *info)
void callBack(const opensplice_dds_broker::GSDFPacket &packet)
Definition: sub.cpp:59
void checkHandle(void *handle, const char *info)
boost::function< void(const GSDFPacket &)> callBack_


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