Skip to content

Commit 75f5060

Browse files
Refs #12368: Included PR #2122 more requested changes
Signed-off-by: Juan López Fernández <[email protected]>
1 parent d312629 commit 75f5060

File tree

10 files changed

+119
-21
lines changed

10 files changed

+119
-21
lines changed

examples/C++/DDS/HelloWorldExampleDiscoveryServer/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019 Proyectos y Sistemas de Mantenimiento SL (eProsima).
1+
# Copyright 2021 Proyectos y Sistemas de Mantenimiento SL (eProsima).
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

examples/C++/DDS/HelloWorldExampleDiscoveryServer/HelloWorldPublisher.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,12 @@ void HelloWorldPublisher::run(
222222
}
223223
else
224224
{
225-
std::cout << "Publisher running " << samples << " samples." << std::endl;
225+
std::cout << "Publisher running " << samples <<
226+
" samples. Please press CTRL+C to stop the Publisher at any time." << std::endl;
226227
}
227228
signal(SIGINT, [](int signum)
228229
{
230+
std::cout << "SIGINT received, stopping Publisher execution." << std::endl;
229231
static_cast<void>(signum); HelloWorldPublisher::stop();
230232
});
231233
thread.join();

examples/C++/DDS/HelloWorldExampleDiscoveryServer/HelloWorldPublisher.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030

3131
#include "HelloWorldPubSubTypes.h"
3232

33+
/**
34+
* Class used to group into a single working unit a Publisher with a DataWriter, its listener, and a TypeSupport member
35+
* corresponding to the HelloWorld datatype
36+
*/
3337
class HelloWorldPublisher
3438
{
3539
public:
@@ -38,22 +42,24 @@ class HelloWorldPublisher
3842

3943
virtual ~HelloWorldPublisher();
4044

41-
//!Initialize
45+
//! Initialize the publisher
4246
bool init(
4347
const std::string& topic_name,
4448
uint32_t num_wait_matched,
4549
eprosima::fastdds::rtps::Locator server_address);
4650

47-
//!Publish a sample
51+
//! Publish a sample
4852
void publish();
4953

50-
//!Run for number samples
54+
//! Run for number samples, publish every sleep seconds
5155
void run(
5256
uint32_t number,
5357
uint32_t sleep);
5458

59+
//! Return the current state of execution
5560
static bool is_stopped();
5661

62+
//! Trigger the end of execution
5763
static void stop();
5864

5965
private:
@@ -70,6 +76,9 @@ class HelloWorldPublisher
7076

7177
eprosima::fastdds::dds::TypeSupport type_;
7278

79+
/**
80+
* Class handling discovery events and dataflow
81+
*/
7382
class PubListener : public eprosima::fastdds::dds::DataWriterListener
7483
{
7584
public:
@@ -84,35 +93,46 @@ class HelloWorldPublisher
8493
{
8594
}
8695

96+
//! Callback executed when a DataReader is matched or unmatched
8797
void on_publication_matched(
8898
eprosima::fastdds::dds::DataWriter* writer,
8999
const eprosima::fastdds::dds::PublicationMatchedStatus& info) override;
90100

101+
//! Set the number of matched DataReaders required for publishing
91102
void set_num_wait_matched(
92103
uint32_t num_wait_matched);
93104

105+
//! Return true if there are at least num_wait_matched_ matched DataReaders
94106
bool enough_matched();
95107

108+
//! Block the thread until enough DataReaders are matched
96109
void wait();
97110

111+
//! Unblock the thread so publication of samples begins/resumes
98112
static void awake();
99113

100114
private:
101115

116+
//! Number of DataReaders matched to the associated DataWriter
102117
std::atomic<std::uint32_t> matched_;
103118

119+
//! Number of matched DataReaders required for publishing
104120
uint32_t num_wait_matched_;
105121

122+
//! Protects wait_matched condition variable
106123
static std::mutex wait_matched_cv_mtx_;
107124

125+
//! Waits until enough DataReaders are matched
108126
static std::condition_variable wait_matched_cv_;
109127
}
110128
listener_;
111129

130+
//! Run thread for number samples, publish every sleep seconds
112131
void runThread(
113132
uint32_t number,
114133
uint32_t sleep);
115134

135+
//! Member used for control flow purposes
116136
static std::atomic<bool> stop_;
117137
};
118138

examples/C++/DDS/HelloWorldExampleDiscoveryServer/HelloWorldServer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ HelloWorldServer::~HelloWorldServer()
8787
void HelloWorldServer::run()
8888
{
8989
stop_ = false;
90-
std::cout << "Server running. Please press CTRL+C to stop the Server" << std::endl;
90+
std::cout << "Server running. Please press CTRL+C to stop the Server." << std::endl;
9191
signal(SIGINT, [](int signum)
9292
{
93+
std::cout << "SIGINT received, stopping Server execution." << std::endl;
9394
static_cast<void>(signum); HelloWorldServer::stop();
9495
});
9596
std::unique_lock<std::mutex> lck(terminate_cv_mtx_);

examples/C++/DDS/HelloWorldExampleDiscoveryServer/HelloWorldServer.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626

2727
#include <fastdds/dds/domain/DomainParticipant.hpp>
2828

29+
/**
30+
* Class with a partipant configured to function as server in the Discovery Server mechanism
31+
*/
2932
class HelloWorldServer
3033
{
3134
public:
@@ -34,25 +37,30 @@ class HelloWorldServer
3437

3538
virtual ~HelloWorldServer();
3639

37-
//!Initialize
40+
//! Initialize the server
3841
bool init(
3942
eprosima::fastdds::rtps::Locator server_address);
4043

41-
//!Run
44+
//! Run
4245
void run();
4346

47+
//! Return the current state of execution
4448
static bool is_stopped();
4549

50+
//! Trigger the end of execution
4651
static void stop();
4752

4853
private:
4954

5055
eprosima::fastdds::dds::DomainParticipant* participant_;
5156

57+
//! Member used for control flow purposes
5258
static std::atomic<bool> stop_;
5359

60+
//! Protects terminate condition variable
5461
static std::mutex terminate_cv_mtx_;
5562

63+
//! Waits during execution until SIGINT or max_messages_ samples are received
5664
static std::condition_variable terminate_cv_;
5765
};
5866

examples/C++/DDS/HelloWorldExampleDiscoveryServer/HelloWorldSubscriber.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ void HelloWorldSubscriber::SubListener::on_data_available(
177177
DataReader* reader)
178178
{
179179
SampleInfo info;
180-
while (reader->take_next_sample(&hello_, &info) == ReturnCode_t::RETCODE_OK)
180+
while ((reader->take_next_sample(&hello_, &info) == ReturnCode_t::RETCODE_OK) && !is_stopped())
181181
{
182182
if (info.instance_state == ALIVE_INSTANCE_STATE)
183183
{
@@ -198,14 +198,16 @@ void HelloWorldSubscriber::run(
198198
stop_ = false;
199199
if (samples > 0)
200200
{
201-
std::cout << "Subscriber running until " << samples << " samples have been received" << std::endl;
201+
std::cout << "Subscriber running until " << samples <<
202+
" samples have been received. Please press CTRL+C to stop the Subscriber at any time." << std::endl;
202203
}
203204
else
204205
{
205-
std::cout << "Subscriber running. Please press CTRL+C to stop the Subscriber" << std::endl;
206+
std::cout << "Subscriber running. Please press CTRL+C to stop the Subscriber." << std::endl;
206207
}
207208
signal(SIGINT, [](int signum)
208209
{
210+
std::cout << "SIGINT received, stopping Subscriber execution." << std::endl;
209211
static_cast<void>(signum); HelloWorldSubscriber::stop();
210212
});
211213
std::unique_lock<std::mutex> lck(terminate_cv_mtx_);

examples/C++/DDS/HelloWorldExampleDiscoveryServer/HelloWorldSubscriber.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030

3131
#include "HelloWorldPubSubTypes.h"
3232

33+
/**
34+
* Class used to group into a single working unit a Subscriber with a DataReader, its listener, and a TypeSupport member
35+
* corresponding to the HelloWorld datatype
36+
*/
3337
class HelloWorldSubscriber
3438
{
3539
public:
@@ -38,18 +42,20 @@ class HelloWorldSubscriber
3842

3943
virtual ~HelloWorldSubscriber();
4044

41-
//!Initialize the subscriber
45+
//! Initialize the subscriber
4246
bool init(
4347
const std::string& topic_name,
4448
uint32_t max_messages,
4549
eprosima::fastdds::rtps::Locator server_address);
4650

47-
//!RUN the subscriber
51+
//! RUN the subscriber until number samples are received
4852
void run(
4953
uint32_t number);
5054

55+
//! Return the current state of execution
5156
static bool is_stopped();
5257

58+
//! Trigger the end of execution
5359
static void stop();
5460

5561
private:
@@ -64,6 +70,9 @@ class HelloWorldSubscriber
6470

6571
eprosima::fastdds::dds::TypeSupport type_;
6672

73+
/**
74+
* Class handling discovery and dataflow events
75+
*/
6776
class SubListener : public eprosima::fastdds::dds::DataReaderListener
6877
{
6978
public:
@@ -79,12 +88,15 @@ class HelloWorldSubscriber
7988
{
8089
}
8190

91+
//! Set the maximum number of messages to receive before exiting
8292
void set_max_messages(
8393
uint32_t max_messages);
8494

95+
//! Callback executed when a new sample is received
8596
void on_data_available(
8697
eprosima::fastdds::dds::DataReader* reader) override;
8798

99+
//! Callback executed when a DataWriter is matched or unmatched
88100
void on_subscription_matched(
89101
eprosima::fastdds::dds::DataReader* reader,
90102
const eprosima::fastdds::dds::SubscriptionMatchedStatus& info) override;
@@ -93,18 +105,24 @@ class HelloWorldSubscriber
93105

94106
HelloWorld hello_;
95107

108+
//! Number of DataWriters matched to the associated DataReader
96109
int matched_;
97110

111+
//! Number of samples received
98112
uint32_t samples_;
99113

114+
//! Number of messages to be received before triggering termination of execution
100115
uint32_t max_messages_;
101116
}
102117
listener_;
103118

119+
//! Member used for control flow purposes
104120
static std::atomic<bool> stop_;
105121

122+
//! Protects terminate condition variable
106123
static std::mutex terminate_cv_mtx_;
107124

125+
//! Waits during execution until SIGINT or max_messages_ samples are received
108126
static std::condition_variable terminate_cv_;
109127
};
110128

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Helloworld Example Discovery Server
2+
3+
This example demonstrates how communication between a publisher and subscriber can be established through the Discovery
4+
Server mechanism.
5+
6+
## Execution instructions
7+
8+
To launch this test open three different consoles:
9+
10+
In the first one launch: ./HelloWorldExampleDiscoveryServer publisher (or HelloWorldExampleDiscoveryServer.exe publisher on windows).
11+
In the second one: ./HelloWorldExampleDiscoveryServer subscriber (or HelloWorldExampleDiscoveryServer.exe subscriber on windows).
12+
In the third one: ./HelloWorldExampleDiscoveryServer server (or HelloWorldExampleDiscoveryServer.exe server on windows).
13+
14+
## Arguments
15+
16+
First argument is `publisher`, `subscriber` or `server` and then the rest of arguments are read unordered
17+
18+
```sh
19+
Usage: HelloWorldExampleDiscoveryServer <publisher|subscriber|server>
20+
21+
General options:
22+
-h --help
23+
Produce help message.
24+
25+
Publisher options:
26+
-t <topic_name> --topic=<topic_name>
27+
Topic name (Default: HelloWorldTopic).
28+
-w <num> --wait=<num>
29+
Number of matched subscribers required to publish (Default:
30+
0 => does not wait).
31+
-s <num> --samples=<num>
32+
Number of samples to send (Default: 0 => infinite samples).
33+
-i <num> --interval=<num>
34+
Time between samples in milliseconds (Default: 100).
35+
--ip=<IPaddress[:port number]>
36+
Server address (Default address: 127.0.0.1, default port:
37+
60006).
38+
39+
Subscriber options:
40+
-t <topic_name> --topic=<topic_name>
41+
Topic name (Default: HelloWorldTopic).
42+
-s <num> --samples=<num>
43+
Number of samples to wait for (Default: 0 => infinite
44+
samples).
45+
--ip=<IPaddress[:port number]>
46+
Server address (Default address: 127.0.0.1, default port:
47+
60006).
48+
49+
DiscoveryServer options:
50+
--ip=<IPaddress[:port number]>
51+
Server address (Default address: 127.0.0.1, default port:
52+
60006).
53+
```

examples/C++/DDS/HelloWorldExampleDiscoveryServer/README.txt

Lines changed: 0 additions & 6 deletions
This file was deleted.

examples/C++/DDS/HelloWorldExampleDiscoveryServer/arg_configuration.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,15 @@ enum optionIndex
135135

136136
const option::Descriptor usage[] = {
137137
{ UNKNOWN_OPT, 0, "", "", Arg::None,
138-
"Usage: HelloWorldExampleDS <publisher|subscriber|server>\n\nGeneral options:" },
138+
"Usage: HelloWorldExampleDiscoveryServer <publisher|subscriber|server>\n\nGeneral options:" },
139139
{ HELP, 0, "h", "help", Arg::None, " -h \t--help \tProduce help message." },
140140

141141
{ UNKNOWN_OPT, 0, "", "", Arg::None, "\nPublisher options:"},
142142
{ TOPIC, 0, "t", "topic", Arg::String,
143143
" -t <topic_name> \t--topic=<topic_name> \tTopic name (Default: HelloWorldTopic)." },
144144
{ WAIT, 0, "w", "wait", Arg::Numeric,
145145
" -w <num> \t--wait=<num> \tNumber of matched subscribers required to publish"
146-
"(Default: 0 => does not wait)." },
146+
" (Default: 0 => does not wait)." },
147147
{ SAMPLES, 0, "s", "samples", Arg::Numeric,
148148
" -s <num> \t--samples=<num> \tNumber of samples to send (Default: 0 => infinite samples)." },
149149
{ INTERVAL, 0, "i", "interval", Arg::Numeric,

0 commit comments

Comments
 (0)