FairMQ  1.3.8
C++ Message Passing Framework
Plugin.h
1 /********************************************************************************
2  * Copyright (C) 2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 
9 #ifndef FAIR_MQ_PLUGIN_H
10 #define FAIR_MQ_PLUGIN_H
11 
12 #include <fairmq/Tools.h>
13 #include <fairmq/PluginServices.h>
14 
15 #include <boost/dll/alias.hpp>
16 #include <boost/optional.hpp>
17 #include <boost/program_options.hpp>
18 
19 #include <functional>
20 #include <unordered_map>
21 #include <ostream>
22 #include <memory>
23 #include <string>
24 #include <tuple>
25 #include <utility>
26 
27 namespace fair
28 {
29 namespace mq
30 {
31 
38 class Plugin
39 {
40  public:
41  using ProgOptions = boost::optional<boost::program_options::options_description>;
42 
43  using Version = tools::Version;
44 
45  Plugin() = delete;
46  Plugin(const std::string name, const Version version, const std::string maintainer, const std::string homepage, PluginServices* pluginServices);
47 
48  Plugin(const Plugin&) = delete;
49  Plugin operator=(const Plugin&) = delete;
50 
51  virtual ~Plugin();
52 
53  auto GetName() const -> const std::string& { return fkName; }
54  auto GetVersion() const -> const Version { return fkVersion; }
55  auto GetMaintainer() const -> const std::string& { return fkMaintainer; }
56  auto GetHomepage() const -> const std::string& { return fkHomepage; }
57 
58  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()); }
59  friend auto operator!=(const Plugin& lhs, const Plugin& rhs) -> bool { return !(lhs == rhs); }
60  friend auto operator<<(std::ostream& os, const Plugin& p) -> std::ostream&
61  {
62  return os << "'" << p.GetName() << "', "
63  << "version '" << p.GetVersion() << "', "
64  << "maintainer '" << p.GetMaintainer() << "', "
65  << "homepage '" << p.GetHomepage() << "'";
66  }
67  static auto NoProgramOptions() -> ProgOptions { return boost::none; }
68 
69  // device control API
70  // see <fairmq/PluginServices.h> for docs
72  using DeviceStateTransition = fair::mq::PluginServices::DeviceStateTransition;
73  auto ToDeviceState(const std::string& state) const -> DeviceState { return fPluginServices->ToDeviceState(state); }
74  auto ToDeviceStateTransition(const std::string& transition) const -> DeviceStateTransition { return fPluginServices->ToDeviceStateTransition(transition); }
75  auto ToStr(DeviceState state) const -> std::string { return fPluginServices->ToStr(state); }
76  auto ToStr(DeviceStateTransition transition) const -> std::string { return fPluginServices->ToStr(transition); }
77  auto GetCurrentDeviceState() const -> DeviceState { return fPluginServices->GetCurrentDeviceState(); }
78  auto TakeDeviceControl() -> void { fPluginServices->TakeDeviceControl(fkName); };
79  auto StealDeviceControl() -> void { fPluginServices->StealDeviceControl(fkName); };
80  auto ReleaseDeviceControl() -> void { fPluginServices->ReleaseDeviceControl(fkName); };
81  auto ChangeDeviceState(const DeviceStateTransition next) -> void { fPluginServices->ChangeDeviceState(fkName, next); }
82  auto SubscribeToDeviceStateChange(std::function<void(DeviceState)> callback) -> void { fPluginServices->SubscribeToDeviceStateChange(fkName, callback); }
83  auto UnsubscribeFromDeviceStateChange() -> void { fPluginServices->UnsubscribeFromDeviceStateChange(fkName); }
84 
85  // device config API
86  // see <fairmq/PluginServices.h> for docs
87  auto PropertyExists(const std::string& key) -> int { return fPluginServices->PropertyExists(key); }
88  template<typename T>
89  auto SetProperty(const std::string& key, T val) -> void { fPluginServices->SetProperty(key, val); }
90  template<typename T>
91  auto GetProperty(const std::string& key) const -> T { return fPluginServices->GetProperty<T>(key); }
92  auto GetPropertyAsString(const std::string& key) const -> std::string { return fPluginServices->GetPropertyAsString(key); }
93  auto GetChannelInfo() const -> std::unordered_map<std::string, int> { return fPluginServices->GetChannelInfo(); }
94  auto GetPropertyKeys() const -> std::vector<std::string> { return fPluginServices->GetPropertyKeys(); }
95  template<typename T>
96  auto SubscribeToPropertyChange(std::function<void(const std::string& key, T newValue)> callback) -> void { fPluginServices->SubscribeToPropertyChange<T>(fkName, callback); }
97  template<typename T>
98  auto UnsubscribeFromPropertyChange() -> void { fPluginServices->UnsubscribeFromPropertyChange<T>(fkName); }
99  auto SubscribeToPropertyChangeAsString(std::function<void(const std::string& key, std::string newValue)> callback) -> void { fPluginServices->SubscribeToPropertyChangeAsString(fkName, callback); }
100  auto UnsubscribeFromPropertyChangeAsString() -> void { fPluginServices->UnsubscribeFromPropertyChangeAsString(fkName); }
101 
102  auto CycleLogConsoleSeverityUp() -> void { fPluginServices->CycleLogConsoleSeverityUp(); }
103  auto CycleLogConsoleSeverityDown() -> void { fPluginServices->CycleLogConsoleSeverityDown(); }
104  auto CycleLogVerbosityUp() -> void { fPluginServices->CycleLogVerbosityUp(); }
105  auto CycleLogVerbosityDown() -> void { fPluginServices->CycleLogVerbosityDown(); }
106 
107  private:
108  const std::string fkName;
109  const Version fkVersion;
110  const std::string fkMaintainer;
111  const std::string fkHomepage;
112  PluginServices* fPluginServices;
113 }; /* class Plugin */
114 
115 } /* namespace mq */
116 } /* namespace fair */
117 
118 #define REGISTER_FAIRMQ_PLUGIN(KLASS, NAME, VERSION, MAINTAINER, HOMEPAGE, PROGOPTIONS) \
119 static auto Make_##NAME##_Plugin(fair::mq::PluginServices* pluginServices) -> std::unique_ptr<fair::mq::Plugin> \
120 { \
121  return fair::mq::tools::make_unique<KLASS>(std::string{#NAME}, VERSION, std::string{MAINTAINER}, std::string{HOMEPAGE}, pluginServices); \
122 } \
123 BOOST_DLL_ALIAS(Make_##NAME##_Plugin, make_##NAME##_plugin) \
124 BOOST_DLL_ALIAS(PROGOPTIONS, get_##NAME##_plugin_progoptions)
125 
126 #endif /* FAIR_MQ_PLUGIN_H */
Facilitates communication between devices and plugins.
Definition: PluginServices.h:38
auto StealDeviceControl(const std::string &controller) -> void
Become device controller by force.
Definition: PluginServices.cxx:133
auto UnsubscribeFromPropertyChange(const std::string &subscriber) -> void
Unsubscribe from property updates of type T.
Definition: PluginServices.h:249
auto TakeDeviceControl(const std::string &controller) -> void
Become device controller.
Definition: PluginServices.cxx:112
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
auto UnsubscribeFromDeviceStateChange(const std::string &subscriber) -> void
Unsubscribe from device state changes.
Definition: PluginServices.h:171
static auto ToStr(DeviceState state) -> std::string
Convert DeviceState to string.
Definition: PluginServices.h:107
auto GetCurrentDeviceState() const -> DeviceState
Definition: PluginServices.h:118
auto GetPropertyAsString(const std::string &key) const -> std::string
Read config property as string.
Definition: PluginServices.h:222
Base class for FairMQ plugins.
Definition: Plugin.h:38
auto ReleaseDeviceControl(const std::string &controller) -> void
Release device controller role.
Definition: PluginServices.cxx:140
auto SubscribeToDeviceStateChange(const std::string &subscriber, std::function< void(DeviceState)> callback) -> void
Subscribe with a callback to device state changes.
Definition: PluginServices.h:162
static auto ToDeviceState(const std::string &state) -> DeviceState
Convert string to DeviceState.
Definition: PluginServices.h:96
auto GetProperty(const std::string &key) const -> T
Read config property.
Definition: PluginServices.h:210
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
DeviceState
See https://github.com/FairRootGroup/FairRoot/blob/dev/fairmq/docs/Device.md#13-state-machine.
Definition: PluginServices.h:60
auto SetProperty(const std::string &key, T val) -> void
Set config property.
Definition: PluginServices.h:186
auto UnsubscribeFromPropertyChangeAsString(const std::string &subscriber) -> void
Unsubscribe from property updates that convert to string.
Definition: PluginServices.h:263
static auto ToDeviceStateTransition(const std::string &transition) -> DeviceStateTransition
Convert string to DeviceStateTransition.
Definition: PluginServices.h:102
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23
auto GetPropertyKeys() const -> std::vector< std::string >
Discover the list of property keys.
Definition: PluginServices.h:233
auto ChangeDeviceState(const std::string &controller, const DeviceStateTransition next) -> void
Request a device state transition.
Definition: PluginServices.cxx:93
Definition: Version.h:22

privacy