Skip to content

Commit 56f9f5a

Browse files
Add examples for ContentFilterTopic (#2596)
* Refs #13791. Add DefaultSQLFilter example Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev> * Refs #13791. Add CustomFilter example Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev> * Refs #14220: rename example. First step to unify both examples into the same one with an user option indicating which filter to use (default SQL or custom one) Signed-off-by: JLBuenoLopez-eProsima <joseluisbueno@eprosima.com> * Refs #14220: infrastructure to run either the default or the custom filter. Default filter expression has been modified to filter different messages than the custom filter Signed-off-by: JLBuenoLopez-eProsima <joseluisbueno@eprosima.com> * Refs #14220: fix installation folder Signed-off-by: JLBuenoLopez-eProsima <joseluisbueno@eprosima.com> * Refs #14220: add example README Signed-off-by: JLBuenoLopez-eProsima <joseluisbueno@eprosima.com> * Refs #14220: apply review suggestions Signed-off-by: JLBuenoLopez-eProsima <joseluisbueno@eprosima.com> * Refs #14220: linters Signed-off-by: JLBuenoLopez-eProsima <joseluisbueno@eprosima.com> * Refs #14220: Use argument parser Signed-off-by: JLBuenoLopez-eProsima <joseluisbueno@eprosima.com> * Refs #14220: apply code review suggestions Signed-off-by: JLBuenoLopez-eProsima <joseluisbueno@eprosima.com> * Refs #14220: linters Signed-off-by: JLBuenoLopez-eProsima <joseluisbueno@eprosima.com> * Refs #14220: fix argument parser if option is unknown Signed-off-by: JLBuenoLopez-eProsima <joseluisbueno@eprosima.com> * Refs #14220: improve documentation Signed-off-by: JLBuenoLopez-eProsima <joseluisbueno@eprosima.com> * Refs #14220: apply review suggestions to README Signed-off-by: JLBuenoLopez-eProsima <joseluisbueno@eprosima.com> * Refs #14220: fix clang error Signed-off-by: JLBuenoLopez-eProsima <joseluisbueno@eprosima.com> * Refs #14220: apply new review suggestion batch Signed-off-by: JLBuenoLopez-eProsima <joseluisbueno@eprosima.com> * Refs #14220: fix building error Signed-off-by: JLBuenoLopez-eProsima <joseluisbueno@eprosima.com> Co-authored-by: JLBuenoLopez-eProsima <joseluisbueno@eprosima.com>
1 parent 4463567 commit 56f9f5a

17 files changed

Lines changed: 2114 additions & 0 deletions

examples/C++/DDS/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ add_subdirectory(BasicConfigurationExample)
1818
add_subdirectory(Benchmark)
1919
add_subdirectory(ClientServerTest)
2020
add_subdirectory(Configurability)
21+
add_subdirectory(ContentFilteredTopicExample)
2122
add_subdirectory(CustomListenerExample)
2223
add_subdirectory(DeadlineQoSExample)
2324
add_subdirectory(DisablePositiveACKs)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
cmake_minimum_required(VERSION 3.16.3)
16+
17+
project("ContentFilterTopic" VERSION 1 LANGUAGES CXX)
18+
19+
# Find requirements
20+
if(NOT fastcdr_FOUND)
21+
find_package(fastcdr REQUIRED)
22+
endif()
23+
24+
if(NOT fastrtps_FOUND)
25+
find_package(fastrtps REQUIRED)
26+
endif()
27+
28+
message(STATUS "Configuring ContentFilterTopic examples...")
29+
30+
set(CFT_COMMON_SOURCES
31+
ContentFilteredTopicExample_main.cpp
32+
ContentFilteredTopicExamplePublisher.cpp
33+
ContentFilteredTopicExampleSubscriber.cpp
34+
HelloWorld.cxx
35+
HelloWorldPubSubTypes.cxx
36+
HelloWorldTypeObject.cxx
37+
)
38+
39+
add_executable(DDSContentFilteredTopicExample ${CFT_COMMON_SOURCES})
40+
target_compile_definitions(DDSContentFilteredTopicExample PRIVATE
41+
$<$<AND:$<NOT:$<BOOL:${WIN32}>>,$<STREQUAL:"${CMAKE_BUILD_TYPE}","Debug">>:__DEBUG>
42+
$<$<BOOL:${INTERNAL_DEBUG}>:__INTERNALDEBUG> # Internal debug activated.
43+
)
44+
target_compile_features(DDSContentFilteredTopicExample PRIVATE cxx_std_11)
45+
target_link_libraries(DDSContentFilteredTopicExample fastrtps fastcdr fastdds::optionparser)
46+
install(TARGETS DDSContentFilteredTopicExample
47+
RUNTIME DESTINATION examples/C++/DDS/ContentFilteredTopicExample/${BIN_INSTALL_DIR})
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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 ContentFilteredTopicExamplePublisher.cpp
17+
*
18+
*/
19+
20+
#include "ContentFilteredTopicExamplePublisher.hpp"
21+
22+
#include <thread>
23+
24+
#include <fastdds/dds/core/status/PublicationMatchedStatus.hpp>
25+
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
26+
#include <fastdds/dds/domain/qos/DomainParticipantQos.hpp>
27+
#include <fastdds/dds/publisher/DataWriter.hpp>
28+
#include <fastdds/dds/publisher/Publisher.hpp>
29+
#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
30+
#include <fastdds/dds/publisher/qos/PublisherQos.hpp>
31+
32+
using namespace eprosima::fastdds::dds;
33+
34+
bool ContentFilteredTopicExamplePublisher::init()
35+
{
36+
// Initialize internal variables
37+
matched_ = 0;
38+
39+
// Initialize data sample
40+
hello_.index(0);
41+
hello_.message("HelloWorld");
42+
43+
// Set DomainParticipant name
44+
DomainParticipantQos pqos;
45+
pqos.name("Participant_pub");
46+
// Create DomainParticipant in domain 0
47+
participant_ = DomainParticipantFactory::get_instance()->create_participant(0, pqos);
48+
if (nullptr == participant_)
49+
{
50+
return false;
51+
}
52+
53+
// Register the type
54+
type_.register_type(participant_);
55+
56+
// Create the Publisher
57+
publisher_ = participant_->create_publisher(PUBLISHER_QOS_DEFAULT, nullptr);
58+
if (nullptr == publisher_)
59+
{
60+
return false;
61+
}
62+
63+
// Create the Topic
64+
topic_ = participant_->create_topic("HelloWorldTopic", type_->getName(), TOPIC_QOS_DEFAULT);
65+
if (nullptr == topic_)
66+
{
67+
return false;
68+
}
69+
70+
// Create the DataWriter
71+
writer_ = publisher_->create_datawriter(topic_, DATAWRITER_QOS_DEFAULT, this);
72+
if (nullptr == writer_)
73+
{
74+
return false;
75+
}
76+
return true;
77+
}
78+
79+
ContentFilteredTopicExamplePublisher::~ContentFilteredTopicExamplePublisher()
80+
{
81+
// Delete DDS entities contained within the DomainParticipant
82+
participant_->delete_contained_entities();
83+
// Delete DomainParticipant
84+
DomainParticipantFactory::get_instance()->delete_participant(participant_);
85+
}
86+
87+
void ContentFilteredTopicExamplePublisher::on_publication_matched(
88+
DataWriter*,
89+
const PublicationMatchedStatus& info)
90+
{
91+
// New remote DataReader discovered
92+
if (info.current_count_change == 1)
93+
{
94+
matched_ = info.current_count;
95+
first_connected_ = true;
96+
std::cout << "Publisher matched." << std::endl;
97+
}
98+
// New remote DataReader undiscovered
99+
else if (info.current_count_change == -1)
100+
{
101+
matched_ = info.current_count;
102+
std::cout << "Publisher unmatched." << std::endl;
103+
}
104+
// Non-valid option
105+
else
106+
{
107+
std::cout << info.current_count_change
108+
<< " is not a valid value for PublicationMatchedStatus current count change" << std::endl;
109+
}
110+
}
111+
112+
void ContentFilteredTopicExamplePublisher::runThread(
113+
uint32_t samples,
114+
uint32_t sleep)
115+
{
116+
// Publish samples continously until stopped by the user
117+
if (samples == 0)
118+
{
119+
while (!stop_)
120+
{
121+
if (publish(false))
122+
{
123+
std::cout << "Message: " << hello_.message() << " with index: " << hello_.index()
124+
<< " SENT" << std::endl;
125+
}
126+
std::this_thread::sleep_for(std::chrono::milliseconds(sleep));
127+
}
128+
}
129+
// Publish given number of samples
130+
else
131+
{
132+
for (uint32_t i = 0; i < samples; ++i)
133+
{
134+
if (!publish())
135+
{
136+
--i;
137+
}
138+
else
139+
{
140+
std::cout << "Message: " << hello_.message() << " with index: " << hello_.index()
141+
<< " SENT" << std::endl;
142+
}
143+
std::this_thread::sleep_for(std::chrono::milliseconds(sleep));
144+
}
145+
}
146+
}
147+
148+
void ContentFilteredTopicExamplePublisher::run(
149+
uint32_t samples,
150+
uint32_t sleep)
151+
{
152+
// Spawn publisher application thread
153+
stop_ = false;
154+
std::thread thread(&ContentFilteredTopicExamplePublisher::runThread, this, samples, sleep);
155+
// Thread runs indefinitely until stopped by the user
156+
if (samples == 0)
157+
{
158+
std::cout << "Publisher running. Please press enter to stop the Publisher at any time." << std::endl;
159+
std::cin.ignore();
160+
stop_ = true;
161+
}
162+
// Thread runs only for the given samples
163+
else
164+
{
165+
std::cout << "Publisher running " << samples << " samples." << std::endl;
166+
}
167+
thread.join();
168+
}
169+
170+
bool ContentFilteredTopicExamplePublisher::publish(
171+
bool wait_for_listener)
172+
{
173+
// Wait until there is a matched DataReader, unless wait_for_listener flag is set to false
174+
if (first_connected_ || !wait_for_listener || matched_ > 0)
175+
{
176+
// Update sample
177+
hello_.index(hello_.index() + 1);
178+
// Write sample
179+
writer_->write(&hello_);
180+
return true;
181+
}
182+
return false;
183+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 ContentFilteredTopicExamplePublisher.hpp
17+
*
18+
*/
19+
20+
#ifndef _CONTENTFILTEREDTOPICEXAMPLEPUBLISHER_H_
21+
#define _CONTENTFILTEREDTOPICEXAMPLEPUBLISHER_H_
22+
23+
#include <atomic>
24+
25+
#include <fastdds/dds/core/status/PublicationMatchedStatus.hpp>
26+
#include <fastdds/dds/domain/DomainParticipant.hpp>
27+
#include <fastdds/dds/publisher/DataWriterListener.hpp>
28+
#include <fastdds/dds/publisher/DataWriter.hpp>
29+
#include <fastdds/dds/publisher/Publisher.hpp>
30+
#include <fastdds/dds/topic/Topic.hpp>
31+
#include <fastdds/dds/topic/TypeSupport.hpp>
32+
33+
#include "HelloWorldPubSubTypes.h"
34+
35+
//! Publisher application class
36+
class ContentFilteredTopicExamplePublisher : public eprosima::fastdds::dds::DataWriterListener
37+
{
38+
public:
39+
40+
//! Constructor
41+
ContentFilteredTopicExamplePublisher() = default;
42+
43+
//! Destructor
44+
virtual ~ContentFilteredTopicExamplePublisher();
45+
46+
//! Initialize
47+
bool init();
48+
49+
//! Publish a sample
50+
bool publish(
51+
bool wait_for_listener = true);
52+
53+
//! Run for the given number of samples (0 => infinite samples)
54+
void run(
55+
uint32_t number,
56+
uint32_t sleep);
57+
58+
private:
59+
60+
//! Data type
61+
HelloWorld hello_;
62+
63+
//! DDS DomainParticipant pointer
64+
eprosima::fastdds::dds::DomainParticipant* participant_ = nullptr;
65+
66+
//! DDS Publisher pointer
67+
eprosima::fastdds::dds::Publisher* publisher_ = nullptr;
68+
69+
//! DDS Topic pointer
70+
eprosima::fastdds::dds::Topic* topic_ = nullptr;
71+
72+
//! DDS DataWriter pointer
73+
eprosima::fastdds::dds::DataWriter* writer_ = nullptr;
74+
75+
//! DDS TypeSupport pointer
76+
eprosima::fastdds::dds::TypeSupport type_ = eprosima::fastdds::dds::TypeSupport(new HelloWorldPubSubType());
77+
78+
//! Flag to terminate application
79+
std::atomic<bool> stop_;
80+
81+
//! Number of DataReaders matched with the publisher application
82+
std::atomic<int> matched_;
83+
84+
//! Flag set once the first subscriber is discovered and matched
85+
std::atomic<bool> first_connected_;
86+
87+
//! Discovery callback specialization when the DataWriter received discovery information from a remote DataReader
88+
void on_publication_matched(
89+
eprosima::fastdds::dds::DataWriter* writer,
90+
const eprosima::fastdds::dds::PublicationMatchedStatus& info) override;
91+
92+
//! Publisher application thread
93+
void runThread(
94+
uint32_t number,
95+
uint32_t sleep);
96+
97+
};
98+
99+
#endif // _CONTENTFILTEREDTOPICEXAMPLEPUBLISHER_H_

0 commit comments

Comments
 (0)