FairMQ  1.3.7
C++ Message Passing 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/Tools.h>
13 #include <FairMQDevice.h>
14 #include <options/FairMQProgOptions.h>
15 
16 #include <boost/optional.hpp>
17 #include <boost/optional/optional_io.hpp>
18 
19 #include <functional>
20 #include <string>
21 #include <unordered_map>
22 #include <mutex>
23 #include <condition_variable>
24 #include <stdexcept>
25 
26 namespace fair
27 {
28 namespace mq
29 {
30 
39 {
40  public:
41  PluginServices() = delete;
43  : fConfig(config)
44  , fDevice(device)
45  , fDeviceController()
46  , fDeviceControllerMutex()
47  , fReleaseDeviceControlCondition()
48  {
49  }
50 
52  {
53  LOG(debug) << "Shutting down Plugin Services";
54  }
55 
56  PluginServices(const PluginServices&) = delete;
57  PluginServices operator=(const PluginServices&) = delete;
58 
60  enum class DeviceState : int
61  {
62  Ok,
63  Error,
64  Idle,
65  InitializingDevice,
66  DeviceReady,
67  InitializingTask,
68  Ready,
69  Running,
70  Paused,
71  ResettingTask,
72  ResettingDevice,
73  Exiting
74  };
75 
76  enum class DeviceStateTransition : int // transition event between DeviceStates
77  {
78  InitDevice,
79  InitTask,
80  Run,
81  Pause,
82  Resume,
83  Stop,
84  ResetTask,
85  ResetDevice,
86  End,
87  ErrorFound
88  };
89 
90  // Control API
91 
96  static auto ToDeviceState(const std::string& state) -> DeviceState { return fkDeviceStateStrMap.at(state); }
97 
102  static auto ToDeviceStateTransition(const std::string& transition) -> DeviceStateTransition { return fkDeviceStateTransitionStrMap.at(transition); }
103 
107  static auto ToStr(DeviceState state) -> std::string { return fkStrDeviceStateMap.at(state); }
108 
112  static auto ToStr(DeviceStateTransition transition) -> std::string { return fkStrDeviceStateTransitionMap.at(transition); }
113 
114  friend auto operator<<(std::ostream& os, const DeviceState& state) -> std::ostream& { return os << ToStr(state); }
115  friend auto operator<<(std::ostream& os, const DeviceStateTransition& transition) -> std::ostream& { return os << ToStr(transition); }
116 
118  auto GetCurrentDeviceState() const -> DeviceState { return fkDeviceStateMap.at(static_cast<FairMQDevice::State>(fDevice.GetCurrentState())); }
119 
125  auto TakeDeviceControl(const std::string& controller) -> void;
126  struct DeviceControlError : std::runtime_error { using std::runtime_error::runtime_error; };
127 
133  auto StealDeviceControl(const std::string& controller) -> void;
134 
138  auto ReleaseDeviceControl(const std::string& controller) -> void;
139 
141  auto GetDeviceController() const -> boost::optional<std::string>;
142 
144  auto WaitForReleaseDeviceControl() -> void;
145 
154  auto ChangeDeviceState(const std::string& controller, const DeviceStateTransition next) -> void;
155 
162  auto SubscribeToDeviceStateChange(const std::string& subscriber, std::function<void(DeviceState /*newState*/)> callback) -> void
163  {
164  fDevice.SubscribeToStateChange(subscriber, [&,callback](FairMQDevice::State newState){
165  callback(fkDeviceStateMap.at(newState));
166  });
167  }
168 
171  auto UnsubscribeFromDeviceStateChange(const std::string& subscriber) -> void { fDevice.UnsubscribeFromStateChange(subscriber); }
172 
173  // Config API
174  struct PropertyNotFoundError : std::runtime_error { using std::runtime_error::runtime_error; };
175 
176  auto PropertyExists(const std::string& key) const -> bool { return fConfig.Count(key) > 0; }
177 
185  template<typename T>
186  auto SetProperty(const std::string& key, T val) -> void
187  {
188  auto currentState = GetCurrentDeviceState();
189  if ( (currentState == DeviceState::InitializingDevice)
190  || ((currentState == DeviceState::Idle) && (key == "channel-config")))
191  {
192  fConfig.SetValue(key, val);
193  }
194  else
195  {
196  throw InvalidStateError{
197  tools::ToString("PluginServices::SetProperty is not supported in device state ", currentState, ". ",
198  "Supported state is ", DeviceState::InitializingDevice, ".")};
199  }
200  }
201  struct InvalidStateError : std::runtime_error { using std::runtime_error::runtime_error; };
202 
209  template<typename T>
210  auto GetProperty(const std::string& key) const -> T {
211  if (PropertyExists(key)) {
212  return fConfig.GetValue<T>(key);
213  }
214  throw PropertyNotFoundError(fair::mq::tools::ToString("Config has no key: ", key));
215  }
216 
222  auto GetPropertyAsString(const std::string& key) const -> std::string {
223  if (PropertyExists(key)) {
224  return fConfig.GetStringValue(key);
225  }
226  throw PropertyNotFoundError(fair::mq::tools::ToString("Config has no key: ", key));
227  }
228 
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 
265  auto CycleLogConsoleSeverityUp() -> void { Logger::CycleConsoleSeverityUp(); }
266  auto CycleLogConsoleSeverityDown() -> void { Logger::CycleConsoleSeverityDown(); }
267  auto CycleLogVerbosityUp() -> void { Logger::CycleVerbosityUp(); }
268  auto CycleLogVerbosityDown() -> void { Logger::CycleVerbosityDown(); }
269 
270  static const std::unordered_map<std::string, DeviceState> fkDeviceStateStrMap;
271  static const std::unordered_map<DeviceState, std::string, tools::HashEnum<DeviceState>> fkStrDeviceStateMap;
272  static const std::unordered_map<std::string, DeviceStateTransition> fkDeviceStateTransitionStrMap;
273  static const std::unordered_map<DeviceStateTransition, std::string, tools::HashEnum<DeviceStateTransition>> fkStrDeviceStateTransitionMap;
274  static const std::unordered_map<FairMQDevice::State, DeviceState, tools::HashEnum<FairMQDevice::State>> fkDeviceStateMap;
275  static const std::unordered_map<DeviceStateTransition, FairMQDevice::Event, tools::HashEnum<DeviceStateTransition>> fkDeviceStateTransitionMap;
276 
277  private:
278  FairMQProgOptions& fConfig;
279  FairMQDevice& fDevice;
280  boost::optional<std::string> fDeviceController;
281  mutable std::mutex fDeviceControllerMutex;
282  std::condition_variable fReleaseDeviceControlCondition;
283 }; /* class PluginServices */
284 
285 } /* namespace mq */
286 } /* namespace fair */
287 
288 #endif /* FAIR_MQ_PLUGINSERVICES_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
static auto ToStr(DeviceStateTransition transition) -> std::string
Convert DeviceStateTransition to string.
Definition: PluginServices.h:112
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
Definition: FairMQStateMachine.cxx:40
auto GetCurrentDeviceState() const -> DeviceState
Definition: PluginServices.h:118
Definition: PluginServices.h:126
auto GetPropertyAsString(const std::string &key) const -> std::string
Read config property as string.
Definition: PluginServices.h:222
Definition: FairMQProgOptions.h:37
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
Definition: PluginServices.h:174
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
Definition: PluginServices.h:201
static auto ToDeviceStateTransition(const std::string &transition) -> DeviceStateTransition
Convert string to DeviceStateTransition.
Definition: PluginServices.h:102
Definition: FairMQDevice.h:46
auto WaitForReleaseDeviceControl() -> void
Block until control is released.
Definition: PluginServices.cxx:165
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
auto GetDeviceController() const -> boost::optional< std::string >
Get current device controller.
Definition: PluginServices.cxx:158

privacy