SDK: Pass CSession as shared ptr

Even though it is copyable the copy does not work.
This commit is contained in:
Dennis Klein 2019-07-25 14:40:24 +02:00
parent 5ab328b01f
commit 363576496d
No known key found for this signature in database
GPG Key ID: 08E62D23FA0ECBBC
8 changed files with 75 additions and 64 deletions

View File

@ -59,8 +59,9 @@ struct DDSSession::Impl
explicit Impl(DDSEnvironment env) explicit Impl(DDSEnvironment env)
: fEnv(std::move(env)) : fEnv(std::move(env))
, fRMSPlugin(DDSRMSPlugin::localhost) , fRMSPlugin(DDSRMSPlugin::localhost)
, fSession(std::make_shared<dds::tools_api::CSession>())
, fDDSCustomCmd(fDDSService) , fDDSCustomCmd(fDDSService)
, fId(to_string(fSession.create())) , fId(to_string(fSession->create()))
, fStopOnDestruction(false) , fStopOnDestruction(false)
{ {
setenv("DDS_SESSION_ID", fId.c_str(), 1); setenv("DDS_SESSION_ID", fId.c_str(), 1);
@ -73,13 +74,14 @@ struct DDSSession::Impl
explicit Impl(Id existing, DDSEnvironment env) explicit Impl(Id existing, DDSEnvironment env)
: fEnv(std::move(env)) : fEnv(std::move(env))
, fRMSPlugin(DDSRMSPlugin::localhost) , fRMSPlugin(DDSRMSPlugin::localhost)
, fSession(std::make_shared<dds::tools_api::CSession>())
, fDDSCustomCmd(fDDSService) , fDDSCustomCmd(fDDSService)
, fId(std::move(existing)) , fId(std::move(existing))
, fStopOnDestruction(false) , fStopOnDestruction(false)
{ {
fSession.attach(fId); fSession->attach(fId);
std::string envId(std::getenv("DDS_SESSION_ID")); auto envId(std::getenv("DDS_SESSION_ID"));
if (envId != fId) { if (envId != nullptr && std::string(envId) != fId) {
setenv("DDS_SESSION_ID", fId.c_str(), 1); setenv("DDS_SESSION_ID", fId.c_str(), 1);
} }
@ -88,16 +90,21 @@ struct DDSSession::Impl
}); });
} }
explicit Impl(dds::tools_api::CSession nativeSession, DDSEnv env) explicit Impl(std::shared_ptr<dds::tools_api::CSession> nativeSession, DDSEnv env)
: fEnv(std::move(env)) : fEnv(std::move(env))
, fRMSPlugin(DDSRMSPlugin::localhost) , fRMSPlugin(DDSRMSPlugin::localhost)
, fSession(std::move(nativeSession)) , fSession(std::move(nativeSession))
, fDDSCustomCmd(fDDSService) , fDDSCustomCmd(fDDSService)
, fId(to_string(fSession.getSessionID())) , fId(to_string(fSession->getSessionID()))
, fStopOnDestruction(false) , fStopOnDestruction(false)
{ {
auto envId(std::getenv("DDS_SESSION_ID"));
if (envId != nullptr && std::string(envId) != fId) {
setenv("DDS_SESSION_ID", fId.c_str(), 1);
}
// Sanity check // Sanity check
if (!fSession.IsRunning()) { if (!fSession->IsRunning()) {
throw std::runtime_error("Given CSession must be running"); throw std::runtime_error("Given CSession must be running");
} }
} }
@ -105,7 +112,7 @@ struct DDSSession::Impl
~Impl() ~Impl()
{ {
if (fStopOnDestruction) { if (fStopOnDestruction) {
fSession.shutdown(); fSession->shutdown();
} }
} }
@ -122,7 +129,7 @@ struct DDSSession::Impl
DDSEnvironment fEnv; DDSEnvironment fEnv;
DDSRMSPlugin fRMSPlugin; DDSRMSPlugin fRMSPlugin;
Path fRMSConfig; Path fRMSConfig;
dds::tools_api::CSession fSession; std::shared_ptr<dds::tools_api::CSession> fSession;
dds::intercom_api::CIntercomService fDDSService; dds::intercom_api::CIntercomService fDDSService;
dds::intercom_api::CCustomCmd fDDSCustomCmd; dds::intercom_api::CCustomCmd fDDSCustomCmd;
Id fId; Id fId;
@ -137,17 +144,17 @@ DDSSession::DDSSession(Id existing, DDSEnvironment env)
: fImpl(std::make_shared<Impl>(std::move(existing), std::move(env))) : fImpl(std::make_shared<Impl>(std::move(existing), std::move(env)))
{} {}
DDSSession::DDSSession(dds::tools_api::CSession nativeSession, DDSEnv env) DDSSession::DDSSession(std::shared_ptr<dds::tools_api::CSession> nativeSession, DDSEnv env)
: fImpl(std::make_shared<Impl>(std::move(nativeSession), std::move(env))) : fImpl(std::make_shared<Impl>(std::move(nativeSession), std::move(env)))
{} {}
auto DDSSession::GetEnv() const -> DDSEnvironment { return fImpl->fEnv; } auto DDSSession::GetEnv() const -> DDSEnvironment { return fImpl->fEnv; }
auto DDSSession::IsRunning() const -> bool { return fImpl->fSession.IsRunning(); } auto DDSSession::IsRunning() const -> bool { return fImpl->fSession->IsRunning(); }
auto DDSSession::GetId() const -> Id { return fImpl->fId; } auto DDSSession::GetId() const -> Id { return fImpl->fId; }
auto DDSSession::Stop() -> void { return fImpl->fSession.shutdown(); } auto DDSSession::Stop() -> void { return fImpl->fSession->shutdown(); }
auto DDSSession::GetRMSPlugin() const -> DDSRMSPlugin { return fImpl->fRMSPlugin; } auto DDSSession::GetRMSPlugin() const -> DDSRMSPlugin { return fImpl->fRMSPlugin; }
@ -183,7 +190,7 @@ auto DDSSession::SubmitAgents(Quantity agents) -> void
blocker.Signal(); blocker.Signal();
}); });
fImpl->fSession.sendRequest<dds::tools_api::SSubmitRequest>(submitRequest); fImpl->fSession->sendRequest<dds::tools_api::SSubmitRequest>(submitRequest);
blocker.Wait(); blocker.Wait();
// perfect // perfect
@ -204,12 +211,12 @@ auto DDSSession::RequestAgentInfo() -> AgentInfo
info.executingAgentsCount = _response.m_executingAgentsCount; info.executingAgentsCount = _response.m_executingAgentsCount;
info.agents.reserve(_response.m_activeAgentsCount); info.agents.reserve(_response.m_activeAgentsCount);
} }
info.agents.emplace_back(*this, std::move(_response.m_agentInfo)); info.agents.emplace_back(*this, _response.m_agentInfo);
}); });
agentInfoRequest->setMessageCallback( agentInfoRequest->setMessageCallback(
[](const dds::tools_api::SMessageResponseData& _message) { LOG(debug) << _message; }); [](const dds::tools_api::SMessageResponseData& _message) { LOG(debug) << _message; });
agentInfoRequest->setDoneCallback([&]() { blocker.Signal(); }); agentInfoRequest->setDoneCallback([&]() { blocker.Signal(); });
fImpl->fSession.sendRequest<dds::tools_api::SAgentInfoRequest>(agentInfoRequest); fImpl->fSession->sendRequest<dds::tools_api::SAgentInfoRequest>(agentInfoRequest);
blocker.Wait(); blocker.Wait();
return info; return info;
@ -230,7 +237,7 @@ auto DDSSession::RequestCommanderInfo() -> CommanderInfo
commanderInfoRequest->setMessageCallback( commanderInfoRequest->setMessageCallback(
[](const dds::tools_api::SMessageResponseData& _message) { LOG(debug) << _message; }); [](const dds::tools_api::SMessageResponseData& _message) { LOG(debug) << _message; });
commanderInfoRequest->setDoneCallback([&]() { blocker.Signal(); }); commanderInfoRequest->setDoneCallback([&]() { blocker.Signal(); });
fImpl->fSession.sendRequest<dds::tools_api::SCommanderInfoRequest>(commanderInfoRequest); fImpl->fSession->sendRequest<dds::tools_api::SCommanderInfoRequest>(commanderInfoRequest);
blocker.Wait(); blocker.Wait();
return info; return info;
@ -269,7 +276,7 @@ auto DDSSession::ActivateTopology(DDSTopology topo) -> void
topologyRequest->setMessageCallback( topologyRequest->setMessageCallback(
[](const dds::tools_api::SMessageResponseData& _message) { LOG(debug) << _message; }); [](const dds::tools_api::SMessageResponseData& _message) { LOG(debug) << _message; });
topologyRequest->setDoneCallback([&]() { blocker.Signal(); }); topologyRequest->setDoneCallback([&]() { blocker.Signal(); });
fImpl->fSession.sendRequest<dds::tools_api::STopologyRequest>(topologyRequest); fImpl->fSession->sendRequest<dds::tools_api::STopologyRequest>(topologyRequest);
blocker.Wait(); blocker.Wait();
WaitForExecutingAgents(topo.GetNumRequiredAgents()); WaitForExecutingAgents(topo.GetNumRequiredAgents());

View File

@ -60,7 +60,7 @@ class DDSSession
/// @brief Construct with already existing native DDS API objects /// @brief Construct with already existing native DDS API objects
/// @param nativeSession Existing and initialized CSession (either via create() or attach()) /// @param nativeSession Existing and initialized CSession (either via create() or attach())
/// @param env Optional DDSEnv /// @param env Optional DDSEnv
explicit DDSSession(dds::tools_api::CSession nativeSession, DDSEnv env = {}); explicit DDSSession(std::shared_ptr<dds::tools_api::CSession> nativeSession, DDSEnv env = {});
auto GetEnv() const -> DDSEnvironment; auto GetEnv() const -> DDSEnvironment;
auto GetId() const -> Id; auto GetId() const -> Id;

View File

@ -105,20 +105,22 @@ Topology::Topology(DDSTopology topo, DDSSession session)
} }
Topology::Topology(dds::topology_api::CTopology nativeTopo, Topology::Topology(dds::topology_api::CTopology nativeTopo,
dds::tools_api::CSession nativeSession, std::shared_ptr<dds::tools_api::CSession> nativeSession,
DDSEnv env) DDSEnv env)
: Topology(DDSTopo(std::move(nativeTopo), env), DDSSession(std::move(nativeSession), env)) : Topology(DDSTopo(std::move(nativeTopo), env), DDSSession(std::move(nativeSession), env))
{} {
if (fDDSSession.RequestCommanderInfo().activeTopologyName != fDDSTopo.GetName()) {
throw std::runtime_error("Given topology must be activated");
}
}
auto Topology::ChangeState(TopologyTransition transition, ChangeStateCallback cb, Duration timeout) -> void auto Topology::ChangeState(TopologyTransition transition, ChangeStateCallback cb, Duration timeout) -> void
{ {
{ {
std::unique_lock<std::mutex> lock(fMtx); std::unique_lock<std::mutex> lock(fMtx);
if (fStateChangeOngoing) { if (fStateChangeOngoing) {
LOG(error) << "A state change request is already in progress, concurrent requests are currently not supported"; throw std::runtime_error("A state change request is already in progress, concurrent requests are currently not supported");
lock.unlock(); lock.unlock();
cb({{AsyncOpResultCode::Error, "A state change request is already in progress, concurrent requests are currently not supported"}, fState});
return;
} }
LOG(info) << "Initiating ChangeState with " << transition << " to " << expectedState.at(transition); LOG(info) << "Initiating ChangeState with " << transition << " to " << expectedState.at(transition);
fStateChangeOngoing = true; fStateChangeOngoing = true;

View File

@ -61,7 +61,7 @@ using TopologyTransition = fair::mq::Transition;
struct MixedState : std::runtime_error { using std::runtime_error::runtime_error; }; struct MixedState : std::runtime_error { using std::runtime_error::runtime_error; };
DeviceState AggregateState(const TopologyState& topologyState) inline DeviceState AggregateState(const TopologyState& topologyState)
{ {
DeviceState first = topologyState.begin()->second.state; DeviceState first = topologyState.begin()->second.state;
@ -69,12 +69,13 @@ DeviceState AggregateState(const TopologyState& topologyState)
return i.second.state == first; return i.second.state == first;
})) { })) {
return first; return first;
} else {
throw MixedState("State is not uniform");
} }
throw MixedState("State is not uniform");
} }
bool StateEqualsTo(const TopologyState& topologyState, DeviceState state) inline bool StateEqualsTo(const TopologyState& topologyState, DeviceState state)
{ {
return AggregateState(topologyState) == state; return AggregateState(topologyState) == state;
} }
@ -92,11 +93,11 @@ class Topology
explicit Topology(DDSTopology topo, DDSSession session = DDSSession()); explicit Topology(DDSTopology topo, DDSSession session = DDSSession());
/// @brief (Re)Construct a FairMQ topology based on already existing native DDS API objects /// @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 nativeSession Existing and initialized CSession (either via create() or attach())
/// @param nativeTopo Existing CTopology that is activated on the given nativeSession
/// @param env Optional DDSEnv (needed primarily for unit testing) /// @param env Optional DDSEnv (needed primarily for unit testing)
explicit Topology(dds::topology_api::CTopology nativeTopo, explicit Topology(dds::topology_api::CTopology nativeTopo,
dds::tools_api::CSession nativeSession, std::shared_ptr<dds::tools_api::CSession> nativeSession,
DDSEnv env = {}); DDSEnv env = {});
explicit Topology(const Topology&) = delete; explicit Topology(const Topology&) = delete;

View File

@ -289,7 +289,7 @@ if(BUILD_SDK)
${CMAKE_CURRENT_BINARY_DIR}/runner.cxx ${CMAKE_CURRENT_BINARY_DIR}/runner.cxx
sdk/_dds.cxx sdk/_dds.cxx
sdk/_topology.cxx sdk/_topology.cxx
sdk/TopologyFixture.h sdk/Fixtures.h
LINKS LINKS
SDK SDK

View File

@ -6,8 +6,8 @@
* copied verbatim in the file "LICENSE" * * copied verbatim in the file "LICENSE" *
********************************************************************************/ ********************************************************************************/
#ifndef FAIR_MQ_TEST_TOPOLOGYFIXTURE #ifndef FAIR_MQ_TEST_FIXTURES
#define FAIR_MQ_TEST_TOPOLOGYFIXTURE #define FAIR_MQ_TEST_FIXTURES
#include "TestEnvironment.h" #include "TestEnvironment.h"
#include <fairmq/SDK.h> #include <fairmq/SDK.h>
@ -51,10 +51,6 @@ struct TopologyFixture : ::testing::Test
mDDSSession.StopOnDestruction(); mDDSSession.StopOnDestruction();
} }
// auto ActivateDDSTopology(const std::string& topology_file) -> void {
// LOG(debug) << "ActivateDDSTopology(\"" << topology_file << "\")";
// }
auto SetUp() -> void override { auto SetUp() -> void override {
LOG(info) << mDDSEnv; LOG(info) << mDDSEnv;
LOG(info) << mDDSSession; LOG(info) << mDDSSession;
@ -78,5 +74,5 @@ struct TopologyFixture : ::testing::Test
} /* namespace mq */ } /* namespace mq */
} /* namespace fair */ } /* namespace fair */
#endif /* FAIR_MQ_TEST_TOPOLOGYFIXTURE */ #endif /* FAIR_MQ_TEST_FIXTURES */

View File

@ -6,41 +6,44 @@
* copied verbatim in the file "LICENSE" * * copied verbatim in the file "LICENSE" *
********************************************************************************/ ********************************************************************************/
#include <TestEnvironment.h> #include "Fixtures.h"
#include <fairlogger/Logger.h>
#include <fairmq/sdk/DDSEnvironment.h> #include <DDS/Topology.h>
#include <fairmq/sdk/DDSInfo.h> #include <DDS/Tools.h>
#include <fairmq/sdk/DDSSession.h>
#include <gtest/gtest.h>
namespace { namespace {
auto setup() -> void TEST(DDSEnvironment, Construction)
{ {
fair::Logger::SetConsoleSeverity("debug"); fair::mq::test::LoggerConfig cfg;
fair::Logger::DefineVerbosity("user1",
fair::VerbositySpec::Make(fair::VerbositySpec::Info::timestamp_us,
fair::VerbositySpec::Info::severity));
fair::Logger::SetVerbosity("user1");
fair::Logger::SetConsoleColor();
}
TEST(DDS, Environment)
{
setup();
fair::mq::sdk::DDSEnvironment env(CMAKE_CURRENT_BINARY_DIR); fair::mq::sdk::DDSEnvironment env(CMAKE_CURRENT_BINARY_DIR);
LOG(debug) << env; LOG(debug) << env;
} }
TEST(DDS, Session) TEST(DDSSession, Construction)
{ {
setup(); fair::mq::test::LoggerConfig cfg;
fair::mq::sdk::DDSEnvironment env(CMAKE_CURRENT_BINARY_DIR); fair::mq::sdk::DDSEnvironment env(CMAKE_CURRENT_BINARY_DIR);
fair::mq::sdk::DDSSession session(env); fair::mq::sdk::DDSSession session(env);
session.StopOnDestruction(); session.StopOnDestruction();
LOG(debug) << session; LOG(debug) << session;
} }
TEST(DDSSession, Construction2)
{
fair::mq::test::LoggerConfig cfg;
fair::mq::sdk::DDSEnvironment env(CMAKE_CURRENT_BINARY_DIR);
auto nativeSession(std::make_shared<dds::tools_api::CSession>());
nativeSession->create();
fair::mq::sdk::DDSSession session(nativeSession, env);
session.StopOnDestruction();
LOG(debug) << session;
session.RequestCommanderInfo();
}
} // namespace } // namespace

View File

@ -6,7 +6,7 @@
* copied verbatim in the file "LICENSE" * * copied verbatim in the file "LICENSE" *
********************************************************************************/ ********************************************************************************/
#include "TopologyFixture.h" #include "Fixtures.h"
#include <DDS/Topology.h> #include <DDS/Topology.h>
#include <DDS/Tools.h> #include <DDS/Tools.h>
@ -20,13 +20,15 @@ using Topology = fair::mq::test::TopologyFixture;
TEST(Topology2, ConstructionWithNativeDdsApiObjects) TEST(Topology2, ConstructionWithNativeDdsApiObjects)
{ {
// This is only needed for this unit test // This is only needed for this unit test
fair::mq::test::LoggerConfig cfg;
fair::mq::sdk::DDSEnv env(CMAKE_CURRENT_BINARY_DIR); fair::mq::sdk::DDSEnv env(CMAKE_CURRENT_BINARY_DIR);
/////////////////////////////////////////
// Example usage: // Example usage:
dds::topology_api::CTopology nativeTopo(fair::mq::tools::ToString(SDK_TESTSUITE_SOURCE_DIR, "/test_topo.xml")); dds::topology_api::CTopology nativeTopo(fair::mq::tools::ToString(SDK_TESTSUITE_SOURCE_DIR, "/test_topo.xml"));
dds::tools_api::CSession nativeSession; auto nativeSession(std::make_shared<dds::tools_api::CSession>());
nativeSession.create(); nativeSession->create();
fair::mq::sdk::Topology topo(nativeTopo, nativeSession, env); EXPECT_THROW(fair::mq::sdk::Topology topo(nativeTopo, nativeSession, env), std::runtime_error);
} }
TEST_F(Topology, Construction) TEST_F(Topology, Construction)