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> 46 , fDeviceControllerMutex()
47 , fReleaseDeviceControlCondition()
53 LOG(debug) <<
"Shutting down Plugin Services";
76 enum class DeviceStateTransition : int
102 static auto ToDeviceStateTransition(
const std::string& transition) -> DeviceStateTransition {
return fkDeviceStateTransitionStrMap.at(transition); }
107 static auto ToStr(
DeviceState state) -> std::string {
return fkStrDeviceStateMap.at(state); }
112 static auto ToStr(DeviceStateTransition transition) -> std::string {
return fkStrDeviceStateTransitionMap.at(transition); }
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); }
154 auto
ChangeDeviceState(const std::
string& controller, const DeviceStateTransition next) ->
void;
164 fDevice.SubscribeToStateChange(subscriber, [&,callback](FairMQDevice::State newState){
165 callback(fkDeviceStateMap.at(newState));
176 auto PropertyExists(
const std::string& key)
const ->
bool {
return fConfig.Count(key) > 0; }
189 if ( (currentState == DeviceState::InitializingDevice)
190 || ((currentState == DeviceState::Idle) && (key ==
"channel-config")))
192 fConfig.SetValue(key, val);
197 tools::ToString(
"PluginServices::SetProperty is not supported in device state ", currentState,
". ",
198 "Supported state is ", DeviceState::InitializingDevice,
".")};
211 if (PropertyExists(key)) {
212 return fConfig.GetValue<T>(key);
223 if (PropertyExists(key)) {
224 return fConfig.GetStringValue(key);
229 auto GetChannelInfo() const -> std::unordered_map<std::
string,
int> {
return fConfig.GetChannelInfo(); }
233 auto GetPropertyKeys() const -> std::vector<std::
string> {
return fConfig.GetPropertyKeys(); }
243 fConfig.Subscribe<T>(subscriber, callback);
258 fConfig.SubscribeAsString(subscriber, callback);
265 auto CycleLogConsoleSeverityUp() ->
void { Logger::CycleConsoleSeverityUp(); }
266 auto CycleLogConsoleSeverityDown() ->
void { Logger::CycleConsoleSeverityDown(); }
267 auto CycleLogVerbosityUp() ->
void { Logger::CycleVerbosityUp(); }
268 auto CycleLogVerbosityDown() ->
void { Logger::CycleVerbosityDown(); }
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;
280 boost::optional<std::string> fDeviceController;
281 mutable std::mutex fDeviceControllerMutex;
282 std::condition_variable fReleaseDeviceControlCondition;
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
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