diff --git a/examples/cpp/rtps/Application.cpp b/examples/cpp/rtps/Application.cpp new file mode 100644 index 00000000000..de08992042b --- /dev/null +++ b/examples/cpp/rtps/Application.cpp @@ -0,0 +1,56 @@ +// Copyright 2024 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 Application.cpp + * + */ + +#include "Application.hpp" + +#include "CLIParser.hpp" +#include "ReaderApp.hpp" +#include "WriterApp.hpp" + +namespace eprosima { +namespace fastdds { +namespace examples { +namespace rtps { + +//! Factory method to create a publisher or subscriber +std::shared_ptr Application::make_app( + const CLIParser::rtps_config& config, + const std::string& topic_name) +{ + std::shared_ptr entity; + switch (config.entity) + { + case CLIParser::EntityKind::WRITER: + entity = std::make_shared(config, topic_name); + break; + case CLIParser::EntityKind::READER: + entity = std::make_shared(config, topic_name); + break; + case CLIParser::EntityKind::UNDEFINED: + default: + throw std::runtime_error("Entity initialization failed"); + break; + } + return entity; +} + +} // namespace rtps +} // namespace examples +} // namespace fastdds +} // namespace eprosima diff --git a/examples/cpp/rtps/Application.hpp b/examples/cpp/rtps/Application.hpp new file mode 100644 index 00000000000..580f6fbbe85 --- /dev/null +++ b/examples/cpp/rtps/Application.hpp @@ -0,0 +1,54 @@ +// Copyright 2024 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 Application.hpp + * + */ + +#ifndef FASTDDS_EXAMPLES_CPP_RTPS__APPLICATION_HPP +#define FASTDDS_EXAMPLES_CPP_RTPS__APPLICATION_HPP + +#include "CLIParser.hpp" + +namespace eprosima { +namespace fastdds { +namespace examples { +namespace rtps { + +class Application +{ +public: + + //! Virtual destructor + virtual ~Application() = default; + + //! Run application + virtual void run() = 0; + + //! Trigger the end of execution + virtual void stop() = 0; + + //! Factory method to create applications based on configuration + static std::shared_ptr make_app( + const CLIParser::rtps_config& config, + const std::string& topic_name); +}; + +} // namespace rtps +} // namespace examples +} // namespace fastdds +} // namespace eprosima + +#endif // FASTDDS_EXAMPLES_CPP_RTPS__APPLICATION_HPP diff --git a/examples/cpp/rtps/AsSocket/CMakeLists.txt b/examples/cpp/rtps/AsSocket/CMakeLists.txt deleted file mode 100644 index 5fcdcb79a42..00000000000 --- a/examples/cpp/rtps/AsSocket/CMakeLists.txt +++ /dev/null @@ -1,52 +0,0 @@ -# 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. - -cmake_minimum_required(VERSION 3.20) - -project(RTPSTest_as_socket VERSION 1 LANGUAGES CXX) - -# Find requirements -if(NOT fastcdr_FOUND) - find_package(fastcdr 2 REQUIRED) -endif() - -if(NOT foonathan_memory_FOUND) - find_package(foonathan_memory REQUIRED) -endif() - -if(NOT fastdds_FOUND) - find_package(fastdds 3 REQUIRED) -endif() - -#Check C++11 -include(CheckCXXCompilerFlag) -if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") - check_cxx_compiler_flag(-std=c++11 SUPPORTS_CXX11) - if(NOT SUPPORTS_CXX11) - message(FATAL_ERROR "Compiler doesn't support C++11") - endif() -endif() - -message(STATUS "Configuring AsSocket...") -file(GLOB RTPSTESTASSOCKET_SOURCES "*.cpp") - -add_executable(AsSocket ${RTPSTESTASSOCKET_SOURCES}) -target_compile_definitions(AsSocket PRIVATE - $<$>,$>:__DEBUG> - $<$:__INTERNALDEBUG> # Internal debug activated. -) -target_link_libraries(AsSocket fastdds fastcdr foonathan_memory) -install(TARGETS AsSocket - RUNTIME DESTINATION examples/cpp/rtps/AsSocket/${BIN_INSTALL_DIR} - ) diff --git a/examples/cpp/rtps/AsSocket/README.txt b/examples/cpp/rtps/AsSocket/README.txt deleted file mode 100644 index f89de34616d..00000000000 --- a/examples/cpp/rtps/AsSocket/README.txt +++ /dev/null @@ -1,4 +0,0 @@ -To launch this test open two different consoles: - -In the first one launch: ./AsSocket reader (or AsSocket.exe reader on windows). -In the second one: ./AsSocket writer (or AsSocket.exe writer on windows). diff --git a/examples/cpp/rtps/AsSocket/TestReaderSocket.cpp b/examples/cpp/rtps/AsSocket/TestReaderSocket.cpp deleted file mode 100644 index 3cbb13f92b0..00000000000 --- a/examples/cpp/rtps/AsSocket/TestReaderSocket.cpp +++ /dev/null @@ -1,98 +0,0 @@ -// 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 TestReaderSocket.cpp - * - */ - -#include "TestReaderSocket.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace eprosima::fastdds; -using namespace eprosima::fastdds::rtps; - -TestReaderSocket::TestReaderSocket() - : mp_participant(nullptr) - , mp_reader(nullptr) - , mp_history(nullptr) -{ - - -} - -TestReaderSocket::~TestReaderSocket() -{ - RTPSDomain::removeRTPSParticipant(mp_participant); - delete(mp_history); -} - -bool TestReaderSocket::init( - std::string ip, - uint32_t port) -{ - //CREATE PARTICIPANT - RTPSParticipantAttributes PParam; - PParam.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol::NONE; - PParam.builtin.use_WriterLivelinessProtocol = false; - mp_participant = RTPSDomain::createParticipant(0, PParam); - if (mp_participant == nullptr) - { - return false; - } - //CREATE READERHISTORY - HistoryAttributes hatt; - hatt.payloadMaxSize = 255; - mp_history = new ReaderHistory(hatt); - - //CREATE READER - ReaderAttributes ratt; - ratt.endpoint.setEntityID(3); - Locator_t loc; - IPLocator::setIPv4(loc, ip); - loc.port = static_cast(port); - ratt.endpoint.multicastLocatorList.push_back(loc); - ratt.accept_messages_from_unkown_writers = true; - mp_reader = RTPSDomain::createRTPSReader(mp_participant, ratt, mp_history, &m_listener); - if (mp_reader == nullptr) - { - return false; - } - - return true; -} - -void TestReaderSocket::run() -{ - printf("Enter number to stop reader.\n"); - int aux; - std::cin >> aux; -} - -void TestReaderSocket::MyListener::on_new_cache_change_added( - RTPSReader* reader, - const CacheChange_t* const change) -{ - printf("Received: %s\n", change->serializedPayload.data); - reader->get_history()->remove_change((CacheChange_t*)change); - m_received++; -} diff --git a/examples/cpp/rtps/AsSocket/TestReaderSocket.h b/examples/cpp/rtps/AsSocket/TestReaderSocket.h deleted file mode 100644 index 665074146f1..00000000000 --- a/examples/cpp/rtps/AsSocket/TestReaderSocket.h +++ /dev/null @@ -1,78 +0,0 @@ -// 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 TestReaderSocket.h - * - */ - -#ifndef FASTDDS_EXAMPLES_CPP_RTPS_AS_SOCKET__TESTREADERSOCKET_H -#define FASTDDS_EXAMPLES_CPP_RTPS_AS_SOCKET__TESTREADERSOCKET_H - -#include - -namespace eprosima { -namespace fastdds { -namespace rtps { -class RTPSParticipant; -class ReaderHistory; -class RTPSReader; -} // namespace rtps -} // namespace fastdds -} // namespace eprosima - -class TestReaderSocket -{ -public: - - TestReaderSocket(); - - virtual ~TestReaderSocket(); - - eprosima::fastdds::rtps::RTPSParticipant* mp_participant; - - eprosima::fastdds::rtps::RTPSReader* mp_reader; - - eprosima::fastdds::rtps::ReaderHistory* mp_history; - - bool init( - std::string ip, - uint32_t port); - - void run(); - - class MyListener : public eprosima::fastdds::rtps::ReaderListener - { - public: - - MyListener() - : m_received(0) - { - } - - ~MyListener() - { - } - - void on_new_cache_change_added( - eprosima::fastdds::rtps::RTPSReader* reader, - const eprosima::fastdds::rtps::CacheChange_t* const change) override; - - uint32_t m_received; - } - m_listener; - -}; - -#endif // FASTDDS_EXAMPLES_CPP_RTPS_AS_SOCKET__TESTREADERSOCKET_H diff --git a/examples/cpp/rtps/AsSocket/TestWriterSocket.cpp b/examples/cpp/rtps/AsSocket/TestWriterSocket.cpp deleted file mode 100644 index 46216b86aaa..00000000000 --- a/examples/cpp/rtps/AsSocket/TestWriterSocket.cpp +++ /dev/null @@ -1,103 +0,0 @@ -// 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 TestWriterSocket.cpp - * - */ - -#include "TestWriterSocket.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace eprosima::fastdds; -using namespace eprosima::fastdds::rtps; - -TestWriterSocket::TestWriterSocket() - : mp_participant(nullptr) - , mp_writer(nullptr) - , mp_history(nullptr) -{ -} - -TestWriterSocket::~TestWriterSocket() -{ - RTPSDomain::removeRTPSParticipant(mp_participant); - delete(mp_history); -} - -bool TestWriterSocket::init( - std::string ip, - uint32_t port) -{ - //CREATE PARTICIPANT - RTPSParticipantAttributes PParam; - PParam.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol::NONE; - PParam.builtin.use_WriterLivelinessProtocol = false; - mp_participant = RTPSDomain::createParticipant(0, PParam); - if (mp_participant == nullptr) - { - return false; - } - - //CREATE WRITERHISTORY - HistoryAttributes hatt; - hatt.payloadMaxSize = 255; - mp_history = new WriterHistory(hatt); - - //CREATE WRITER - WriterAttributes watt; - watt.endpoint.reliabilityKind = BEST_EFFORT; - mp_writer = RTPSDomain::createRTPSWriter(mp_participant, watt, mp_history); - if (mp_writer == nullptr) - { - return false; - } - - //ADD REMOTE READER (IN THIS CASE A READER IN THE SAME MACHINE) - SubscriptionBuiltinTopicData rdata; - rdata.guid = GUID_t{c_GuidPrefix_Unknown, 0x304}; - Locator_t loc; - IPLocator::setIPv4(loc, ip); - loc.port = static_cast(port); - rdata.remote_locators.add_unicast_locator(loc); - mp_writer->matched_reader_add(rdata); - return true; -} - -void TestWriterSocket::run( - uint16_t nmsgs) -{ - for (int i = 0; i < nmsgs; ++i ) - { - CacheChange_t* ch = mp_history->create_change(255, ALIVE); -#if defined(_WIN32) - ch->serializedPayload.length = - sprintf_s((char*)ch->serializedPayload.data, 255, "My example string %d", i) + 1; -#else - ch->serializedPayload.length = - snprintf((char*)ch->serializedPayload.data, 255, "My example string %d", i) + 1; -#endif // if defined(_WIN32) - printf("Sending: %s\n", (char*)ch->serializedPayload.data); - mp_history->add_change(ch); - } -} diff --git a/examples/cpp/rtps/AsSocket/TestWriterSocket.h b/examples/cpp/rtps/AsSocket/TestWriterSocket.h deleted file mode 100644 index 59c4ea042c7..00000000000 --- a/examples/cpp/rtps/AsSocket/TestWriterSocket.h +++ /dev/null @@ -1,60 +0,0 @@ -// 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 TestWriterSocket.h - * - */ - -#ifndef FASTDDS_EXAMPLES_CPP_RTPS_AS_SOCKET__TESTWRITERSOCKET_H -#define FASTDDS_EXAMPLES_CPP_RTPS_AS_SOCKET__TESTWRITERSOCKET_H - -#include -#include -#include - -namespace eprosima { -namespace fastdds { -namespace rtps { -class RTPSParticipant; -class WriterHistory; -class RTPSWriter; -} // namespace rtps -} // namespace fastdds -} // namespace eprosima - -class TestWriterSocket -{ - -public: - - TestWriterSocket(); - - virtual ~TestWriterSocket(); - - eprosima::fastdds::rtps::RTPSParticipant* mp_participant; - - eprosima::fastdds::rtps::RTPSWriter* mp_writer; - - eprosima::fastdds::rtps::WriterHistory* mp_history; - - bool init( - std::string ip, - uint32_t port); - - void run( - uint16_t nmsgs); -}; - -#endif // FASTDDS_EXAMPLES_CPP_RTPS_AS_SOCKET__TESTWRITERSOCKET_H diff --git a/examples/cpp/rtps/AsSocket/main_RTPSTest.cpp b/examples/cpp/rtps/AsSocket/main_RTPSTest.cpp deleted file mode 100644 index 2fa6119d413..00000000000 --- a/examples/cpp/rtps/AsSocket/main_RTPSTest.cpp +++ /dev/null @@ -1,89 +0,0 @@ -// 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. - -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "TestWriterSocket.h" -#include "TestReaderSocket.h" - -using namespace eprosima; -using namespace fastdds; -using namespace rtps; -using namespace std; - -int main( - int argc, - char** argv) -{ - - std::cout << "Starting" << std::endl; - int type; - if (argc > 1) - { - if (strcmp(argv[1], "writer") == 0) - { - type = 1; - } - else if (strcmp(argv[1], "reader") == 0) - { - type = 2; - } - else - { - std::cout << "NEEDS writer OR reader as first argument" << std::endl; - return 0; - } - } - else - { - std::cout << "NEEDS writer OR reader ARGUMENT" << std::endl; - std::cout << "RTPSTest writer" << std::endl; - std::cout << "RTPSTest reader" << std::endl; - return 0; - } - switch (type) - { - case 1: - { - TestWriterSocket TW; - if (TW.init("239.255.1.4", 22222)) - { - TW.run(10); - } - break; - } - case 2: - { - TestReaderSocket TR; - if (TR.init("239.255.1.4", 22222)) - { - TR.run(); - } - break; - } - } - - RTPSDomain::stopAll(); - std::cout << "EVERYTHING STOPPED FINE" << std::endl; - - return 0; -} diff --git a/examples/cpp/rtps/CLIParser.hpp b/examples/cpp/rtps/CLIParser.hpp new file mode 100644 index 00000000000..507c13e5739 --- /dev/null +++ b/examples/cpp/rtps/CLIParser.hpp @@ -0,0 +1,220 @@ +// Copyright 2024 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. + +#include +#include +#include + +#include + +#ifndef FASTDDS_EXAMPLES_CPP_RTPS__CLIPARSER_HPP +#define FASTDDS_EXAMPLES_CPP_RTPS__CLIPARSER_HPP + +namespace eprosima { +namespace fastdds { +namespace examples { +namespace rtps { + +using dds::Log; + +class CLIParser +{ +public: + + CLIParser() = delete; + + //! Entity kind enumeration + enum class EntityKind : uint8_t + { + WRITER, + READER, + UNDEFINED + }; + + //! Configuration structure for the application + struct rtps_config + { + CLIParser::EntityKind entity = CLIParser::EntityKind::UNDEFINED; + uint16_t samples = 0; + }; + + /** + * @brief Print usage help message and exit with the given return code + * + * @param return_code return code to exit with + * + * @warning This method finishes the execution of the program with the input return code + */ + static void print_help( + uint8_t return_code) + { + std::cout << "Usage: rtps [options]" << std::endl; + std::cout << "" << std::endl; + std::cout << "Entities:" << std::endl; + std::cout << " writer Run a RTPS Writer entity" << std::endl; + std::cout << " reader Run a RTPS Reader entity" << std::endl; + std::cout << "" << std::endl; + std::cout << "Common options:" << std::endl; + std::cout << " -h, --help Print this help message" << std::endl; + std::cout << " -s , --samples Number of samples to send or receive" << std::endl; + std::cout << " [0 <= <= 65535]" << std::endl; + std::cout << " (Default: 0 [unlimited])" << std::endl; + std::exit(return_code); + } + + /** + * @brief Parse the command line options and return the rtps_config object + * + * @param argc number of arguments + * @param argv array of arguments + * @return rtps_config object with the parsed options + * + * @warning This method finishes the execution of the program if the input arguments are invalid + */ + static rtps_config parse_cli_options( + int argc, + char* argv[]) + { + rtps_config config; + + if (argc < 2) + { + EPROSIMA_LOG_ERROR(CLI_PARSER, "missing entity argument"); + print_help(EXIT_FAILURE); + } + + std::string first_argument = argv[1]; + + if (first_argument == "writer" ) + { + config.entity = CLIParser::EntityKind::WRITER; + } + else if (first_argument == "reader") + { + config.entity = CLIParser::EntityKind::READER; + } + else if (first_argument == "-h" || first_argument == "--help") + { + print_help(EXIT_SUCCESS); + } + else + { + EPROSIMA_LOG_ERROR(CLI_PARSER, "parsing entity argument " + first_argument); + print_help(EXIT_FAILURE); + } + + for (int i = 2; i < argc; ++i) + { + std::string arg = argv[i]; + if (arg == "-h" || arg == "--help") + { + print_help(EXIT_SUCCESS); + } + else if (arg == "-s" || arg == "--samples") + { + if (i + 1 < argc) + { + try + { + unsigned long input = std::stoul(argv[++i]); + if (static_cast(input) > std::numeric_limits::max()) + { + throw std::out_of_range("sample argument out of range"); + print_help(EXIT_FAILURE); + } + + config.samples = static_cast(input); + } + catch (const std::invalid_argument& e) + { + EPROSIMA_LOG_ERROR(CLI_PARSER, "invalid sample argument for " + arg + ": " + e.what()); + print_help(EXIT_FAILURE); + } + catch (const std::out_of_range& e) + { + EPROSIMA_LOG_ERROR(CLI_PARSER, "sample argument out of range for " + arg + ": " + e.what()); + print_help(EXIT_FAILURE); + } + } + else + { + EPROSIMA_LOG_ERROR(CLI_PARSER, "missing argument for " + arg); + print_help(EXIT_FAILURE); + } + } + else + { + EPROSIMA_LOG_ERROR(CLI_PARSER, "unknown option " + arg); + print_help(EXIT_FAILURE); + } + } + + return config; + } + + /** + * @brief Parse the signal number into the signal name + * + * @param signum signal number + * @return std::string signal name + */ + static std::string parse_signal( + const int& signum) + { + switch (signum) + { + case SIGINT: + return "SIGINT"; + case SIGTERM: + return "SIGTERM"; +#ifndef _WIN32 + case SIGQUIT: + return "SIGQUIT"; + case SIGHUP: + return "SIGHUP"; +#endif // _WIN32 + default: + return "UNKNOWN SIGNAL"; + } + } + + /** + * @brief Parse the entity kind into std::string + * + * @param entity entity kind + * @return std::string entity kind + */ + static std::string parse_entity_kind( + const EntityKind& entity) + { + switch (entity) + { + case EntityKind::WRITER: + return "RTPS Writer"; + case EntityKind::READER: + return "RTPS Reader"; + case EntityKind::UNDEFINED: + default: + return "Undefined entity"; + } + } + +}; + +} // namespace rtps +} // namespace examples +} // namespace fastdds +} // namespace eprosima + +#endif // FASTDDS_EXAMPLES_CPP_RTPS__CLIPARSER_HPP diff --git a/examples/cpp/rtps/CMakeLists.txt b/examples/cpp/rtps/CMakeLists.txt index 2e64f2e7285..7506022bde8 100644 --- a/examples/cpp/rtps/CMakeLists.txt +++ b/examples/cpp/rtps/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright 2019 Proyectos y Sistemas de Mantenimiento SL (eProsima). +# Copyright 2024 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. @@ -12,9 +12,56 @@ # See the License for the specific language governing permissions and # limitations under the License. -add_subdirectory(AsSocket) -add_subdirectory(Registered) +cmake_minimum_required(VERSION 3.20) -if(SQLITE3_SUPPORT) - add_subdirectory(Persistent) +project(fastdds_rtps_example VERSION 1 LANGUAGES CXX) + +# Find requirements +if(NOT fastcdr_FOUND) + find_package(fastcdr 2 REQUIRED) +endif() + +if(NOT foonathan_memory_FOUND) + find_package(foonathan_memory REQUIRED) +endif() + +if(NOT fastdds_FOUND) + find_package(fastdds 3 REQUIRED) endif() + +#Check C++11 +include(CheckCXXCompilerFlag) +if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") + check_cxx_compiler_flag(-std=c++11 SUPPORTS_CXX11) + if(NOT SUPPORTS_CXX11) + message(FATAL_ERROR "Compiler doesn't support C++11") + endif() +endif() + +message(STATUS "Configuring Registered...") +file(GLOB RTPS_SOURCES_CPP "*.cpp") + +add_executable(rtps ${RTPS_SOURCES_CPP}) +target_compile_definitions(rtps PRIVATE + $<$>,$>:__DEBUG> + $<$:__INTERNALDEBUG> # Internal debug activated. +) +target_link_libraries(rtps fastdds fastcdr foonathan_memory) +install(TARGETS rtps + RUNTIME DESTINATION {DATA_INSTALL_DIR}/fastdds/examples/cpp/rtps/${BIN_INSTALL_DIR} + ) + +# Copy the XML files over to the build directory +file(GLOB_RECURSE XML_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.xml) +# for each xml file detected +foreach(XML_FILE_COMPLETE_PATH ${XML_FILES}) + # obtain the file name + get_filename_component(XML_FILE ${XML_FILE_COMPLETE_PATH} NAME_WE) + # copy the file from src to build folders + configure_file( + ${XML_FILE_COMPLETE_PATH} # from full src path + ${CMAKE_CURRENT_BINARY_DIR}/${XML_FILE}.xml # to relative build path + COPYONLY) + install(FILES ${XML_FILE_COMPLETE_PATH} + DESTINATION ${DATA_INSTALL_DIR}/fastdds/examples/cpp/rtps/${BIN_INSTALL_DIR}) +endforeach() diff --git a/examples/cpp/rtps/HelloWorld.hpp b/examples/cpp/rtps/HelloWorld.hpp new file mode 100644 index 00000000000..46d0d7b5814 --- /dev/null +++ b/examples/cpp/rtps/HelloWorld.hpp @@ -0,0 +1,227 @@ +// 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 HelloWorld.hpp + * This header file contains the declaration of the described types in the IDL file. + * + * This file was generated by the tool fastddsgen. + */ + +#ifndef FAST_DDS_GENERATED__HELLOWORLD_HPP +#define FAST_DDS_GENERATED__HELLOWORLD_HPP + +#include +#include +#include +#include + +#if defined(_WIN32) +#if defined(EPROSIMA_USER_DLL_EXPORT) +#define eProsima_user_DllExport __declspec( dllexport ) +#else +#define eProsima_user_DllExport +#endif // EPROSIMA_USER_DLL_EXPORT +#else +#define eProsima_user_DllExport +#endif // _WIN32 + +#if defined(_WIN32) +#if defined(EPROSIMA_USER_DLL_EXPORT) +#if defined(HELLOWORLD_SOURCE) +#define HELLOWORLD_DllAPI __declspec( dllexport ) +#else +#define HELLOWORLD_DllAPI __declspec( dllimport ) +#endif // HELLOWORLD_SOURCE +#else +#define HELLOWORLD_DllAPI +#endif // EPROSIMA_USER_DLL_EXPORT +#else +#define HELLOWORLD_DllAPI +#endif // _WIN32 + +/*! + * @brief This class represents the structure HelloWorld defined by the user in the IDL file. + * @ingroup HelloWorld + */ +class HelloWorld +{ +public: + + /*! + * @brief Default constructor. + */ + eProsima_user_DllExport HelloWorld() + { + } + + /*! + * @brief Default destructor. + */ + eProsima_user_DllExport ~HelloWorld() + { + } + + /*! + * @brief Copy constructor. + * @param x Reference to the object HelloWorld that will be copied. + */ + eProsima_user_DllExport HelloWorld( + const HelloWorld& x) + { + m_index = x.m_index; + + m_message = x.m_message; + + } + + /*! + * @brief Move constructor. + * @param x Reference to the object HelloWorld that will be copied. + */ + eProsima_user_DllExport HelloWorld( + HelloWorld&& x) noexcept + { + m_index = x.m_index; + m_message = std::move(x.m_message); + } + + /*! + * @brief Copy assignment. + * @param x Reference to the object HelloWorld that will be copied. + */ + eProsima_user_DllExport HelloWorld& operator =( + const HelloWorld& x) + { + + m_index = x.m_index; + + m_message = x.m_message; + + return *this; + } + + /*! + * @brief Move assignment. + * @param x Reference to the object HelloWorld that will be copied. + */ + eProsima_user_DllExport HelloWorld& operator =( + HelloWorld&& x) noexcept + { + + m_index = x.m_index; + m_message = std::move(x.m_message); + return *this; + } + + /*! + * @brief Comparison operator. + * @param x HelloWorld object to compare. + */ + eProsima_user_DllExport bool operator ==( + const HelloWorld& x) const + { + return (m_index == x.m_index && + m_message == x.m_message); + } + + /*! + * @brief Comparison operator. + * @param x HelloWorld object to compare. + */ + eProsima_user_DllExport bool operator !=( + const HelloWorld& x) const + { + return !(*this == x); + } + + /*! + * @brief This function sets a value in member index + * @param _index New value for member index + */ + eProsima_user_DllExport void index( + uint32_t _index) + { + m_index = _index; + } + + /*! + * @brief This function returns the value of member index + * @return Value of member index + */ + eProsima_user_DllExport uint32_t index() const + { + return m_index; + } + + /*! + * @brief This function returns a reference to member index + * @return Reference to member index + */ + eProsima_user_DllExport uint32_t& index() + { + return m_index; + } + + + /*! + * @brief This function copies the value in member message + * @param _message New value to be copied in member message + */ + eProsima_user_DllExport void message( + const std::string& _message) + { + m_message = _message; + } + + /*! + * @brief This function moves the value in member message + * @param _message New value to be moved in member message + */ + eProsima_user_DllExport void message( + std::string&& _message) + { + m_message = std::move(_message); + } + + /*! + * @brief This function returns a constant reference to member message + * @return Constant reference to member message + */ + eProsima_user_DllExport const std::string& message() const + { + return m_message; + } + + /*! + * @brief This function returns a reference to member message + * @return Reference to member message + */ + eProsima_user_DllExport std::string& message() + { + return m_message; + } + + + +private: + + uint32_t m_index{0}; + std::string m_message; + +}; + +#endif // _FAST_DDS_GENERATED_HELLOWORLD_HPP_ + + diff --git a/examples/cpp/rtps/HelloWorld.idl b/examples/cpp/rtps/HelloWorld.idl new file mode 100644 index 00000000000..192f8f9d487 --- /dev/null +++ b/examples/cpp/rtps/HelloWorld.idl @@ -0,0 +1,6 @@ +@extensibility(APPENDABLE) +struct HelloWorld +{ + unsigned long index; + string message; +}; diff --git a/examples/cpp/rtps/Persistent/CMakeLists.txt b/examples/cpp/rtps/Persistent/CMakeLists.txt deleted file mode 100644 index 4766274f096..00000000000 --- a/examples/cpp/rtps/Persistent/CMakeLists.txt +++ /dev/null @@ -1,52 +0,0 @@ -# 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. - -cmake_minimum_required(VERSION 3.20) - -project(RTPSTest_persistent VERSION 1 LANGUAGES CXX) - -# Find requirements -if(NOT fastcdr_FOUND) - find_package(fastcdr 2 REQUIRED) -endif() - -if(NOT foonathan_memory_FOUND) - find_package(foonathan_memory REQUIRED) -endif() - -if(NOT fastdds_FOUND) - find_package(fastdds 3 REQUIRED) -endif() - -#Check C++11 -include(CheckCXXCompilerFlag) -if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") - check_cxx_compiler_flag(-std=c++11 SUPPORTS_CXX11) - if(NOT SUPPORTS_CXX11) - message(FATAL_ERROR "Compiler doesn't support C++11") - endif() -endif() - -message(STATUS "Configuring Persistent...") -file(GLOB RTPSTESTPERSISTENT_SOURCES "*.cpp") - -add_executable(Persistent ${RTPSTESTPERSISTENT_SOURCES}) -target_compile_definitions(Persistent PRIVATE - $<$>,$>:__DEBUG> - $<$:__INTERNALDEBUG> # Internal debug activated. - ) -target_link_libraries(Persistent fastdds fastcdr foonathan_memory) -install(TARGETS Persistent - RUNTIME DESTINATION examples/cpp/rtps/Persistent/${BIN_INSTALL_DIR} - ) diff --git a/examples/cpp/rtps/Persistent/README.txt b/examples/cpp/rtps/Persistent/README.txt deleted file mode 100644 index 8e06c3f637f..00000000000 --- a/examples/cpp/rtps/Persistent/README.txt +++ /dev/null @@ -1,4 +0,0 @@ -To launch this test open two different consoles: - -In the first one launch: ./Persistent writer (or Persistent.exe writer on windows). -In the second one: ./Persistent reader (or Persistent.exe reader on windows). diff --git a/examples/cpp/rtps/Persistent/TestReaderPersistent.cpp b/examples/cpp/rtps/Persistent/TestReaderPersistent.cpp deleted file mode 100644 index b1664f57b20..00000000000 --- a/examples/cpp/rtps/Persistent/TestReaderPersistent.cpp +++ /dev/null @@ -1,111 +0,0 @@ -// 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 TestReaderPersistent.cpp - * - */ - -#include "TestReaderPersistent.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace eprosima::fastdds; -using namespace eprosima::fastdds::rtps; - -TestReaderPersistent::TestReaderPersistent() - : mp_participant(nullptr) - , mp_reader(nullptr) - , mp_history(nullptr) -{ - - -} - -TestReaderPersistent::~TestReaderPersistent() -{ - RTPSDomain::removeRTPSParticipant(mp_participant); - delete(mp_history); -} - -bool TestReaderPersistent::init() -{ - //CREATE PARTICIPANT - RTPSParticipantAttributes PParam; - PParam.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol::SIMPLE; - PParam.builtin.use_WriterLivelinessProtocol = true; - mp_participant = RTPSDomain::createParticipant(0, PParam); - if (mp_participant == nullptr) - { - return false; - } - //CREATE READERHISTORY - HistoryAttributes hatt; - hatt.payloadMaxSize = 255; - mp_history = new ReaderHistory(hatt); - - PropertyPolicy property_policy; - property_policy.properties().emplace_back("dds.persistence.plugin", "builtin.SQLITE3"); - property_policy.properties().emplace_back("dds.persistence.sqlite3.filename", "test.db"); - - //CREATE READER - ReaderAttributes ratt; - Locator_t loc(22222); - ratt.endpoint.unicastLocatorList.push_back(loc); - ratt.endpoint.durabilityKind = TRANSIENT; - ratt.endpoint.persistence_guid.guidPrefix.value[11] = 2; - ratt.endpoint.persistence_guid.entityId.value[3] = 1; - ratt.endpoint.properties = property_policy; - mp_reader = RTPSDomain::createRTPSReader(mp_participant, ratt, mp_history, &m_listener); - if (mp_reader == nullptr) - { - return false; - } - - return true; -} - -bool TestReaderPersistent::reg() -{ - std::cout << "Registering Reader" << std::endl; - TopicAttributes Tatt; - Tatt.topicKind = NO_KEY; - Tatt.topicDataType = "string"; - Tatt.topicName = "exampleTopic"; - eprosima::fastdds::dds::ReaderQos Rqos; - return mp_participant->registerReader(mp_reader, Tatt, Rqos); -} - -void TestReaderPersistent::run() -{ - printf("Press Enter to stop the Reader.\n"); - std::cin.ignore(); -} - -void TestReaderPersistent::MyListener::on_new_cache_change_added( - RTPSReader* reader, - const CacheChange_t* const change) -{ - printf("Received: %s\n", change->serializedPayload.data); - reader->get_history()->remove_change((CacheChange_t*)change); - n_received++; -} diff --git a/examples/cpp/rtps/Persistent/TestReaderPersistent.h b/examples/cpp/rtps/Persistent/TestReaderPersistent.h deleted file mode 100644 index 3a7b05047d5..00000000000 --- a/examples/cpp/rtps/Persistent/TestReaderPersistent.h +++ /dev/null @@ -1,81 +0,0 @@ -// 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 TestReaderPersistent.h - * - */ - -#ifndef FASTDDS_EXAMPLES_CPP_RTPS_PERSISTENT__TESTREADERPERSISTENT_H -#define FASTDDS_EXAMPLES_CPP_RTPS_PERSISTENT__TESTREADERPERSISTENT_H - -#include - -namespace eprosima { -namespace fastdds { -namespace rtps { -class RTPSParticipant; -class ReaderHistory; -class RTPSReader; -} // namespace rtps -} // namespace fastdds -} // namespace eprosima - -class TestReaderPersistent -{ -public: - - TestReaderPersistent(); - virtual ~TestReaderPersistent(); - eprosima::fastdds::rtps::RTPSParticipant* mp_participant; - eprosima::fastdds::rtps::RTPSReader* mp_reader; - eprosima::fastdds::rtps::ReaderHistory* mp_history; - bool init(); //Initialization - bool reg(); //Register - void run(); //Run - class MyListener : public eprosima::fastdds::rtps::ReaderListener - { - public: - - MyListener() - : n_received(0) - , n_matched(0) - { - } - - ~MyListener() - { - } - - void on_new_cache_change_added( - eprosima::fastdds::rtps::RTPSReader* reader, - const eprosima::fastdds::rtps::CacheChange_t* const change) override; - void on_reader_matched( - eprosima::fastdds::rtps::RTPSReader*, - const eprosima::fastdds::rtps::MatchingInfo& info) override - - { - if (info.status == eprosima::fastdds::rtps::MATCHED_MATCHING) - { - n_matched++; - } - } - - uint32_t n_received; - uint32_t n_matched; - } - m_listener; -}; - -#endif // FASTDDS_EXAMPLES_CPP_RTPS_PERSISTENT__TESTREADERPERSISTENT_H diff --git a/examples/cpp/rtps/Persistent/TestWriterPersistent.cpp b/examples/cpp/rtps/Persistent/TestWriterPersistent.cpp deleted file mode 100644 index 3701b5770f2..00000000000 --- a/examples/cpp/rtps/Persistent/TestWriterPersistent.cpp +++ /dev/null @@ -1,133 +0,0 @@ -// 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 TestWriterPersistent.cpp - * - */ - -#include "TestWriterPersistent.h" - -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace eprosima::fastdds; -using namespace eprosima::fastdds::rtps; -using namespace std; - -TestWriterPersistent::TestWriterPersistent() - : mp_participant(nullptr) - , mp_writer(nullptr) - , mp_history(nullptr) -{ - - -} - -TestWriterPersistent::~TestWriterPersistent() -{ - RTPSDomain::removeRTPSParticipant(mp_participant); - delete(mp_history); -} - -bool TestWriterPersistent::init() -{ - //CREATE PARTICIPANT - RTPSParticipantAttributes PParam; - PParam.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol::SIMPLE; - PParam.builtin.use_WriterLivelinessProtocol = true; - mp_participant = RTPSDomain::createParticipant(0, PParam); - if (mp_participant == nullptr) - { - return false; - } - - //CREATE WRITERHISTORY - HistoryAttributes hatt; - hatt.payloadMaxSize = 255; - hatt.maximumReservedCaches = 50; - mp_history = new WriterHistory(hatt); - - PropertyPolicy property_policy; - property_policy.properties().emplace_back("dds.persistence.plugin", "builtin.SQLITE3"); - property_policy.properties().emplace_back("dds.persistence.sqlite3.filename", "test.db"); - - //CREATE WRITER - WriterAttributes watt; - watt.endpoint.reliabilityKind = BEST_EFFORT; - watt.endpoint.durabilityKind = TRANSIENT; - watt.endpoint.persistence_guid.guidPrefix.value[11] = 1; - watt.endpoint.persistence_guid.entityId.value[3] = 1; - watt.endpoint.properties = property_policy; - std::cout << "PID: " << watt.endpoint.persistence_guid << std::endl; - mp_writer = RTPSDomain::createRTPSWriter(mp_participant, watt, mp_history, &m_listener); - if (mp_writer == nullptr) - { - return false; - } - - return true; -} - -bool TestWriterPersistent::reg() -{ - cout << "Registering Writer" << endl; - TopicAttributes Tatt; - Tatt.topicKind = NO_KEY; - Tatt.topicDataType = "string"; - Tatt.topicName = "exampleTopic"; - eprosima::fastdds::dds::WriterQos Wqos; - return mp_participant->registerWriter(mp_writer, Tatt, Wqos); -} - -void TestWriterPersistent::run( - uint16_t samples) -{ - cout << "Waiting for matched Readers" << endl; - while (m_listener.n_matched == 0) - { - std::this_thread::sleep_for(std::chrono::milliseconds(250)); - } - - for (int i = 0; i < samples; ++i ) - { - CacheChange_t* ch = mp_history->create_change(255, ALIVE); - if (!ch) // In the case history is full, remove some old changes - { - std::cout << "cleaning history..."; - mp_history->remove_min_change(); - ch = mp_history->create_change(255, ALIVE); - } - -#if defined(_WIN32) - ch->serializedPayload.length = - sprintf_s((char*)ch->serializedPayload.data, 255, "My example string %d", i) + 1; -#else - ch->serializedPayload.length = - snprintf((char*)ch->serializedPayload.data, 255, "My example string %d", i) + 1; -#endif // if defined(_WIN32) - printf("Sending: %s\n", (char*)ch->serializedPayload.data); - mp_history->add_change(ch); - } -} diff --git a/examples/cpp/rtps/Persistent/TestWriterPersistent.h b/examples/cpp/rtps/Persistent/TestWriterPersistent.h deleted file mode 100644 index 5d1e97a2f6d..00000000000 --- a/examples/cpp/rtps/Persistent/TestWriterPersistent.h +++ /dev/null @@ -1,77 +0,0 @@ -// 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 TestWriterPersistent.h - * - */ - -#ifndef FASTDDS_EXAMPLES_CPP_RTPS_PERSISTENT__TESTWRITERPERSISTENT_H -#define FASTDDS_EXAMPLES_CPP_RTPS_PERSISTENT__TESTWRITERPERSISTENT_H - -#include - -namespace eprosima { -namespace fastdds { -namespace rtps { -class RTPSParticipant; -class WriterHistory; -class RTPSWriter; -} // namespace rtps -} // namespace fastdds -} // namespace eprosima - -class TestWriterPersistent -{ -public: - - TestWriterPersistent(); - virtual ~TestWriterPersistent(); - eprosima::fastdds::rtps::RTPSParticipant* mp_participant; - eprosima::fastdds::rtps::RTPSWriter* mp_writer; - eprosima::fastdds::rtps::WriterHistory* mp_history; - bool init(); //Initialize writer - bool reg(); //Register the Writer - void run( - uint16_t samples); //Run the Writer - class MyListener : public eprosima::fastdds::rtps::WriterListener - { - public: - - MyListener() - : n_matched(0) - { - } - - ~MyListener() - { - } - - void on_writer_matched( - eprosima::fastdds::rtps::RTPSWriter*, - const eprosima::fastdds::rtps::MatchingInfo& info) override - { - if (info.status == eprosima::fastdds::rtps::MATCHED_MATCHING) - { - ++n_matched; - } - } - - int n_matched; - - } - m_listener; -}; - -#endif // FASTDDS_EXAMPLES_CPP_RTPS_PERSISTENT__TESTWRITERPERSISTENT_H diff --git a/examples/cpp/rtps/Persistent/main_RTPSTest.cpp b/examples/cpp/rtps/Persistent/main_RTPSTest.cpp deleted file mode 100644 index 93522612bdc..00000000000 --- a/examples/cpp/rtps/Persistent/main_RTPSTest.cpp +++ /dev/null @@ -1,88 +0,0 @@ -// 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. - -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "TestReaderPersistent.h" -#include "TestWriterPersistent.h" - -using namespace eprosima; -using namespace fastdds; -using namespace rtps; -using namespace std; - -int main( - int argc, - char** argv) -{ - cout << "Starting RTPS example" << endl; - int type; - if (argc > 1) - { - if (strcmp(argv[1], "writer") == 0) - { - type = 1; - } - else if (strcmp(argv[1], "reader") == 0) - { - type = 2; - } - else - { - cout << "NEEDS writer OR reader as first argument" << endl; - return 0; - } - } - else - { - cout << "NEEDS writer OR reader ARGUMENT" << endl; - cout << "RTPSTest writer" << endl; - cout << "RTPSTest reader" << endl; - return 0; - } - switch (type) - { - case 1: - { - TestWriterPersistent TW; - if (TW.init() && TW.reg()) - { - TW.run(10); - } - break; - } - case 2: - { - TestReaderPersistent TR; - if (TR.init() && TR.reg()) - { - TR.run(); - } - break; - } - } - - RTPSDomain::stopAll(); - cout << "EVERYTHING STOPPED FINE" << endl; - - return 0; -} diff --git a/examples/cpp/rtps/README.md b/examples/cpp/rtps/README.md new file mode 100644 index 00000000000..8584f010e47 --- /dev/null +++ b/examples/cpp/rtps/README.md @@ -0,0 +1,98 @@ +# RTPS example + +The *eProsima Fast DDS RTPS* example is a simple application intended to demonstrate a basic RTPS deployment. + +This example is part of the suite of examples designed by eProsima that aims to illustrate the features and possible configurations of DDS and RTPS deployments through *eProsima Fast DDS*. + +In this case, the *RTPS* example describes the simplest deployment of a Fast DDS reader and writer. + +* [Description of the example](#description-of-the-example) +* [Run the example](#run-the-example) + +## Description of the example + +Each example application creates a different entity in the RTPS (Real-Time Publish-Subscribe) layer: writer and reader; +The lower-level RTPS layer in eProsima Fast DDS implements the RTPS protocol as defined in the standard. +This layer offers more granular control over the communication protocol's internals compared to the higher-level DDS (Data Distribution Service) Layer, giving advanced users greater command over the library's features. + +The serialization of the payload is done on the writer side and is designed to convert a `HelloWorld` object into a serialized data format. +This is useful for transmitting the object over a network or saving it in a compact form for later use. +The deserialization of the payload on the reader side consists of converting a serialized data payload into a usable `HelloWorld` object. +This is typically used in applications where data is transmitted in a compact format and needs to be deserialized back into its original form for further processing. + + +## Run the example + +To launch this example, two different terminals are required. +One of them will run the reader example application, and the other will run the writer application. + +### RTPS writer + +* Ubuntu ( / MacOS ) + + ```shell + user@machine:example_path$ ./rtps writer + RTPS Writer running. Please press Ctrl+C to stop the RTPS Writer at any time. + ``` + +* Windows + + ```powershell + example_path> rtps.exe writer + RTPS Writer running. Please press Ctrl+C to stop the RTPS Writer at any time. + ``` + +### RTPS reader + +* Ubuntu ( / MacOS ) + + ```shell + user@machine:example_path$ ./rtps reader + Registering RTPS Reader + RTPS Reader running. Please press Ctrl+C to stop the RTPS Reader at any time. + ``` + +* Windows + + ```powershell + example_path> rtps.exe reader + Registering RTPS Reader + RTPS Reader running. Please press Ctrl+C to stop the RTPS Reader at any time. + ``` + +All the example available flags can be queried running the executable with the ``-h`` or ``--help`` flag. + +### Expected output + +Regardless of which application is run first, since the publisher will not start sending data until a subscriber is discovered, the expected output both for publishers and subscribers is a first displayed message acknowledging the match, followed by the amount of samples sent or received until Ctrl+C is pressed. + +### RTPS writer + +```shell +Remote endpoint with GUID 01.0f.da.70.2b.e0.c8.b2.00.00.00.00|0.0.1.4 matched. +Message Hello World with index 1 SENT +Message Hello World with index 2 SENT +Message Hello World with index 3 SENT +... +``` + +### RTPS reader + +```shell +Remote endpoint with GUID 01.0f.da.70.f5.ea.d7.9b.00.00.00.00|0.0.1.3 matched. +Message: Hello World with index 1 RECEIVED +Message: Hello World with index 2 RECEIVED +Message: Hello World with index 3 RECEIVED +... +``` + +When Ctrl+C is pressed to stop one of the applications, the other one will show the unmatched status, displaying an informative message, and it will stop sending / receiving messages. +The following is a possible output of the writer application when stopping the reader app. + +```shell +... +Message Hello World with index 8 SENT +Message Hello World with index 9 SENT +Message Hello World with index 10 SENT +Remote endpoint with GUID 01.0f.da.70.2b.e0.c8.b2.00.00.00.00|0.0.1.4 unmatched. +``` diff --git a/examples/cpp/rtps/ReaderApp.cpp b/examples/cpp/rtps/ReaderApp.cpp new file mode 100644 index 00000000000..1c150835a6d --- /dev/null +++ b/examples/cpp/rtps/ReaderApp.cpp @@ -0,0 +1,236 @@ +// Copyright 2024 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 ReaderApp.cpp + * + */ + +#include "ReaderApp.hpp" + +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "HelloWorld.hpp" + +template<> +void eprosima::fastcdr::deserialize( + eprosima::fastcdr::Cdr& cdr, + HelloWorld& data) +{ + cdr.deserialize_type(eprosima::fastcdr::CdrVersion::XCDRv2 == cdr.get_cdr_version() ? + eprosima::fastcdr::EncodingAlgorithmFlag::DELIMIT_CDR2 : + eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR, + [&data](eprosima::fastcdr::Cdr& dcdr, const eprosima::fastcdr::MemberId& mid) -> bool + { + bool ret_value = true; + switch (mid.id) + { + case 0: + dcdr >> data.index(); + break; + + case 1: + dcdr >> data.message(); + break; + + default: + ret_value = false; + break; + } + return ret_value; + }); +} + +using namespace eprosima::fastdds; +using namespace eprosima::fastdds::rtps; + +namespace eprosima { +namespace fastdds { +namespace examples { +namespace rtps { + +ReaderApp::ReaderApp( + const CLIParser::rtps_config& config, + const std::string& topic_name) + : samples_(config.samples) + , rtps_participant_(nullptr) + , rtps_reader_(nullptr) + , reader_history_(nullptr) + , stop_(false) + , data_(new HelloWorld) +{ + // Create RTPS Participant + RTPSParticipantAttributes part_attr; + part_attr.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol::SIMPLE; + part_attr.builtin.use_WriterLivelinessProtocol = true; + rtps_participant_ = RTPSDomain::createParticipant(0, part_attr); + + if (rtps_participant_ == nullptr) + { + throw std::runtime_error("RTPS Participant creation failed"); + } + + // Reader History Attributes + HistoryAttributes hatt; + hatt.payloadMaxSize = 255; + reader_history_ = new ReaderHistory(hatt); + + // Create RTPS Reader + ReaderAttributes reader_att; + reader_att.endpoint.reliabilityKind = RELIABLE; + reader_att.endpoint.durabilityKind = TRANSIENT_LOCAL; + + rtps_reader_ = RTPSDomain::createRTPSReader(rtps_participant_, reader_att, reader_history_, this); + + if (rtps_reader_ == nullptr) + { + throw std::runtime_error("RTPS Reader creation failed"); + } + + std::cout << "Registering RTPS Reader" << std::endl; + + TopicAttributes topic_att; + topic_att.topicKind = NO_KEY; + topic_att.topicDataType = "HelloWorld"; + topic_att.topicName = topic_name; + + eprosima::fastdds::dds::ReaderQos reader_qos; + reader_qos.m_durability.kind = eprosima::fastdds::dds::TRANSIENT_LOCAL_DURABILITY_QOS; + reader_qos.m_reliability.kind = eprosima::fastdds::dds::RELIABLE_RELIABILITY_QOS; + + // Register entity + if (!rtps_participant_->registerReader(rtps_reader_, topic_att, reader_qos)) + { + throw std::runtime_error("Entity registration failed"); + } +} + +ReaderApp::~ReaderApp() +{ + RTPSDomain::removeRTPSParticipant(rtps_participant_); + delete(reader_history_); +} + +void ReaderApp::on_reader_matched( + eprosima::fastdds::rtps::RTPSReader*, + const eprosima::fastdds::rtps::MatchingInfo& info) + +{ + if (info.status == MATCHED_MATCHING) + { + std::cout << "Remote endpoint with GUID " << info.remoteEndpointGuid << " matched." << std::endl; + } + else if (info.status == REMOVED_MATCHING) + { + std::cout << "Remote endpoint with GUID " << info.remoteEndpointGuid << " unmatched." << std::endl; + } +} + +void ReaderApp::run() +{ + std::unique_lock lck(terminate_cv_mtx_); + terminate_cv_.wait(lck, [&] + { + return is_stopped(); + }); +} + +void ReaderApp::on_new_cache_change_added( + RTPSReader* reader, + const CacheChange_t* const change) +{ + if (!is_stopped()) + { + if (deserialize_payload(change->serializedPayload, data_)) + { + std::cout << "Message: " << data_->message() << " with index " << data_->index() << " RECEIVED" << + std::endl; + samples_received_++; + } + else + { + std::cout << "Message: not deserialized" << std::endl; + } + + reader->get_history()->remove_change((CacheChange_t*)change); + + if ((samples_ > 0) && (samples_received_ >= samples_)) + { + stop(); + } + } +} + +bool ReaderApp::deserialize_payload( + const SerializedPayload_t& payload, + HelloWorld* data) +{ + try + { + HelloWorld* p_type = data; + + // Object that manages the raw buffer. + eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast(payload.data), payload.length); + + // Object that deserializes the data. + eprosima::fastcdr::Cdr deser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN +#if FASTCDR_VERSION_MAJOR == 1 + , eprosima::fastcdr::Cdr::CdrType::DDS_CDR +#endif // FASTCDR_VERSION_MAJOR == 1 + ); + + // Deserialize encapsulation. + deser.read_encapsulation(); + //payload.encapsulation = deser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE : CDR_LE; + + // Deserialize the object. + deser >> *p_type; + } + catch (eprosima::fastcdr::exception::Exception& /*exception*/) + { + return false; + } + + return true; +} + +bool ReaderApp::is_stopped() +{ + return stop_.load(); +} + +void ReaderApp::stop() +{ + stop_.store(true); + terminate_cv_.notify_all(); +} + +} // namespace rtps +} // namespace examples +} // namespace fastdds +} // namespace eprosima diff --git a/examples/cpp/rtps/ReaderApp.hpp b/examples/cpp/rtps/ReaderApp.hpp new file mode 100644 index 00000000000..4f7ba44cbec --- /dev/null +++ b/examples/cpp/rtps/ReaderApp.hpp @@ -0,0 +1,102 @@ +// Copyright 2024 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 ReaderApp.hpp + * + */ + +#ifndef FASTDDS_EXAMPLES_CPP_RTPS__READERAPP_HPP +#define FASTDDS_EXAMPLES_CPP_RTPS__READERAPP_HPP + +#include +#include + +#include +#include +#include +#include + +#include "CLIParser.hpp" +#include "Application.hpp" +#include "HelloWorld.hpp" + +using namespace eprosima::fastdds::rtps; + +namespace eprosima { +namespace fastdds { +namespace examples { +namespace rtps { + +class ReaderApp : public Application, public ReaderListener +{ +public: + + ReaderApp( + const CLIParser::rtps_config& config, + const std::string& topic_name); + + virtual ~ReaderApp(); + + //! Run RTPS Reader + void run() override; + + //! New CacheChange_t added to the history callback + void on_new_cache_change_added( + RTPSReader* reader, + const CacheChange_t* const change) override; + + //! Reader matched method + void on_reader_matched( + RTPSReader*, + const MatchingInfo& info) override; + + //! Trigger the end of execution + void stop() override; + +private: + + //! Method to deserialize the payload + bool deserialize_payload( + const SerializedPayload_t& payload, + HelloWorld* data); + + //! Return the current state of execution + bool is_stopped(); + + uint16_t samples_; + + uint16_t samples_received_; + + RTPSParticipant* rtps_participant_; + + RTPSReader* rtps_reader_; + + ReaderHistory* reader_history_; + + std::atomic stop_; + + mutable std::mutex terminate_cv_mtx_; + + std::condition_variable terminate_cv_; + + HelloWorld* data_; +}; + +} // namespace rtps +} // namespace examples +} // namespace fastdds +} // namespace eprosima + +#endif // FASTDDS_EXAMPLES_CPP_RTPS__READERAPP_HPP diff --git a/examples/cpp/rtps/Registered/CMakeLists.txt b/examples/cpp/rtps/Registered/CMakeLists.txt deleted file mode 100644 index d28ff14cadd..00000000000 --- a/examples/cpp/rtps/Registered/CMakeLists.txt +++ /dev/null @@ -1,52 +0,0 @@ -# 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. - -cmake_minimum_required(VERSION 3.20) - -project(RTPSTest_registered VERSION 1 LANGUAGES CXX) - -# Find requirements -if(NOT fastcdr_FOUND) - find_package(fastcdr 2 REQUIRED) -endif() - -if(NOT foonathan_memory_FOUND) - find_package(foonathan_memory REQUIRED) -endif() - -if(NOT fastdds_FOUND) - find_package(fastdds 3 REQUIRED) -endif() - -#Check C++11 -include(CheckCXXCompilerFlag) -if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") - check_cxx_compiler_flag(-std=c++11 SUPPORTS_CXX11) - if(NOT SUPPORTS_CXX11) - message(FATAL_ERROR "Compiler doesn't support C++11") - endif() -endif() - -message(STATUS "Configuring Registered...") -file(GLOB RTPSTESTREGISTERED_SOURCES "*.cpp") - -add_executable(Registered ${RTPSTESTREGISTERED_SOURCES}) -target_compile_definitions(Registered PRIVATE - $<$>,$>:__DEBUG> - $<$:__INTERNALDEBUG> # Internal debug activated. -) -target_link_libraries(Registered fastdds fastcdr foonathan_memory) -install(TARGETS Registered - RUNTIME DESTINATION examples/cpp/rtps/Registered/${BIN_INSTALL_DIR} - ) diff --git a/examples/cpp/rtps/Registered/README.txt b/examples/cpp/rtps/Registered/README.txt deleted file mode 100644 index dcf12012407..00000000000 --- a/examples/cpp/rtps/Registered/README.txt +++ /dev/null @@ -1,4 +0,0 @@ -To launch this test open two different consoles: - -In the first one launch: ./Registered writer (or Registered.exe writer on windows). -In the second one: ./Registered reader (or Registered.exe reader on windows). diff --git a/examples/cpp/rtps/Registered/TestReaderRegistered.cpp b/examples/cpp/rtps/Registered/TestReaderRegistered.cpp deleted file mode 100644 index b62a92cf082..00000000000 --- a/examples/cpp/rtps/Registered/TestReaderRegistered.cpp +++ /dev/null @@ -1,103 +0,0 @@ -// 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 TestReaderRegistered.cpp - * - */ - -#include "TestReaderRegistered.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace eprosima::fastdds; -using namespace eprosima::fastdds::rtps; - -TestReaderRegistered::TestReaderRegistered() - : mp_participant(nullptr) - , mp_reader(nullptr) - , mp_history(nullptr) -{ - - -} - -TestReaderRegistered::~TestReaderRegistered() -{ - RTPSDomain::removeRTPSParticipant(mp_participant); - delete(mp_history); -} - -bool TestReaderRegistered::init() -{ - //CREATE PARTICIPANT - RTPSParticipantAttributes PParam; - PParam.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol::SIMPLE; - PParam.builtin.use_WriterLivelinessProtocol = true; - mp_participant = RTPSDomain::createParticipant(0, PParam); - if (mp_participant == nullptr) - { - return false; - } - //CREATE READERHISTORY - HistoryAttributes hatt; - hatt.payloadMaxSize = 255; - mp_history = new ReaderHistory(hatt); - - //CREATE READER - ReaderAttributes ratt; - Locator_t loc(22222); - ratt.endpoint.unicastLocatorList.push_back(loc); - mp_reader = RTPSDomain::createRTPSReader(mp_participant, ratt, mp_history, &m_listener); - if (mp_reader == nullptr) - { - return false; - } - - return true; -} - -bool TestReaderRegistered::reg() -{ - std::cout << "Registering Reader" << std::endl; - TopicAttributes Tatt; - Tatt.topicKind = NO_KEY; - Tatt.topicDataType = "string"; - Tatt.topicName = "exampleTopic"; - eprosima::fastdds::dds::ReaderQos Rqos; - return mp_participant->registerReader(mp_reader, Tatt, Rqos); -} - -void TestReaderRegistered::run() -{ - printf("Press Enter to stop the Reader.\n"); - std::cin.ignore(); -} - -void TestReaderRegistered::MyListener::on_new_cache_change_added( - RTPSReader* reader, - const CacheChange_t* const change) -{ - printf("Received: %s\n", change->serializedPayload.data); - reader->get_history()->remove_change((CacheChange_t*)change); - n_received++; -} diff --git a/examples/cpp/rtps/Registered/TestReaderRegistered.h b/examples/cpp/rtps/Registered/TestReaderRegistered.h deleted file mode 100644 index c177224ba3f..00000000000 --- a/examples/cpp/rtps/Registered/TestReaderRegistered.h +++ /dev/null @@ -1,82 +0,0 @@ -// 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 TestReaderRegistered.h - * - */ - -#ifndef FASTDDS_EXAMPLES_CPP_RTPS_REGISTERED__TESTREADERREGISTERED_H -#define FASTDDS_EXAMPLES_CPP_RTPS_REGISTERED__TESTREADERREGISTERED_H - -#include - - -namespace eprosima { -namespace fastdds { -namespace rtps { -class RTPSParticipant; -class ReaderHistory; -class RTPSReader; -} // namespace rtps -} // namespace fastdds -} // namespace eprosima - -class TestReaderRegistered -{ -public: - - TestReaderRegistered(); - virtual ~TestReaderRegistered(); - eprosima::fastdds::rtps::RTPSParticipant* mp_participant; - eprosima::fastdds::rtps::RTPSReader* mp_reader; - eprosima::fastdds::rtps::ReaderHistory* mp_history; - bool init(); //Initialization - bool reg(); //Register - void run(); //Run - class MyListener : public eprosima::fastdds::rtps::ReaderListener - { - public: - - MyListener() - : n_received(0) - , n_matched(0) - { - } - - ~MyListener() - { - } - - void on_new_cache_change_added( - eprosima::fastdds::rtps::RTPSReader* reader, - const eprosima::fastdds::rtps::CacheChange_t* const change) override; - void on_reader_matched( - eprosima::fastdds::rtps::RTPSReader*, - const eprosima::fastdds::rtps::MatchingInfo& info) override - - { - if (info.status == eprosima::fastdds::rtps::MATCHED_MATCHING) - { - n_matched++; - } - } - - uint32_t n_received; - uint32_t n_matched; - } - m_listener; -}; - -#endif // FASTDDS_EXAMPLES_CPP_RTPS_REGISTERED__TESTREADERREGISTERED_H diff --git a/examples/cpp/rtps/Registered/TestWriterRegistered.cpp b/examples/cpp/rtps/Registered/TestWriterRegistered.cpp deleted file mode 100644 index 31e7d898a6c..00000000000 --- a/examples/cpp/rtps/Registered/TestWriterRegistered.cpp +++ /dev/null @@ -1,124 +0,0 @@ -// 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 TestWriterRegistered.cpp - * - */ - -#include "TestWriterRegistered.h" - -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace eprosima::fastdds; -using namespace eprosima::fastdds::rtps; -using namespace std; - -TestWriterRegistered::TestWriterRegistered() - : mp_participant(nullptr) - , mp_writer(nullptr) - , mp_history(nullptr) -{ - - -} - -TestWriterRegistered::~TestWriterRegistered() -{ - RTPSDomain::removeRTPSParticipant(mp_participant); - delete(mp_history); -} - -bool TestWriterRegistered::init() -{ - //CREATE PARTICIPANT - RTPSParticipantAttributes PParam; - PParam.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol::SIMPLE; - PParam.builtin.use_WriterLivelinessProtocol = true; - mp_participant = RTPSDomain::createParticipant(0, PParam); - if (mp_participant == nullptr) - { - return false; - } - - //CREATE WRITERHISTORY - HistoryAttributes hatt; - hatt.payloadMaxSize = 255; - hatt.maximumReservedCaches = 50; - mp_history = new WriterHistory(hatt); - - //CREATE WRITER - WriterAttributes watt; - watt.endpoint.reliabilityKind = BEST_EFFORT; - mp_writer = RTPSDomain::createRTPSWriter(mp_participant, watt, mp_history, &m_listener); - if (mp_writer == nullptr) - { - return false; - } - - return true; -} - -bool TestWriterRegistered::reg() -{ - cout << "Registering Writer" << endl; - TopicAttributes Tatt; - Tatt.topicKind = NO_KEY; - Tatt.topicDataType = "string"; - Tatt.topicName = "exampleTopic"; - eprosima::fastdds::dds::WriterQos Wqos; - return mp_participant->registerWriter(mp_writer, Tatt, Wqos); -} - -void TestWriterRegistered::run( - uint16_t samples) -{ - cout << "Waiting for matched Readers" << endl; - while (m_listener.n_matched == 0) - { - std::this_thread::sleep_for(std::chrono::milliseconds(250)); - } - - for (int i = 0; i < samples; ++i ) - { - CacheChange_t* ch = mp_history->create_change(255, ALIVE); - if (!ch) // In the case history is full, remove some old changes - { - std::cout << "cleaning history..."; - mp_history->remove_min_change(); - ch = mp_history->create_change(255, ALIVE); - } - -#if defined(_WIN32) - ch->serializedPayload.length = - sprintf_s((char*)ch->serializedPayload.data, 255, "My example string %d", i) + 1; -#else - ch->serializedPayload.length = - snprintf((char*)ch->serializedPayload.data, 255, "My example string %d", i) + 1; -#endif // if defined(_WIN32) - printf("Sending: %s\n", (char*)ch->serializedPayload.data); - mp_history->add_change(ch); - } -} diff --git a/examples/cpp/rtps/Registered/TestWriterRegistered.h b/examples/cpp/rtps/Registered/TestWriterRegistered.h deleted file mode 100644 index c9e10e3e6af..00000000000 --- a/examples/cpp/rtps/Registered/TestWriterRegistered.h +++ /dev/null @@ -1,76 +0,0 @@ -// 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 TestWriterRegistered.h - * - */ - -#ifndef FASTDDS_EXAMPLES_CPP_RTPS_REGISTERED__TESTWRITERREGISTERED_H -#define FASTDDS_EXAMPLES_CPP_RTPS_REGISTERED__TESTWRITERREGISTERED_H - -#include - -namespace eprosima { -namespace fastdds { -namespace rtps { -class RTPSParticipant; -class WriterHistory; -} // namespace rtps -} // namespace fastdds -} // namespace eprosima - -class TestWriterRegistered -{ -public: - - TestWriterRegistered(); - virtual ~TestWriterRegistered(); - eprosima::fastdds::rtps::RTPSParticipant* mp_participant; - eprosima::fastdds::rtps::RTPSWriter* mp_writer; - eprosima::fastdds::rtps::WriterHistory* mp_history; - bool init(); //Initialize writer - bool reg(); //Register the Writer - void run( - uint16_t samples); //Run the Writer - class MyListener : public eprosima::fastdds::rtps::WriterListener - { - public: - - MyListener() - : n_matched(0) - { - } - - ~MyListener() - { - } - - void on_writer_matched( - eprosima::fastdds::rtps::RTPSWriter*, - const eprosima::fastdds::rtps::MatchingInfo& info) override - { - if (info.status == eprosima::fastdds::rtps::MATCHED_MATCHING) - { - ++n_matched; - } - } - - int n_matched; - - } - m_listener; -}; - -#endif // FASTDDS_EXAMPLES_CPP_RTPS_REGISTERED__TESTWRITERREGISTERED_H diff --git a/examples/cpp/rtps/Registered/main_RTPSTest.cpp b/examples/cpp/rtps/Registered/main_RTPSTest.cpp deleted file mode 100644 index 4029c0e13e3..00000000000 --- a/examples/cpp/rtps/Registered/main_RTPSTest.cpp +++ /dev/null @@ -1,88 +0,0 @@ -// 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. - -#include -#include -#include -#include -#include -#include -#include - -#include - -#include "TestReaderRegistered.h" -#include "TestWriterRegistered.h" - -using namespace eprosima; -using namespace fastdds; -using namespace rtps; -using namespace std; - -int main( - int argc, - char** argv) -{ - cout << "Starting RTPS example" << endl; - int type; - if (argc > 1) - { - if (strcmp(argv[1], "writer") == 0) - { - type = 1; - } - else if (strcmp(argv[1], "reader") == 0) - { - type = 2; - } - else - { - cout << "NEEDS writer OR reader as first argument" << endl; - return 0; - } - } - else - { - cout << "NEEDS writer OR reader ARGUMENT" << endl; - cout << "RTPSTest writer" << endl; - cout << "RTPSTest reader" << endl; - return 0; - } - switch (type) - { - case 1: - { - TestWriterRegistered TW; - if (TW.init() && TW.reg()) - { - TW.run(10); - } - break; - } - case 2: - { - TestReaderRegistered TR; - if (TR.init() && TR.reg()) - { - TR.run(); - } - break; - } - } - - RTPSDomain::stopAll(); - cout << "EVERYTHING STOPPED FINE" << endl; - - return 0; -} diff --git a/examples/cpp/rtps/WriterApp.cpp b/examples/cpp/rtps/WriterApp.cpp new file mode 100644 index 00000000000..6929d1b37ec --- /dev/null +++ b/examples/cpp/rtps/WriterApp.cpp @@ -0,0 +1,265 @@ +// Copyright 2024 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 WriterApp.cpp + * + */ + +#include "WriterApp.hpp" + +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "HelloWorld.hpp" + +template<> +void eprosima::fastcdr::serialize( + eprosima::fastcdr::Cdr& scdr, + const HelloWorld& data) +{ + eprosima::fastcdr::Cdr::state current_state(scdr); + scdr.begin_serialize_type(current_state, + eprosima::fastcdr::CdrVersion::XCDRv2 == scdr.get_cdr_version() ? + eprosima::fastcdr::EncodingAlgorithmFlag::DELIMIT_CDR2 : + eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR); + + scdr + << eprosima::fastcdr::MemberId(0) << data.index() + << eprosima::fastcdr::MemberId(1) << data.message(); + scdr.end_serialize_type(current_state); +} + +using namespace eprosima::fastdds; +using namespace eprosima::fastdds::rtps; + +namespace eprosima { +namespace fastdds { +namespace examples { +namespace rtps { + +WriterApp::WriterApp( + const CLIParser::rtps_config& config, + const std::string& topic_name) + : samples_(config.samples) + , samples_sent_(0) + , rtps_participant_(nullptr) + , rtps_writer_(nullptr) + , writer_history_(nullptr) + , matched_(0) + , stop_(false) + , data_(new HelloWorld) +{ + // Create RTPS Participant + RTPSParticipantAttributes part_attr; + part_attr.builtin.discovery_config.discoveryProtocol = DiscoveryProtocol::SIMPLE; + part_attr.builtin.use_WriterLivelinessProtocol = true; + rtps_participant_ = RTPSDomain::createParticipant(0, part_attr); + + if (rtps_participant_ == nullptr) + { + throw std::runtime_error("RTPS Participant creation failed"); + } + + // Writer History Attributes + HistoryAttributes hatt; + hatt.payloadMaxSize = 255; + hatt.maximumReservedCaches = 50; + writer_history_ = new WriterHistory(hatt); + + // Create RTPS Writer + WriterAttributes writer_att; + writer_att.endpoint.reliabilityKind = RELIABLE; + writer_att.endpoint.durabilityKind = TRANSIENT_LOCAL; + + rtps_writer_ = RTPSDomain::createRTPSWriter(rtps_participant_, writer_att, writer_history_, this); + + if (rtps_writer_ == nullptr) + { + throw std::runtime_error("RTPS Writer creation failed"); + } + + std::cout << "Registering RTPS Writer" << std::endl; + + TopicAttributes topic_att; + topic_att.topicKind = NO_KEY; + topic_att.topicDataType = "HelloWorld"; + topic_att.topicName = topic_name; + + eprosima::fastdds::dds::WriterQos writer_qos; + writer_qos.m_durability.kind = eprosima::fastdds::dds::TRANSIENT_LOCAL_DURABILITY_QOS; + writer_qos.m_reliability.kind = eprosima::fastdds::dds::RELIABLE_RELIABILITY_QOS; + + // Register entity + if (!rtps_participant_->registerWriter(rtps_writer_, topic_att, writer_qos)) + { + throw std::runtime_error("Entity registration failed"); + } +} + +WriterApp::~WriterApp() +{ + RTPSDomain::removeRTPSParticipant(rtps_participant_); + delete(writer_history_); +} + +void WriterApp::on_writer_matched( + RTPSWriter*, + const MatchingInfo& info) +{ + if (info.status == MATCHED_MATCHING) + { + ++matched_; + std::cout << "Remote endpoint with GUID " << info.remoteEndpointGuid << " matched." << std::endl; + + terminate_cv_.notify_one(); + } + else if (info.status == REMOVED_MATCHING) + { + --matched_; + std::cout << "Remote endpoint with GUID " << info.remoteEndpointGuid << " unmatched." << std::endl; + } +} + +void WriterApp::run() +{ + while (!is_stopped() && ((samples_ == 0) || (samples_sent_ < samples_))) + { + if (add_change_to_history()) + { + std::cout << "Message " << data_->message() << " with index " << data_->index() << " SENT" << std::endl; + } + + // Wait for period or stop event + std::unique_lock period_lock(terminate_cv_mtx_); + terminate_cv_.wait_for(period_lock, std::chrono::milliseconds(period_ms_), [&]() + { + return is_stopped(); + }); + } +} + +bool WriterApp::serialize_payload( + const HelloWorld* data, + SerializedPayload_t& payload) +{ + const HelloWorld* p_type = data; + + // Object that manages the raw buffer. + eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast(payload.data), payload.max_size); + + // Object that serializes the data. + eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, + eprosima::fastcdr::CdrVersion::XCDRv1); + + payload.encapsulation = ser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE : CDR_LE; + +#if FASTCDR_VERSION_MAJOR > 1 + ser.set_encoding_flag(eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR); +#endif // FASTCDR_VERSION_MAJOR > 1 + + try + { + // Serialize encapsulation + ser.serialize_encapsulation(); + + // Serialize the object. + ser << *p_type; + } + catch (eprosima::fastcdr::exception::Exception& /*exception*/) + { + return false; + } + + // Get the serialized length +#if FASTCDR_VERSION_MAJOR == 1 + payload.length = static_cast(ser.getSerializedDataLength()); +#else + payload.length = static_cast(ser.get_serialized_data_length()); +#endif // FASTCDR_VERSION_MAJOR == 1 + return true; +} + +bool WriterApp::add_change_to_history() +{ + // Wait for the data endpoints discovery + std::unique_lock matched_lock(terminate_cv_mtx_); + terminate_cv_.wait(matched_lock, [&]() + { + // at least one has been discovered + return ((matched_ > 0) || is_stopped()); + }); + + bool ret = false; + + CacheChange_t* ch = writer_history_->create_change(255, ALIVE); + + // In the case history is full, remove some old changes + if (writer_history_->isFull()) + { + writer_history_->remove_min_change(); + ch = writer_history_->create_change(255, ALIVE); + } + + data_->message("Hello World"); + data_->index(data_->index() + 1); + + if (serialize_payload(data_, ch->serializedPayload)) + { + if (writer_history_->add_change(ch)) + { + ++samples_sent_; + ret = true; + } + else + { + std::cout << "Fail to add the change to the history!" << std::endl; + } + } + else + { + std::cout << "Fail to serialize the payload!" << std::endl; + } + + return ret; +} + +bool WriterApp::is_stopped() +{ + return stop_.load(); +} + +void WriterApp::stop() +{ + stop_.store(true); + terminate_cv_.notify_all(); +} + +} // namespace rtps +} // namespace examples +} // namespace fastdds +} // namespace eprosima diff --git a/examples/cpp/rtps/WriterApp.hpp b/examples/cpp/rtps/WriterApp.hpp new file mode 100644 index 00000000000..b940d5ed56b --- /dev/null +++ b/examples/cpp/rtps/WriterApp.hpp @@ -0,0 +1,105 @@ +// Copyright 2024 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 WriterApp.hpp + * + */ + +#ifndef FASTDDS_EXAMPLES_CPP_RTPS__WRITERAPP_HPP +#define FASTDDS_EXAMPLES_CPP_RTPS__WRITERAPP_HPP + +#include + +#include +#include +#include + +#include "CLIParser.hpp" +#include "Application.hpp" +#include "HelloWorld.hpp" +#include + +using namespace eprosima::fastdds::rtps; + +namespace eprosima { +namespace fastdds { +namespace examples { +namespace rtps { + +class WriterApp : public Application, public WriterListener +{ +public: + + WriterApp( + const CLIParser::rtps_config& config, + const std::string& topic_name); + + virtual ~WriterApp(); + + //! Run RTPS Writer + void run() override; + + //! Writer matched method + void on_writer_matched( + RTPSWriter*, + const MatchingInfo& info) override; + + //! Trigger the end of execution + void stop() override; + +private: + + //! Add a new change to Writer History + bool add_change_to_history(); + + //! Return the current state of execution + bool is_stopped(); + + //! Serialize payload + bool serialize_payload( + const HelloWorld* data, + eprosima::fastdds::rtps::SerializedPayload_t& payload); + + uint16_t samples_; + + uint16_t samples_sent_; + + std::vector remote_endpoints_guid; + + RTPSParticipant* rtps_participant_; + + RTPSWriter* rtps_writer_; + + WriterHistory* writer_history_; + + int16_t matched_; + + std::atomic stop_; + + mutable std::mutex terminate_cv_mtx_; + + std::condition_variable terminate_cv_; + + const uint32_t period_ms_ = 100; // in ms + + HelloWorld* data_; +}; + +} // namespace rtps +} // namespace examples +} // namespace fastdds +} // namespace eprosima + +#endif // FASTDDS_EXAMPLES_CPP_RTPS__WRITERAPP_HPP diff --git a/examples/cpp/rtps/hello_world_profile.xml b/examples/cpp/rtps/hello_world_profile.xml new file mode 100644 index 00000000000..d34f6e154f7 --- /dev/null +++ b/examples/cpp/rtps/hello_world_profile.xml @@ -0,0 +1,30 @@ + + + + 0 + + hello_world_participant + + + + + + TRANSIENT_LOCAL + + + RELIABLE + + + + + + + + TRANSIENT_LOCAL + + + RELIABLE + + + + diff --git a/examples/cpp/rtps/main.cpp b/examples/cpp/rtps/main.cpp new file mode 100644 index 00000000000..9195702f6fe --- /dev/null +++ b/examples/cpp/rtps/main.cpp @@ -0,0 +1,97 @@ +// Copyright 2024 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 main.cpp + * + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "Application.hpp" +#include "CLIParser.hpp" + +using eprosima::fastdds::dds::Log; + +using namespace eprosima::fastdds::examples::rtps; + +std::function stop_app_handler; +void signal_handler( + int signum) +{ + stop_app_handler(signum); +} + +int main( + int argc, + char** argv) +{ + auto ret = EXIT_SUCCESS; + const std::string topic_name = "hello_world_topic"; + CLIParser::rtps_config config = CLIParser::parse_cli_options(argc, argv); + + std::string app_name = CLIParser::parse_entity_kind(config.entity); + std::shared_ptr app; + + try + { + app = Application::make_app(config, topic_name); + } + catch (const std::runtime_error& e) + { + EPROSIMA_LOG_ERROR(app_name, e.what()); + ret = EXIT_FAILURE; + } + + if (EXIT_FAILURE != ret) + { + std::thread thread(&Application::run, app); + + if (config.samples == 0) + { + std::cout << app_name << " running. Please press Ctrl+C to stop the " + << app_name << " at any time." << std::endl; + } + else + { + std::cout << app_name << " running for " << config.samples << " samples. Please press Ctrl+C to stop the " + << app_name << " at any time." << std::endl; + } + + stop_app_handler = [&](int signum) + { + std::cout << "\n" << CLIParser::parse_signal(signum) << " received, stopping " << app_name + << " execution." << std::endl; + app->stop(); + }; + + signal(SIGINT, signal_handler); + signal(SIGTERM, signal_handler); + #ifndef _WIN32 + signal(SIGQUIT, signal_handler); + signal(SIGHUP, signal_handler); + #endif // _WIN32 + + thread.join(); + } + + Log::Reset(); + return ret; +} diff --git a/test/examples/rtps.compose.yml b/test/examples/rtps.compose.yml new file mode 100644 index 00000000000..0584a4ccda7 --- /dev/null +++ b/test/examples/rtps.compose.yml @@ -0,0 +1,71 @@ +# Copyright 2024 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. +version: "3" + +services: + sub-rtps: + image: @DOCKER_IMAGE_NAME@ + volumes: + - @PROJECT_BINARY_DIR@:@PROJECT_BINARY_DIR@ + - @fastcdr_LIB_DIR@:@fastcdr_LIB_DIR@ + @TINYXML2_LIB_DIR_COMPOSE_VOLUME@ + environment: + # TODO(eduponz): LD_LIBRARY_PATH is not the correct variable for Windows + LD_LIBRARY_PATH: @PROJECT_BINARY_DIR@/src/cpp:@fastcdr_LIB_DIR@@TINYXML2_LIB_DIR_COMPOSE_LD_LIBRARY_PATH@ + EXAMPLE_DIR: @PROJECT_BINARY_DIR@/examples/cpp/rtps@FILE_EXTENSION@ + command: @SHELL_EXECUTABLE@ -c "$${EXAMPLE_DIR}/rtps@FILE_EXTENSION@ reader --samples 20" + + sub-dds: + image: @DOCKER_IMAGE_NAME@ + volumes: + - @PROJECT_BINARY_DIR@:@PROJECT_BINARY_DIR@ + - @fastcdr_LIB_DIR@:@fastcdr_LIB_DIR@ + @TINYXML2_LIB_DIR_COMPOSE_VOLUME@ + environment: + # TODO(eduponz): LD_LIBRARY_PATH is not the correct variable for Windows + LD_LIBRARY_PATH: @PROJECT_BINARY_DIR@/src/cpp:@fastcdr_LIB_DIR@@TINYXML2_LIB_DIR_COMPOSE_LD_LIBRARY_PATH@ + EXAMPLE_DIR: @PROJECT_BINARY_DIR@/examples/cpp/hello_world + FASTDDS_DEFAULT_PROFILES_FILE: @PROJECT_BINARY_DIR@/examples/cpp/rtps/hello_world_profile.xml + command: @SHELL_EXECUTABLE@ -c "$${EXAMPLE_DIR}/hello_world@FILE_EXTENSION@ subscriber --samples 20" + + pub-rtps: + image: @DOCKER_IMAGE_NAME@ + volumes: + - @PROJECT_BINARY_DIR@:@PROJECT_BINARY_DIR@ + - @fastcdr_LIB_DIR@:@fastcdr_LIB_DIR@ + @TINYXML2_LIB_DIR_COMPOSE_VOLUME@ + environment: + # TODO(eduponz): LD_LIBRARY_PATH is not the correct variable for Windows + LD_LIBRARY_PATH: @PROJECT_BINARY_DIR@/src/cpp:@fastcdr_LIB_DIR@@TINYXML2_LIB_DIR_COMPOSE_LD_LIBRARY_PATH@ + EXAMPLE_DIR: @PROJECT_BINARY_DIR@/examples/cpp/rtps + command: @SHELL_EXECUTABLE@ -c "$${EXAMPLE_DIR}/rtps@FILE_EXTENSION@ writer --samples 10" + depends_on: + - sub-rtps + - sub-dds + + pub-dds: + image: @DOCKER_IMAGE_NAME@ + volumes: + - @PROJECT_BINARY_DIR@:@PROJECT_BINARY_DIR@ + - @fastcdr_LIB_DIR@:@fastcdr_LIB_DIR@ + @TINYXML2_LIB_DIR_COMPOSE_VOLUME@ + environment: + # TODO(eduponz): LD_LIBRARY_PATH is not the correct variable for Windows + LD_LIBRARY_PATH: @PROJECT_BINARY_DIR@/src/cpp:@fastcdr_LIB_DIR@@TINYXML2_LIB_DIR_COMPOSE_LD_LIBRARY_PATH@ + EXAMPLE_DIR: @PROJECT_BINARY_DIR@/examples/cpp/hello_world + FASTDDS_DEFAULT_PROFILES_FILE: @PROJECT_BINARY_DIR@/examples/cpp/rtps/hello_world_profile.xml + command: @SHELL_EXECUTABLE@ -c "$${EXAMPLE_DIR}/hello_world@FILE_EXTENSION@ publisher --samples 10" + depends_on: + - sub-rtps + - sub-dds diff --git a/test/examples/test_rtps.py b/test/examples/test_rtps.py new file mode 100644 index 00000000000..6fa757ad6d5 --- /dev/null +++ b/test/examples/test_rtps.py @@ -0,0 +1,53 @@ +# Copyright 2024 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. + +import subprocess + +def test_rtps(): + """.""" + ret = False + out = '' + try: + out = subprocess.check_output( + '@DOCKER_EXECUTABLE@ compose -f rtps.compose.yml up', + stderr=subprocess.STDOUT, + shell=True, + timeout=20 + ).decode().split('\n') + + sent = 0 + received = 0 + for line in out: + print(line) + if 'SENT' in line: + sent += 1 + + if 'RECEIVED' in line: + received += 1 + + if sent != 0 and received != 0 and sent * 2 == received: + ret = True + else: + print('ERROR: sent: ' + str(sent) + ', but received: ' + str(received) + + ' (expected: ' + str(sent * 2) + ')') + raise subprocess.CalledProcessError(1, '') + + except subprocess.CalledProcessError: + for l in out: + print(l) + except subprocess.TimeoutExpired: + print('TIMEOUT') + print(out) + + assert(ret) diff --git a/utils/scripts/update_generated_code_from_idl.sh b/utils/scripts/update_generated_code_from_idl.sh index c83ef652f09..4f0214311e5 100755 --- a/utils/scripts/update_generated_code_from_idl.sh +++ b/utils/scripts/update_generated_code_from_idl.sh @@ -148,4 +148,8 @@ sed -i 's+"../../../../../../include/fastdds/dds/core/detail/DDSReturnCode.hpp"+ sed -i 's+"../../../../../../include/fastdds/dds/xtypes/type_representation/detail/dds-xtypes_typeobjectPubSubTypes.hpp"++' ./src/cpp/fastdds/builtin/type_lookup_service/detail/TypeLookupTypesPubSubTypes.hpp sed -i 's+"../../../../../../include/fastdds/dds/core/detail/DDSReturnCodePubSubTypes.hpp"++' ./src/cpp/fastdds/builtin/type_lookup_service/detail/TypeLookupTypesPubSubTypes.hpp +rm ./examples/cpp/rtps/HelloWorld*.cxx +rm ./examples/cpp/rtps/HelloWorld*.ipp +find ./examples/cpp/rtps/ -name 'HelloWorld*.hpp' ! -name 'HelloWorld.hpp' -exec rm {} + + exit $ret_value diff --git a/versions.md b/versions.md index 1cfa97a2914..39e3e3420de 100644 --- a/versions.md +++ b/versions.md @@ -66,6 +66,7 @@ Forthcoming * Security example with environment XML profiles. * Static EDP discovery example to avoid EDP meta-traffic. * Topic instances example, compatible with _ShapesDemo_ app. + * RTPS example to show the creation of entities in the RTPS layer. * Removed `TypeConsistencyQos` from DataReader, and included `TypeConsistencyEnforcementQosPolicy` and `DataRepresentationQosPolicy` * Added new `flow_controller_descriptor_list` XML configuration, remove `ThroughtputController`. * Migrate `#define`s within `BuiltinEndpoints.hpp` to namespaced `constexpr` variables.