FairMQ  1.4.33
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 <condition_variable>
21 #include <functional>
22 #include <map>
23 #include <mutex>
24 #include <stdexcept>
25 #include <string>
26 #include <unordered_map>
27 #include <vector>
28 
29 namespace fair::mq
30 {
31 
39 class PluginServices
40 {
41  public:
42  PluginServices() = delete;
43  PluginServices(ProgOptions& config, FairMQDevice& device)
44  : fConfig(config)
45  , fDevice(device)
46  , fDeviceController()
47  , fDeviceControllerMutex()
48  , fReleaseDeviceControlCondition()
49  {
50  }
51 
53  {
54  LOG(debug) << "Shutting down Plugin Services";
55  }
56 
57  PluginServices(const PluginServices&) = delete;
58  PluginServices operator=(const PluginServices&) = delete;
59 
60  using DeviceState = fair::mq::State;
61  using DeviceStateTransition = fair::mq::Transition;
62 
63  // Control API
64 
69  static auto ToDeviceState(const std::string& state) -> DeviceState { return GetState(state); }
70 
75  static auto ToDeviceStateTransition(const std::string& transition) -> DeviceStateTransition { return GetTransition(transition); }
76 
80  static auto ToStr(DeviceState state) -> std::string { return GetStateName(state); }
81 
85  static auto ToStr(DeviceStateTransition transition) -> std::string { return GetTransitionName(transition); }
86 
88  auto GetCurrentDeviceState() const -> DeviceState { return fDevice.GetCurrentState(); }
89 
95  auto TakeDeviceControl(const std::string& controller) -> void;
96  struct DeviceControlError : std::runtime_error { using std::runtime_error::runtime_error; };
97 
103  auto StealDeviceControl(const std::string& controller) -> void;
104 
108  auto ReleaseDeviceControl(const std::string& controller) -> void;
109 
111  auto GetDeviceController() const -> boost::optional<std::string>;
112 
114  auto WaitForReleaseDeviceControl() -> void;
115 
124  auto ChangeDeviceState(const std::string& controller, const DeviceStateTransition next) -> bool;
125 
132  auto SubscribeToDeviceStateChange(const std::string& subscriber, std::function<void(DeviceState /*newState*/)> callback) -> void
133  {
134  fDevice.SubscribeToStateChange(subscriber, [&,callback](fair::mq::State newState){
135  callback(newState);
136  });
137  }
138 
141  auto UnsubscribeFromDeviceStateChange(const std::string& subscriber) -> void { fDevice.UnsubscribeFromStateChange(subscriber); }
142 
143  // Config API
144 
148  auto PropertyExists(const std::string& key) const -> bool { return fConfig.Count(key) > 0; }
149 
156  template<typename T>
157  auto SetProperty(const std::string& key, T val) -> void { fConfig.SetProperty(key, val); }
160  void SetProperties(const fair::mq::Properties& props) { fConfig.SetProperties(props); }
164  template<typename T>
165  bool UpdateProperty(const std::string& key, T val) { return fConfig.UpdateProperty(key, val); }
168  bool UpdateProperties(const fair::mq::Properties& input) { return fConfig.UpdateProperties(input); }
169 
172  void DeleteProperty(const std::string& key) { fConfig.DeleteProperty(key); }
173 
177  template<typename T>
178  auto GetProperty(const std::string& key) const -> T { return fConfig.GetProperty<T>(key); }
179 
184  template<typename T>
185  T GetProperty(const std::string& key, const T& ifNotFound) const { return fConfig.GetProperty(key, ifNotFound); }
186 
194  auto GetPropertyAsString(const std::string& key) const -> std::string { return fConfig.GetPropertyAsString(key); }
195 
204  auto GetPropertyAsString(const std::string& key, const std::string& ifNotFound) const -> std::string { return fConfig.GetPropertyAsString(key, ifNotFound); }
205 
209  fair::mq::Properties GetProperties(const std::string& q) const { return fConfig.GetProperties(q); }
215  fair::mq::Properties GetPropertiesStartingWith(const std::string& q) const { return fConfig.GetPropertiesStartingWith(q); }
219  std::map<std::string, std::string> GetPropertiesAsString(const std::string& q) const { return fConfig.GetPropertiesAsString(q); }
225  std::map<std::string, std::string> GetPropertiesAsStringStartingWith(const std::string& q) const { return fConfig.GetPropertiesAsStringStartingWith(q); }
226 
229  auto GetChannelInfo() const -> std::unordered_map<std::string, int> { return fConfig.GetChannelInfo(); }
230 
233  auto GetPropertyKeys() const -> std::vector<std::string> { return fConfig.GetPropertyKeys(); }
234 
240  template<typename T>
241  auto SubscribeToPropertyChange(const std::string& subscriber, std::function<void(const std::string& key, T)> callback) const -> void
242  {
243  fConfig.Subscribe<T>(subscriber, callback);
244  }
245 
248  template<typename T>
249  auto UnsubscribeFromPropertyChange(const std::string& subscriber) -> void { fConfig.Unsubscribe<T>(subscriber); }
250 
256  auto SubscribeToPropertyChangeAsString(const std::string& subscriber, std::function<void(const std::string& key, std::string)> callback) const -> void
257  {
258  fConfig.SubscribeAsString(subscriber, callback);
259  }
260 
263  auto UnsubscribeFromPropertyChangeAsString(const std::string& subscriber) -> void { fConfig.UnsubscribeAsString(subscriber); }
264 
266  auto CycleLogConsoleSeverityUp() -> void { Logger::CycleConsoleSeverityUp(); }
268  auto CycleLogConsoleSeverityDown() -> void { Logger::CycleConsoleSeverityDown(); }
270  auto CycleLogVerbosityUp() -> void { Logger::CycleVerbosityUp(); }
272  auto CycleLogVerbosityDown() -> void { Logger::CycleVerbosityDown(); }
273 
274  private:
275  fair::mq::ProgOptions& fConfig;
276  FairMQDevice& fDevice;
277  boost::optional<std::string> fDeviceController;
278  mutable std::mutex fDeviceControllerMutex;
279  std::condition_variable fReleaseDeviceControlCondition;
280 }; /* class PluginServices */
281 
282 } // namespace fair::mq
283 
284 #endif /* FAIR_MQ_PLUGINSERVICES_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::ProgOptions
Definition: ProgOptions.h:41
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
FairMQDevice::GetCurrentState
fair::mq::State GetCurrentState() const
Returns the current state.
Definition: FairMQDevice.h:473
fair::mq::ProgOptions::GetPropertyKeys
std::vector< std::string > GetPropertyKeys() const
Discover the list of property keys.
Definition: ProgOptions.cxx:189
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::ProgOptions::GetPropertiesStartingWith
fair::mq::Properties GetPropertiesStartingWith(const std::string &q) const
Read several config properties whose keys start with the provided string.
Definition: ProgOptions.cxx:256
fair::mq::ProgOptions::UpdateProperty
bool UpdateProperty(const std::string &key, T val)
Updates an existing config property (or fails if it doesn't exist)
Definition: ProgOptions.h:152
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::ProgOptions::Unsubscribe
void Unsubscribe(const std::string &subscriber) const
Unsubscribe from property updates of type T.
Definition: ProgOptions.h:202
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::GetDeviceController
auto GetDeviceController() const -> boost::optional< std::string >
Get current device controller.
Definition: PluginServices.cxx:70
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::ProgOptions::GetProperty
T GetProperty(const std::string &key) const
Read config property, throw if no property with this key exists.
Definition: ProgOptions.h:69
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::ProgOptions::GetPropertyAsString
std::string GetPropertyAsString(const std::string &key) const
Read config property as string, throw if no property with this key exists.
fair::mq::PluginServices::TakeDeviceControl
auto TakeDeviceControl(const std::string &controller) -> void
Become device controller.
Definition: PluginServices.cxx:31
fair::mq::ProgOptions::SetProperty
void SetProperty(const std::string &key, T val)
Set config property.
Definition: ProgOptions.h:136
fair::mq::ProgOptions::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: ProgOptions.cxx:271
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::WaitForReleaseDeviceControl
auto WaitForReleaseDeviceControl() -> void
Block until control is released.
Definition: PluginServices.cxx:77
fair::mq::ProgOptions::SubscribeAsString
void SubscribeAsString(const std::string &subscriber, std::function< void(typename fair::mq::PropertyChange::KeyType, std::string)> func) const
Subscribe to property updates, with values converted to string.
Definition: ProgOptions.h:213
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
FairMQDevice::SubscribeToStateChange
void SubscribeToStateChange(const std::string &key, std::function< void(const fair::mq::State)> callback)
Subscribe with a callback to state changes.
Definition: FairMQDevice.h:454
fair::mq::ProgOptions::UpdateProperties
bool UpdateProperties(const fair::mq::Properties &input)
Updates multiple existing config properties (or fails of any of then do not exist,...
Definition: ProgOptions.cxx:323
fair::mq::PluginServices::UnsubscribeFromPropertyChange
auto UnsubscribeFromPropertyChange(const std::string &subscriber) -> void
Unsubscribe from property updates of type T.
Definition: PluginServices.h:255
fair::mq::ProgOptions::Subscribe
void Subscribe(const std::string &subscriber, std::function< void(typename fair::mq::PropertyChange::KeyType, T)> func) const
Subscribe to property updates of type T.
Definition: ProgOptions.h:191
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::ProgOptions::UnsubscribeAsString
void UnsubscribeAsString(const std::string &subscriber) const
Unsubscribe from property updates that convert to string.
Definition: ProgOptions.h:221
fair::mq::PluginServices::StealDeviceControl
auto StealDeviceControl(const std::string &controller) -> void
Become device controller by force.
Definition: PluginServices.cxx:47
fair::mq::ProgOptions::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: ProgOptions.cxx:291
fair::mq::ProgOptions::SetProperties
void SetProperties(const fair::mq::Properties &input)
Set multiple config properties.
Definition: ProgOptions.cxx:306
fair::mq::ProgOptions::GetProperties
fair::mq::Properties GetProperties(const std::string &q) const
Read several config properties whose keys match the provided regular expression.
Definition: ProgOptions.cxx:236
fair::mq::ProgOptions::Count
int Count(const std::string &key) const
Checks a property with the given key exist in the configuration.
Definition: ProgOptions.cxx:155
fair::mq::ProgOptions::DeleteProperty
void DeleteProperty(const std::string &key)
Deletes a property with the given key from the config store.
Definition: ProgOptions.cxx:349
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
FairMQDevice
Definition: FairMQDevice.h:50
FairMQDevice::UnsubscribeFromStateChange
void UnsubscribeFromStateChange(const std::string &key)
Unsubscribe from state changes.
Definition: FairMQDevice.h:457
fair::mq::ProgOptions::GetChannelInfo
std::unordered_map< std::string, int > GetChannelInfo() const
Retrieve current channel information.
Definition: ProgOptions.cxx:161
fair::mq::PluginServices::ToStr
static auto ToStr(DeviceState state) -> std::string
Convert DeviceState to string.
Definition: PluginServices.h:86

privacy