This repository was archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageBoard.cpp
More file actions
404 lines (337 loc) · 14.7 KB
/
MessageBoard.cpp
File metadata and controls
404 lines (337 loc) · 14.7 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* OpenSplice DDS
*
* This software and documentation are Copyright 2006 to 2009 PrismTech
* Limited and its licensees. All rights reserved. See file:
*
* $OSPL_HOME/LICENSE
*
* for full copyright notice and license terms.
*
*/
/************************************************************************
* LOGICAL_NAME: MessageBoard.cpp
* FUNCTION: OpenSplice Tutorial example code.
* MODULE: Tutorial for the C++ programming language.
* DATE june 2007.
************************************************************************
*
* This file contains the implementation for the 'MessageBoard' executable.
*
***/
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <iomanip>
#include "ccpp_dds_dcps.h"
#include "CheckStatus.h"
#include "ccpp_Chat.h"
#include "multitopic.h"
using namespace DDS;
using namespace Chat;
#define TERMINATION_MESSAGE -1
void printTopicQos(DDS::TopicQos topicQos);
void printReaderQos(DDS::DataReaderQos readerQos);
void printWriterQos(DDS::DataWriterQos readerQos);
void printCurrentTime(DDS::DomainParticipant &participant);
int
main (
int argc,
char *argv[])
{
/* Generic DDS entities */
DomainParticipantFactory_var dpf;
DomainParticipant_ptr parentDP;
ExtDomainParticipant_var participant;
Topic_var chatMessageTopic;
Topic_var nameServiceTopic;
TopicDescription_var namedMessageTopic;
Subscriber_var chatSubscriber;
DataReader_ptr parentReader;
/* Type-specific DDS entities */
ChatMessageTypeSupport_var chatMessageTS;
NameServiceTypeSupport_var nameServiceTS;
NamedMessageTypeSupport_var namedMessageTS;
ChatMessageDataReader_var chatAdmin;
ChatMessageSeq_var msgSeq = new ChatMessageSeq();
SampleInfoSeq_var infoSeq = new SampleInfoSeq();
/* QosPolicy holders */
TopicQos reliable_topic_qos;
TopicQos setting_topic_qos;
SubscriberQos sub_qos;
DDS::StringSeq parameterList;
/* DDS Identifiers */
DomainId_t domain = NULL;
ReturnCode_t status;
/* Others */
bool terminated = FALSE;
const char * partitionName = "ChatRoom";
char * chatMessageTypeName = NULL;
char * nameServiceTypeName = NULL;
char * namedMessageTypeName = NULL;
#ifdef USE_NANOSLEEP
struct timespec sleeptime;
struct timespec remtime;
#endif
/* Options: MessageBoard [ownID] */
/* Messages having owner ownID will be ignored */
parameterList.length(1);
if (argc > 1) {
parameterList[0] = string_dup(argv[1]);
}
else
{
parameterList[0] = "0";
}
/* Create a DomainParticipantFactory and a DomainParticipant (using Default QoS settings. */
dpf = DomainParticipantFactory::get_instance();
checkHandle(dpf.in(), "DDS::DomainParticipantFactory::get_instance");
parentDP = dpf->create_participant (
domain,
PARTICIPANT_QOS_DEFAULT,
NULL,
STATUS_MASK_NONE);
checkHandle(parentDP, "DDS::DomainParticipantFactory::create_participant");
/* Narrow the normal participant to its extended representative */
participant = ExtDomainParticipantImpl::_narrow(parentDP);
checkHandle(participant.in(), "DDS::ExtDomainParticipant::_narrow");
/* Register the required datatype for ChatMessage. */
chatMessageTS = new ChatMessageTypeSupport();
checkHandle(chatMessageTS.in(), "new ChatMessageTypeSupport");
chatMessageTypeName = chatMessageTS->get_type_name();
status = chatMessageTS->register_type(
participant.in(),
chatMessageTypeName);
checkStatus(status, "Chat::ChatMessageTypeSupport::register_type");
/* Register the required datatype for NameService. */
nameServiceTS = new NameServiceTypeSupport();
checkHandle(nameServiceTS.in(), "new NameServiceTypeSupport");
nameServiceTypeName = nameServiceTS->get_type_name();
status = nameServiceTS->register_type(
participant.in(),
nameServiceTypeName);
checkStatus(status, "Chat::NameServiceTypeSupport::register_type");
/* Register the required datatype for NamedMessage. */
namedMessageTS = new NamedMessageTypeSupport();
checkHandle(namedMessageTS.in(), "new NamedMessageTypeSupport");
namedMessageTypeName = namedMessageTS->get_type_name();
status = namedMessageTS->register_type(
participant.in(),
namedMessageTypeName);
checkStatus(status, "Chat::NamedMessageTypeSupport::register_type");
/* Set the ReliabilityQosPolicy to RELIABLE. */
status = participant->get_default_topic_qos(reliable_topic_qos);
checkStatus(status, "DDS::DomainParticipant::get_default_topic_qos");
reliable_topic_qos.reliability.kind = DDS::BEST_EFFORT_RELIABILITY_QOS;
/* Make the tailored QoS the new default. */
status = participant->set_default_topic_qos(reliable_topic_qos);
checkStatus(status, "DDS::DomainParticipant::set_default_topic_qos");
cout << "Topic QOS: " << endl;
printTopicQos(reliable_topic_qos);
/* Use the changed policy when defining the ChatMessage topic */
chatMessageTopic = participant->create_topic(
"Chat_ChatMessage",
chatMessageTypeName,
reliable_topic_qos,
NULL,
STATUS_MASK_NONE);
checkHandle(chatMessageTopic.in(), "DDS::DomainParticipant::create_topic (ChatMessage)");
/* Adapt the default SubscriberQos to read from the "ChatRoom" Partition. */
status = participant->get_default_subscriber_qos (sub_qos);
checkStatus(status, "DDS::DomainParticipant::get_default_subscriber_qos");
sub_qos.partition.name.length(1);
sub_qos.partition.name[0] = partitionName;
/* Create a Subscriber for the MessageBoard application. */
chatSubscriber = participant->create_subscriber(sub_qos, NULL, STATUS_MASK_NONE);
checkHandle(chatSubscriber.in(), "DDS::DomainParticipant::create_subscriber");
/* Create a DataReader for the NamedMessage Topic (using the appropriate QoS). */
parentReader = chatSubscriber->create_datareader(
chatMessageTopic.in(),
DATAREADER_QOS_USE_TOPIC_QOS,
NULL,
STATUS_MASK_NONE);
DDS::DataReaderQos dr_qos;
status = parentReader->get_qos(dr_qos);
checkStatus(status, "DDS::DataReader::get_default_datareader_qos");
checkHandle(parentReader, "DDS::Subscriber::create_datareader");
cout << "Data Reader QOS: " << endl;
printReaderQos(dr_qos);
/* Narrow the abstract parent into its typed representative. */
chatAdmin = Chat::ChatMessageDataReader::_narrow(parentReader);
checkHandle(chatAdmin.in(), "Chat::NamedMessageDataReader::_narrow");
/* Print a message that the MessageBoard has opened. */
cout << "MessageBoard has opened: send a ChatMessage with userID = -1 to close it...." << endl << endl;
while (!terminated) {
/* Note: using read does not remove the samples from
unregistered instances from the DataReader. This means
that the DataRase would use more and more resources.
That's why we use take here instead. */
status = chatAdmin->take(
msgSeq,
infoSeq,
LENGTH_UNLIMITED,
ANY_SAMPLE_STATE,
ANY_VIEW_STATE,
ALIVE_INSTANCE_STATE );
checkStatus(status, "Chat::ChatMessageDataReader::take");
for (ULong i = 0; i < msgSeq->length(); i++) {
ChatMessage *msg = &(msgSeq[i]);
if (msg->userID == TERMINATION_MESSAGE) {
cout << "Termination message received: exiting..." << endl;
terminated = TRUE;
} else {
printCurrentTime(*participant);
cout << msg->content << endl;
}
}
status = chatAdmin->return_loan(msgSeq, infoSeq);
checkStatus(status, "Chat::ChatMessageDataReader::return_loan");
/* Sleep for some amount of time, as not to consume too much CPU cycles. */
#ifdef USE_NANOSLEEP
sleeptime.tv_sec = 0;
sleeptime.tv_nsec = 100000000;
nanosleep(&sleeptime, &remtime);
#else
usleep(100000);
#endif
}
/* Remove the DataReader */
status = chatSubscriber->delete_datareader(chatAdmin.in());
checkStatus(status, "DDS::Subscriber::delete_datareader");
/* Remove the Subscriber. */
status = participant->delete_subscriber(chatSubscriber.in());
checkStatus(status, "DDS::DomainParticipant::delete_subscriber");
/* Remove the Topics. */
status = participant->delete_simulated_multitopic(namedMessageTopic.in());
checkStatus(status, "DDS::ExtDomainParticipant::delete_simulated_multitopic");
status = participant->delete_topic(nameServiceTopic.in());
checkStatus(status, "DDS::DomainParticipant::delete_topic (nameServiceTopic)");
status = participant->delete_topic(chatMessageTopic.in());
checkStatus(status, "DDS::DomainParticipant::delete_topic (chatMessageTopic)");
/* De-allocate the type-names. */
string_free(namedMessageTypeName);
string_free(nameServiceTypeName);
string_free(chatMessageTypeName);
/* Remove the DomainParticipant. */
status = dpf->delete_participant(participant.in());
checkStatus(status, "DDS::DomainParticipantFactory::delete_participant");
exit(0);
}
void printTopicQos(DDS::TopicQos topicQos) {
cout << endl;
cout << "--Qos Policy for " << &topicQos << endl;
//reliability
cout << "Reliability: " << endl;
cout << " kind:";
if(topicQos.reliability.kind == BEST_EFFORT_RELIABILITY_QOS) {
cout << "BEST_EFFORT" << endl;
} else if(topicQos.reliability.kind == RELIABLE_RELIABILITY_QOS) {
cout << "RELIABLE" << endl;
} else {
cout << "??" << endl;
}
double seconds = topicQos.reliability.max_blocking_time.sec + topicQos.reliability.max_blocking_time.nanosec/1.0E9;
cout << " max_blocking_time (sec): " << seconds << endl;
cout << " synchronous: " << (int)topicQos.reliability.synchronous << endl;
//liveliness
cout << "Liveliness: " << endl;
cout << " kind: ";
if(topicQos.liveliness.kind == AUTOMATIC_LIVELINESS_QOS) {
cout << "AUTOMATIC" << endl;
} else if(topicQos.liveliness.kind == MANUAL_BY_PARTICIPANT_LIVELINESS_QOS) {
cout << "MANUAL_BY_PARTICIPANT" << endl;
} else if(topicQos.liveliness.kind == MANUAL_BY_TOPIC_LIVELINESS_QOS) {
cout << "MANUAL_BY_TOPIC" << endl;
} else {
cout << "??" << endl;
}
seconds = topicQos.liveliness.lease_duration.sec + topicQos.liveliness.lease_duration.nanosec/1.0E9;
cout << " lease duration: " << seconds << endl;
//latency budget
seconds = topicQos.latency_budget.duration.sec + topicQos.latency_budget.duration.nanosec/1.0E9;
cout << "Latency Budget: " << seconds << endl;
//lifespan
seconds = topicQos.lifespan.duration.sec + topicQos.lifespan.duration.nanosec/1.0E9;
cout << "Lifespan: " << seconds << endl;
cout << endl;
}
void printWriterQos(DDS::DataWriterQos writerQos) {
cout << endl;
cout << "--Qos Policy for " << &writerQos << endl;
//reliability
cout << "Reliability: " << endl;
cout << " kind:";
if(writerQos.reliability.kind == BEST_EFFORT_RELIABILITY_QOS) {
cout << "BEST_EFFORT" << endl;
} else if(writerQos.reliability.kind == RELIABLE_RELIABILITY_QOS) {
cout << "RELIABLE" << endl;
} else {
cout << "??" << endl;
}
double seconds = writerQos.reliability.max_blocking_time.sec + writerQos.reliability.max_blocking_time.nanosec/1.0E9;
cout << " max_blocking_time (sec): " << seconds << endl;
cout << " synchronous: " << (int)writerQos.reliability.synchronous << endl;
//liveliness
cout << "Liveliness: " << endl;
cout << " kind: ";
if(writerQos.liveliness.kind == AUTOMATIC_LIVELINESS_QOS) {
cout << "AUTOMATIC" << endl;
} else if(writerQos.liveliness.kind == MANUAL_BY_PARTICIPANT_LIVELINESS_QOS) {
cout << "MANUAL_BY_PARTICIPANT" << endl;
} else if(writerQos.liveliness.kind == MANUAL_BY_TOPIC_LIVELINESS_QOS) {
cout << "MANUAL_BY_TOPIC" << endl;
} else {
cout << "??" << endl;
}
seconds = writerQos.liveliness.lease_duration.sec + writerQos.liveliness.lease_duration.nanosec/1.0E9;
cout << " lease duration: " << seconds << endl;
//latency budget
seconds = writerQos.latency_budget.duration.sec + writerQos.latency_budget.duration.nanosec/1.0E9;
cout << "Latency Budget: " << seconds << endl;
//lifespan
seconds = writerQos.lifespan.duration.sec + writerQos.lifespan.duration.nanosec/1.0E9;
cout << "Lifespan: " << seconds << endl;
cout << endl;
}
void printReaderQos(DDS::DataReaderQos readerQos) {
cout << endl;
cout << "--Qos Policy for " << &readerQos << endl;
//reliability
cout << "Reliability: " << endl;
cout << " kind:";
if(readerQos.reliability.kind == BEST_EFFORT_RELIABILITY_QOS) {
cout << "BEST_EFFORT" << endl;
} else if(readerQos.reliability.kind == RELIABLE_RELIABILITY_QOS) {
cout << "RELIABLE" << endl;
} else {
cout << "??" << endl;
}
double seconds = readerQos.reliability.max_blocking_time.sec + readerQos.reliability.max_blocking_time.nanosec/1.0E9;
cout << " max_blocking_time (sec): " << seconds << endl;
cout << " synchronous: " << (int)readerQos.reliability.synchronous << endl;
//liveliness
cout << "Liveliness: " << endl;
cout << " kind: ";
if(readerQos.liveliness.kind == AUTOMATIC_LIVELINESS_QOS) {
cout << "AUTOMATIC" << endl;
} else if(readerQos.liveliness.kind == MANUAL_BY_PARTICIPANT_LIVELINESS_QOS) {
cout << "MANUAL_BY_PARTICIPANT" << endl;
} else if(readerQos.liveliness.kind == MANUAL_BY_TOPIC_LIVELINESS_QOS) {
cout << "MANUAL_BY_READER" << endl;
} else {
cout << "??" << endl;
}
seconds = readerQos.liveliness.lease_duration.sec + readerQos.liveliness.lease_duration.nanosec/1.0E9;
cout << " lease duration: " << seconds << endl;
//latency budget
seconds = readerQos.latency_budget.duration.sec + readerQos.latency_budget.duration.nanosec/1.0E9;
cout << "Latency Budget: " << seconds << endl;
cout << endl;
}
void printCurrentTime(DDS::DomainParticipant &participant) {
Time_t currentTime;
ReturnCode_t status = participant.get_current_time(currentTime);
checkStatus(status, "DDS::DomainParticipant::get_current_time");
double formattedTime = (double)currentTime.sec + (double)currentTime.nanosec * 1.0E-9;
cout << "Current time: " << fixed << setprecision(6) << formattedTime << endl;
}