Skip to content

Source · fairmq/

PluginServices.cxxsource

Described in Plugins and control, custom-controller: control from code, Control a device from code, Write a plugin, Logging.

fairmq/PluginServices.cxx91 lines · C++GitHub ↗
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#include <fairlogger/Logger.h>10#include <fairmq/PluginServices.h>11#include <fairmq/tools/Strings.h>12 13using namespace fair::mq;14using namespace std;15 16auto PluginServices::ChangeDeviceState(const string& controller, const DeviceStateTransition next) -> bool17{18    lock_guard<mutex> lock{fDeviceControllerMutex};19 20    if (!fDeviceController) {21        fDeviceController = controller;22    }23 24    if (fDeviceController == controller) {25        return fDevice.ChangeState(next);26    } else {27        throw DeviceControlError{tools::ToString(28            "Plugin '", controller, "' is not allowed to change device states. ",29            "Currently, plugin '", *fDeviceController, "' has taken control."30        )};31    }32}33 34auto PluginServices::TakeDeviceControl(const string& controller) -> void35{36    lock_guard<mutex> lock{fDeviceControllerMutex};37 38    if (!fDeviceController) {39        fDeviceController = controller;40        LOG(trace) << "Plugin '" << controller << "' took over control.";41    } else if (fDeviceController == controller) {42        // nothing to do43        LOG(trace) << "Plugin '" << controller << "' is already in control.";44    } else {45        throw DeviceControlError{tools::ToString(46            "Plugin '", controller, "' is not allowed to take over control. ",47            "Currently, plugin '", *fDeviceController, "' has taken control."48        )};49    }50}51 52auto PluginServices::StealDeviceControl(const string& controller) -> void53{54    lock_guard<mutex> lock{fDeviceControllerMutex};55 56    fDeviceController = controller;57    LOG(trace) << "Plugin '" << controller << "' steals control!";58}59 60auto PluginServices::ReleaseDeviceControl(const string& controller) -> void61{62    {63        lock_guard<mutex> lock{fDeviceControllerMutex};64 65        if (fDeviceController == controller) {66            fDeviceController = boost::none;67            LOG(trace) << "Plugin '" << controller << "' releases control.";68        } else {69            LOG(debug) << "Plugin '" << controller << "' cannot release control "70                       << "because it has no control.";71        }72    }73 74    fReleaseDeviceControlCondition.notify_one();75}76 77auto PluginServices::GetDeviceController() const -> boost::optional<string>78{79    lock_guard<mutex> lock{fDeviceControllerMutex};80 81    return fDeviceController;82}83 84auto PluginServices::WaitForReleaseDeviceControl() -> void85{86    unique_lock<mutex> lock{fDeviceControllerMutex};87 88    while (fDeviceController) {89        fReleaseDeviceControlCondition.wait(lock);90    }91}