mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-15 09:31:45 +00:00
FairMQ: Implement DeviceRunner
This commit is contained in:
committed by
Mohammad Al-Turany
parent
7f23a70670
commit
4ae2e025c9
@@ -6,15 +6,10 @@
|
||||
* copied verbatim in the file "LICENSE" *
|
||||
********************************************************************************/
|
||||
|
||||
#include <FairMQLogger.h>
|
||||
#include <options/FairMQProgOptions.h>
|
||||
#include <FairMQDevice.h>
|
||||
#include <fairmq/PluginManager.h>
|
||||
#include <fairmq/Tools.h>
|
||||
#include <fairmq/DeviceRunner.h>
|
||||
#include <boost/program_options.hpp>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
template <typename R>
|
||||
class GenericFairMQDevice : public FairMQDevice
|
||||
@@ -45,80 +40,34 @@ void addCustomOptions(boost::program_options::options_description&);
|
||||
|
||||
int main(int argc, const char** argv)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Call custom program options hook
|
||||
using namespace fair::mq;
|
||||
using namespace fair::mq::hooks;
|
||||
|
||||
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");
|
||||
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));
|
||||
// runner.AddHook<ModifyRawCmdLineArgs>([](DeviceRunner& r){
|
||||
// // for example:
|
||||
// r.fRawCmdLineArgs.push_back("--blubb");
|
||||
// });
|
||||
|
||||
// Load builtin plugins last
|
||||
pluginManager->LoadPlugin("s:control");
|
||||
runner.AddHook<InstantiateDevice>([](DeviceRunner& r){
|
||||
r.fDevice = std::shared_ptr<FairMQDevice>{getDevice(r.fConfig)};
|
||||
});
|
||||
|
||||
// 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());
|
||||
return runner.RunWithExceptionHandlers();
|
||||
|
||||
// Parse command line options
|
||||
config.ParseAll(argc, argv, true);
|
||||
|
||||
// Call device creation hook
|
||||
std::shared_ptr<FairMQDevice> device{getDevice(config)};
|
||||
if (!device)
|
||||
{
|
||||
LOG(ERROR) << "getDevice(): no valid device provided. Exiting.";
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 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;
|
||||
// Run without builtin catch all exception handler, just:
|
||||
// return runner.Run();
|
||||
}
|
||||
|
Reference in New Issue
Block a user