mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-13 16:46:47 +00:00
FairMQ: Add heartbeats to DDS plugin
This commit is contained in:
parent
72cdd1e3d7
commit
56c0b2fd2b
|
@ -37,11 +37,18 @@ DDS::DDS(const string name, const Plugin::Version version, const string maintain
|
||||||
, fEventsMutex()
|
, fEventsMutex()
|
||||||
, fNewEvent()
|
, fNewEvent()
|
||||||
, fDeviceTerminationRequested(false)
|
, fDeviceTerminationRequested(false)
|
||||||
|
, fIosWork{fIos}
|
||||||
|
, fHeartbeatInterval{100}
|
||||||
|
, fHeartbeatTimer{fIos, fHeartbeatInterval}
|
||||||
|
, fIosWorkerThread([&](){ fIos.run(); })
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
TakeDeviceControl();
|
TakeDeviceControl();
|
||||||
fControllerThread = thread(&DDS::HandleControl, this);
|
fControllerThread = thread(&DDS::HandleControl, this);
|
||||||
|
|
||||||
|
fHeartbeatTimer.expires_from_now(chrono::milliseconds{0});
|
||||||
|
fHeartbeatTimer.async_wait(bind(&DDS::Heartbeat, this, placeholders::_1));
|
||||||
}
|
}
|
||||||
catch (PluginServices::DeviceControlError& e)
|
catch (PluginServices::DeviceControlError& e)
|
||||||
{
|
{
|
||||||
|
@ -203,6 +210,24 @@ auto DDS::PublishBoundChannels() -> void
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto DDS::Heartbeat(const boost::system::error_code&) -> void
|
||||||
|
{
|
||||||
|
string id = GetProperty<string>("id");
|
||||||
|
string pid(to_string(getpid()));
|
||||||
|
|
||||||
|
{
|
||||||
|
lock_guard<mutex> lock{fHeartbeatSubscriberMutex};
|
||||||
|
|
||||||
|
for (const auto subscriberId : fHeartbeatSubscribers) {
|
||||||
|
fDDSCustomCmd.send("heartbeat: " + id + "," + pid, to_string(subscriberId));
|
||||||
|
}
|
||||||
|
if (!fHeartbeatSubscribers.empty()) {
|
||||||
|
fHeartbeatTimer.expires_at(fHeartbeatTimer.expires_at() + fHeartbeatInterval);
|
||||||
|
fHeartbeatTimer.async_wait(bind(&DDS::Heartbeat, this, placeholders::_1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
auto DDS::SubscribeForCustomCommands() -> void
|
auto DDS::SubscribeForCustomCommands() -> void
|
||||||
{
|
{
|
||||||
string id = GetProperty<string>("id");
|
string id = GetProperty<string>("id");
|
||||||
|
@ -241,6 +266,23 @@ auto DDS::SubscribeForCustomCommands() -> void
|
||||||
}
|
}
|
||||||
fDDSCustomCmd.send(ss.str(), to_string(senderId));
|
fDDSCustomCmd.send(ss.str(), to_string(senderId));
|
||||||
}
|
}
|
||||||
|
else if (cmd == "subscribe-to-heartbeats")
|
||||||
|
{
|
||||||
|
{
|
||||||
|
auto size = fHeartbeatSubscribers.size();
|
||||||
|
std::lock_guard<std::mutex> lock{fHeartbeatSubscriberMutex};
|
||||||
|
fHeartbeatSubscribers.insert(senderId);
|
||||||
|
}
|
||||||
|
fDDSCustomCmd.send("heartbeat-subscription: OK", to_string(senderId));
|
||||||
|
}
|
||||||
|
else if (cmd == "unsubscribe-from-heartbeats")
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock{fHeartbeatSubscriberMutex};
|
||||||
|
fHeartbeatSubscribers.erase(senderId);
|
||||||
|
}
|
||||||
|
fDDSCustomCmd.send("heartbeat-unsubscription: OK", to_string(senderId));
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LOG(WARN) << "Unknown command: " << cmd;
|
LOG(WARN) << "Unknown command: " << cmd;
|
||||||
|
@ -269,6 +311,9 @@ DDS::~DDS()
|
||||||
{
|
{
|
||||||
fControllerThread.join();
|
fControllerThread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fIos.stop();
|
||||||
|
fIosWorkerThread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
} /* namespace plugins */
|
} /* namespace plugins */
|
||||||
|
|
|
@ -21,6 +21,9 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
#include <boost/asio.hpp>
|
||||||
|
#include <chrono>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
namespace fair
|
namespace fair
|
||||||
{
|
{
|
||||||
|
@ -58,6 +61,8 @@ class DDS : public Plugin
|
||||||
auto PublishBoundChannels() -> void;
|
auto PublishBoundChannels() -> void;
|
||||||
auto SubscribeForCustomCommands() -> void;
|
auto SubscribeForCustomCommands() -> void;
|
||||||
|
|
||||||
|
auto Heartbeat(const boost::system::error_code&) -> void;
|
||||||
|
|
||||||
dds::intercom_api::CIntercomService fService;
|
dds::intercom_api::CIntercomService fService;
|
||||||
dds::intercom_api::CCustomCmd fDDSCustomCmd;
|
dds::intercom_api::CCustomCmd fDDSCustomCmd;
|
||||||
dds::intercom_api::CKeyValue fDDSKeyValue;
|
dds::intercom_api::CKeyValue fDDSKeyValue;
|
||||||
|
@ -76,6 +81,15 @@ class DDS : public Plugin
|
||||||
std::condition_variable fNewEvent;
|
std::condition_variable fNewEvent;
|
||||||
|
|
||||||
std::atomic<bool> fDeviceTerminationRequested;
|
std::atomic<bool> fDeviceTerminationRequested;
|
||||||
|
|
||||||
|
std::set<uint64_t> fHeartbeatSubscribers;
|
||||||
|
std::mutex fHeartbeatSubscriberMutex;
|
||||||
|
|
||||||
|
boost::asio::io_service fIos;
|
||||||
|
boost::asio::io_service::work fIosWork;
|
||||||
|
std::thread fIosWorkerThread;
|
||||||
|
boost::asio::basic_waitable_timer<std::chrono::steady_clock> fHeartbeatTimer;
|
||||||
|
std::chrono::milliseconds fHeartbeatInterval;
|
||||||
};
|
};
|
||||||
|
|
||||||
REGISTER_FAIRMQ_PLUGIN(
|
REGISTER_FAIRMQ_PLUGIN(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user