FairMQ  1.4.33
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/Version.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::mq
28 {
29 
36 class Plugin
37 {
38  public:
39  using ProgOptions = boost::optional<boost::program_options::options_description>;
40 
41  using Version = tools::Version;
42 
43  Plugin() = delete;
44  Plugin(std::string name,
45  Version version,
46  std::string maintainer,
47  std::string homepage,
48  PluginServices* pluginServices);
49 
50  Plugin(const Plugin&) = delete;
51  Plugin operator=(const Plugin&) = delete;
52 
53  virtual ~Plugin();
54 
55  auto GetName() const -> const std::string& { return fkName; }
56  auto GetVersion() const -> const Version { return fkVersion; }
57  auto GetMaintainer() const -> const std::string& { return fkMaintainer; }
58  auto GetHomepage() const -> const std::string& { return fkHomepage; }
59 
60  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()); }
61  friend auto operator!=(const Plugin& lhs, const Plugin& rhs) -> bool { return !(lhs == rhs); }
62  friend auto operator<<(std::ostream& os, const Plugin& p) -> std::ostream&
63  {
64  return os << "'" << p.GetName() << "', "
65  << "version '" << p.GetVersion() << "', "
66  << "maintainer '" << p.GetMaintainer() << "', "
67  << "homepage '" << p.GetHomepage() << "'";
68  }
69  static auto NoProgramOptions() -> ProgOptions { return boost::none; }
70 
71  // device control API
72  // see <fairmq/PluginServices.h> for docs
73  using DeviceState = fair::mq::PluginServices::DeviceState;
74  using DeviceStateTransition = fair::mq::PluginServices::DeviceStateTransition;
75  auto ToDeviceState(const std::string& state) const -> DeviceState { return fPluginServices->ToDeviceState(state); }
76  auto ToDeviceStateTransition(const std::string& transition) const -> DeviceStateTransition { return fPluginServices->ToDeviceStateTransition(transition); }
77  auto ToStr(DeviceState state) const -> std::string { return fPluginServices->ToStr(state); }
78  auto ToStr(DeviceStateTransition transition) const -> std::string { return fPluginServices->ToStr(transition); }
79  auto GetCurrentDeviceState() const -> DeviceState { return fPluginServices->GetCurrentDeviceState(); }
80  auto TakeDeviceControl() -> void { fPluginServices->TakeDeviceControl(fkName); };
81  auto StealDeviceControl() -> void { fPluginServices->StealDeviceControl(fkName); };
82  auto ReleaseDeviceControl() -> void { fPluginServices->ReleaseDeviceControl(fkName); };
83  auto ChangeDeviceState(const DeviceStateTransition next) -> bool { return fPluginServices->ChangeDeviceState(fkName, next); }
84  auto SubscribeToDeviceStateChange(std::function<void(DeviceState)> callback) -> void { fPluginServices->SubscribeToDeviceStateChange(fkName, callback); }
85  auto UnsubscribeFromDeviceStateChange() -> void { fPluginServices->UnsubscribeFromDeviceStateChange(fkName); }
86 
87  // device config API
88  // see <fairmq/PluginServices.h> for docs
89  auto PropertyExists(const std::string& key) -> int { return fPluginServices->PropertyExists(key); }
90 
91  template<typename T>
92  T GetProperty(const std::string& key) const { return fPluginServices->GetProperty<T>(key); }
93  template<typename T>
94  T GetProperty(const std::string& key, const T& ifNotFound) const { return fPluginServices->GetProperty(key, ifNotFound); }
95  std::string GetPropertyAsString(const std::string& key) const { return fPluginServices->GetPropertyAsString(key); }
96  std::string GetPropertyAsString(const std::string& key, const std::string& ifNotFound) const { return fPluginServices->GetPropertyAsString(key, ifNotFound); }
97  fair::mq::Properties GetProperties(const std::string& q) const { return fPluginServices->GetProperties(q); }
98  fair::mq::Properties GetPropertiesStartingWith(const std::string& q) const { return fPluginServices->GetPropertiesStartingWith(q); };
99  std::map<std::string, std::string> GetPropertiesAsString(const std::string& q) const { return fPluginServices->GetPropertiesAsString(q); }
100  std::map<std::string, std::string> GetPropertiesAsStringStartingWith(const std::string& q) const { return fPluginServices->GetPropertiesAsStringStartingWith(q); };
101 
102  auto GetChannelInfo() const -> std::unordered_map<std::string, int> { return fPluginServices->GetChannelInfo(); }
103  auto GetPropertyKeys() const -> std::vector<std::string> { return fPluginServices->GetPropertyKeys(); }
104 
105  template<typename T>
106  auto SetProperty(const std::string& key, T val) -> void { fPluginServices->SetProperty(key, val); }
107  void SetProperties(const fair::mq::Properties& props) { fPluginServices->SetProperties(props); }
108  template<typename T>
109  bool UpdateProperty(const std::string& key, T val) { return fPluginServices->UpdateProperty(key, val); }
110  bool UpdateProperties(const fair::mq::Properties& input) { return fPluginServices->UpdateProperties(input); }
111 
112  void DeleteProperty(const std::string& key) { fPluginServices->DeleteProperty(key); }
113 
114  template<typename T>
115  auto SubscribeToPropertyChange(std::function<void(const std::string& key, T newValue)> callback) -> void { fPluginServices->SubscribeToPropertyChange<T>(fkName, callback); }
116  template<typename T>
117  auto UnsubscribeFromPropertyChange() -> void { fPluginServices->UnsubscribeFromPropertyChange<T>(fkName); }
118  auto SubscribeToPropertyChangeAsString(std::function<void(const std::string& key, std::string newValue)> callback) -> void { fPluginServices->SubscribeToPropertyChangeAsString(fkName, callback); }
119  auto UnsubscribeFromPropertyChangeAsString() -> void { fPluginServices->UnsubscribeFromPropertyChangeAsString(fkName); }
120 
121  auto CycleLogConsoleSeverityUp() -> void { fPluginServices->CycleLogConsoleSeverityUp(); }
122  auto CycleLogConsoleSeverityDown() -> void { fPluginServices->CycleLogConsoleSeverityDown(); }
123  auto CycleLogVerbosityUp() -> void { fPluginServices->CycleLogVerbosityUp(); }
124  auto CycleLogVerbosityDown() -> void { fPluginServices->CycleLogVerbosityDown(); }
125 
126  private:
127  const std::string fkName;
128  const Version fkVersion;
129  const std::string fkMaintainer;
130  const std::string fkHomepage;
131  PluginServices* fPluginServices;
132 }; /* class Plugin */
133 
134 } // namespace fair::mq
135 
136 #define REGISTER_FAIRMQ_PLUGIN(KLASS, NAME, VERSION, MAINTAINER, HOMEPAGE, PROGOPTIONS) \
137 static auto Make_##NAME##_Plugin(fair::mq::PluginServices* pluginServices) -> std::unique_ptr<fair::mq::Plugin> \
138 { \
139  return std::make_unique<KLASS>(std::string{#NAME}, VERSION, std::string{MAINTAINER}, std::string{HOMEPAGE}, pluginServices); \
140 } \
141 BOOST_DLL_ALIAS(Make_##NAME##_Plugin, make_##NAME##_plugin) \
142 BOOST_DLL_ALIAS(PROGOPTIONS, get_##NAME##_plugin_progoptions)
143 
144 #endif /* FAIR_MQ_PLUGIN_H */
fair::mq::PluginServices::SetProperty
auto SetProperty(const std::string &key, T val) -> void
Set config property.
Definition: PluginServices.h:163
fair::mq::PluginServices::CycleLogConsoleSeverityDown
auto CycleLogConsoleSeverityDown() -> void
Decreases console logging severity, or sets it to highest if it is already lowest.
Definition: PluginServices.h:274
fair::mq::PluginServices::GetCurrentDeviceState
auto GetCurrentDeviceState() const -> DeviceState
Definition: PluginServices.h:94
fair::mq::PluginServices::UnsubscribeFromPropertyChangeAsString
auto UnsubscribeFromPropertyChangeAsString(const std::string &subscriber) -> void
Unsubscribe from property updates that convert to string.
Definition: PluginServices.h:269
fair::mq::PluginServices
Facilitates communication between devices and plugins.
Definition: PluginServices.h:46
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::tools::Version
Definition: Version.h:25
fair::mq::PluginServices::CycleLogVerbosityDown
auto CycleLogVerbosityDown() -> void
Decreases logging verbosity, or sets it to highest if it is already lowest.
Definition: PluginServices.h:278
fair::mq::PluginServices::CycleLogConsoleSeverityUp
auto CycleLogConsoleSeverityUp() -> void
Increases console logging severity, or sets it to lowest if it is already highest.
Definition: PluginServices.h:272
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:221
fair::mq
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:239
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:138
fair::mq::PluginServices::ToDeviceState
static auto ToDeviceState(const std::string &state) -> DeviceState
Convert string to DeviceState.
Definition: PluginServices.h:75
fair::mq::PluginServices::UpdateProperty
bool UpdateProperty(const std::string &key, T val)
Updates an existing config property (or fails if it doesn't exist)
Definition: PluginServices.h:171
fair::mq::PluginServices::ToDeviceStateTransition
static auto ToDeviceStateTransition(const std::string &transition) -> DeviceStateTransition
Convert string to DeviceStateTransition.
Definition: PluginServices.h:81
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,...
Definition: PluginServices.h:174
fair::mq::PluginServices::DeleteProperty
void DeleteProperty(const std::string &key)
Deletes a property with the given key from the config store.
Definition: PluginServices.h:178
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:231
fair::mq::PluginServices::TakeDeviceControl
auto TakeDeviceControl(const std::string &controller) -> void
Become device controller.
Definition: PluginServices.cxx:31
fair::mq::PluginServices::UnsubscribeFromDeviceStateChange
auto UnsubscribeFromDeviceStateChange(const std::string &subscriber) -> void
Unsubscribe from device state changes.
Definition: PluginServices.h:147
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:247
fair::mq::PluginServices::CycleLogVerbosityUp
auto CycleLogVerbosityUp() -> void
Increases logging verbosity, or sets it to lowest if it is already highest.
Definition: PluginServices.h:276
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:225
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:154
fair::mq::PluginServices::UnsubscribeFromPropertyChange
auto UnsubscribeFromPropertyChange(const std::string &subscriber) -> void
Unsubscribe from property updates of type T.
Definition: PluginServices.h:255
fair::mq::PluginServices::GetChannelInfo
auto GetChannelInfo() const -> std::unordered_map< std::string, int >
Retrieve current channel information.
Definition: PluginServices.h:235
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:200
fair::mq::PluginServices::StealDeviceControl
auto StealDeviceControl(const std::string &controller) -> void
Become device controller by force.
Definition: PluginServices.cxx:47
fair::mq::PluginServices::ReleaseDeviceControl
auto ReleaseDeviceControl(const std::string &controller) -> void
Release device controller role.
Definition: PluginServices.cxx:54
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:215
fair::mq::PluginServices::SetProperties
void SetProperties(const fair::mq::Properties &props)
Set multiple config properties.
Definition: PluginServices.h:166
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:262
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:184
fair::mq::Plugin
Base class for FairMQ plugins.
Definition: Plugin.h:43
fair::mq::PluginServices::ToStr
static auto ToStr(DeviceState state) -> std::string
Convert DeviceState to string.
Definition: PluginServices.h:86

privacy