mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-15 09:31:45 +00:00
Use boost::program_options for managing command line options of the executables.
Existing scripts in example/Tutorial3/macro have been updated to use the new format. Your own executables are not affected, but your scripts which use FairMQ executables have to be updated to the new format. Use the `--help` option with any FairMQ executable to find out the available options.
This commit is contained in:
@@ -15,6 +15,8 @@
|
||||
#include <iostream>
|
||||
#include <csignal>
|
||||
|
||||
#include "boost/program_options.hpp"
|
||||
|
||||
#include "FairMQLogger.h"
|
||||
#include "FairMQProxy.h"
|
||||
|
||||
@@ -24,10 +26,7 @@
|
||||
#include "FairMQTransportFactoryZMQ.h"
|
||||
#endif
|
||||
|
||||
using std::cout;
|
||||
using std::cin;
|
||||
using std::endl;
|
||||
using std::stringstream;
|
||||
using namespace std;
|
||||
|
||||
FairMQProxy proxy;
|
||||
|
||||
@@ -52,18 +51,100 @@ static void s_catch_signals(void)
|
||||
sigaction(SIGTERM, &action, NULL);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
typedef struct DeviceOptions
|
||||
{
|
||||
if (argc != 11)
|
||||
string id;
|
||||
int ioThreads;
|
||||
string inputSocketType;
|
||||
int inputBufSize;
|
||||
string inputMethod;
|
||||
string inputAddress;
|
||||
string outputSocketType;
|
||||
int outputBufSize;
|
||||
string outputMethod;
|
||||
string outputAddress;
|
||||
} DeviceOptions_t;
|
||||
|
||||
inline bool parse_cmd_line(int _argc, char* _argv[], DeviceOptions* _options)
|
||||
{
|
||||
if (_options == NULL)
|
||||
throw std::runtime_error("Internal error: options' container is empty.");
|
||||
|
||||
namespace bpo = boost::program_options;
|
||||
bpo::options_description desc("Options");
|
||||
desc.add_options()
|
||||
("id", bpo::value<string>()->required(), "Device ID")
|
||||
("io-threads", bpo::value<int>()->default_value(1), "Number of I/O threads")
|
||||
("input-socket-type", bpo::value<string>()->required(), "Input socket type: sub/pull")
|
||||
("input-buff-size", bpo::value<int>()->required(), "Input buffer size in number of messages (ZeroMQ)/bytes(nanomsg)")
|
||||
("input-method", bpo::value<string>()->required(), "Input method: bind/connect")
|
||||
("input-address", bpo::value<string>()->required(), "Input address, e.g.: \"tcp://localhost:5555\"")
|
||||
("output-socket-type", bpo::value<string>()->required(), "Output socket type: pub/push")
|
||||
("output-buff-size", bpo::value<int>()->required(), "Output buffer size in number of messages (ZeroMQ)/bytes(nanomsg)")
|
||||
("output-method", bpo::value<string>()->required(), "Output method: bind/connect")
|
||||
("output-address", bpo::value<string>()->required(), "Output address, e.g.: \"tcp://localhost:5555\"")
|
||||
("help", "Print help messages");
|
||||
|
||||
bpo::variables_map vm;
|
||||
bpo::store(bpo::parse_command_line(_argc, _argv, desc), vm);
|
||||
|
||||
if ( vm.count("help") )
|
||||
{
|
||||
cout << "Usage: proxy \tID numIoTreads\n"
|
||||
<< "\t\tinputSocketType inputRcvBufSize inputMethod inputAddress\n"
|
||||
<< "\t\toutputSocketType outputSndBufSize outputMethod outputAddress\n" << endl;
|
||||
return 1;
|
||||
LOG(INFO) << "FairMQ Proxy" << endl << desc;
|
||||
return false;
|
||||
}
|
||||
|
||||
bpo::notify(vm);
|
||||
|
||||
if ( vm.count("id") )
|
||||
_options->id = vm["id"].as<string>();
|
||||
|
||||
if ( vm.count("io-threads") )
|
||||
_options->ioThreads = vm["io-threads"].as<int>();
|
||||
|
||||
if ( vm.count("input-socket-type") )
|
||||
_options->inputSocketType = vm["input-socket-type"].as<string>();
|
||||
|
||||
if ( vm.count("input-buff-size") )
|
||||
_options->inputBufSize = vm["input-buff-size"].as<int>();
|
||||
|
||||
if ( vm.count("input-method") )
|
||||
_options->inputMethod = vm["input-method"].as<string>();
|
||||
|
||||
if ( vm.count("input-address") )
|
||||
_options->inputAddress = vm["input-address"].as<string>();
|
||||
|
||||
if ( vm.count("output-socket-type") )
|
||||
_options->outputSocketType = vm["output-socket-type"].as<string>();
|
||||
|
||||
if ( vm.count("output-buff-size") )
|
||||
_options->outputBufSize = vm["output-buff-size"].as<int>();
|
||||
|
||||
if ( vm.count("output-method") )
|
||||
_options->outputMethod = vm["output-method"].as<string>();
|
||||
|
||||
if ( vm.count("output-address") )
|
||||
_options->outputAddress = vm["output-address"].as<string>();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
s_catch_signals();
|
||||
|
||||
DeviceOptions_t options;
|
||||
try
|
||||
{
|
||||
if (!parse_cmd_line(argc, argv, &options))
|
||||
return 0;
|
||||
}
|
||||
catch (exception& e)
|
||||
{
|
||||
LOG(ERROR) << e.what();
|
||||
return 1;
|
||||
}
|
||||
|
||||
LOG(INFO) << "PID: " << getpid();
|
||||
|
||||
#ifdef NANOMSG
|
||||
@@ -74,42 +155,23 @@ int main(int argc, char** argv)
|
||||
|
||||
proxy.SetTransport(transportFactory);
|
||||
|
||||
int i = 1;
|
||||
|
||||
proxy.SetProperty(FairMQProxy::Id, argv[i]);
|
||||
++i;
|
||||
|
||||
int numIoThreads;
|
||||
stringstream(argv[i]) >> numIoThreads;
|
||||
proxy.SetProperty(FairMQProxy::NumIoThreads, numIoThreads);
|
||||
++i;
|
||||
proxy.SetProperty(FairMQProxy::Id, options.id);
|
||||
proxy.SetProperty(FairMQProxy::NumIoThreads, options.ioThreads);
|
||||
|
||||
proxy.SetProperty(FairMQProxy::NumInputs, 1);
|
||||
proxy.SetProperty(FairMQProxy::NumOutputs, 1);
|
||||
|
||||
proxy.ChangeState(FairMQProxy::INIT);
|
||||
|
||||
proxy.SetProperty(FairMQProxy::InputSocketType, argv[i], 0);
|
||||
++i;
|
||||
int inputRcvBufSize;
|
||||
stringstream(argv[i]) >> inputRcvBufSize;
|
||||
proxy.SetProperty(FairMQProxy::InputRcvBufSize, inputRcvBufSize, 0);
|
||||
++i;
|
||||
proxy.SetProperty(FairMQProxy::InputMethod, argv[i], 0);
|
||||
++i;
|
||||
proxy.SetProperty(FairMQProxy::InputAddress, argv[i], 0);
|
||||
++i;
|
||||
proxy.SetProperty(FairMQProxy::InputSocketType, options.inputSocketType);
|
||||
proxy.SetProperty(FairMQProxy::InputSndBufSize, options.inputBufSize);
|
||||
proxy.SetProperty(FairMQProxy::InputMethod, options.inputMethod);
|
||||
proxy.SetProperty(FairMQProxy::InputAddress, options.inputAddress);
|
||||
|
||||
proxy.SetProperty(FairMQProxy::OutputSocketType, argv[i], 0);
|
||||
++i;
|
||||
int outputSndBufSize;
|
||||
stringstream(argv[i]) >> outputSndBufSize;
|
||||
proxy.SetProperty(FairMQProxy::OutputSndBufSize, outputSndBufSize, 0);
|
||||
++i;
|
||||
proxy.SetProperty(FairMQProxy::OutputMethod, argv[i], 0);
|
||||
++i;
|
||||
proxy.SetProperty(FairMQProxy::OutputAddress, argv[i], 0);
|
||||
++i;
|
||||
proxy.SetProperty(FairMQProxy::OutputSocketType, options.outputSocketType);
|
||||
proxy.SetProperty(FairMQProxy::OutputSndBufSize, options.outputBufSize);
|
||||
proxy.SetProperty(FairMQProxy::OutputMethod, options.outputMethod);
|
||||
proxy.SetProperty(FairMQProxy::OutputAddress, options.outputAddress);
|
||||
|
||||
proxy.ChangeState(FairMQProxy::SETOUTPUT);
|
||||
proxy.ChangeState(FairMQProxy::SETINPUT);
|
||||
|
Reference in New Issue
Block a user