-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublisher.cpp
More file actions
executable file
·384 lines (330 loc) · 13.3 KB
/
publisher.cpp
File metadata and controls
executable file
·384 lines (330 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
*
*
* Distributed under the OpenDDS License.
* See: http://www.opendds.org/license.html
*/
#include <ace/Get_Opt.h>
#include <ace/Log_Msg.h>
#include <ace/OS_NS_stdlib.h>
#include <ace/OS_NS_unistd.h>
#include <dds/DCPS/Marked_Default_Qos.h>
#include <dds/DCPS/PublisherImpl.h>
#include <dds/DCPS/Service_Participant.h>
#include <dds/DCPS/SubscriberImpl.h>
#include <dds/DCPS/WaitSet.h>
//#include "DataReaderListener.h"
#include <dds/DCPS/SubscriberImpl.h>
#include "dds/DCPS/StaticIncludes.h"
#include <iostream>
#include "string"
#include <boost/format.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/chrono.hpp>
#include <boost/program_options.hpp>
#include <boost/format.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/thread/thread.hpp>
#ifdef ACE_AS_STATIC_LIBS
#ifndef OPENDDS_SAFETY_PROFILE
#include <dds/DCPS/transport/udp/Udp.h>
#include <dds/DCPS/transport/multicast/Multicast.h>
#include <dds/DCPS/RTPS/RtpsDiscovery.h>
#include <dds/DCPS/transport/shmem/Shmem.h>
#endif
#include <dds/DCPS/transport/rtps_udp/RtpsUdp.h>
#endif
#include "MessengerTypeSupportImpl.h"
//#include "Writer.h"
#ifdef OPENDDS_SECURITY
const char auth_ca_file[] = "file:../../security/certs/identity/identity_ca_cert.pem";
const char perm_ca_file[] = "file:../../security/certs/permissions/permissions_ca_cert.pem";
const char id_cert_file[] = "file:../../security/certs/identity/test_participant_01_cert.pem";
const char id_key_file[] = "file:../../security/certs/identity/test_participant_01_private_key.pem";
const char governance_file[] = "file:./governance_signed.p7s";
const char permissions_file[] = "file:./permissions_1_signed.p7s";
const char DDSSEC_PROP_IDENTITY_CA[] = "dds.sec.auth.identity_ca";
const char DDSSEC_PROP_IDENTITY_CERT[] = "dds.sec.auth.identity_certificate";
const char DDSSEC_PROP_IDENTITY_PRIVKEY[] = "dds.sec.auth.private_key";
const char DDSSEC_PROP_PERM_CA[] = "dds.sec.access.permissions_ca";
const char DDSSEC_PROP_PERM_GOV_DOC[] = "dds.sec.access.governance";
const char DDSSEC_PROP_PERM_DOC[] = "dds.sec.access.permissions";
#endif
using namespace std;
namespace po = boost::program_options;
class Config
{
public:
static std::string pubTopic;
static std::string subTopic;
static std::string publisherUniqueName;
static std::string transportConfig;
static uint64_t subCount;
static uint64_t updateRate;
static uint64_t payloadSize;
static int64_t roundtripCount;
};
std::string Config::pubTopic;
std::string Config::subTopic;
std::string Config::publisherUniqueName;
std::string Config::transportConfig;
uint64_t Config::updateRate;
uint64_t Config::subCount;
uint64_t Config::payloadSize;
int64_t Config::roundtripCount;
void append(DDS::PropertySeq &props, const char *name, const char *value)
{
const DDS::Property_t prop = {name, value, false /*propagate*/};
const unsigned int len = props.length();
props.length(len + 1);
props[len] = prop;
}
int main(int argc, char *argv[])
{
#pragma region Init
DDS::ReturnCode_t retcode;
po::options_description description("Options");
description.add_options()
("help", "produce help message. Execution example - ./DDSInitiator --PubTopic pingTopic --SubTopic pongTopic -msgLength 1000")
("TransportConfig", po::value(&Config::transportConfig)->default_value("rtps_disc.ini"), "TransportConfig e.g. rtps_disc.ini, shmem.ini Note that SHMEM requires running the DCPSInfoRepo via '$DDS_ROOT/bin/DCPSInfoRepo'")
("PubTopic", po::value(&Config::pubTopic)->default_value("pingTopic"), "Publish to Topic (Ping)")
("SubTopic", po::value(&Config::subTopic)->default_value("pongTopic"), "Subscribe to Topic (Pong) ")
("msgLength", po::value(&Config::payloadSize)->default_value(100), "Message Length (bytes)")
("roundtripCount", po::value(&Config::roundtripCount)->default_value(1000), "ping-pong intervals (0 == Infinite, only for general load purposes)")
("subCount", po::value(&Config::subCount)->default_value(1), "number of subscribers to wait for a ping")
("pubName", po::value(&Config::publisherUniqueName)->default_value("Initiator"), "publisher name. Name 'Dummy' will cause the echoer to not echo messages")
("updateRate", po::value(&Config::updateRate)->default_value(0), "update rate (for general load purposes only)");
po::variables_map vm;
try
{
po::store(po::parse_command_line(argc, argv, description), vm);
po::notify(vm);
}
catch (const po::error &e)
{
std::cerr << e.what() << "\n";
return EXIT_FAILURE;
}
if (vm.count("help"))
{
std::cout << description << "\n";
return EXIT_SUCCESS;
}
DDS::DomainParticipantFactory_var dpf;
DDS::DomainParticipant_var participant;
try
{
std::cout << "Starting Initiator" << std::endl;
{
int argsNum = 3;
char *param_list[] = {(char *)".", (char *)"-DCPSConfigFile", (char *)Config::transportConfig.c_str()};
// Initialize DomainParticipantFactory
dpf = TheParticipantFactoryWithArgs(argsNum, param_list);
DDS::DomainParticipantQos part_qos;
dpf->get_default_participant_qos(part_qos);
#if defined(OPENDDS_SECURITY)
if (TheServiceParticipant->get_security())
{
DDS::PropertySeq &props = part_qos.property.value;
append(props, DDSSEC_PROP_IDENTITY_CA, auth_ca_file);
append(props, DDSSEC_PROP_IDENTITY_CERT, id_cert_file);
append(props, DDSSEC_PROP_IDENTITY_PRIVKEY, id_key_file);
append(props, DDSSEC_PROP_PERM_CA, perm_ca_file);
append(props, DDSSEC_PROP_PERM_GOV_DOC, governance_file);
append(props, DDSSEC_PROP_PERM_DOC, permissions_file);
}
#endif
// Create DomainParticipant
participant = dpf->create_participant(4,
part_qos,
DDS::DomainParticipantListener::_nil(),
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (CORBA::is_nil(participant.in()))
{
ACE_ERROR_RETURN((LM_ERROR,
ACE_TEXT("%N:%l: main()")
ACE_TEXT(" ERROR: create_participant failed!\n")),
-1);
}
#pragma endregion
#pragma region PubSide
//Creating publisher side (pong)
// Register TypeSupport (Messenger::Message)
Messenger::MessageTypeSupport_var mts =
new Messenger::MessageTypeSupportImpl();
if (mts->register_type(participant.in(), "") != DDS::RETCODE_OK)
{
ACE_ERROR_RETURN((LM_ERROR,
ACE_TEXT("%N:%l: main()")
ACE_TEXT(" ERROR: register_type failed!\n")),
-1);
}
// Create Topic
CORBA::String_var type_name = mts->get_type_name();
DDS::Topic_var topic =
participant->create_topic(Config::pubTopic.c_str(),
type_name.in(),
TOPIC_QOS_DEFAULT,
DDS::TopicListener::_nil(),
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (CORBA::is_nil(topic.in()))
{
ACE_ERROR_RETURN((LM_ERROR,
ACE_TEXT("%N:%l: main()")
ACE_TEXT(" ERROR: create_topic failed!\n")),
-1);
}
// Create Publisher
DDS::Publisher_var pub =
participant->create_publisher(PUBLISHER_QOS_DEFAULT,
DDS::PublisherListener::_nil(),
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (CORBA::is_nil(pub.in()))
{
ACE_ERROR_RETURN((LM_ERROR,
ACE_TEXT("%N:%l: main()")
ACE_TEXT(" ERROR: create_publisher failed!\n")),
-1);
}
DDS::DataWriterQos qos;
pub->get_default_datawriter_qos(qos);
qos.reliability.kind = DDS::RELIABLE_RELIABILITY_QOS;
qos.history.depth = 10;
// Create DataWriter
DDS::DataWriter_var dw =
pub->create_datawriter(topic.in(),
qos,
DDS::DataWriterListener::_nil(),
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (CORBA::is_nil(dw.in()))
{
ACE_ERROR_RETURN((LM_ERROR,
ACE_TEXT("%N:%l: main()")
ACE_TEXT(" ERROR: create_datawriter failed!\n")),
-1);
}
Messenger::MessageDataWriter_var message_dw = Messenger::MessageDataWriter::_narrow(dw.in());
if (CORBA::is_nil(message_dw.in()))
{
ACE_ERROR((LM_ERROR,
ACE_TEXT("%N:%l: svc()")
ACE_TEXT(" ERROR: _narrow failed!\n")));
ACE_OS::exit(-1);
}
//Message initialization
Messenger::Message message;
message.subject_id = 99;
DDS::InstanceHandle_t handle = message_dw->register_instance(message);
DDS::ReturnCode_t status;
message.from = Config::publisherUniqueName.c_str();
message.subject = "";
std::string s(Config::payloadSize, '*');
message.text = s.c_str();
message.count = 0;
#pragma endregion
#pragma region SubSide
//Creating subscriber side (pong)
// Create Topic
DDS::Topic_var pongTopic =
participant->create_topic(Config::subTopic.c_str(),
type_name.in(),
TOPIC_QOS_DEFAULT,
DDS::TopicListener::_nil(),
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (CORBA::is_nil(pongTopic.in()))
{
ACE_ERROR_RETURN((LM_ERROR,
ACE_TEXT("%N:%l main()")
ACE_TEXT(" ERROR: create_topic() failed!\n")),
-1);
}
// Create Subscriber
DDS::Subscriber_var sub =
participant->create_subscriber(SUBSCRIBER_QOS_DEFAULT,
DDS::SubscriberListener::_nil(),
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (CORBA::is_nil(sub.in()))
{
ACE_ERROR_RETURN((LM_ERROR,
ACE_TEXT("%N:%l main()")
ACE_TEXT(" ERROR: create_subscriber() failed!\n")),
-1);
}
// Create DataReader
DDS::DataReaderQos dr_qos;
sub->get_default_datareader_qos(dr_qos);
//dr_qos.reliability.kind = DDS::RELIABLE_RELIABILITY_QOS;
dr_qos.history.depth = 10;
DDS::DataReader_var reader =
sub->create_datareader(pongTopic.in(),
dr_qos,
DDS::DataReaderListener::_nil(),
OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (CORBA::is_nil(reader.in()))
{
ACE_ERROR_RETURN((LM_ERROR,
ACE_TEXT("%N:%l main()")
ACE_TEXT(" ERROR: create_datareader() failed!\n")),
-1);
}
Messenger::MessageDataReader_var message_dr =
Messenger::MessageDataReader::_narrow(reader);
if (CORBA::is_nil(message_dr.in()))
{
ACE_ERROR((LM_ERROR,
ACE_TEXT("%N:%l: on_data_available()")
ACE_TEXT(" ERROR: _narrow failed!\n")));
ACE_OS::exit(-1);
}
Messenger::Message receivedMessage;
DDS::SampleInfo si;
#pragma endregion
sleep(5); //Waiting for discovery to finish (TBU)
cout << "Running scenario ..." << endl;
boost::chrono::high_resolution_clock::time_point start = boost::chrono::high_resolution_clock::now();
for (int i = 1; i != Config::roundtripCount; i++)
{
//std::cout << "Writing #" << message.count << std::endl;
retcode = message_dw->write(message, handle);
if (retcode != DDS::RETCODE_OK)
{
ACE_ERROR((LM_ERROR, ACE_TEXT("%N:%l: svc()") ACE_TEXT(" ERROR: write returned %d!\n"), retcode));
return 1;
}
if (Config::updateRate != 0) //only for general load puropses
{
usleep(1000 * (1000 / Config::updateRate));
}
else
{
for (size_t jj = 0; jj < Config::subCount; )
{
status = message_dr->take_next_sample(receivedMessage, si);
if (status != DDS::RETCODE_NO_DATA){
jj++;
//cout<<"Received #" << jj <<endl;
}
else{
//cout<<"Not received"<<endl;
}
}
}
message.count++;
}
boost::chrono::high_resolution_clock::time_point end = boost::chrono::high_resolution_clock::now();
boost::chrono::microseconds diff = boost::chrono::duration_cast<boost::chrono::microseconds>(end - start);
cout << "One way average latency = " << diff.count() / Config::roundtripCount / 2 << " microseconds with roundtrip count of " << Config::roundtripCount << endl;
std::cout << "Shutting down..." << std::endl;
}
// Clean-up
participant->delete_contained_entities();
dpf->delete_participant(participant.in());
TheServiceParticipant->shutdown();
}
catch (const CORBA::Exception &e)
{
e._tao_print_exception("Exception caught in main():");
ACE_OS::exit(-1);
}
return 0;
}