Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples for ContentFilterTopic [14220] #2596

Merged
merged 17 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions examples/C++/DDS/ContentFilterTopic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ set(CFT_DSF_SOURCES
DefaultSQLFilterSubscriber.cpp
DefaultSQLFilter_main.cpp)

set(CFT_CF_SOURCES
CustomFilterPublisher.cpp
CustomFilterSubscriber.cpp
CustomFilter_main.cpp)
EduPonz marked this conversation as resolved.
Show resolved Hide resolved


add_executable(DefaultSQLFilter ${CFT_COMMON_SOURCES} ${CFT_DSF_SOURCES})
target_compile_definitions(DefaultSQLFilter PRIVATE
Expand All @@ -49,12 +54,12 @@ target_link_libraries(DefaultSQLFilter fastrtps fastcdr)
install(TARGETS DefaultSQLFilter
RUNTIME DESTINATION examples/C++/DDS/ContentFilterTopic/${BIN_INSTALL_DIR})

#add_executable(CustomFilter ${CFT_COMMON_SOURCES})
#target_compile_definitions(CustomFilter PRIVATE
# $<$<AND:$<NOT:$<BOOL:${WIN32}>>,$<STREQUAL:"${CMAKE_BUILD_TYPE}","Debug">>:__DEBUG>
# $<$<BOOL:${INTERNAL_DEBUG}>:__INTERNALDEBUG> # Internal debug activated.
#)
#target_compile_features(CustomFilter PRIVATE cxx_std_11)
#target_link_libraries(CustomFilter fastrtps fastcdr)
#install(TARGETS CustomFilter
# RUNTIME DESTINATION examples/C++/DDS/ContentFilterTopic/${BIN_INSTALL_DIR})
add_executable(CustomFilter ${CFT_COMMON_SOURCES} ${CFT_CF_SOURCES})
target_compile_definitions(CustomFilter PRIVATE
$<$<AND:$<NOT:$<BOOL:${WIN32}>>,$<STREQUAL:"${CMAKE_BUILD_TYPE}","Debug">>:__DEBUG>
$<$<BOOL:${INTERNAL_DEBUG}>:__INTERNALDEBUG> # Internal debug activated.
)
target_compile_features(CustomFilter PRIVATE cxx_std_11)
target_link_libraries(CustomFilter fastrtps fastcdr)
install(TARGETS CustomFilter
RUNTIME DESTINATION examples/C++/DDS/ContentFilterTopic/${BIN_INSTALL_DIR})
185 changes: 185 additions & 0 deletions examples/C++/DDS/ContentFilterTopic/CustomFilterPublisher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// Copyright 2022 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 CustomFilterPublisher.cpp
*
*/

#include "CustomFilterPublisher.hpp"
#include <fastrtps/attributes/ParticipantAttributes.h>
#include <fastrtps/attributes/PublisherAttributes.h>
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/publisher/Publisher.hpp>
#include <fastdds/dds/publisher/qos/PublisherQos.hpp>
#include <fastdds/dds/publisher/DataWriter.hpp>
#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>

#include <thread>

using namespace eprosima::fastdds::dds;

CustomFilterPublisher::CustomFilterPublisher()
: participant_(nullptr)
, publisher_(nullptr)
, topic_(nullptr)
, writer_(nullptr)
, type_(new HelloWorldPubSubType())
{
}

bool CustomFilterPublisher::init()
{
hello_.index(0);
hello_.message("HelloWorld");
DomainParticipantQos pqos;
pqos.name("Participant_pub");
participant_ = DomainParticipantFactory::get_instance()->create_participant(0, pqos);

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

//REGISTER THE TYPE
type_.register_type(participant_);

//CREATE THE PUBLISHER
publisher_ = participant_->create_publisher(PUBLISHER_QOS_DEFAULT, nullptr);

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

topic_ = participant_->create_topic("HelloWorldTopic", "HelloWorld", TOPIC_QOS_DEFAULT);

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

// CREATE THE WRITER
writer_ = publisher_->create_datawriter(topic_, DATAWRITER_QOS_DEFAULT, &listener_);

if (writer_ == nullptr)
{
return false;
}
return true;
}

CustomFilterPublisher::~CustomFilterPublisher()
{
if (writer_ != nullptr)
{
publisher_->delete_datawriter(writer_);
}
if (publisher_ != nullptr)
{
participant_->delete_publisher(publisher_);
}
if (topic_ != nullptr)
{
participant_->delete_topic(topic_);
}
DomainParticipantFactory::get_instance()->delete_participant(participant_);
}

void CustomFilterPublisher::PubListener::on_publication_matched(
eprosima::fastdds::dds::DataWriter*,
const eprosima::fastdds::dds::PublicationMatchedStatus& info)
{
if (info.current_count_change == 1)
{
matched_ = info.total_count;
firstConnected_ = true;
std::cout << "Publisher matched." << std::endl;
}
else if (info.current_count_change == -1)
{
matched_ = info.total_count;
std::cout << "Publisher unmatched." << std::endl;
}
else
{
std::cout << info.current_count_change
<< " is not a valid value for PublicationMatchedStatus current count change" << std::endl;
}
}

void CustomFilterPublisher::runThread(
uint32_t samples,
uint32_t sleep)
{
if (samples == 0)
{
while (!stop_)
{
if (publish(false))
{
std::cout << "Message: " << hello_.message() << " with index: " << hello_.index()
<< " SENT" << std::endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(sleep));
}
}
else
{
for (uint32_t i = 0; i < samples; ++i)
{
if (!publish())
{
--i;
}
else
{
std::cout << "Message: " << hello_.message() << " with index: " << hello_.index()
<< " SENT" << std::endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(sleep));
}
}
}

void CustomFilterPublisher::run(
uint32_t samples,
uint32_t sleep)
{
stop_ = false;
std::thread thread(&CustomFilterPublisher::runThread, this, samples, sleep);
if (samples == 0)
{
std::cout << "Publisher running. Please press enter to stop the Publisher at any time." << std::endl;
std::cin.ignore();
stop_ = true;
}
else
{
std::cout << "Publisher running " << samples << " samples." << std::endl;
}
thread.join();
}

bool CustomFilterPublisher::publish(
bool waitForListener)
{
if (listener_.firstConnected_ || !waitForListener || listener_.matched_ > 0)
{
hello_.index(hello_.index() + 1);
writer_->write(&hello_);
return true;
}
return false;
}
96 changes: 96 additions & 0 deletions examples/C++/DDS/ContentFilterTopic/CustomFilterPublisher.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2022 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 CustomFilterPublisher.hpp
*
*/

#ifndef _DEFAULTSQLFILTERPUBLISHER_H_
#define _DEFAULTSQLFILTERPUBLISHER_H_

#include "HelloWorldPubSubTypes.h"

#include <fastdds/dds/publisher/DataWriterListener.hpp>
#include <fastdds/dds/topic/TypeSupport.hpp>
#include <fastdds/dds/domain/DomainParticipant.hpp>

class CustomFilterPublisher
{
public:

CustomFilterPublisher();

virtual ~CustomFilterPublisher();

//!Initialize
bool init();

//!Publish a sample
bool publish(
bool waitForListener = true);

//!Run for number samples
void run(
uint32_t number,
uint32_t sleep);

private:

HelloWorld hello_;

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

eprosima::fastdds::dds::Publisher* publisher_;

eprosima::fastdds::dds::Topic* topic_;

eprosima::fastdds::dds::DataWriter* writer_;

bool stop_;

class PubListener : public eprosima::fastdds::dds::DataWriterListener
{
public:

PubListener()
: matched_(0)
, firstConnected_(false)
{
}

~PubListener() override
{
}

void on_publication_matched(
eprosima::fastdds::dds::DataWriter* writer,
const eprosima::fastdds::dds::PublicationMatchedStatus& info) override;

int matched_;

bool firstConnected_;
}
listener_;

void runThread(
uint32_t number,
uint32_t sleep);

eprosima::fastdds::dds::TypeSupport type_;
};



#endif // _DEFAULTSQLFILTERPUBLISHER_H_
Loading