FairMQ  1.4.14
C++ Message Queuing Library and Framework
Plugin.h
1 /********************************************************************************
2  * Copyright (C) 2017-2019 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/CppSTL.h>
13 #include <fairmq/tools/Version.h>
14 #include <fairmq/PluginServices.h>
15 
16 #include <boost/dll/alias.hpp>
17 #include <boost/optional.hpp>
18 #include <boost/program_options.hpp>
19 
20 #include <functional>
21 #include <unordered_map>
22 #include <ostream>
23 #include <memory>
24 #include <string>
25 #include <tuple>
26 #include <utility>
27 
28 namespace fair
29 {
30 namespace mq
31 {
32 
39 class Plugin
40 {
41  public:
42  using ProgOptions = boost::optional<boost::program_options::options_description>;
43 
44  using Version = tools::Version;
45 
46  Plugin() = delete;
47  Plugin(std::string name,
48  Version version,
49  std::string maintainer,
50  std::string homepage,
51  PluginServices* pluginServices);
52 
53  Plugin(const Plugin&) = delete;
54  Plugin operator=(const Plugin&) = delete;
55 
56  virtual ~Plugin();
57 
58  auto GetName() const -> const std::string& { return fkName; }
59  auto GetVersion() const -> const Version { return fkVersion; }
60  auto GetMaintainer() const -> const std::string& { return fkMaintainer; }
61  auto GetHomepage() const -> const std::string& { return fkHomepage; }
62 
63  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()); }
64  friend auto operator!=(const Plugin& lhs, const Plugin& rhs) -> bool { return !(lhs == rhs); }
65  friend auto operator<<(std::ostream& os, const Plugin& p) -> std::ostream&
66  {
67  return os << "'" << p.GetName() << "', "
68  << "version '" << p.GetVersion() << "', "
69  << "maintainer '" << p.GetMaintainer() << "', "
70  << "homepage '" << p.GetHomepage() << "'";
71  }
72  static auto NoProgramOptions() -> ProgOptions { return boost::none; }
73 
74  // device control API
75  // see <fairmq/PluginServices.h> for docs
76  using DeviceState = fair::mq::PluginServices::DeviceState;
77  using DeviceStateTransition = fair::mq::PluginServices::DeviceStateTransition;
78  auto ToDeviceState(const std::string& state) const -> DeviceState { return fPluginServices->ToDeviceState(state); }
79  auto ToDeviceStateTransition(const std::string& transition) const -> DeviceStateTransition { return fPluginServices->ToDeviceStateTransition(transition); }
80  auto ToStr(DeviceState state) const -> std::string { return fPluginServices->ToStr(state); }
81  auto ToStr(DeviceStateTransition transition) const -> std::string { return fPluginServices->ToStr(transition); }
82  auto GetCurrentDeviceState() const -> DeviceState { return fPluginServices->GetCurrentDeviceState(); }
83  auto TakeDeviceControl() -> void { fPluginServices->TakeDeviceControl(fkName); };
84  auto StealDeviceControl() -> void { fPluginServices->StealDeviceControl(fkName); };
85  auto ReleaseDeviceControl() -> void { fPluginServices->ReleaseDeviceControl(fkName); };
86  auto ChangeDeviceState(const DeviceStateTransition next) -> bool { return fPluginServices->ChangeDeviceState(fkName, next); }
87  auto SubscribeToDeviceStateChange(std::function<void(DeviceState)> callback) -> void { fPluginServices->SubscribeToDeviceStateChange(fkName, callback); }
88  auto UnsubscribeFromDeviceStateChange() -> void { fPluginServices->UnsubscribeFromDeviceStateChange(fkName); }
89 
90  // device config API
91  // see <fairmq/PluginServices.h> for docs
92  auto PropertyExists(const std::string& key) -> int { return fPluginServices->PropertyExists(key); }
93 
94  template<typename T>
95  T GetProperty(const std::string& key) const { return fPluginServices->GetProperty<T>(key); }
96  template<typename T>
97  T GetProperty(const std::string& key, const T& ifNotFound) const { return fPluginServices->GetProperty(key, ifNotFound); }
98  std::string GetPropertyAsString(const std::string& key) const { return fPluginServices->GetPropertyAsString(key); }
99  std::string GetPropertyAsString(const std::string& key, const std::string& ifNotFound) const { return fPluginServices->GetPropertyAsString(key, ifNotFound); }
100  fair::mq::Properties GetProperties(const std::string& q) const { return fPluginServices->GetProperties(q); }
101  fair::mq::Properties GetPropertiesStartingWith(const std::string& q) const { return fPluginServices->GetPropertiesStartingWith(q); };
102  std::map<std::string, std::string> GetPropertiesAsString(const std::string& q) const { return fPluginServices->GetPropertiesAsString(q); }
103  std::map<std::string, std::string> GetPropertiesAsStringStartingWith(const std::string& q) const { return fPluginServices->GetPropertiesAsStringStartingWith(q); };
104 
105  auto GetChannelInfo() const -> std::unordered_map<std::string, int> { return fPluginServices->GetChannelInfo(); }
106  auto GetPropertyKeys() const -> std::vector<std::string> { return fPluginServices->GetPropertyKeys(); }
107 
108  template<typename T>
109  auto SetProperty(const std::string& key, T val) -> void { fPluginServices->SetProperty(key, val); }
110  void SetProperties(const fair::mq::Properties& props) { fPluginServices->SetProperties(props); }
111  template<typename T>
112  bool UpdateProperty(const std::string& key, T val) { return fPluginServices->UpdateProperty(key, val); }
113  bool UpdateProperties(const fair::mq::Properties& input) { return fPluginServices->UpdateProperties(input); }
114 
115  void DeleteProperty(const std::string& key) { fPluginServices->DeleteProperty(key); }
116 
117  template<typename T>
118  auto SubscribeToPropertyChange(std::function<void(const std::string& key, T newValue)> callback) -> void { fPluginServices->SubscribeToPropertyChange<T>(fkName, callback); }
119  template<typename T>
120  auto UnsubscribeFromPropertyChange() -> void { fPluginServices->UnsubscribeFromPropertyChange<T>(fkName); }
121  auto SubscribeToPropertyChangeAsString(std::function<void(const std::string& key, std::string newValue)> callback) -> void { fPluginServices->SubscribeToPropertyChangeAsString(fkName, callback); }
122  auto UnsubscribeFromPropertyChangeAsString() -> void { fPluginServices->UnsubscribeFromPropertyChangeAsString(fkName); }
123 
124  auto CycleLogConsoleSeverityUp() -> void { fPluginServices->CycleLogConsoleSeverityUp(); }
125  auto CycleLogConsoleSeverityDown() -> void { fPluginServices->CycleLogConsoleSeverityDown(); }
126  auto CycleLogVerbosityUp() -> void { fPluginServices->CycleLogVerbosityUp(); }
127  auto CycleLogVerbosityDown() -> void { fPluginServices->CycleLogVerbosityDown(); }
128 
129  private:
130  const std::string fkName;
131  const Version fkVersion;
132  const std::string fkMaintainer;
133  const std::string fkHomepage;
134  PluginServices* fPluginServices;
135 }; /* class Plugin */
136 
137 } /* namespace mq */
138 } /* namespace fair */
139 
140 #define REGISTER_FAIRMQ_PLUGIN(KLASS, NAME, VERSION, MAINTAINER, HOMEPAGE, PROGOPTIONS) \
141 static auto Make_##NAME##_Plugin(fair::mq::PluginServices* pluginServices) -> std::unique_ptr<fair::mq::Plugin> \
142 { \
143  return fair::mq::tools::make_unique<KLASS>(std::string{#NAME}, VERSION, std::string{MAINTAINER}, std::string{HOMEPAGE}, pluginServices); \
144 } \
145 BOOST_DLL_ALIAS(Make_##NAME##_Plugin, make_##NAME##_plugin) \
146 BOOST_DLL_ALIAS(PROGOPTIONS, get_##NAME##_plugin_progoptions)
147 
148 #endif /* FAIR_MQ_PLUGIN_H */
Facilitates communication between devices and plugins.
Definition: PluginServices.h:40
auto StealDeviceControl(const std::string &controller) -> void
Become device controller by force.
Definition: PluginServices.cxx:47
auto UnsubscribeFromPropertyChange(const std::string &subscriber) -> void
Unsubscribe from property updates of type T.
Definition: PluginServices.h:250
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
void DeleteProperty(const std::string &key)
Deletes a property with the given key from the config store.
Definition: PluginServices.h:173
auto TakeDeviceControl(const std::string &controller) -> void
Become device controller.
Definition: PluginServices.cxx:31
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::Properties GetProperties(const std::string &q) const
Read several config properties whose keys match the provided regular expression.
Definition: PluginServices.h:210
auto CycleLogVerbosityUp() -> void
Increases logging verbosity, or sets it to lowest if it is already highest.
Definition: PluginServices.h:271
auto UnsubscribeFromDeviceStateChange(const std::string &subscriber) -> void
Unsubscribe from device state changes.
Definition: PluginServices.h:142
static auto ToStr(DeviceState state) -> std::string
Convert DeviceState to string.
Definition: PluginServices.h:81
auto GetCurrentDeviceState() const -> DeviceState
Definition: PluginServices.h:89
auto PropertyExists(const std::string &key) const -> bool
Checks a property with the given key exist in the configuration.
Definition: PluginServices.h:149
auto CycleLogVerbosityDown() -> void
Decreases logging verbosity, or sets it to highest if it is already lowest.
Definition: PluginServices.h:273
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
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
auto GetChannelInfo() const -> std::unordered_map< std::string, int >
Retrieve current channel information.
Definition: PluginServices.h:230
Base class for FairMQ plugins.
Definition: Plugin.h:39
auto ReleaseDeviceControl(const std::string &controller) -> void
Release device controller role.
Definition: PluginServices.cxx:54
auto SubscribeToDeviceStateChange(const std::string &subscriber, std::function< void(DeviceState)> callback) -> void
Subscribe with a callback to device state changes.
Definition: PluginServices.h:133
void SetProperties(const fair::mq::Properties &props)
Set multiple config properties.
Definition: PluginServices.h:161
static auto ToDeviceState(const std::string &state) -> DeviceState
Convert string to DeviceState.
Definition: PluginServices.h:70
auto GetProperty(const std::string &key) const -> T
Read config property, throw if no property with this key exists.
Definition: PluginServices.h:179
auto CycleLogConsoleSeverityDown() -> void
Decreases console logging severity, or sets it to highest if it is already lowest.
Definition: PluginServices.h:269
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
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
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
auto SetProperty(const std::string &key, T val) -> void
Set config property.
Definition: PluginServices.h:158
auto UnsubscribeFromPropertyChangeAsString(const std::string &subscriber) -> void
Unsubscribe from property updates that convert to string.
Definition: PluginServices.h:264
static auto ToDeviceStateTransition(const std::string &transition) -> DeviceStateTransition
Convert string to DeviceStateTransition.
Definition: PluginServices.h:76
auto CycleLogConsoleSeverityUp() -> void
Increases console logging severity, or sets it to lowest if it is already highest.
Definition: PluginServices.h:267
auto ChangeDeviceState(const std::string &controller, const DeviceStateTransition next) -> bool
Request a device state transition.
Definition: PluginServices.cxx:15
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23
fair::mq::Properties GetPropertiesStartingWith(const std::string &q) const
Read several config properties whose keys start with the provided string.
Definition: PluginServices.h:216
auto GetPropertyKeys() const -> std::vector< std::string >
Discover the list of property keys.
Definition: PluginServices.h:234
Definition: Version.h:22

privacy