FairMQ  1.4.14
C++ Message Queuing Library and Framework
PluginServices.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_PLUGINSERVICES_H
10 #define FAIR_MQ_PLUGINSERVICES_H
11 
12 #include <fairmq/States.h>
13 #include <FairMQDevice.h>
14 #include <fairmq/ProgOptions.h>
15 #include <fairmq/Properties.h>
16 
17 #include <boost/optional.hpp>
18 #include <boost/optional/optional_io.hpp>
19 
20 #include <functional>
21 #include <string>
22 #include <unordered_map>
23 #include <mutex>
24 #include <map>
25 #include <condition_variable>
26 #include <stdexcept>
27 
28 namespace fair
29 {
30 namespace mq
31 {
32 
41 {
42  public:
43  PluginServices() = delete;
44  PluginServices(ProgOptions& config, FairMQDevice& device)
45  : fConfig(config)
46  , fDevice(device)
47  , fDeviceController()
48  , fDeviceControllerMutex()
49  , fReleaseDeviceControlCondition()
50  {
51  }
52 
54  {
55  LOG(debug) << "Shutting down Plugin Services";
56  }
57 
58  PluginServices(const PluginServices&) = delete;
59  PluginServices operator=(const PluginServices&) = delete;
60 
61  using DeviceState = fair::mq::State;
62  using DeviceStateTransition = fair::mq::Transition;
63 
64  // Control API
65 
70  static auto ToDeviceState(const std::string& state) -> DeviceState { return GetState(state); }
71 
76  static auto ToDeviceStateTransition(const std::string& transition) -> DeviceStateTransition { return GetTransition(transition); }
77 
81  static auto ToStr(DeviceState state) -> std::string { return GetStateName(state); }
82 
86  static auto ToStr(DeviceStateTransition transition) -> std::string { return GetTransitionName(transition); }
87 
89  auto GetCurrentDeviceState() const -> DeviceState { return fDevice.GetCurrentState(); }
90 
96  auto TakeDeviceControl(const std::string& controller) -> void;
97  struct DeviceControlError : std::runtime_error { using std::runtime_error::runtime_error; };
98 
104  auto StealDeviceControl(const std::string& controller) -> void;
105 
109  auto ReleaseDeviceControl(const std::string& controller) -> void;
110 
112  auto GetDeviceController() const -> boost::optional<std::string>;
113 
115  auto WaitForReleaseDeviceControl() -> void;
116 
125  auto ChangeDeviceState(const std::string& controller, const DeviceStateTransition next) -> bool;
126 
133  auto SubscribeToDeviceStateChange(const std::string& subscriber, std::function<void(DeviceState /*newState*/)> callback) -> void
134  {
135  fDevice.SubscribeToStateChange(subscriber, [&,callback](fair::mq::State newState){
136  callback(newState);
137  });
138  }
139 
142  auto UnsubscribeFromDeviceStateChange(const std::string& subscriber) -> void { fDevice.UnsubscribeFromStateChange(subscriber); }
143 
144  // Config API
145 
149  auto PropertyExists(const std::string& key) const -> bool { return fConfig.Count(key) > 0; }
150 
157  template<typename T>
158  auto SetProperty(const std::string& key, T val) -> void { fConfig.SetProperty(key, val); }
161  void SetProperties(const fair::mq::Properties& props) { fConfig.SetProperties(props); }
165  template<typename T>
166  bool UpdateProperty(const std::string& key, T val) { return fConfig.UpdateProperty(key, val); }
169  bool UpdateProperties(const fair::mq::Properties& input) { return fConfig.UpdateProperties(input); }
170 
173  void DeleteProperty(const std::string& key) { fConfig.DeleteProperty(key); }
174 
178  template<typename T>
179  auto GetProperty(const std::string& key) const -> T { return fConfig.GetProperty<T>(key); }
180 
185  template<typename T>
186  T GetProperty(const std::string& key, const T& ifNotFound) const { return fConfig.GetProperty(key, ifNotFound); }
187 
195  auto GetPropertyAsString(const std::string& key) const -> std::string { return fConfig.GetPropertyAsString(key); }
196 
205  auto GetPropertyAsString(const std::string& key, const std::string& ifNotFound) const -> std::string { return fConfig.GetPropertyAsString(key, ifNotFound); }
206 
210  fair::mq::Properties GetProperties(const std::string& q) const { return fConfig.GetProperties(q); }
216  fair::mq::Properties GetPropertiesStartingWith(const std::string& q) const { return fConfig.GetPropertiesStartingWith(q); }
220  std::map<std::string, std::string> GetPropertiesAsString(const std::string& q) const { return fConfig.GetPropertiesAsString(q); }
226  std::map<std::string, std::string> GetPropertiesAsStringStartingWith(const std::string& q) const { return fConfig.GetPropertiesAsStringStartingWith(q); }
227 
230  auto GetChannelInfo() const -> std::unordered_map<std::string, int> { return fConfig.GetChannelInfo(); }
231 
234  auto GetPropertyKeys() const -> std::vector<std::string> { return fConfig.GetPropertyKeys(); }
235 
241  template<typename T>
242  auto SubscribeToPropertyChange(const std::string& subscriber, std::function<void(const std::string& key, T)> callback) const -> void
243  {
244  fConfig.Subscribe<T>(subscriber, callback);
245  }
246 
249  template<typename T>
250  auto UnsubscribeFromPropertyChange(const std::string& subscriber) -> void { fConfig.Unsubscribe<T>(subscriber); }
251 
257  auto SubscribeToPropertyChangeAsString(const std::string& subscriber, std::function<void(const std::string& key, std::string)> callback) const -> void
258  {
259  fConfig.SubscribeAsString(subscriber, callback);
260  }
261 
264  auto UnsubscribeFromPropertyChangeAsString(const std::string& subscriber) -> void { fConfig.UnsubscribeAsString(subscriber); }
265 
267  auto CycleLogConsoleSeverityUp() -> void { Logger::CycleConsoleSeverityUp(); }
269  auto CycleLogConsoleSeverityDown() -> void { Logger::CycleConsoleSeverityDown(); }
271  auto CycleLogVerbosityUp() -> void { Logger::CycleVerbosityUp(); }
273  auto CycleLogVerbosityDown() -> void { Logger::CycleVerbosityDown(); }
274 
275  private:
276  fair::mq::ProgOptions& fConfig;
277  FairMQDevice& fDevice;
278  boost::optional<std::string> fDeviceController;
279  mutable std::mutex fDeviceControllerMutex;
280  std::condition_variable fReleaseDeviceControlCondition;
281 }; /* class PluginServices */
282 
283 } /* namespace mq */
284 } /* namespace fair */
285 
286 #endif /* FAIR_MQ_PLUGINSERVICES_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
static auto ToStr(DeviceStateTransition transition) -> std::string
Convert DeviceStateTransition to string.
Definition: PluginServices.h:86
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
Definition: Error.h:56
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
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
auto CycleLogVerbosityDown() -> void
Decreases logging verbosity, or sets it to highest if it is already lowest.
Definition: PluginServices.h:273
Definition: PluginServices.h:97
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
auto ReleaseDeviceControl(const std::string &controller) -> void
Release device controller role.
Definition: PluginServices.cxx:54
Definition: ProgOptions.h:36
auto SubscribeToDeviceStateChange(const std::string &subscriber, std::function< void(DeviceState)> callback) -> void
Subscribe with a callback to device state changes.
Definition: PluginServices.h:133
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
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
Definition: FairMQDevice.h:53
auto ChangeDeviceState(const std::string &controller, const DeviceStateTransition next) -> bool
Request a device state transition.
Definition: PluginServices.cxx:15
auto WaitForReleaseDeviceControl() -> void
Block until control is released.
Definition: PluginServices.cxx:77
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
auto GetDeviceController() const -> boost::optional< std::string >
Get current device controller.
Definition: PluginServices.cxx:70

privacy