Repository navigation
Expand file tree
/
Copy pathsubscriber.cpp
More file actions
248 lines (218 loc) · 7.86 KB
/
Copy pathsubscriber.cpp
File metadata and controls
248 lines (218 loc) · 7.86 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
/* ADLINK Advanced Robotic Platfrom Group
* Author: Ewing Kang (ewing.kang@adlinktech.com)
*
* OpenSplice DDS
*
* This software and documentation are Copyright 2006 to PrismTech
* Limited, its affiliated companies and licensors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <thread>
#include <functional>
#include <chrono>
#include "gen/CoherentData_DCPS.hpp"
void topic_subA( dds::core::cond::WaitSet &,
dds::core::cond::StatusCondition &,
dds::sub::DataReader<CoherentData::Stock> &);
void topic_subB( dds::core::cond::WaitSet &,
dds::core::cond::StatusCondition &,
dds::sub::DataReader<CoherentData::Stock> &);
void usagePub();
static int clamp(int, int, int);
int main (int argc, char *argv[])
{
int result = 0;
if(argc != 2)
{
usagePub();
return -1;
}
int coherent_choice = atoi(argv[1]);
clamp(coherent_choice, 0, 3);
try
{
/** A domain participant and topic are created identically as in
the ::publisher */
dds::domain::DomainParticipant dp(org::opensplice::domain::default_id());
dds::topic::qos::TopicQos topicQos = dp.default_topic_qos()
<< dds::core::policy::Durability::Transient()
<< dds::core::policy::Reliability::Reliable();
dds::topic::Topic<CoherentData::Stock> topicA(dp, "CoherentTopicA", topicQos);
dds::topic::Topic<CoherentData::Stock> topicB(dp, "CoherentTopicB", topicQos);
/** A dds::sub::Subscriber is created on the domain participant. */
std::string name = "Coherent example";
dds::sub::qos::SubscriberQos subQos
= dp.default_subscriber_qos() << dds::core::policy::Partition(name);
std::string coherent_name;
switch (coherent_choice) {
case 1 :
subQos << dds::core::policy::Presentation::InstanceAccessScope(true, false);
coherent_name = "Instance";
break;
case 2 :
subQos << dds::core::policy::Presentation::TopicAccessScope(true, false);
coherent_name = "Topic";
break;
case 3 :
subQos << dds::core::policy::Presentation::GroupAccessScope(true, false);
coherent_name = "Group";
break;
default:
coherent_name = "No coherent";
break;
}
dds::sub::Subscriber sub(dp, subQos);
/** The dds::sub::qos::DataReaderQos are derived from the topic qos */
dds::sub::qos::DataReaderQos drqos = topicA.qos();
drqos << dds::core::policy::History::KeepAll();
/** A dds::sub::DataReader is created on the Subscriber & Topic with the DataReaderQos. */
dds::sub::DataReader<CoherentData::Stock> drA(sub, topicA, drqos);
dds::sub::DataReader<CoherentData::Stock> drB(sub, topicB, drqos);
// Create a Condition to attach to a Waitset
dds::core::cond::StatusCondition readerSCA = dds::core::cond::StatusCondition(drA);
dds::core::cond::StatusCondition readerSCB = dds::core::cond::StatusCondition(drB);
readerSCA.enabled_statuses(dds::core::status::StatusMask::data_available());
readerSCB.enabled_statuses(dds::core::status::StatusMask::data_available());
// Create WaitSet and attach Condition
dds::core::cond::WaitSet waitsetA, waitsetB;
waitsetA.attach_condition(readerSCA); // or waitset += readerSC;
waitsetB.attach_condition(readerSCB);
std::cout << "===== [Subscriber] =====" << std::endl;
std::cout << "Listening with coherent type: \"" << coherent_name << "\"" << std::endl;
std::cout << "=== subscribing ===" << std::endl;
std::cout << " Topic ID Publisher = 1" << std::endl;
// Start two thread that wait and print the data
std::thread thrd_subA(topic_subA, std::ref(waitsetA), std::ref(readerSCA), std::ref(drA) );
std::thread thrd_subB(topic_subB, std::ref(waitsetB), std::ref(readerSCB), std::ref(drB) );
thrd_subA.join();
thrd_subB.join();
std::cout << "===[Subscriber] Market Closed" << std::endl;
}
catch (const dds::core::Exception& e)
{
std::cerr << "ERROR: Exception: " << e.what() << std::endl;
result = -1;
}
return result;
}
void topic_subA( dds::core::cond::WaitSet& waitsetA,
dds::core::cond::StatusCondition& readerSCA,
dds::sub::DataReader<CoherentData::Stock>& drA)
{
bool closed = false;
int count = 0;
dds::core::cond::WaitSet::ConditionSeq conditions;
do
{
/* wait set for topic A */
conditions = waitsetA.wait();
std::cout << "== A call ==" << std::endl;
for (uint i=0; i < conditions.size(); i++)
{
if (conditions[i] != readerSCA) {
std::cerr << "Something else triggered the waitSet" << std::endl;
continue;
}
dds::sub::LoanedSamples<CoherentData::Stock> samplesA = drA.take();
if( 0 == samplesA.length() )
{
std::cout << "-- EMPTY SAMPLES A--"<< std::endl;
++count;
break;
}
for (dds::sub::LoanedSamples<CoherentData::Stock>::const_iterator sampleA = samplesA.begin();
sampleA < samplesA.end();
++sampleA)
{
if (sampleA->info().valid())
{
if(sampleA->data().price() < -0.0f)
{
closed = true;
break;
}
std::cout << std::fixed << std::setprecision(1)
<< " " << sampleA->data().ticker() << " " << sampleA->data().price()
<< " " << sampleA->data().publisher() << " " << sampleA->data().strength() << std::endl;
}else {
std::cout << "-- instance_state change A--"<< std::endl;
}
}
std::cout << " -- loop A: " << count << " --" << std::endl;
++count;
}
}
while (!closed && count < 1500);
}
void topic_subB( dds::core::cond::WaitSet& waitsetB,
dds::core::cond::StatusCondition& readerSCB,
dds::sub::DataReader<CoherentData::Stock>& drB)
{
bool closed = false;
int count = 0;
dds::core::cond::WaitSet::ConditionSeq conditions;
do {
/* wait for topic B */
conditions = waitsetB.wait();
std::cout << "== B call ==" << std::endl;
for (uint i=0; i < conditions.size(); i++)
{
if (conditions[i] != readerSCB) {
std::cerr << "Something else triggered the waitSet" << std::endl;
continue;
}
dds::sub::LoanedSamples<CoherentData::Stock> samplesB = drB.take();
if( 0 == samplesB.length())
{
std::cout << "-- EMPTY SAMPLES B --"<< std::endl;
++count;
break;
}
for (dds::sub::LoanedSamples<CoherentData::Stock>::const_iterator sampleB = samplesB.begin();
sampleB < samplesB.end();
++sampleB)
{
if (sampleB->info().valid())
{
if(sampleB->data().price() < -0.0f)
{
closed = true;
break;
}
std::cout << std::fixed << std::setprecision(1)
<< " " << sampleB->data().ticker() << " " << sampleB->data().price()
<< " " << sampleB->data().publisher() << " " << sampleB->data().strength() << std::endl;
}else {
std::cout << "-- instance_state change B--"<< std::endl;
}
}
std::cout << " -- loop B: " << count << " --" << std::endl;
++count;
}
}while (!closed && count < 1500);
}
void usagePub()
{
std::cerr << "*** ERROR" << std::endl;
std::cerr << "*** usage : subscriber <coherent_type> " << std::endl;
std::cerr << "<coherent_type>: 1 instance; 2 topic; 3 group" << std::endl;
}
static int clamp(int v, int v_min, int v_max)
{
return std::min(std::max(v_min,v),v_max);
}