Plugin.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_PLUGIN_H
#define FAIR_MQ_PLUGIN_H
#include <fairmq/Tools.h>
#include <fairmq/PluginServices.h>
#include <boost/dll/alias.hpp>
#include <boost/optional.hpp>
#include <boost/program_options.hpp>
#include <functional>
#include <unordered_map>
#include <ostream>
#include <memory>
#include <string>
#include <tuple>
#include <utility>
namespace fair
{
namespace mq
{
class Plugin
{
public:
using ProgOptions = boost::optional<boost::program_options::options_description>;
using Version = tools::Version;
Plugin() = delete;
Plugin(const std::string name, const Version version, const std::string maintainer, const std::string homepage, PluginServices* pluginServices);
Plugin(const Plugin&) = delete;
Plugin operator=(const Plugin&) = delete;
virtual ~Plugin();
auto GetName() const -> const std::string& { return fkName; }
auto GetVersion() const -> const Version { return fkVersion; }
auto GetMaintainer() const -> const std::string& { return fkMaintainer; }
auto GetHomepage() const -> const std::string& { return fkHomepage; }
friend auto operator==(const Plugin& lhs, const Plugin& rhs) -> bool { return std::make_tuple(lhs.GetName(), lhs.GetVersion()) == std::make_tuple(rhs.GetName(), rhs.GetVersion()); }
friend auto operator!=(const Plugin& lhs, const Plugin& rhs) -> bool { return !(lhs == rhs); }
friend auto operator<<(std::ostream& os, const Plugin& p) -> std::ostream&
{
return os << "'" << p.GetName() << "', "
<< "version '" << p.GetVersion() << "', "
<< "maintainer '" << p.GetMaintainer() << "', "
<< "homepage '" << p.GetHomepage() << "'";
}
static auto NoProgramOptions() -> ProgOptions { return boost::none; }
// device control API
// see <fairmq/PluginServices.h> for docs
using DeviceState = fair::mq::PluginServices::DeviceState;
using DeviceStateTransition = fair::mq::PluginServices::DeviceStateTransition;
auto ToDeviceState(const std::string& state) const -> DeviceState { return fPluginServices->ToDeviceState(state); }
auto ToDeviceStateTransition(const std::string& transition) const -> DeviceStateTransition { return fPluginServices->ToDeviceStateTransition(transition); }
auto ToStr(DeviceState state) const -> std::string { return fPluginServices->ToStr(state); }
auto ToStr(DeviceStateTransition transition) const -> std::string { return fPluginServices->ToStr(transition); }
auto GetCurrentDeviceState() const -> DeviceState { return fPluginServices->GetCurrentDeviceState(); }
auto TakeDeviceControl() -> void { fPluginServices->TakeDeviceControl(fkName); };
auto StealDeviceControl() -> void { fPluginServices->StealDeviceControl(fkName); };
auto ReleaseDeviceControl() -> void { fPluginServices->ReleaseDeviceControl(fkName); };
auto ChangeDeviceState(const DeviceStateTransition next) -> void { fPluginServices->ChangeDeviceState(fkName, next); }
auto SubscribeToDeviceStateChange(std::function<void(DeviceState)> callback) -> void { fPluginServices->SubscribeToDeviceStateChange(fkName, callback); }
auto UnsubscribeFromDeviceStateChange() -> void { fPluginServices->UnsubscribeFromDeviceStateChange(fkName); }
// device config API
// see <fairmq/PluginServices.h> for docs
auto PropertyExists(const std::string& key) -> int { return fPluginServices->PropertyExists(key); }
template<typename T>
auto SetProperty(const std::string& key, T val) -> void { fPluginServices->SetProperty(key, val); }
template<typename T>
auto GetProperty(const std::string& key) const -> T { return fPluginServices->GetProperty<T>(key); }
auto GetPropertyAsString(const std::string& key) const -> std::string { return fPluginServices->GetPropertyAsString(key); }
auto GetChannelInfo() const -> std::unordered_map<std::string, int> { return fPluginServices->GetChannelInfo(); }
auto GetPropertyKeys() const -> std::vector<std::string> { return fPluginServices->GetPropertyKeys(); }
template<typename T>
auto SubscribeToPropertyChange(std::function<void(const std::string& key, T newValue)> callback) -> void { fPluginServices->SubscribeToPropertyChange<T>(fkName, callback); }
template<typename T>
auto UnsubscribeFromPropertyChange() -> void { fPluginServices->UnsubscribeFromPropertyChange<T>(fkName); }
auto SubscribeToPropertyChangeAsString(std::function<void(const std::string& key, std::string newValue)> callback) -> void { fPluginServices->SubscribeToPropertyChangeAsString(fkName, callback); }
auto UnsubscribeFromPropertyChangeAsString() -> void { fPluginServices->UnsubscribeFromPropertyChangeAsString(fkName); }
auto CycleLogConsoleSeverityUp() -> void { fPluginServices->CycleLogConsoleSeverityUp(); }
auto CycleLogConsoleSeverityDown() -> void { fPluginServices->CycleLogConsoleSeverityDown(); }
auto CycleLogVerbosityUp() -> void { fPluginServices->CycleLogVerbosityUp(); }
auto CycleLogVerbosityDown() -> void { fPluginServices->CycleLogVerbosityDown(); }
private:
const std::string fkName;
const Version fkVersion;
const std::string fkMaintainer;
const std::string fkHomepage;
PluginServices* fPluginServices;
}; /* class Plugin */
} /* namespace mq */
} /* namespace fair */
#define REGISTER_FAIRMQ_PLUGIN(KLASS, NAME, VERSION, MAINTAINER, HOMEPAGE, PROGOPTIONS) \
static auto Make_##NAME##_Plugin(fair::mq::PluginServices* pluginServices) -> std::unique_ptr<fair::mq::Plugin> \
{ \
return fair::mq::tools::make_unique<KLASS>(std::string{#NAME}, VERSION, std::string{MAINTAINER}, std::string{HOMEPAGE}, pluginServices); \
} \
BOOST_DLL_ALIAS(Make_##NAME##_Plugin, make_##NAME##_plugin) \
BOOST_DLL_ALIAS(PROGOPTIONS, get_##NAME##_plugin_progoptions)
#endif /* FAIR_MQ_PLUGIN_H */
fair::mq::PluginServices
Facilitates communication between devices and plugins.
Definition: PluginServices.h:38
fair::mq::PluginServices::StealDeviceControl
auto StealDeviceControl(const std::string &controller) -> void
Become device controller by force.
Definition: PluginServices.cxx:133
fair::mq::PluginServices::UnsubscribeFromPropertyChange
auto UnsubscribeFromPropertyChange(const std::string &subscriber) -> void
Unsubscribe from property updates of type T.
Definition: PluginServices.h:249
fair::mq::PluginServices::TakeDeviceControl
auto TakeDeviceControl(const std::string &controller) -> void
Become device controller.
Definition: PluginServices.cxx:112
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:241
fair::mq::PluginServices::UnsubscribeFromDeviceStateChange
auto UnsubscribeFromDeviceStateChange(const std::string &subscriber) -> void
Unsubscribe from device state changes.
Definition: PluginServices.h:171
fair::mq::PluginServices::ToStr
static auto ToStr(DeviceState state) -> std::string
Convert DeviceState to string.
Definition: PluginServices.h:107
fair::mq::PluginServices::GetCurrentDeviceState
auto GetCurrentDeviceState() const -> DeviceState
Definition: PluginServices.h:118
fair::mq::PluginServices::GetPropertyAsString
auto GetPropertyAsString(const std::string &key) const -> std::string
Read config property as string.
Definition: PluginServices.h:222
fair::mq::Plugin
Base class for FairMQ plugins.
Definition: Plugin.h:38
fair::mq::PluginServices::ReleaseDeviceControl
auto ReleaseDeviceControl(const std::string &controller) -> void
Release device controller role.
Definition: PluginServices.cxx:140
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:162
fair::mq::PluginServices::ToDeviceState
static auto ToDeviceState(const std::string &state) -> DeviceState
Convert string to DeviceState.
Definition: PluginServices.h:96
fair::mq::PluginServices::GetProperty
auto GetProperty(const std::string &key) const -> T
Read config property.
Definition: PluginServices.h:210
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:256
fair::mq::PluginServices::DeviceState
DeviceState
See https://github.com/FairRootGroup/FairRoot/blob/dev/fairmq/docs/Device.md#13-state-machine.
Definition: PluginServices.h:60
fair::mq::PluginServices::SetProperty
auto SetProperty(const std::string &key, T val) -> void
Set config property.
Definition: PluginServices.h:186
fair::mq::PluginServices::UnsubscribeFromPropertyChangeAsString
auto UnsubscribeFromPropertyChangeAsString(const std::string &subscriber) -> void
Unsubscribe from property updates that convert to string.
Definition: PluginServices.h:263
fair::mq::PluginServices::ToDeviceStateTransition
static auto ToDeviceStateTransition(const std::string &transition) -> DeviceStateTransition
Convert string to DeviceStateTransition.
Definition: PluginServices.h:102
fair
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23
fair::mq::PluginServices::GetPropertyKeys
auto GetPropertyKeys() const -> std::vector< std::string >
Discover the list of property keys.
Definition: PluginServices.h:233
fair::mq::PluginServices::ChangeDeviceState
auto ChangeDeviceState(const std::string &controller, const DeviceStateTransition next) -> void
Request a device state transition.
Definition: PluginServices.cxx:93
fair::mq::tools::Version
Definition: Version.h:22

privacy