Repository navigation
Expand file tree
/
Copy pathsubscriber.cpp
More file actions
92 lines (83 loc) · 3.6 KB
/
Copy pathsubscriber.cpp
File metadata and controls
92 lines (83 loc) · 3.6 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
/*
* 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 <string>
#include <thread>
#include <chrono>
#include "gen/OwnershipData_DCPS.hpp"
int main (int argc, char *argv[])
{
int result = 0;
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::core::policy::Ownership::Exclusive();
dds::topic::Topic<OwnershipData::Stock> topic(dp, "OwnershipStockTracker", topicQos);
/** A dds::sub::Subscriber is created on the domain participant. */
std::string name = "Ownership example";
dds::sub::qos::SubscriberQos subQos
= dp.default_subscriber_qos()
<< dds::core::policy::Partition(name);
dds::sub::Subscriber sub(dp, subQos);
/** The dds::sub::qos::DataReaderQos are derived from the topic qos */
dds::sub::qos::DataReaderQos drqos = topic.qos();
/** A dds::sub::DataReader is created on the Subscriber & Topic with the DataReaderQos. */
dds::sub::DataReader<OwnershipData::Stock> dr(sub, topic, drqos);
std::cout << "===[Subscriber] Ready ..." << std::endl;
std::cout << " Ticker Price Publisher ownership strength" << std::endl;
/** The subscriber will read messages sent by the publishers and display those with the
* highest ownership strength. */
bool closed = false;
int count = 0;
do
{
dds::sub::LoanedSamples<OwnershipData::Stock> samples = dr.take();
for (dds::sub::LoanedSamples<OwnershipData::Stock>::const_iterator sample = samples.begin();
sample < samples.end();
++sample)
{
if (sample->info().valid())
{
if(sample->data().price() < -0.0f)
{
closed = true;
break;
}
std::cout << " " << sample->data().ticker() << " " << sample->data().price() << " " << sample->data().publisher() << " " << sample->data().strength() << std::endl;
}
}
std::this_thread::sleep_for(std::chrono::seconds(2));
++count;
}
while (!closed && count < 1500);
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;
}