9 #ifndef FAIR_MQ_PLUGINSERVICES_H 10 #define FAIR_MQ_PLUGINSERVICES_H 12 #include <fairmq/Tools.h> 13 #include <FairMQDevice.h> 14 #include <options/FairMQProgOptions.h> 16 #include <boost/optional.hpp> 17 #include <boost/optional/optional_io.hpp> 21 #include <unordered_map> 23 #include <condition_variable> 45 , fDeviceControllerMutex()
46 , fReleaseDeviceControlCondition()
70 enum class DeviceStateTransition : int
96 static auto ToDeviceStateTransition(
const std::string& transition) -> DeviceStateTransition {
return fkDeviceStateTransitionStrMap.at(transition); }
101 static auto ToStr(
DeviceState state) -> std::string {
return fkStrDeviceStateMap.at(state); }
106 static auto ToStr(DeviceStateTransition transition) -> std::string {
return fkStrDeviceStateTransitionMap.at(transition); }
108 friend auto operator<<(std::ostream& os,
const DeviceState& state) -> std::ostream& {
return os <<
ToStr(state); }
109 friend auto operator<<(std::ostream& os,
const DeviceStateTransition& transition) -> std::ostream& {
return os <<
ToStr(transition); }
148 auto ChangeDeviceState(
const std::string& controller,
const DeviceStateTransition next) -> void;
158 fDevice->SubscribeToStateChange(subscriber, [&,callback](FairMQDevice::State newState){
159 callback(fkDeviceStateMap.at(newState));
170 auto PropertyExists(
const std::string& key)
const ->
bool {
return fConfig->Count(key) > 0; }
183 if (currentState == DeviceState::InitializingDevice)
185 fConfig->SetValue(key, val);
189 throw InvalidStateError{tools::ToString(
"PluginServices::SetProperty is not supported in device state ", currentState,
". Supported state is ", DeviceState::InitializingDevice,
".")};
202 if (PropertyExists(key)) {
203 return fConfig->GetValue<T>(key);
214 if (PropertyExists(key)) {
215 return fConfig->GetStringValue(key);
220 auto GetChannelInfo()
const -> std::unordered_map<std::string, int> {
return fConfig->GetChannelInfo(); }
234 fConfig->Subscribe<T>(subscriber, callback);
249 fConfig->SubscribeAsString(subscriber, callback);
257 static const std::unordered_map<std::string, DeviceState> fkDeviceStateStrMap;
258 static const std::unordered_map<DeviceState, std::string, tools::HashEnum<DeviceState>> fkStrDeviceStateMap;
259 static const std::unordered_map<std::string, DeviceStateTransition> fkDeviceStateTransitionStrMap;
260 static const std::unordered_map<DeviceStateTransition, std::string, tools::HashEnum<DeviceStateTransition>> fkStrDeviceStateTransitionMap;
261 static const std::unordered_map<FairMQDevice::State, DeviceState, tools::HashEnum<FairMQDevice::State>> fkDeviceStateMap;
262 static const std::unordered_map<DeviceStateTransition, FairMQDevice::Event, tools::HashEnum<DeviceStateTransition>> fkDeviceStateTransitionMap;
266 std::shared_ptr<FairMQDevice> fDevice;
267 boost::optional<std::string> fDeviceController;
268 mutable std::mutex fDeviceControllerMutex;
269 std::condition_variable fReleaseDeviceControlCondition;
Facilitates communication between devices and plugins.
Definition: PluginServices.h:37
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:240
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:232
static auto ToStr(DeviceStateTransition transition) -> std::string
Convert DeviceStateTransition to string.
Definition: PluginServices.h:106
auto UnsubscribeFromDeviceStateChange(const std::string &subscriber) -> void
Unsubscribe from device state changes.
Definition: PluginServices.h:165
static auto ToStr(DeviceState state) -> std::string
Convert DeviceState to string.
Definition: PluginServices.h:101
Definition: Transports.h:53
auto GetCurrentDeviceState() const -> DeviceState
Definition: PluginServices.h:112
Definition: PluginServices.h:120
auto GetPropertyAsString(const std::string &key) const -> std::string
Read config property as string.
Definition: PluginServices.h:213
Definition: FairMQProgOptions.h:41
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:156
Definition: PluginServices.h:168
static auto ToDeviceState(const std::string &state) -> DeviceState
Convert string to DeviceState.
Definition: PluginServices.h:90
auto GetProperty(const std::string &key) const -> T
Read config property.
Definition: PluginServices.h:201
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:247
DeviceState
See https://github.com/FairRootGroup/FairRoot/blob/dev/fairmq/docs/Device.md#13-state-machine.
Definition: PluginServices.h:54
auto SetProperty(const std::string &key, T val) -> void
Set config property.
Definition: PluginServices.h:180
auto UnsubscribeFromPropertyChangeAsString(const std::string &subscriber) -> void
Unsubscribe from property updates that convert to string.
Definition: PluginServices.h:254
Definition: PluginServices.h:192
static auto ToDeviceStateTransition(const std::string &transition) -> DeviceStateTransition
Convert string to DeviceStateTransition.
Definition: PluginServices.h:96
auto WaitForReleaseDeviceControl() -> void
Block until control is released.
Definition: PluginServices.cxx:165
Definition: DeviceRunner.h:23
auto GetPropertyKeys() const -> std::vector< std::string >
Discover the list of property keys.
Definition: PluginServices.h:224
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