diff --git a/fairmq/sdk/DDSSession.cxx b/fairmq/sdk/DDSSession.cxx index c3adf113..04c42ab8 100644 --- a/fairmq/sdk/DDSSession.cxx +++ b/fairmq/sdk/DDSSession.cxx @@ -59,7 +59,6 @@ struct DDSSession::Impl explicit Impl(DDSEnvironment env) : fEnv(std::move(env)) , fRMSPlugin(DDSRMSPlugin::localhost) - , fDDSService() , fDDSCustomCmd(fDDSService) , fId(to_string(fSession.create())) , fStopOnDestruction(false) @@ -74,7 +73,6 @@ struct DDSSession::Impl explicit Impl(Id existing, DDSEnvironment env) : fEnv(std::move(env)) , fRMSPlugin(DDSRMSPlugin::localhost) - , fDDSService() , fDDSCustomCmd(fDDSService) , fId(std::move(existing)) , fStopOnDestruction(false) @@ -90,6 +88,20 @@ struct DDSSession::Impl }); } + explicit Impl(dds::tools_api::CSession nativeSession, DDSEnv env) + : fEnv(std::move(env)) + , fRMSPlugin(DDSRMSPlugin::localhost) + , fSession(std::move(nativeSession)) + , fDDSCustomCmd(fDDSService) + , fId(to_string(fSession.getSessionID())) + , fStopOnDestruction(false) + { + // Sanity check + if (!fSession.IsRunning()) { + throw std::runtime_error("Given CSession must be running"); + } + } + ~Impl() { if (fStopOnDestruction) { @@ -118,11 +130,15 @@ struct DDSSession::Impl }; DDSSession::DDSSession(DDSEnvironment env) - : fImpl(std::make_shared(env)) + : fImpl(std::make_shared(std::move(env))) {} DDSSession::DDSSession(Id existing, DDSEnvironment env) - : fImpl(std::make_shared(std::move(existing), env)) + : fImpl(std::make_shared(std::move(existing), std::move(env))) +{} + +DDSSession::DDSSession(dds::tools_api::CSession nativeSession, DDSEnv env) + : fImpl(std::make_shared(std::move(nativeSession), std::move(env))) {} auto DDSSession::GetEnv() const -> DDSEnvironment { return fImpl->fEnv; } diff --git a/fairmq/sdk/DDSSession.h b/fairmq/sdk/DDSSession.h index 93156401..44c846d2 100644 --- a/fairmq/sdk/DDSSession.h +++ b/fairmq/sdk/DDSSession.h @@ -57,6 +57,11 @@ class DDSSession explicit DDSSession(DDSEnvironment env = DDSEnvironment()); explicit DDSSession(Id existing, DDSEnvironment env = DDSEnvironment()); + /// @brief Construct with already existing native DDS API objects + /// @param nativeSession Existing and initialized CSession (either via create() or attach()) + /// @param env Optional DDSEnv + explicit DDSSession(dds::tools_api::CSession nativeSession, DDSEnv env = {}); + auto GetEnv() const -> DDSEnvironment; auto GetId() const -> Id; auto GetRMSPlugin() const -> DDSRMSPlugin; @@ -92,6 +97,7 @@ class DDSSession void SendCommand(const std::string&); friend auto operator<<(std::ostream& os, const DDSSession& session) -> std::ostream&; + private: struct Impl; std::shared_ptr fImpl; diff --git a/fairmq/sdk/DDSTopology.cxx b/fairmq/sdk/DDSTopology.cxx index ec72bffd..c2d46415 100644 --- a/fairmq/sdk/DDSTopology.cxx +++ b/fairmq/sdk/DDSTopology.cxx @@ -34,6 +34,11 @@ struct DDSTopology::Impl , fTopo(fTopoFile.string()) {} + explicit Impl(dds::topology_api::CTopology nativeTopology, DDSEnvironment env) + : fEnv(std::move(env)) + , fTopo(std::move(nativeTopology)) + {} + DDSEnvironment fEnv; Path fTopoFile; dds::topology_api::CTopology fTopo; @@ -43,6 +48,10 @@ DDSTopology::DDSTopology(Path topoFile, DDSEnvironment env) : fImpl(std::make_shared(std::move(topoFile), std::move(env))) {} +DDSTopology::DDSTopology(dds::topology_api::CTopology nativeTopology, DDSEnv env) + : fImpl(std::make_shared(std::move(nativeTopology), std::move(env))) +{} + auto DDSTopology::GetEnv() const -> DDSEnvironment { return fImpl->fEnv; } auto DDSTopology::GetTopoFile() const -> Path diff --git a/fairmq/sdk/DDSTopology.h b/fairmq/sdk/DDSTopology.h index 635094eb..c530d73e 100644 --- a/fairmq/sdk/DDSTopology.h +++ b/fairmq/sdk/DDSTopology.h @@ -10,6 +10,7 @@ #define FAIR_MQ_SDK_DDSTOPOLOGY_H #include +#include #include #include #include @@ -34,6 +35,11 @@ class DDSTopology /// @param env DDS environment explicit DDSTopology(Path topoFile, DDSEnvironment env = DDSEnvironment()); + /// @brief Construct with already existing native DDS API objects + /// @param nativeTopology Existing and initialized CTopology + /// @param env Optional DDSEnv + explicit DDSTopology(dds::topology_api::CTopology nativeTopology, DDSEnv env = {}); + /// @brief Get associated DDS environment auto GetEnv() const -> DDSEnvironment; diff --git a/fairmq/sdk/Topology.cxx b/fairmq/sdk/Topology.cxx index 3516d1ce..418aede0 100644 --- a/fairmq/sdk/Topology.cxx +++ b/fairmq/sdk/Topology.cxx @@ -8,16 +8,16 @@ #include "Topology.h" -#include - +#include +#include #include #include - -#include -#include +#include +#include #include #include -#include +#include +#include namespace fair { namespace mq { @@ -104,6 +104,12 @@ Topology::Topology(DDSTopology topo, DDSSession session) fExecutionThread = std::thread(&Topology::WaitForState, this); } +Topology::Topology(dds::topology_api::CTopology nativeTopo, + dds::tools_api::CSession nativeSession, + DDSEnv env) + : Topology(DDSTopo(std::move(nativeTopo), env), DDSSession(std::move(nativeSession), env)) +{} + auto Topology::ChangeState(TopologyTransition transition, ChangeStateCallback cb, Duration timeout) -> void { { diff --git a/fairmq/sdk/Topology.h b/fairmq/sdk/Topology.h index 3ae5fa22..8813e7e9 100644 --- a/fairmq/sdk/Topology.h +++ b/fairmq/sdk/Topology.h @@ -87,9 +87,18 @@ class Topology { public: /// @brief (Re)Construct a FairMQ topology from an existing DDS topology - /// @param topo Initialized DDS CTopology + /// @param topo DDSTopology + /// @param session DDSSession explicit Topology(DDSTopology topo, DDSSession session = DDSSession()); + /// @brief (Re)Construct a FairMQ topology based on already existing native DDS API objects + /// @param nativeTopo Existing CTopology + /// @param nativeSession Existing and initialized CSession (either via create() or attach()) + /// @param env Optional DDSEnv (needed primarily for unit testing) + explicit Topology(dds::topology_api::CTopology nativeTopo, + dds::tools_api::CSession nativeSession, + DDSEnv env = {}); + explicit Topology(const Topology&) = delete; Topology& operator=(const Topology&) = delete; explicit Topology(Topology&&) = delete; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ab18edc4..74395e69 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -295,6 +295,7 @@ if(BUILD_SDK) SDK Tools DDS::dds_topology_lib + DDS::dds_tools_lib INCLUDES ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} TIMEOUT 15 diff --git a/test/sdk/_topology.cxx b/test/sdk/_topology.cxx index b322c7e2..e280a7d7 100644 --- a/test/sdk/_topology.cxx +++ b/test/sdk/_topology.cxx @@ -9,6 +9,7 @@ #include "TopologyFixture.h" #include +#include #include #include @@ -16,6 +17,18 @@ namespace { using Topology = fair::mq::test::TopologyFixture; +TEST(Topology2, ConstructionWithNativeDdsApiObjects) +{ + // This is only needed for this unit test + fair::mq::sdk::DDSEnv env(CMAKE_CURRENT_BINARY_DIR); + + // Example usage: + dds::topology_api::CTopology nativeTopo(fair::mq::tools::ToString(SDK_TESTSUITE_SOURCE_DIR, "/test_topo.xml")); + dds::tools_api::CSession nativeSession; + nativeSession.create(); + fair::mq::sdk::Topology topo(nativeTopo, nativeSession, env); +} + TEST_F(Topology, Construction) { fair::mq::sdk::Topology topo(mDDSTopo, mDDSSession);