mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-13 08:41:16 +00:00
FairMQ: Implement DeviceRunner
This commit is contained in:
parent
7f23a70670
commit
4ae2e025c9
|
@ -63,6 +63,7 @@ set(FAIRMQ_DEPRECATED_HEADER_FILES
|
||||||
)
|
)
|
||||||
set(FAIRMQ_HEADER_FILES
|
set(FAIRMQ_HEADER_FILES
|
||||||
${FAIRMQ_DEPRECATED_HEADER_FILES}
|
${FAIRMQ_DEPRECATED_HEADER_FILES}
|
||||||
|
DeviceRunner.h
|
||||||
EventManager.h
|
EventManager.h
|
||||||
FairMQChannel.h
|
FairMQChannel.h
|
||||||
FairMQDevice.h
|
FairMQDevice.h
|
||||||
|
@ -83,7 +84,6 @@ set(FAIRMQ_HEADER_FILES
|
||||||
devices/FairMQSink.h
|
devices/FairMQSink.h
|
||||||
devices/FairMQSplitter.h
|
devices/FairMQSplitter.h
|
||||||
logger/logger.h
|
logger/logger.h
|
||||||
options/FairMQEventManager.h
|
|
||||||
options/FairMQParser.h
|
options/FairMQParser.h
|
||||||
options/FairMQProgOptions.h
|
options/FairMQProgOptions.h
|
||||||
options/FairMQSuboptParser.h
|
options/FairMQSuboptParser.h
|
||||||
|
@ -133,6 +133,7 @@ endif()
|
||||||
|
|
||||||
|
|
||||||
set(FAIRMQ_SOURCE_FILES
|
set(FAIRMQ_SOURCE_FILES
|
||||||
|
DeviceRunner.cxx
|
||||||
FairMQChannel.cxx
|
FairMQChannel.cxx
|
||||||
FairMQDevice.cxx
|
FairMQDevice.cxx
|
||||||
FairMQLogger.cxx
|
FairMQLogger.cxx
|
||||||
|
|
107
fairmq/DeviceRunner.cxx
Normal file
107
fairmq/DeviceRunner.cxx
Normal file
|
@ -0,0 +1,107 @@
|
||||||
|
/********************************************************************************
|
||||||
|
* Copyright (C) 2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||||
|
* *
|
||||||
|
* This software is distributed under the terms of the *
|
||||||
|
* GNU Lesser General Public Licence (LGPL) version 3, *
|
||||||
|
* copied verbatim in the file "LICENSE" *
|
||||||
|
********************************************************************************/
|
||||||
|
|
||||||
|
#include "DeviceRunner.h"
|
||||||
|
#include <fairmq/Tools.h>
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
|
using namespace fair::mq;
|
||||||
|
|
||||||
|
DeviceRunner::DeviceRunner(int argc, char const* argv[])
|
||||||
|
: fRawCmdLineArgs{tools::ToStrVector(argc, argv, false)}
|
||||||
|
, fPluginManager{PluginManager::MakeFromCommandLineOptions(fRawCmdLineArgs)}
|
||||||
|
, fDevice{nullptr}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
auto DeviceRunner::Run() -> int
|
||||||
|
{
|
||||||
|
////// CALL HOOK ///////
|
||||||
|
fEvents.Emit<hooks::LoadPlugins>(*this);
|
||||||
|
////////////////////////
|
||||||
|
|
||||||
|
// Load builtin plugins last
|
||||||
|
fPluginManager->LoadPlugin("s:control");
|
||||||
|
|
||||||
|
////// CALL HOOK ///////
|
||||||
|
fEvents.Emit<hooks::SetCustomCmdLineOptions>(*this);
|
||||||
|
////////////////////////
|
||||||
|
|
||||||
|
fPluginManager->ForEachPluginProgOptions([&](boost::program_options::options_description options){
|
||||||
|
fConfig.AddToCmdLineOptions(options);
|
||||||
|
});
|
||||||
|
fConfig.AddToCmdLineOptions(fPluginManager->ProgramOptions());
|
||||||
|
|
||||||
|
////// CALL HOOK ///////
|
||||||
|
fEvents.Emit<hooks::ModifyRawCmdLineArgs>(*this);
|
||||||
|
////////////////////////
|
||||||
|
|
||||||
|
fConfig.ParseAll(fRawCmdLineArgs, true);
|
||||||
|
|
||||||
|
////// CALL HOOK ///////
|
||||||
|
fEvents.Emit<hooks::InstantiateDevice>(*this);
|
||||||
|
////////////////////////
|
||||||
|
|
||||||
|
if (!fDevice)
|
||||||
|
{
|
||||||
|
LOG(ERROR) << "getDevice(): no valid device provided. Exiting.";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle --print-channels
|
||||||
|
fDevice->RegisterChannelEndpoints();
|
||||||
|
if (fConfig.Count("print-channels"))
|
||||||
|
{
|
||||||
|
fDevice->PrintRegisteredChannels();
|
||||||
|
fDevice->ChangeState(FairMQDevice::END);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle --version
|
||||||
|
if (fConfig.Count("version"))
|
||||||
|
{
|
||||||
|
std::cout << "User device version: " << fDevice->GetVersion() << std::endl;
|
||||||
|
std::cout << "FAIRMQ_INTERFACE_VERSION: " << FAIRMQ_INTERFACE_VERSION << std::endl;
|
||||||
|
fDevice->ChangeState(FairMQDevice::END);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG(DEBUG) << "PID: " << getpid();
|
||||||
|
|
||||||
|
// Configure device
|
||||||
|
fDevice->SetConfig(fConfig);
|
||||||
|
|
||||||
|
// Initialize plugin services
|
||||||
|
fPluginManager->EmplacePluginServices(&fConfig, fDevice);
|
||||||
|
|
||||||
|
// Instantiate and run plugins
|
||||||
|
fPluginManager->InstantiatePlugins();
|
||||||
|
|
||||||
|
// Wait for control plugin to release device control
|
||||||
|
fPluginManager->WaitForPluginsToReleaseDeviceControl();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto DeviceRunner::RunWithExceptionHandlers() -> int
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return Run();
|
||||||
|
}
|
||||||
|
catch (std::exception& e)
|
||||||
|
{
|
||||||
|
LOG(ERROR) << "Unhandled exception reached the top of main: " << e.what() << ", application will now exit";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
LOG(ERROR) << "Non-exception instance being thrown. Please make sure you use std::runtime_exception() instead. Application will now exit.";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
84
fairmq/DeviceRunner.h
Normal file
84
fairmq/DeviceRunner.h
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
/********************************************************************************
|
||||||
|
* Copyright (C) 2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
||||||
|
* *
|
||||||
|
* This software is distributed under the terms of the *
|
||||||
|
* GNU Lesser General Public Licence (LGPL) version 3, *
|
||||||
|
* copied verbatim in the file "LICENSE" *
|
||||||
|
********************************************************************************/
|
||||||
|
|
||||||
|
#ifndef FAIR_MQ_DEVICERUNNER_H
|
||||||
|
#define FAIR_MQ_DEVICERUNNER_H
|
||||||
|
|
||||||
|
#include <fairmq/EventManager.h>
|
||||||
|
#include <fairmq/PluginManager.h>
|
||||||
|
#include <FairMQDevice.h>
|
||||||
|
#include <FairMQLogger.h>
|
||||||
|
#include <options/FairMQProgOptions.h>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace fair
|
||||||
|
{
|
||||||
|
namespace mq
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @class DeviceRunner DeviceRunner.h <fairmq/DeviceRunner.h>
|
||||||
|
* @brief Utility class to facilitate a convenient top-level device launch/shutdown.
|
||||||
|
*
|
||||||
|
* Runs a single FairMQ device with config and plugin support.
|
||||||
|
*
|
||||||
|
* For customization user hooks are executed at various steps during device launch/shutdown in the following sequence:
|
||||||
|
*
|
||||||
|
* LoadPlugins
|
||||||
|
* |
|
||||||
|
* v
|
||||||
|
* SetCustomCmdLineOptions
|
||||||
|
* |
|
||||||
|
* v
|
||||||
|
* ModifyRawCmdLineArgs
|
||||||
|
* |
|
||||||
|
* v
|
||||||
|
* InstatiateDevice
|
||||||
|
*
|
||||||
|
* Each hook has access to all members of the DeviceRunner and really only differs by the point in time it is called.
|
||||||
|
*
|
||||||
|
* For an example usage of this class see the fairmq/runFairMQDevice.h header.
|
||||||
|
*/
|
||||||
|
class DeviceRunner
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
DeviceRunner(int argc, char const* argv[]);
|
||||||
|
|
||||||
|
auto Run() -> int;
|
||||||
|
auto RunWithExceptionHandlers() -> int;
|
||||||
|
|
||||||
|
template<typename H>
|
||||||
|
auto AddHook(std::function<void(DeviceRunner&)> hook) -> void { fEvents.Subscribe<H>("runner", hook); }
|
||||||
|
template<typename H>
|
||||||
|
auto RemoveHook() -> void { fEvents.Unsubscribe<H>("runner"); }
|
||||||
|
|
||||||
|
std::vector<std::string> fRawCmdLineArgs;
|
||||||
|
std::shared_ptr<PluginManager> fPluginManager;
|
||||||
|
FairMQProgOptions fConfig;
|
||||||
|
std::shared_ptr<FairMQDevice> fDevice;
|
||||||
|
|
||||||
|
private:
|
||||||
|
EventManager fEvents;
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace hooks
|
||||||
|
{
|
||||||
|
struct LoadPlugins : Event<DeviceRunner&> {};
|
||||||
|
struct SetCustomCmdLineOptions : Event<DeviceRunner&> {};
|
||||||
|
struct ModifyRawCmdLineArgs : Event<DeviceRunner&> {};
|
||||||
|
struct InstantiateDevice : Event<DeviceRunner&> {};
|
||||||
|
} /* namespace hooks */
|
||||||
|
|
||||||
|
} /* namespace mq */
|
||||||
|
} /* namespace fair */
|
||||||
|
|
||||||
|
#endif /* FAIR_MQ_DEVICERUNNER_H */
|
|
@ -39,6 +39,18 @@ FairMQProgOptions::~FairMQProgOptions()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FairMQProgOptions::ParseAll(const std::vector<std::string>& cmdLineArgs, bool allowUnregistered)
|
||||||
|
{
|
||||||
|
std::vector<const char*> argv(cmdLineArgs.size());
|
||||||
|
|
||||||
|
std::transform(cmdLineArgs.begin(), cmdLineArgs.end(), argv.begin(), [](const std::string& str)
|
||||||
|
{
|
||||||
|
return str.c_str();
|
||||||
|
});
|
||||||
|
|
||||||
|
ParseAll(argv.size(), const_cast<char**>(argv.data()), allowUnregistered);
|
||||||
|
}
|
||||||
|
|
||||||
void FairMQProgOptions::ParseAll(const int argc, char const* const* argv, bool allowUnregistered)
|
void FairMQProgOptions::ParseAll(const int argc, char const* const* argv, bool allowUnregistered)
|
||||||
{
|
{
|
||||||
// init description
|
// init description
|
||||||
|
|
|
@ -49,6 +49,7 @@ class FairMQProgOptions : public FairProgOptions
|
||||||
FairMQProgOptions();
|
FairMQProgOptions();
|
||||||
virtual ~FairMQProgOptions();
|
virtual ~FairMQProgOptions();
|
||||||
|
|
||||||
|
void ParseAll(const std::vector<std::string>& cmdLineArgs, bool allowUnregistered);
|
||||||
// parse command line and txt/INI configuration file.
|
// parse command line and txt/INI configuration file.
|
||||||
// default parser for the mq-configuration file (JSON/XML) is called if command line key mq-config is called
|
// default parser for the mq-configuration file (JSON/XML) is called if command line key mq-config is called
|
||||||
virtual void ParseAll(const int argc, char const* const* argv, bool allowUnregistered = false);
|
virtual void ParseAll(const int argc, char const* const* argv, bool allowUnregistered = false);
|
||||||
|
|
|
@ -6,15 +6,10 @@
|
||||||
* copied verbatim in the file "LICENSE" *
|
* copied verbatim in the file "LICENSE" *
|
||||||
********************************************************************************/
|
********************************************************************************/
|
||||||
|
|
||||||
#include <FairMQLogger.h>
|
#include <fairmq/DeviceRunner.h>
|
||||||
#include <options/FairMQProgOptions.h>
|
|
||||||
#include <FairMQDevice.h>
|
|
||||||
#include <fairmq/PluginManager.h>
|
|
||||||
#include <fairmq/Tools.h>
|
|
||||||
#include <boost/program_options.hpp>
|
#include <boost/program_options.hpp>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
template <typename R>
|
template <typename R>
|
||||||
class GenericFairMQDevice : public FairMQDevice
|
class GenericFairMQDevice : public FairMQDevice
|
||||||
|
@ -45,80 +40,34 @@ void addCustomOptions(boost::program_options::options_description&);
|
||||||
|
|
||||||
int main(int argc, const char** argv)
|
int main(int argc, const char** argv)
|
||||||
{
|
{
|
||||||
try
|
using namespace fair::mq;
|
||||||
{
|
using namespace fair::mq::hooks;
|
||||||
// Call custom program options hook
|
|
||||||
|
fair::mq::DeviceRunner runner{argc, argv};
|
||||||
|
|
||||||
|
// runner.AddHook<LoadPlugins>([](DeviceRunner& r){
|
||||||
|
// // for example:
|
||||||
|
// r.fPluginManager->SetSearchPaths({"/lib", "/lib/plugins"});
|
||||||
|
// r.fPluginManager->LoadPlugin("asdf");
|
||||||
|
// });
|
||||||
|
|
||||||
|
runner.AddHook<SetCustomCmdLineOptions>([](DeviceRunner& r){
|
||||||
boost::program_options::options_description customOptions("Custom options");
|
boost::program_options::options_description customOptions("Custom options");
|
||||||
addCustomOptions(customOptions);
|
addCustomOptions(customOptions);
|
||||||
|
r.fConfig.AddToCmdLineOptions(customOptions);
|
||||||
// Create plugin manager and load command line supplied plugins
|
|
||||||
// Plugin manager needs to be destroyed after config! TODO Investigate why
|
|
||||||
auto pluginManager = fair::mq::PluginManager::MakeFromCommandLineOptions(fair::mq::tools::ToStrVector(argc, argv));
|
|
||||||
|
|
||||||
// Load builtin plugins last
|
|
||||||
pluginManager->LoadPlugin("s:control");
|
|
||||||
|
|
||||||
// Construct command line options parser
|
|
||||||
FairMQProgOptions config;
|
|
||||||
config.AddToCmdLineOptions(customOptions);
|
|
||||||
pluginManager->ForEachPluginProgOptions([&config](boost::program_options::options_description options){
|
|
||||||
config.AddToCmdLineOptions(options);
|
|
||||||
});
|
});
|
||||||
config.AddToCmdLineOptions(pluginManager->ProgramOptions());
|
|
||||||
|
|
||||||
// Parse command line options
|
// runner.AddHook<ModifyRawCmdLineArgs>([](DeviceRunner& r){
|
||||||
config.ParseAll(argc, argv, true);
|
// // for example:
|
||||||
|
// r.fRawCmdLineArgs.push_back("--blubb");
|
||||||
|
// });
|
||||||
|
|
||||||
// Call device creation hook
|
runner.AddHook<InstantiateDevice>([](DeviceRunner& r){
|
||||||
std::shared_ptr<FairMQDevice> device{getDevice(config)};
|
r.fDevice = std::shared_ptr<FairMQDevice>{getDevice(r.fConfig)};
|
||||||
if (!device)
|
});
|
||||||
{
|
|
||||||
LOG(ERROR) << "getDevice(): no valid device provided. Exiting.";
|
return runner.RunWithExceptionHandlers();
|
||||||
return 1;
|
|
||||||
}
|
// Run without builtin catch all exception handler, just:
|
||||||
|
// return runner.Run();
|
||||||
// Handle --print-channels
|
|
||||||
device->RegisterChannelEndpoints();
|
|
||||||
if (config.Count("print-channels"))
|
|
||||||
{
|
|
||||||
device->PrintRegisteredChannels();
|
|
||||||
device->ChangeState(FairMQDevice::END);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle --version
|
|
||||||
if (config.Count("version"))
|
|
||||||
{
|
|
||||||
std::cout << "User device version: " << device->GetVersion() << std::endl;
|
|
||||||
std::cout << "FAIRMQ_INTERFACE_VERSION: " << FAIRMQ_INTERFACE_VERSION << std::endl;
|
|
||||||
device->ChangeState(FairMQDevice::END);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG(DEBUG) << "PID: " << getpid();
|
|
||||||
|
|
||||||
// Configure device
|
|
||||||
device->SetConfig(config);
|
|
||||||
|
|
||||||
// Initialize plugin services
|
|
||||||
pluginManager->EmplacePluginServices(&config, device);
|
|
||||||
|
|
||||||
// Instantiate and run plugins
|
|
||||||
pluginManager->InstantiatePlugins();
|
|
||||||
|
|
||||||
// Wait for control plugin to release device control
|
|
||||||
pluginManager->WaitForPluginsToReleaseDeviceControl();
|
|
||||||
}
|
|
||||||
catch (std::exception& e)
|
|
||||||
{
|
|
||||||
LOG(ERROR) << "Unhandled exception reached the top of main: " << e.what() << ", application will now exit";
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
catch (...)
|
|
||||||
{
|
|
||||||
LOG(ERROR) << "Non-exception instance being thrown. Please make sure you use std::runtime_exception() instead. Application will now exit.";
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user