SDK: Implement helper to find most recent running DDS session

This commit is contained in:
Dennis Klein 2019-07-30 19:36:13 +02:00 committed by Dennis Klein
parent 02b20c320c
commit 8d12b908b6
2 changed files with 43 additions and 12 deletions

View File

@ -8,23 +8,21 @@
#include "DDSSession.h"
#include <DDS/Tools.h>
#include <boost/process.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <cassert>
#include <cstdlib>
#include <fairlogger/Logger.h>
#include <fairmq/Tools.h>
#include <fairmq/sdk/DDSAgent.h>
#include <fairmq/sdk/DDSEnvironment.h>
#include <fairmq/sdk/DDSTopology.h>
#include <fairmq/Tools.h>
#include <fairlogger/Logger.h>
#include <DDS/Tools.h>
#include <boost/uuid/uuid_io.hpp>
#include <cassert>
#include <cstdlib>
#include <mutex>
#include <sstream>
#include <unordered_map>
#include <utility>
#include <vector>
namespace fair {
namespace mq {
@ -375,6 +373,39 @@ auto operator<<(std::ostream& os, const DDSSession& session) -> std::ostream&
return os << "$DDS_SESSION_ID: " << session.GetId();
}
auto getMostRecentRunningDDSSession(DDSEnv env) -> DDSSession
{
boost::process::ipstream pipeStream;
boost::process::child c("dds-session list all", boost::process::std_out > pipeStream);
std::string lastLine;
std::string currentLine;
while (pipeStream && std::getline(pipeStream, currentLine) && !currentLine.empty()) {
lastLine = currentLine;
}
c.wait();
std::string sessionId;
if (!lastLine.empty()) {
std::vector<std::string> words;
std::istringstream iss(lastLine);
for (std::string s; iss >> s;) {
if (s != "*") {
words.push_back(s);
}
}
if (words.back() == "RUNNING") {
sessionId = words.front();
}
}
if (sessionId.empty()) {
throw std::runtime_error("could not find most recent DDS session");
}
return DDSSession(DDSSession::Id(sessionId), std::move(env));
}
} // namespace sdk
} // namespace mq
} // namespace fair

View File

@ -27,8 +27,6 @@ namespace fair {
namespace mq {
namespace sdk {
class DDSEnvironment;
/**
* @enum DDSRMSPlugin DDSSession.h <fairmq/sdk/DDSSession.h>
* @brief Supported DDS resource management system plugins
@ -133,6 +131,8 @@ class DDSSession
std::shared_ptr<Impl> fImpl;
};
auto getMostRecentRunningDDSSession(DDSEnv env = {}) -> DDSSession;
} // namespace sdk
} // namespace mq
} // namespace fair