Skip to content

Commit d343373

Browse files
authored
Echo Participant Enhanced (#264)
* Implement Echo Participant with discovery and configuration Signed-off-by: jparisu <javierparis@eprosima.com> * apply suggestions Signed-off-by: jparisu <javierparis@eprosima.com> * uncrustify Signed-off-by: jparisu <javierparis@eprosima.com> Signed-off-by: jparisu <javierparis@eprosima.com>
1 parent 8374b3a commit d343373

15 files changed

Lines changed: 264 additions & 26 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* @file EchoParticipantConfiguration.hpp
17+
*/
18+
19+
#ifndef _DDSROUTERCORE_CONFIGURATION_PARTICIPANT_ECHOPARTICIPANTCONFIGURATION_HPP_
20+
#define _DDSROUTERCORE_CONFIGURATION_PARTICIPANT_ECHOPARTICIPANTCONFIGURATION_HPP_
21+
22+
#include <ddsrouter_core/configuration/participant/ParticipantConfiguration.hpp>
23+
24+
namespace eprosima {
25+
namespace ddsrouter {
26+
namespace core {
27+
namespace configuration {
28+
29+
struct EchoParticipantConfiguration : public ParticipantConfiguration
30+
{
31+
32+
//! Use default parent constructors
33+
using ParticipantConfiguration::ParticipantConfiguration;
34+
35+
/////////////////////////
36+
// CONSTRUCTORS
37+
/////////////////////////
38+
DDSROUTER_CORE_DllAPI EchoParticipantConfiguration() = default;
39+
40+
/////////////////////////
41+
// VARIABLES
42+
/////////////////////////
43+
44+
//! Whether this Participant should echo the data received
45+
bool echo_data = false;
46+
//! Whether this Participant should echo the discovery information
47+
bool echo_discovery = true;
48+
//! Whether this Participant should echo verbose information
49+
bool verbose = false;
50+
};
51+
52+
} /* namespace configuration */
53+
} /* namespace core */
54+
} /* namespace ddsrouter */
55+
} /* namespace eprosima */
56+
57+
#endif /* _DDSROUTERCORE_CONFIGURATION_PARTICIPANT_ECHOPARTICIPANTCONFIGURATION_HPP_ */

ddsrouter_core/include/ddsrouter_core/types/dds/Data.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <ddsrouter_core/library/library_dll.h>
2525
#include <ddsrouter_core/types/dds/Guid.hpp>
26+
#include <ddsrouter_core/types/participant/ParticipantId.hpp>
2627

2728
namespace eprosima {
2829
namespace ddsrouter {
@@ -43,6 +44,9 @@ struct DataReceived
4344

4445
//! Guid of the source entity that has transmit the data
4546
Guid source_guid;
47+
48+
//! Id of the participant from which the Reader has received the data.
49+
ParticipantId participant_receiver;
4650
};
4751

4852
//! \c octet to stream serializator

ddsrouter_core/src/cpp/communication/Track.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ void Track::transmit_() noexcept
177177

178178
// Get data received
179179
std::unique_ptr<DataReceived> data = std::make_unique<DataReceived>();
180+
data->participant_receiver = reader_participant_id_;
180181
utils::ReturnCode ret = reader_->take(data);
181182

182183
if (ret == utils::ReturnCode::RETCODE_NO_DATA)

ddsrouter_core/src/cpp/configuration/DDSRouterConfiguration.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <ddsrouter_core/configuration/DDSRouterConfiguration.hpp>
2121
#include <ddsrouter_core/configuration/participant/DiscoveryServerParticipantConfiguration.hpp>
22+
#include <ddsrouter_core/configuration/participant/EchoParticipantConfiguration.hpp>
2223
#include <ddsrouter_core/configuration/participant/InitialPeersParticipantConfiguration.hpp>
2324
#include <ddsrouter_core/configuration/participant/ParticipantConfiguration.hpp>
2425
#include <ddsrouter_core/configuration/participant/SimpleParticipantConfiguration.hpp>
@@ -133,6 +134,9 @@ bool DDSRouterConfiguration::check_correct_configuration_object_(
133134
case ParticipantKind::wan_initial_peers:
134135
return check_correct_configuration_object_by_type_<InitialPeersParticipantConfiguration>(configuration);
135136

137+
case ParticipantKind::echo:
138+
return check_correct_configuration_object_by_type_<EchoParticipantConfiguration>(configuration);
139+
136140
default:
137141
return check_correct_configuration_object_by_type_<ParticipantConfiguration>(configuration);
138142
}

ddsrouter_core/src/cpp/core/ParticipantFactory.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <ddsrouter_utils/Log.hpp>
2323

2424
#include <ddsrouter_core/configuration/participant/ParticipantConfiguration.hpp>
25+
#include <ddsrouter_core/configuration/participant/EchoParticipantConfiguration.hpp>
2526
#include <ddsrouter_utils/utils.hpp>
2627

2728
#include <core/ParticipantFactory.hpp>
@@ -51,14 +52,28 @@ std::shared_ptr<IParticipant> ParticipantFactory::create_participant(
5152
// BlankParticipant
5253
return std::make_shared<BlankParticipant>(participant_configuration->id);
5354

54-
case ParticipantKind::echo:
55-
// EchoParticipant
56-
return std::make_shared<EchoParticipant>(participant_configuration, payload_pool, discovery_database);
57-
5855
case ParticipantKind::dummy:
5956
// DummyParticipant
6057
return std::make_shared<DummyParticipant>(participant_configuration, payload_pool, discovery_database);
6158

59+
case ParticipantKind::echo:
60+
// Echo Participant
61+
{
62+
std::shared_ptr<configuration::EchoParticipantConfiguration> conf_ =
63+
std::dynamic_pointer_cast<configuration::EchoParticipantConfiguration>(
64+
participant_configuration);
65+
if (!conf_)
66+
{
67+
throw utils::ConfigurationException(
68+
utils::Formatter() << "Configuration from Participant: " << participant_configuration->id <<
69+
" is not for Participant Kind: " << participant_configuration->kind);
70+
}
71+
72+
return std::make_shared<EchoParticipant> (
73+
conf_,
74+
discovery_database);
75+
}
76+
6277
case ParticipantKind::simple_rtps:
6378
// Simple RTPS Participant
6479
{

ddsrouter_core/src/cpp/participant/implementations/auxiliar/EchoParticipant.cpp

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
* @file EchoParticipant.cpp
1717
*/
1818

19+
#include <ddsrouter_utils/Log.hpp>
20+
1921
#include <participant/implementations/auxiliar/EchoParticipant.hpp>
2022
#include <reader/implementations/auxiliar/BlankReader.hpp>
2123
#include <writer/implementations/auxiliar/EchoWriter.hpp>
24+
#include <writer/implementations/auxiliar/BlankWriter.hpp>
2225
#include <ddsrouter_core/types/participant/ParticipantKind.hpp>
2326

2427
namespace eprosima {
@@ -27,16 +30,52 @@ namespace core {
2730

2831
using namespace eprosima::ddsrouter::core::types;
2932

30-
std::shared_ptr<IWriter> EchoParticipant::create_writer_(
33+
EchoParticipant::EchoParticipant(
34+
std::shared_ptr<configuration::EchoParticipantConfiguration> participant_configuration,
35+
std::shared_ptr<DiscoveryDatabase> discovery_database)
36+
: BlankParticipant(participant_configuration->id)
37+
, configuration_(participant_configuration)
38+
{
39+
logDebug(DDSROUTER_TRACK, "Creating Echo Participant : " << configuration_->id << " .");
40+
41+
if (configuration_->echo_discovery)
42+
{
43+
// Register in Discovery DB a callback to be notified each time an endpoint is discovered
44+
discovery_database->add_endpoint_discovered_callback(
45+
[this](const Endpoint& endpoint_discovered)
46+
{
47+
this->echo_discovery(endpoint_discovered);
48+
});
49+
}
50+
}
51+
52+
void EchoParticipant::echo_discovery(
53+
Endpoint endpoint_discovered) const noexcept
54+
{
55+
// TODO write this in a way that is efficient and easy to read and allow verbose option
56+
logUser(
57+
DDSROUTER_ECHO_DISCOVERY,
58+
"New endpoint discovered: " << endpoint_discovered << ".");
59+
}
60+
61+
std::shared_ptr<IWriter> EchoParticipant::create_writer(
3162
RealTopic topic)
3263
{
33-
return std::make_shared<EchoWriter>(id(), topic, payload_pool_);
64+
if (configuration_->echo_data)
65+
{
66+
return std::make_shared<EchoWriter>(
67+
topic,
68+
configuration_->verbose);
69+
}
70+
else
71+
{
72+
return std::make_shared<BlankWriter>();
73+
}
3474
}
3575

36-
std::shared_ptr<IReader> EchoParticipant::create_reader_(
37-
RealTopic)
76+
types::ParticipantKind EchoParticipant::kind() const noexcept
3877
{
39-
return std::make_shared<BlankReader>();
78+
return ParticipantKind::echo;
4079
}
4180

4281
} /* namespace core */

ddsrouter_core/src/cpp/participant/implementations/auxiliar/EchoParticipant.hpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
#ifndef __SRC_DDSROUTERCORE_PARTICIPANT_IMPLEMENTATIONS_AUXILIAR_ECHOPARTICIPANT_HPP_
2020
#define __SRC_DDSROUTERCORE_PARTICIPANT_IMPLEMENTATIONS_AUXILIAR_ECHOPARTICIPANT_HPP_
2121

22-
#include <participant/implementations/auxiliar/BaseParticipant.hpp>
22+
#include <ddsrouter_core/configuration/participant/EchoParticipantConfiguration.hpp>
23+
24+
#include <participant/implementations/auxiliar/BlankParticipant.hpp>
2325

2426
namespace eprosima {
2527
namespace ddsrouter {
@@ -28,24 +30,35 @@ namespace core {
2830
/**
2931
* Concrete Participant that prints in stdout each message that arrives.
3032
*/
31-
class EchoParticipant : public BaseParticipant
33+
class EchoParticipant : public BlankParticipant
3234
{
3335
public:
3436

3537
//! Using parent class constructors
36-
using BaseParticipant::BaseParticipant;
38+
EchoParticipant(
39+
std::shared_ptr<configuration::EchoParticipantConfiguration> participant_configuration,
40+
std::shared_ptr<DiscoveryDatabase> discovery_database);
3741

38-
protected:
42+
//! Override kind() IParticipant method
43+
types::ParticipantKind kind() const noexcept override;
3944

40-
//! Override create_writer_() BaseParticipant method
41-
std::shared_ptr<IWriter> create_writer_(
42-
types::RealTopic topic) override;
45+
//! Print discovery information from endpoint discovered
46+
void echo_discovery(
47+
types::Endpoint endpoint_discovered) const noexcept;
4348

44-
//! Override create_reader_() BaseParticipant method
45-
std::shared_ptr<IReader> create_reader_(
49+
//! Override create_writer() IParticipant method
50+
std::shared_ptr<IWriter> create_writer(
4651
types::RealTopic topic) override;
4752

53+
protected:
54+
4855
// Deleters do not need to be implemented
56+
57+
//! Reference to alias access of this object configuration without casting every time
58+
std::shared_ptr<configuration::EchoParticipantConfiguration> configuration_;
59+
60+
//! DDS Router shared Discovery Database
61+
std::shared_ptr<DiscoveryDatabase> discovery_database_;
4962
};
5063

5164
} /* namespace core */

ddsrouter_core/src/cpp/writer/implementations/auxiliar/EchoWriter.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* @file EchoWriter.cpp
1717
*/
1818

19+
#include <ddsrouter_utils/Log.hpp>
20+
1921
#include <writer/implementations/auxiliar/EchoWriter.hpp>
2022

2123
namespace eprosima {
@@ -24,11 +26,40 @@ namespace core {
2426

2527
using namespace eprosima::ddsrouter::core::types;
2628

27-
utils::ReturnCode EchoWriter::write_(
29+
EchoWriter::EchoWriter(
30+
const types::RealTopic& topic,
31+
bool verbose)
32+
: topic_(topic)
33+
, verbose_(verbose)
34+
{
35+
logDebug(
36+
DDSROUTER_BASEWRITER,
37+
"Creating Echo Writer with verbose: " <<
38+
(verbose_ ? "active" : "inactive") << ".");
39+
}
40+
41+
utils::ReturnCode EchoWriter::write(
2842
std::unique_ptr<DataReceived>& data) noexcept
2943
{
30-
std::cout << "Echo Participant: " << participant_id_ << " has received from Endpoint: " << data->source_guid
31-
<< " in topic: " << topic_ << " the following payload: <" << data->payload << ">" << std::endl;
44+
// TODO: Add Participant receiver Id when added to DataReceived
45+
if (!verbose_)
46+
{
47+
logUser(
48+
DDSROUTER_ECHO_DATA,
49+
"Received data in Participant: " << data->participant_receiver <<
50+
" in topic: " << topic_ <<
51+
".");
52+
}
53+
else
54+
{
55+
logUser(
56+
DDSROUTER_ECHO_DATA,
57+
"In Endpoint: " << data->source_guid <<
58+
" from Participant: " << data->participant_receiver <<
59+
" in topic: " << topic_ <<
60+
" payload received: " << data->payload <<
61+
".");
62+
}
3263

3364
return utils::ReturnCode::RETCODE_OK;
3465
}

ddsrouter_core/src/cpp/writer/implementations/auxiliar/EchoWriter.hpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#include <ddsrouter_core/types/participant/ParticipantId.hpp>
2525

26-
#include <writer/implementations/auxiliar/BaseWriter.hpp>
26+
#include <writer/implementations/auxiliar/BlankWriter.hpp>
2727

2828
namespace eprosima {
2929
namespace ddsrouter {
@@ -32,12 +32,14 @@ namespace core {
3232
/**
3333
* Writer Implementation that prints in stdout every message that is required to write.
3434
*/
35-
class EchoWriter : public BaseWriter
35+
class EchoWriter : public BlankWriter
3636
{
3737
public:
3838

3939
//! Using parent class constructors
40-
using BaseWriter::BaseWriter;
40+
EchoWriter(
41+
const types::RealTopic& topic,
42+
bool verbose);
4143

4244
protected:
4345

@@ -47,10 +49,15 @@ class EchoWriter : public BaseWriter
4749
* @param data : data to print
4850
* @return RETCODE_OK always
4951
*/
50-
virtual utils::ReturnCode write_(
52+
virtual utils::ReturnCode write(
5153
std::unique_ptr<types::DataReceived>& data) noexcept override;
5254

5355
// Specific enable/disable do not need to be implemented
56+
57+
bool verbose_;
58+
59+
//! Topic that this Writer refers to
60+
types::RealTopic topic_;
5461
};
5562

5663
} /* namespace core */

ddsrouter_core/test/TestUtils/test_utils.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,14 @@ std::shared_ptr<core::configuration::ParticipantConfiguration> random_participan
257257
security::TlsConfiguration());
258258
}
259259

260+
case ParticipantKind::echo:
261+
{
262+
return std::make_shared<core::configuration::EchoParticipantConfiguration>(
263+
id,
264+
kind,
265+
false);
266+
}
267+
260268
// Add cases where Participants need specific arguments
261269
default:
262270
return std::make_shared<core::configuration::ParticipantConfiguration>(id, kind, false);

0 commit comments

Comments
 (0)