# FairMQ: fairmq/PluginServices.h Source File

PluginServices.h
/********************************************************************************
* Copyright (C) 2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* *
* This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, *
* copied verbatim in the file "LICENSE" *
********************************************************************************/
#ifndef FAIR_MQ_PLUGINSERVICES_H
#define FAIR_MQ_PLUGINSERVICES_H
#include <fairmq/States.h>
#include <FairMQDevice.h>
#include <fairmq/ProgOptions.h>
#include <fairmq/Properties.h>
#include <boost/optional.hpp>
#include <boost/optional/optional_io.hpp>
#include <functional>
#include <string>
#include <unordered_map>
#include <mutex>
#include <map>
#include <condition_variable>
#include <stdexcept>
namespace fair
{
namespace mq
{
class PluginServices
{
public:
PluginServices() = delete;
PluginServices(ProgOptions& config, FairMQDevice& device)
: fConfig(config)
, fDevice(device)
, fDeviceController()
, fDeviceControllerMutex()
, fReleaseDeviceControlCondition()
{
}
~PluginServices()
{
LOG(debug) << "Shutting down Plugin Services";
}
PluginServices(const PluginServices&) = delete;
PluginServices operator=(const PluginServices&) = delete;
using DeviceState = fair::mq::State;
using DeviceStateTransition = fair::mq::Transition;
// Control API
static auto ToDeviceState(const std::string& state) -> DeviceState { return GetState(state); }
static auto ToDeviceStateTransition(const std::string& transition) -> DeviceStateTransition { return GetTransition(transition); }
static auto ToStr(DeviceState state) -> std::string { return GetStateName(state); }
static auto ToStr(DeviceStateTransition transition) -> std::string { return GetTransitionName(transition); }
auto GetCurrentDeviceState() const -> DeviceState { return fDevice.GetCurrentState(); }
auto TakeDeviceControl(const std::string& controller) -> void;
struct DeviceControlError : std::runtime_error { using std::runtime_error::runtime_error; };
auto StealDeviceControl(const std::string& controller) -> void;
auto ReleaseDeviceControl(const std::string& controller) -> void;
auto GetDeviceController() const -> boost::optional<std::string>;
auto WaitForReleaseDeviceControl() -> void;
auto ChangeDeviceState(const std::string& controller, const DeviceStateTransition next) -> bool;
auto SubscribeToDeviceStateChange(const std::string& subscriber, std::function<void(DeviceState /*newState*/)> callback) -> void
{
fDevice.SubscribeToStateChange(subscriber, [&,callback](fair::mq::State newState){
callback(newState);
});
}
auto UnsubscribeFromDeviceStateChange(const std::string& subscriber) -> void { fDevice.UnsubscribeFromStateChange(subscriber); }
// Config API
auto PropertyExists(const std::string& key) const -> bool { return fConfig.Count(key) > 0; }
template<typename T>
auto SetProperty(const std::string& key, T val) -> void { fConfig.SetProperty(key, val); }
void SetProperties(const fair::mq::Properties& props) { fConfig.SetProperties(props); }
template<typename T>
bool UpdateProperty(const std::string& key, T val) { return fConfig.UpdateProperty(key, val); }
bool UpdateProperties(const fair::mq::Properties& input) { return fConfig.UpdateProperties(input); }
void DeleteProperty(const std::string& key) { fConfig.DeleteProperty(key); }
template<typename T>
auto GetProperty(const std::string& key) const -> T { return fConfig.GetProperty<T>(key); }
template<typename T>
T GetProperty(const std::string& key, const T& ifNotFound) const { return fConfig.GetProperty(key, ifNotFound); }
auto GetPropertyAsString(const std::string& key) const -> std::string { return fConfig.GetPropertyAsString(key); }
auto GetPropertyAsString(const std::string& key, const std::string& ifNotFound) const -> std::string { return fConfig.GetPropertyAsString(key, ifNotFound); }
fair::mq::Properties GetProperties(const std::string& q) const { return fConfig.GetProperties(q); }
fair::mq::Properties GetPropertiesStartingWith(const std::string& q) const { return fConfig.GetPropertiesStartingWith(q); }
std::map<std::string, std::string> GetPropertiesAsString(const std::string& q) const { return fConfig.GetPropertiesAsString(q); }
std::map<std::string, std::string> GetPropertiesAsStringStartingWith(const std::string& q) const { return fConfig.GetPropertiesAsStringStartingWith(q); }
auto GetChannelInfo() const -> std::unordered_map<std::string, int> { return fConfig.GetChannelInfo(); }
auto GetPropertyKeys() const -> std::vector<std::string> { return fConfig.GetPropertyKeys(); }
template<typename T>
auto SubscribeToPropertyChange(const std::string& subscriber, std::function<void(const std::string& key, T)> callback) const -> void
{
fConfig.Subscribe<T>(subscriber, callback);
}
template<typename T>
auto UnsubscribeFromPropertyChange(const std::string& subscriber) -> void { fConfig.Unsubscribe<T>(subscriber); }
auto SubscribeToPropertyChangeAsString(const std::string& subscriber, std::function<void(const std::string& key, std::string)> callback) const -> void
{
fConfig.SubscribeAsString(subscriber, callback);
}
auto UnsubscribeFromPropertyChangeAsString(const std::string& subscriber) -> void { fConfig.UnsubscribeAsString(subscriber); }
auto CycleLogConsoleSeverityUp() -> void { Logger::CycleConsoleSeverityUp(); }
auto CycleLogConsoleSeverityDown() -> void { Logger::CycleConsoleSeverityDown(); }
auto CycleLogVerbosityUp() -> void { Logger::CycleVerbosityUp(); }
auto CycleLogVerbosityDown() -> void { Logger::CycleVerbosityDown(); }
private:
fair::mq::ProgOptions& fConfig;
FairMQDevice& fDevice;
boost::optional<std::string> fDeviceController;
mutable std::mutex fDeviceControllerMutex;
std::condition_variable fReleaseDeviceControlCondition;
}; /* class PluginServices */
} /* namespace mq */
} /* namespace fair */
#endif /* FAIR_MQ_PLUGINSERVICES_H */
fair::mq::PluginServices
Facilitates communication between devices and plugins.
Definition: PluginServices.h:40
fair::mq::PluginServices::StealDeviceControl
auto StealDeviceControl(const std::string &controller) -> void
Become device controller by force.
Definition: PluginServices.cxx:47
fair::mq::PluginServices::UnsubscribeFromPropertyChange
auto UnsubscribeFromPropertyChange(const std::string &subscriber) -> void
Unsubscribe from property updates of type T.
Definition: PluginServices.h:250
fair::mq::PluginServices::UpdateProperty
bool UpdateProperty(const std::string &key, T val)
Updates an existing config property (or fails if it doesn&#39;t exist)
Definition: PluginServices.h:166
fair::mq::PluginServices::DeleteProperty
void DeleteProperty(const std::string &key)
Deletes a property with the given key from the config store.
Definition: PluginServices.h:173
fair::mq::PluginServices::TakeDeviceControl
auto TakeDeviceControl(const std::string &controller) -> void
Become device controller.
Definition: PluginServices.cxx:31
fair::mq::PluginServices::SubscribeToPropertyChange
auto SubscribeToPropertyChange(const std::string &subscriber, std::function< void(const std::string &key, T)> callback) const -> void
Subscribe to property updates of type T.
Definition: PluginServices.h:242
fair::mq::PluginServices::ToStr
static auto ToStr(DeviceStateTransition transition) -> std::string
Convert DeviceStateTransition to string.
Definition: PluginServices.h:86
fair::mq::PluginServices::GetProperties
fair::mq::Properties GetProperties(const std::string &q) const
Read several config properties whose keys match the provided regular expression.
Definition: PluginServices.h:210
fair::mq::PluginServices::CycleLogVerbosityUp
auto CycleLogVerbosityUp() -> void
Increases logging verbosity, or sets it to lowest if it is already highest.
Definition: PluginServices.h:271
fair::mq::PluginServices::UnsubscribeFromDeviceStateChange
auto UnsubscribeFromDeviceStateChange(const std::string &subscriber) -> void
Unsubscribe from device state changes.
Definition: PluginServices.h:142
fair::mq::PluginServices::ToStr
static auto ToStr(DeviceState state) -> std::string
Convert DeviceState to string.
Definition: PluginServices.h:81
std
Definition: Error.h:56
fair::mq::PluginServices::GetCurrentDeviceState
auto GetCurrentDeviceState() const -> DeviceState
Definition: PluginServices.h:89
fair::mq::PluginServices::PropertyExists
auto PropertyExists(const std::string &key) const -> bool
Checks a property with the given key exist in the configuration.
Definition: PluginServices.h:149
fair::mq::PluginServices::GetProperty
T GetProperty(const std::string &key, const T &ifNotFound) const
Read config property, return provided value if no property with this key exists.
Definition: PluginServices.h:186
fair::mq::PluginServices::CycleLogVerbosityDown
auto CycleLogVerbosityDown() -> void
Decreases logging verbosity, or sets it to highest if it is already lowest.
Definition: PluginServices.h:273
fair::mq::PluginServices::DeviceControlError
Definition: PluginServices.h:97
fair::mq::PluginServices::GetPropertiesAsString
std::map< std::string, std::string > GetPropertiesAsString(const std::string &q) const
Read several config properties as string whose keys match the provided regular expression.
Definition: PluginServices.h:220
fair::mq::PluginServices::GetPropertyAsString
auto GetPropertyAsString(const std::string &key) const -> std::string
Read config property as string, throw if no property with this key exists.
Definition: PluginServices.h:195
fair::mq::PluginServices::GetChannelInfo
auto GetChannelInfo() const -> std::unordered_map< std::string, int >
Retrieve current channel information.
Definition: PluginServices.h:230
fair::mq::PluginServices::ReleaseDeviceControl
auto ReleaseDeviceControl(const std::string &controller) -> void
Release device controller role.
Definition: PluginServices.cxx:54
fair::mq::ProgOptions
Definition: ProgOptions.h:36
fair::mq::PluginServices::SubscribeToDeviceStateChange
auto SubscribeToDeviceStateChange(const std::string &subscriber, std::function< void(DeviceState)> callback) -> void
Subscribe with a callback to device state changes.
Definition: PluginServices.h:133
fair::mq::PluginServices::GetPropertyAsString
auto GetPropertyAsString(const std::string &key, const std::string &ifNotFound) const -> std::string
Read config property, return provided value if no property with this key exists.
Definition: PluginServices.h:205
fair::mq::PluginServices::SetProperties
void SetProperties(const fair::mq::Properties &props)
Set multiple config properties.
Definition: PluginServices.h:161
fair::mq::PluginServices::ToDeviceState
static auto ToDeviceState(const std::string &state) -> DeviceState
Convert string to DeviceState.
Definition: PluginServices.h:70
fair::mq::PluginServices::GetProperty
auto GetProperty(const std::string &key) const -> T
Read config property, throw if no property with this key exists.
Definition: PluginServices.h:179
fair::mq::PluginServices::CycleLogConsoleSeverityDown
auto CycleLogConsoleSeverityDown() -> void
Decreases console logging severity, or sets it to highest if it is already lowest.
Definition: PluginServices.h:269
fair::mq::PluginServices::SubscribeToPropertyChangeAsString
auto SubscribeToPropertyChangeAsString(const std::string &subscriber, std::function< void(const std::string &key, std::string)> callback) const -> void
Subscribe to property updates.
Definition: PluginServices.h:257
fair::mq::PluginServices::UpdateProperties
bool UpdateProperties(const fair::mq::Properties &input)
Updates multiple existing config properties (or fails of any of then do not exist, leaving property store unchanged)
Definition: PluginServices.h:169
fair::mq::PluginServices::GetPropertiesAsStringStartingWith
std::map< std::string, std::string > GetPropertiesAsStringStartingWith(const std::string &q) const
Read several config properties as string whose keys start with the provided string.
Definition: PluginServices.h:226
fair::mq::PluginServices::SetProperty
auto SetProperty(const std::string &key, T val) -> void
Set config property.
Definition: PluginServices.h:158
fair::mq::PluginServices::UnsubscribeFromPropertyChangeAsString
auto UnsubscribeFromPropertyChangeAsString(const std::string &subscriber) -> void
Unsubscribe from property updates that convert to string.
Definition: PluginServices.h:264
fair::mq::PluginServices::ToDeviceStateTransition
static auto ToDeviceStateTransition(const std::string &transition) -> DeviceStateTransition
Convert string to DeviceStateTransition.
Definition: PluginServices.h:76
fair::mq::PluginServices::CycleLogConsoleSeverityUp
auto CycleLogConsoleSeverityUp() -> void
Increases console logging severity, or sets it to lowest if it is already highest.
Definition: PluginServices.h:267
FairMQDevice
Definition: FairMQDevice.h:53
fair::mq::PluginServices::ChangeDeviceState
auto ChangeDeviceState(const std::string &controller, const DeviceStateTransition next) -> bool
Request a device state transition.
Definition: PluginServices.cxx:15
fair::mq::PluginServices::WaitForReleaseDeviceControl
auto WaitForReleaseDeviceControl() -> void
Block until control is released.
Definition: PluginServices.cxx:77
fair
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23
fair::mq::PluginServices::GetPropertiesStartingWith
fair::mq::Properties GetPropertiesStartingWith(const std::string &q) const
Read several config properties whose keys start with the provided string.
Definition: PluginServices.h:216
fair::mq::PluginServices::GetPropertyKeys
auto GetPropertyKeys() const -> std::vector< std::string >
Discover the list of property keys.
Definition: PluginServices.h:234
fair::mq::PluginServices::GetDeviceController
auto GetDeviceController() const -> boost::optional< std::string >
Get current device controller.
Definition: PluginServices.cxx:70

privacy