mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-15 17:41:45 +00:00
Move config & control DDS functionality into plugins.
This commit is contained in:
@@ -1,203 +0,0 @@
|
||||
#ifndef FAIRMQDDSTOOLS_H_
|
||||
#define FAIRMQDDSTOOLS_H_
|
||||
|
||||
#include "FairMQLogger.h"
|
||||
#include "FairMQDevice.h"
|
||||
#include "FairMQChannel.h"
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <exception>
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "dds_intercom.h" // DDS
|
||||
|
||||
using namespace std;
|
||||
using namespace dds::intercom_api;
|
||||
|
||||
// container to hold channel config and corresponding dds key values
|
||||
struct DDSConfig
|
||||
{
|
||||
DDSConfig()
|
||||
: subChannels()
|
||||
, ddsValues()
|
||||
{}
|
||||
|
||||
// container of sub channels, e.g. 'i' in data[i]
|
||||
vector<FairMQChannel*> subChannels;
|
||||
// dds values for the channel
|
||||
CKeyValue::valuesMap_t ddsValues;
|
||||
};
|
||||
|
||||
/// Handles channels addresses of the device with configuration from DDS
|
||||
/// Addresses of binding channels are published via DDS using channels names as keys
|
||||
/// Addresses of connecting channels are collected from DDS using channels names as keys
|
||||
/// \param device Reference to FairMQDevice whose channels to handle
|
||||
template<typename TMQDevice>
|
||||
void HandleConfigViaDDS(TMQDevice& device)
|
||||
{
|
||||
// container for binding channels
|
||||
vector<FairMQChannel*> bindingChans;
|
||||
// container for connecting channels
|
||||
map<string, DDSConfig> connectingChans;
|
||||
|
||||
// fill the containers
|
||||
for (auto& mi : device.fChannels)
|
||||
{
|
||||
if ((mi.second).at(0).GetMethod() == "bind")
|
||||
{
|
||||
for (auto& vi : mi.second)
|
||||
{
|
||||
bindingChans.push_back(&vi);
|
||||
}
|
||||
}
|
||||
else if ((mi.second).at(0).GetMethod() == "connect")
|
||||
{
|
||||
// try some trickery with forwarding emplacing values into map
|
||||
connectingChans.emplace(piecewise_construct, forward_as_tuple(mi.first), forward_as_tuple());
|
||||
for (auto& vi : mi.second)
|
||||
{
|
||||
connectingChans.at(mi.first).subChannels.push_back(&vi);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(ERROR) << "Cannot update address configuration. Socket method (bind/connect) not specified.";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for the binding channels to bind
|
||||
device.WaitForInitialValidation();
|
||||
|
||||
// DDS key value store
|
||||
CKeyValue ddsKV;
|
||||
|
||||
// publish bound addresses via DDS at keys corresponding to the channel prefixes, e.g. 'data' in data[i]
|
||||
for (const auto& i : bindingChans)
|
||||
{
|
||||
// LOG(INFO) << "Publishing " << i->GetChannelPrefix() << " address to DDS under '" << i->GetProperty() << "' property name.";
|
||||
ddsKV.putValue(i->GetProperty(), i->GetAddress());
|
||||
}
|
||||
|
||||
// receive connect addresses from DDS via keys corresponding to channel prefixes, e.g. 'data' in data[i]
|
||||
if (connectingChans.size() > 0)
|
||||
{
|
||||
mutex keyMutex;
|
||||
condition_variable keyCV;
|
||||
|
||||
LOG(DEBUG) << "Subscribing for DDS properties.";
|
||||
ddsKV.subscribe([&] (const string& /*key*/, const string& /*value*/)
|
||||
{
|
||||
keyCV.notify_all();
|
||||
});
|
||||
|
||||
// scope based locking
|
||||
{
|
||||
unique_lock<mutex> lock(keyMutex);
|
||||
keyCV.wait_for(lock, chrono::milliseconds(1000), [&] ()
|
||||
{
|
||||
// receive new properties
|
||||
for (auto& mi : connectingChans)
|
||||
{
|
||||
for (auto& vi : mi.second.subChannels)
|
||||
{
|
||||
// LOG(INFO) << "Waiting for " << vi->GetChannelPrefix() << " address from DDS.";
|
||||
ddsKV.getValues(vi->GetProperty(), &(mi.second.ddsValues));
|
||||
}
|
||||
}
|
||||
|
||||
// update channels and remove them from unfinished container
|
||||
for (auto mi = connectingChans.begin(); mi != connectingChans.end(); /* no increment */)
|
||||
{
|
||||
if (mi->second.subChannels.size() == mi->second.ddsValues.size())
|
||||
{
|
||||
auto it = mi->second.ddsValues.begin();
|
||||
for (unsigned int i = 0; i < mi->second.subChannels.size(); ++i)
|
||||
{
|
||||
mi->second.subChannels.at(i)->UpdateAddress(it->second);
|
||||
++it;
|
||||
}
|
||||
// when multiple subChannels are used, their order on every device should be the same, irregardless of arrival order from DDS.
|
||||
device.SortChannel(mi->first);
|
||||
connectingChans.erase(mi++);
|
||||
}
|
||||
else
|
||||
{
|
||||
++mi;
|
||||
}
|
||||
}
|
||||
|
||||
if (connectingChans.empty())
|
||||
{
|
||||
LOG(DEBUG) << "Successfully received all required DDS properties!";
|
||||
}
|
||||
return connectingChans.empty();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Controls device state via DDS custom commands interface
|
||||
/// \param device Reference to FairMQDevice whose state to control
|
||||
void runDDSStateHandler(FairMQDevice& device)
|
||||
{
|
||||
mutex mtx;
|
||||
condition_variable stopCondition;
|
||||
|
||||
string id = device.GetProperty(FairMQDevice::Id, "");
|
||||
string pid(to_string(getpid()));
|
||||
|
||||
try
|
||||
{
|
||||
const set<string> events = { "INIT_DEVICE", "INIT_TASK", "PAUSE", "RUN", "STOP", "RESET_TASK", "RESET_DEVICE" };
|
||||
|
||||
CCustomCmd ddsCustomCmd;
|
||||
|
||||
ddsCustomCmd.subscribe([&](const string& cmd, const string& cond, uint64_t senderId)
|
||||
{
|
||||
LOG(INFO) << "Received command: " << cmd;
|
||||
|
||||
if (cmd == "check-state")
|
||||
{
|
||||
ddsCustomCmd.send(id + ": " + device.GetCurrentStateName() + " (pid: " + pid + ")", to_string(senderId));
|
||||
}
|
||||
else if (events.find(cmd) != events.end())
|
||||
{
|
||||
ddsCustomCmd.send(id + ": attempting to " + cmd, to_string(senderId));
|
||||
device.ChangeState(cmd);
|
||||
}
|
||||
else if (cmd == "END")
|
||||
{
|
||||
ddsCustomCmd.send(id + ": attempting to " + cmd, to_string(senderId));
|
||||
device.ChangeState(cmd);
|
||||
ddsCustomCmd.send(id + ": " + device.GetCurrentStateName(), to_string(senderId));
|
||||
if (device.GetCurrentStateName() == "EXITING")
|
||||
{
|
||||
unique_lock<mutex> lock(mtx);
|
||||
stopCondition.notify_one();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(WARN) << "Unknown command: " << cmd;
|
||||
LOG(WARN) << "Origin: " << senderId;
|
||||
LOG(WARN) << "Destination: " << cond;
|
||||
}
|
||||
});
|
||||
|
||||
LOG(INFO) << "Listening for commands from DDS...";
|
||||
unique_lock<mutex> lock(mtx);
|
||||
stopCondition.wait(lock);
|
||||
}
|
||||
catch (exception& e)
|
||||
{
|
||||
cerr << "Error: " << e.what() << endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* FAIRMQDDSTOOLS_H_ */
|
@@ -9,23 +9,22 @@
|
||||
#define RUNSIMPLEMQSTATEMACHINE_H
|
||||
|
||||
#include "FairMQLogger.h"
|
||||
#include "FairMQConfigPlugin.h"
|
||||
#include "FairMQControlPlugin.h"
|
||||
#include "FairMQParser.h"
|
||||
#include "FairMQProgOptions.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#ifdef DDS_FOUND
|
||||
#include "FairMQDDSTools.h"
|
||||
#endif /*DDS_FOUND*/
|
||||
|
||||
// template function that takes any device
|
||||
// and runs a simple MQ state machine configured from a JSON file and/or (optionally) DDS.
|
||||
// and runs a simple MQ state machine configured from a JSON file and/or a plugin.
|
||||
template<typename TMQDevice>
|
||||
inline int runStateMachine(TMQDevice& device, FairMQProgOptions& config)
|
||||
inline int runStateMachine(TMQDevice& device, FairMQProgOptions& cfg)
|
||||
{
|
||||
if (config.GetValue<int>("catch-signals") > 0)
|
||||
if (cfg.GetValue<int>("catch-signals") > 0)
|
||||
{
|
||||
device.CatchSignals();
|
||||
}
|
||||
@@ -34,20 +33,91 @@ inline int runStateMachine(TMQDevice& device, FairMQProgOptions& config)
|
||||
LOG(WARN) << "Signal handling (e.g. ctrl+C) has been deactivated via command line argument";
|
||||
}
|
||||
|
||||
device.SetConfig(config);
|
||||
std::string control = config.GetValue<std::string>("control");
|
||||
device.SetConfig(cfg);
|
||||
std::string config = cfg.GetValue<std::string>("config");
|
||||
std::string control = cfg.GetValue<std::string>("control");
|
||||
|
||||
// plugin objects
|
||||
void* ldConfigHandle = nullptr;
|
||||
void* ldControlHandle = nullptr;
|
||||
FairMQConfigPlugin* fairmqConfigPlugin = nullptr;
|
||||
FairMQControlPlugin* fairmqControlPlugin = nullptr;
|
||||
|
||||
std::clock_t c_start = std::clock();
|
||||
auto t_start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
device.ChangeState(TMQDevice::INIT_DEVICE);
|
||||
// Wait for the binding channels to bind
|
||||
device.WaitForInitialValidation();
|
||||
|
||||
#ifdef DDS_FOUND
|
||||
if (control == "dds")
|
||||
if (config != "static")
|
||||
{
|
||||
HandleConfigViaDDS(device);
|
||||
LOG(DEBUG) << "Opening config plugin: " << config;
|
||||
ldConfigHandle = dlopen(config.c_str(), RTLD_LAZY);
|
||||
|
||||
if (!ldConfigHandle)
|
||||
{
|
||||
LOG(ERROR) << "Cannot open library: " << dlerror();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// load the fairmqConfigPlugin
|
||||
dlerror();
|
||||
fairmqConfigPlugin = static_cast<FairMQConfigPlugin*>(dlsym(ldConfigHandle, "fairmqConfigPlugin"));
|
||||
const char* dlsymError = dlerror();
|
||||
if (dlsymError)
|
||||
{
|
||||
LOG(ERROR) << "Cannot load fairmqConfigPlugin() from: " << dlsymError;
|
||||
fairmqConfigPlugin = nullptr;
|
||||
dlclose(ldConfigHandle);
|
||||
return 1;
|
||||
}
|
||||
|
||||
fairmqConfigPlugin->initConfig(device);
|
||||
}
|
||||
|
||||
if (control != "interactive" && control != "static")
|
||||
{
|
||||
LOG(DEBUG) << "Opening control plugin: " << control;
|
||||
ldControlHandle = dlopen(control.c_str(), RTLD_LAZY);
|
||||
|
||||
if (!ldControlHandle)
|
||||
{
|
||||
LOG(ERROR) << "Cannot open library: " << dlerror();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// load the fairmqControlPlugin
|
||||
dlerror();
|
||||
fairmqControlPlugin = static_cast<FairMQControlPlugin*>(dlsym(ldControlHandle, "fairmqControlPlugin"));
|
||||
const char* dlsymError = dlerror();
|
||||
if (dlsymError)
|
||||
{
|
||||
LOG(ERROR) << "Cannot load fairmqControlPlugin(): " << dlsymError;
|
||||
fairmqControlPlugin = nullptr;
|
||||
dlclose(ldControlHandle);
|
||||
return 1;
|
||||
}
|
||||
|
||||
fairmqControlPlugin->initControl(device);
|
||||
}
|
||||
|
||||
if (config != "static")
|
||||
{
|
||||
if (fairmqConfigPlugin)
|
||||
{
|
||||
fairmqConfigPlugin->handleInitialConfig(device);
|
||||
}
|
||||
}
|
||||
#endif /*DDS_FOUND*/
|
||||
|
||||
device.WaitForEndOfState(TMQDevice::INIT_DEVICE);
|
||||
|
||||
std::clock_t c_end = std::clock();
|
||||
auto t_end = std::chrono::high_resolution_clock::now();
|
||||
|
||||
LOG(DEBUG) << "Init time (CPU) : " << std::fixed << std::setprecision(2) << 1000.0 * (c_end - c_start) / CLOCKS_PER_SEC << " ms";
|
||||
LOG(DEBUG) << "Init time (Wall): " << std::chrono::duration<double, std::milli>(t_end - t_start).count() << " ms";
|
||||
|
||||
device.ChangeState(TMQDevice::INIT_TASK);
|
||||
device.WaitForEndOfState(TMQDevice::INIT_TASK);
|
||||
|
||||
@@ -72,23 +142,32 @@ inline int runStateMachine(TMQDevice& device, FairMQProgOptions& config)
|
||||
device.ChangeState(TMQDevice::END);
|
||||
}
|
||||
}
|
||||
#ifdef DDS_FOUND
|
||||
else if (control == "dds")
|
||||
{
|
||||
runDDSStateHandler(device);
|
||||
}
|
||||
#endif /*DDS_FOUND*/
|
||||
else
|
||||
{
|
||||
LOG(ERROR) << "Requested control mechanism '" << control << "' is not available.";
|
||||
LOG(ERROR) << "Currently available are:"
|
||||
<< " 'interactive'"
|
||||
<< ", 'static'"
|
||||
#ifdef DDS_FOUND
|
||||
<< ", 'dds'"
|
||||
#endif /*DDS_FOUND*/
|
||||
<< ". Exiting.";
|
||||
return 1;
|
||||
if (fairmqControlPlugin)
|
||||
{
|
||||
fairmqControlPlugin->handleStateChanges(device);
|
||||
}
|
||||
}
|
||||
|
||||
if (config != "static")
|
||||
{
|
||||
if (fairmqConfigPlugin)
|
||||
{
|
||||
LOG(DEBUG) << "Closing FairMQConfigPlugin...";
|
||||
fairmqConfigPlugin->stopConfig();
|
||||
dlclose(ldConfigHandle);
|
||||
}
|
||||
}
|
||||
|
||||
if (control != "interactive" && control != "static")
|
||||
{
|
||||
if (fairmqControlPlugin)
|
||||
{
|
||||
LOG(DEBUG) << "Closing FairMQControlPlugin...";
|
||||
fairmqControlPlugin->stopControl();
|
||||
dlclose(ldControlHandle);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
Reference in New Issue
Block a user