File: MemoryTestSubscriber.cpp

package info (click to toggle)
fastdds 3.3.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 60,540 kB
  • sloc: cpp: 793,735; xml: 15,283; python: 5,902; sh: 219; makefile: 95; ansic: 12
file content (431 lines) | stat: -rw-r--r-- 12,677 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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// Copyright 2016 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 MemoryTestSubscriber.cpp
 *
 */

#include "MemoryTestSubscriber.h"

#include <thread>

#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/domain/qos/DomainParticipantQos.hpp>
#include <fastdds/dds/log/Colors.hpp>
#include <fastdds/dds/log/Log.hpp>
#include <fastdds/dds/xtypes/dynamic_types/DynamicDataFactory.hpp>
#include <fastdds/dds/xtypes/dynamic_types/DynamicTypeBuilder.hpp>
#include <fastdds/dds/xtypes/dynamic_types/DynamicTypeBuilderFactory.hpp>
#include <fastdds/dds/xtypes/dynamic_types/MemberDescriptor.hpp>
#include <fastdds/dds/xtypes/dynamic_types/TypeDescriptor.hpp>

using namespace eprosima::fastdds::dds;

MemoryTestSubscriber::MemoryTestSubscriber()
{
    m_datasublistener.up_ = this;
    m_commandpublistener.up_ = this;
    m_commandsublistener.up_ = this;
}

MemoryTestSubscriber::~MemoryTestSubscriber()
{
    participant_->delete_contained_entities();
}

bool MemoryTestSubscriber::init(
        bool echo,
        int nsam,
        bool reliable,
        uint32_t pid,
        bool hostname,
        const eprosima::fastdds::rtps::PropertyPolicy& part_property_policy,
        const eprosima::fastdds::rtps::PropertyPolicy& property_policy,
        const std::string& sXMLConfigFile,
        uint32_t data_size,
        bool dynamic_types)
{
    m_sXMLConfigFile = sXMLConfigFile;
    m_echo = echo;
    n_samples = nsam;
    m_data_size = data_size;
    dynamic_data_ = dynamic_types;

    if (dynamic_data_) // Dummy type registration
    {
        // Create basic builders
        DynamicTypeBuilderFactory::_ref_type factory {DynamicTypeBuilderFactory::get_instance()};

        // Create basic builders
        TypeDescriptor::_ref_type type_descriptor {traits<TypeDescriptor>::make_shared()};
        type_descriptor->kind(TK_STRUCTURE);
        type_descriptor->name("MemoryType");
        DynamicTypeBuilder::_ref_type struct_type_builder(factory->create_type(type_descriptor));

        // Add members to the struct.
        MemberDescriptor::_ref_type member_descriptor {traits<MemberDescriptor>::make_shared()};
        member_descriptor->type(factory->get_primitive_type(eprosima::fastdds::dds::TK_UINT32));
        member_descriptor->name("seqnum");
        struct_type_builder->add_member(member_descriptor);
        member_descriptor = traits<MemberDescriptor>::make_shared();
        member_descriptor->type(factory->create_sequence_type(factory->get_primitive_type(eprosima::fastdds::dds::
                        TK_BYTE), static_cast<uint32_t>(LENGTH_UNLIMITED))->build());
        member_descriptor->name("data");
        struct_type_builder->add_member(member_descriptor);
        m_pDynType = struct_type_builder->build();
    }

    // Create RTPSParticipant
    std::string participant_profile_name = "participant_profile";
    DomainParticipantQos participant_qos;
    participant_qos.name("MemoryTest_Participant_Subscriber");
    participant_qos.properties(part_property_policy);

    if (m_sXMLConfigFile.length() > 0)
    {
        participant_ = DomainParticipantFactory::get_instance()->create_participant_with_profile(pid % 230,
                        participant_profile_name);
    }
    else
    {
        participant_ = DomainParticipantFactory::get_instance()->create_participant(pid % 230, participant_qos);
    }

    if (nullptr == participant_)
    {
        return false;
    }

    if (dynamic_data_)
    {
        dynamic_type_support_ = new DynamicPubSubType(m_pDynType);
        data_type_ = TypeSupport{dynamic_type_support_};
    }
    else
    {
        data_type_ = TypeSupport{new MemoryDataType()};
    }
    participant_->register_type(data_type_);
    participant_->register_type(command_type_);

    publisher_ = participant_->create_publisher(PUBLISHER_QOS_DEFAULT);

    if (nullptr == publisher_)
    {
        return false;
    }

    subscriber_ = participant_->create_subscriber(SUBSCRIBER_QOS_DEFAULT);

    if (nullptr == subscriber_)
    {
        return false;
    }

    std::ostringstream st;
    st << "MemoryTest_";
    if (hostname)
    {
        st << asio::ip::host_name() << "_";
    }
    st << pid << "_PUB2SUB";
    data_topic_ = participant_->create_topic(st.str(), "MemoryType", TOPIC_QOS_DEFAULT);

    if (nullptr == data_topic_)
    {
        return false;
    }

    // Create Data Subscriber
    std::string profile_name = "subscriber_profile";
    DataReaderQos reader_qos;
    if (reliable)
    {
        reader_qos.reliability().kind = RELIABLE_RELIABILITY_QOS;
    }
    reader_qos.properties(property_policy);

    if (m_sXMLConfigFile.length() > 0)
    {
        data_reader_ = subscriber_->create_datareader_with_profile(data_topic_, profile_name, &this->m_datasublistener);
    }
    else
    {
        data_reader_ = subscriber_->create_datareader(data_topic_, reader_qos, &this->m_datasublistener);
    }

    if (nullptr == data_reader_)
    {
        return false;
    }

    //COMMAND PUBLISHER
    std::ostringstream pct;
    pct << "MemoryTest_Command_";
    if (hostname)
    {
        pct << asio::ip::host_name() << "_";
    }
    pct << pid << "_SUB2PUB";
    command_pub_topic_ = participant_->create_topic(pct.str(), "TestCommandType", TOPIC_QOS_DEFAULT);

    if (nullptr == command_pub_topic_)
    {
        return false;
    }

    DataWriterQos writer_qos;
    writer_qos.history().kind = KEEP_ALL_HISTORY_QOS;
    writer_qos.durability().kind = TRANSIENT_LOCAL_DURABILITY_QOS;
    writer_qos.reliability().kind = RELIABLE_RELIABILITY_QOS;

    command_writer_ = publisher_->create_datawriter(command_pub_topic_, writer_qos, &this->m_commandpublistener);

    if (nullptr == command_writer_)
    {
        return false;
    }

    std::ostringstream sct;
    sct << "MemoryTest_Command_";
    if (hostname)
    {
        sct << asio::ip::host_name() << "_";
    }
    sct << pid << "_PUB2SUB";
    command_sub_topic_ = participant_->create_topic(sct.str(), "TestCommandType", TOPIC_QOS_DEFAULT);

    if (nullptr == command_sub_topic_)
    {
        return false;
    }

    DataReaderQos command_reader_qos;
    command_reader_qos.history().kind = KEEP_ALL_HISTORY_QOS;
    command_reader_qos.durability().kind = TRANSIENT_LOCAL_DURABILITY_QOS;
    command_reader_qos.reliability().kind = RELIABLE_RELIABILITY_QOS;

    command_reader_ = subscriber_->create_datareader(command_sub_topic_, command_reader_qos,
                    &this->m_commandsublistener);

    if (nullptr == command_reader_)
    {
        return false;
    }

    return true;
}

void MemoryTestSubscriber::DataSubListener::on_subscription_matched(
        DataReader*,
        const SubscriptionMatchedStatus& info)
{
    std::unique_lock<std::mutex> lock(up_->mutex_);

    if (0 < info.current_count_change)
    {
        std::cout << C_MAGENTA << "Data Sub Matched " << C_DEF << std::endl;
    }
    else
    {
        std::cout << C_MAGENTA << "Data Sub unmatched " << C_DEF << std::endl;
    }
    up_->disc_count_ += info.current_count_change;

    lock.unlock();
    up_->disc_cond_.notify_one();
}

void MemoryTestSubscriber::CommandPubListener::on_publication_matched(
        DataWriter*,
        const PublicationMatchedStatus& info)
{
    std::unique_lock<std::mutex> lock(up_->mutex_);

    if (0 < info.current_count_change)
    {
        std::cout << C_MAGENTA << "Command Pub Matched " << C_DEF << std::endl;
    }
    else
    {
        std::cout << C_MAGENTA << "Command Pub unmatched " << C_DEF << std::endl;
    }
    up_->disc_count_ += info.current_count_change;

    lock.unlock();
    up_->disc_cond_.notify_one();
}

void MemoryTestSubscriber::CommandSubListener::on_subscription_matched(
        DataReader*,
        const SubscriptionMatchedStatus& info)
{
    std::unique_lock<std::mutex> lock(up_->mutex_);

    if (0 < info.current_count_change)
    {
        std::cout << C_MAGENTA << "Command Sub Matched " << C_DEF << std::endl;
    }
    else
    {
        std::cout << C_MAGENTA << "Command Sub unmatched " << C_DEF << std::endl;
    }
    up_->disc_count_ += info.current_count_change;

    lock.unlock();
    up_->disc_cond_.notify_one();
}

void MemoryTestSubscriber::CommandSubListener::on_data_available(
        DataReader* reader)
{
    TestCommandType command;
    if (RETCODE_OK == reader->take_next_sample(&command, &up_->m_sampleinfo))
    {
        std::cout << "RCOMMAND: " << command.m_command << std::endl;
        if (command.m_command == READY)
        {
            std::cout << "Publisher has new test ready..." << std::endl;
            up_->mutex_.lock();
            ++up_->comm_count_;
            up_->mutex_.unlock();
            up_->comm_cond_.notify_one();
        }
        else if (command.m_command == STOP)
        {
            std::cout << "Publisher has stopped the test" << std::endl;
            up_->mutex_.lock();
            ++up_->data_count_;
            up_->mutex_.unlock();
            up_->data_cond_.notify_one();
        }
        else if (command.m_command == STOP_ERROR)
        {
            std::cout << "Publisher has canceled the test" << std::endl;
            up_->m_status = -1;
            up_->mutex_.lock();
            ++up_->data_count_;
            up_->mutex_.unlock();
            up_->data_cond_.notify_one();
        }
        else if (command.m_command == DEFAULT)
        {
            std::cout << "Something is wrong" << std::endl;
        }
    }
}

void MemoryTestSubscriber::DataSubListener::on_data_available(
        DataReader* reader)
{
    if (up_->dynamic_data_)
    {
        reader->take_next_sample(&up_->m_DynData, &up_->m_sampleinfo);
        ++up_->n_received;
        if (up_->m_echo)
        {
            uint32_t seq_num {0};
            up_->m_DynData->get_uint32_value(seq_num, 0);
            std::cout << "Received data: " << seq_num << "(" << up_->n_received << ")" << std::endl;
        }
    }
    else
    {
        reader->take_next_sample((void*)up_->memory_, &up_->m_sampleinfo);
        ++up_->n_received;
        if (up_->m_echo)
        {
            std::cout << "Received data: " << up_->memory_->seqnum << "(" << up_->n_received << ")" << std::endl;
        }
    }
}

void MemoryTestSubscriber::run()
{
    //WAIT FOR THE DISCOVERY PROCESS FO FINISH:
    //EACH SUBSCRIBER NEEDS 3 Matchings (Comd Pub+Sub and publisher or subscriber)
    std::unique_lock<std::mutex> disc_lock(mutex_);
    disc_cond_.wait(disc_lock, [&]()
            {
                return disc_count_ >= 3;
            });
    disc_lock.unlock();

    test(m_data_size);
}

bool MemoryTestSubscriber::test(
        uint32_t datasize)
{
    std::cout << "Preparing test with data size: " << datasize + 4 << std::endl;

    if (dynamic_data_)
    {
        m_DynData = DynamicDataFactory::get_instance()->create_data(m_pDynType);
    }
    else
    {
        memory_ = new MemoryType(datasize);
    }

    // Finally data matching
    std::unique_lock<std::mutex> disc_lock(mutex_);
    disc_cond_.wait(disc_lock, [&]()
            {
                return disc_count_ >= 3;
            });
    disc_lock.unlock();

    std::cout << C_B_MAGENTA << "DISCOVERY COMPLETE " << C_DEF << std::endl;

    std::unique_lock<std::mutex> lock(mutex_);
    if (comm_count_ == 0)
    {
        comm_cond_.wait(lock);
    }
    --comm_count_;
    lock.unlock();

    m_status = 0;
    n_received = 0;
    TestCommandType command;
    command.m_command = BEGIN;
    std::cout << "Testing with data size: " << datasize + 4 << std::endl;
    command_writer_->write(&command);

    lock.lock();
    data_cond_.wait(lock, [&]()
            {
                return data_count_ > 0;
            });
    --data_count_;
    lock.unlock();

    std::cout << "TEST OF SIZE: " << datasize + 4 << " ENDS" << std::endl;
    std::this_thread::sleep_for(std::chrono::milliseconds(50));
    if (dynamic_data_)
    {
        DynamicDataFactory::get_instance()->delete_data(m_DynData);
    }
    else
    {
        delete(memory_);
    }
    if (m_status == -1)
    {
        return false;
    }
    return true;
}