File: SubscriberApp.cpp

package info (click to toggle)
fastdds 3.1.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 58,132 kB
  • sloc: cpp: 779,516; xml: 15,119; python: 4,356; sh: 190; makefile: 93; ansic: 12
file content (242 lines) | stat: -rw-r--r-- 7,450 bytes parent folder | download | duplicates (2)
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
// Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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.

/**
 * @file SubscriberApp.cpp
 *
 */

#include "SubscriberApp.hpp"

#include <condition_variable>
#include <cstdlib>
#include <iostream>
#include <mutex>
#include <stdexcept>

#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/subscriber/qos/DataReaderQos.hpp>
#include <fastdds/dds/subscriber/SampleInfo.hpp>

#include "Application.hpp"
#include "CLIParser.hpp"

using namespace eprosima::fastdds::dds;

namespace eprosima {
namespace fastdds {
namespace examples {
namespace flow_control {

SubscriberApp::SubscriberApp(
        const CLIParser::flow_control_config& config)
    : participant_(nullptr)
    , subscriber_(nullptr)
    , topic_(nullptr)
    , reader_(nullptr)
    , type_(new FlowControlPubSubType())
    , samples_(config.samples)
    , stop_(false)
{
    StatusMask status_mask = StatusMask::none();
    status_mask << StatusMask::data_available();
    status_mask << StatusMask::subscription_matched();

    // Create Participant
    participant_ = DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT, this,
                    status_mask);
    if (participant_ == nullptr)
    {
        throw std::runtime_error("Participant initialization failed");
    }

    //Register the type
    type_.register_type(participant_);

    // Create Subscriber
    SubscriberQos sub_qos = SUBSCRIBER_QOS_DEFAULT;

    // Retrieve default QoS, in case they have been previously set with an XML file
    participant_->get_default_subscriber_qos(sub_qos);
    subscriber_ = participant_->create_subscriber(sub_qos, nullptr, StatusMask::none());

    if (subscriber_ == nullptr)
    {
        throw std::runtime_error("Subscriber initialization failed");
    }

    // Create Topic
    topic_ = participant_->create_topic("flow_control_topic", type_.get_type_name(), TOPIC_QOS_DEFAULT);

    if (topic_ == nullptr)
    {
        throw std::runtime_error("Topic initialization failed");
    }

    // Create DataReader
    DataReaderQos reader_qos = DATAREADER_QOS_DEFAULT;
    subscriber_->get_default_datareader_qos(reader_qos);
    reader_ = subscriber_->create_datareader(topic_, reader_qos);

    if (reader_ == nullptr)
    {
        throw std::runtime_error("DataReader initialization failed");
    }
}

SubscriberApp::~SubscriberApp()
{
    if (nullptr != participant_)
    {
        // Delete DDS entities contained within the DomainParticipant
        participant_->delete_contained_entities();

        // Delete DomainParticipant
        DomainParticipantFactory::get_instance()->delete_participant(participant_);
    }
}

void SubscriberApp::on_subscription_matched(
        DataReader*,
        const SubscriptionMatchedStatus& info)
{
    if (info.current_count_change == 1)
    {
        std::cout << "Subscriber matched." << std::endl;
    }
    else if (info.current_count_change == -1)
    {
        std::cout << "Subscriber unmatched." << std::endl;
    }
    else
    {
        std::cout << info.current_count_change
                  << " is not a valid value for SubscriptionMatchedStatus current count change" << std::endl;
    }
}

void SubscriberApp::on_data_available(
        DataReader* reader)
{
    SampleInfo info;
    FlowControl msg;
    while ((!is_stopped()) && (RETCODE_OK == reader->take_next_sample(&msg, &info)))
    {
        if ((info.instance_state == ALIVE_INSTANCE_STATE) && info.valid_data)
        {
            static unsigned int fast_messages = 0;
            static unsigned int slow_messages = 0;

            auto it_f = std::find(fast_writer_guid.begin(), fast_writer_guid.end(), info.sample_identity.writer_guid());

            auto it_s = std::find(slow_writer_guid.begin(), slow_writer_guid.end(), info.sample_identity.writer_guid());

            if (it_f != fast_writer_guid.end())
            {
                fast_messages++;
                std::cout << "Sample RECEIVED from FAST writer with id " << *it_f << ", index=" << msg.index() <<
                    std::endl;
            }
            else if (it_s != slow_writer_guid.end())
            {
                slow_messages++;
                std::cout << "Sample RECEIVED from SLOW writer with id " << *it_s << ", index=" << msg.index() <<
                    std::endl;
            }

            if ((samples_ > 0) && (fast_messages >= samples_) && (slow_messages >= samples_))
            {
                stop();
            }
        }
    }
}

void SubscriberApp::on_data_writer_discovery(
        DomainParticipant* /*participant*/,
        eprosima::fastdds::rtps::WriterDiscoveryStatus status,
        const eprosima::fastdds::dds::PublicationBuiltinTopicData& info,
        bool& /*should_be_ignored*/)
{
    std::vector<eprosima::fastdds::rtps::octet> slow_writer_id = {0};
    std::vector<eprosima::fastdds::rtps::octet> fast_writer_id = {1};

    if (info.user_data.data_vec() == fast_writer_id)
    {
        if (status ==
                eprosima::fastdds::rtps::WriterDiscoveryStatus::DISCOVERED_WRITER)
        {
            fast_writer_guid.push_back(info.guid);

            std::cout << "Fast writer with id " << info.guid << " matched" << std::endl;
        }
        else if (status ==
                eprosima::fastdds::rtps::WriterDiscoveryStatus::REMOVED_WRITER)
        {
            auto it = std::find(fast_writer_guid.begin(), fast_writer_guid.end(), info.guid);

            if (it != fast_writer_guid.end())
            {
                fast_writer_guid.erase(it);
                std::cout << "Fast writer with id " << info.guid << " removed" << std::endl;
            }
        }
    }
    else if (info.user_data.data_vec() == slow_writer_id)
    {
        if (status ==
                eprosima::fastdds::rtps::WriterDiscoveryStatus::DISCOVERED_WRITER)
        {
            slow_writer_guid.push_back(info.guid);

            std::cout << "Slow writer with id " << info.guid << " matched" << std::endl;
        }
        else if (status ==
                eprosima::fastdds::rtps::WriterDiscoveryStatus::REMOVED_WRITER)
        {
            auto it = std::find(slow_writer_guid.begin(), slow_writer_guid.end(), info.guid);

            if (it != slow_writer_guid.end())
            {
                slow_writer_guid.erase(it);
                std::cout << "Slow writer with id " << info.guid << " removed" << std::endl;
            }
        }
    }
}

void SubscriberApp::run()
{
    std::unique_lock<std::mutex> lck(terminate_cv_mtx_);
    terminate_cv_.wait(lck, [&]
            {
                return is_stopped();
            });
}

bool SubscriberApp::is_stopped()
{
    return stop_.load();
}

void SubscriberApp::stop()
{
    stop_.store(true);
    terminate_cv_.notify_all();
}

} // namespace flow_control
} // namespace examples
} // namespace fastdds
} // namespace eprosima