mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-13 00:31:14 +00:00
Organize sockets as a map of vectors of FairMQChannels. Update FairMQStateMachine by removing SETTINGINPUT, SETTINGOUTPUT, BIND and CONNECT states and by adding INITIALIZING_TASK, RESETTING_TASK and RESETTING_DEVICE states. Run states functions in their own thread.
173 lines
4.8 KiB
C++
173 lines
4.8 KiB
C++
/********************************************************************************
|
|
* Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
|
|
* *
|
|
* This software is distributed under the terms of the *
|
|
* GNU Lesser General Public Licence version 3 (LGPL) version 3, *
|
|
* copied verbatim in the file "LICENSE" *
|
|
********************************************************************************/
|
|
/**
|
|
* runSink.cxx
|
|
*
|
|
* @since 2013-01-21
|
|
* @author D. Klein, A. Rybalchenko
|
|
*/
|
|
|
|
#include <iostream>
|
|
#include <csignal>
|
|
|
|
#include "boost/program_options.hpp"
|
|
|
|
#include "FairMQLogger.h"
|
|
#include "FairMQSink.h"
|
|
|
|
#ifdef NANOMSG
|
|
#include "FairMQTransportFactoryNN.h"
|
|
#else
|
|
#include "FairMQTransportFactoryZMQ.h"
|
|
#endif
|
|
|
|
using namespace std;
|
|
|
|
FairMQSink sink;
|
|
|
|
static void s_signal_handler(int signal)
|
|
{
|
|
LOG(INFO) << "Caught signal " << signal;
|
|
|
|
sink.ChangeState(FairMQSink::END);
|
|
|
|
LOG(INFO) << "Shutdown complete.";
|
|
exit(1);
|
|
}
|
|
|
|
static void s_catch_signals(void)
|
|
{
|
|
struct sigaction action;
|
|
action.sa_handler = s_signal_handler;
|
|
action.sa_flags = 0;
|
|
sigemptyset(&action.sa_mask);
|
|
sigaction(SIGINT, &action, NULL);
|
|
sigaction(SIGTERM, &action, NULL);
|
|
}
|
|
|
|
typedef struct DeviceOptions
|
|
{
|
|
DeviceOptions() :
|
|
id(), ioThreads(0),
|
|
inputSocketType(), inputBufSize(0), inputMethod(), inputAddress()
|
|
{}
|
|
|
|
string id;
|
|
int ioThreads;
|
|
string inputSocketType;
|
|
int inputBufSize;
|
|
string inputMethod;
|
|
string inputAddress;
|
|
} DeviceOptions_t;
|
|
|
|
inline bool parse_cmd_line(int _argc, char* _argv[], DeviceOptions* _options)
|
|
{
|
|
if (_options == NULL)
|
|
throw 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>()->default_value(1000), "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://*:5555\"")
|
|
("help", "Print help messages");
|
|
|
|
bpo::variables_map vm;
|
|
bpo::store(bpo::parse_command_line(_argc, _argv, desc), vm);
|
|
|
|
if ( vm.count("help") )
|
|
{
|
|
LOG(INFO) << "FairMQ Sink" << 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>();
|
|
|
|
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
|
|
FairMQTransportFactory* transportFactory = new FairMQTransportFactoryNN();
|
|
#else
|
|
FairMQTransportFactory* transportFactory = new FairMQTransportFactoryZMQ();
|
|
#endif
|
|
|
|
sink.SetTransport(transportFactory);
|
|
|
|
FairMQChannel channel(options.inputSocketType, options.inputMethod, options.inputAddress);
|
|
channel.fSndBufSize = options.inputBufSize;
|
|
channel.fRcvBufSize = options.inputBufSize;
|
|
channel.fRateLogging = 1;
|
|
|
|
sink.fChannels["data-in"].push_back(channel);
|
|
|
|
sink.SetProperty(FairMQSink::Id, options.id);
|
|
sink.SetProperty(FairMQSink::NumIoThreads, options.ioThreads);
|
|
|
|
sink.ChangeState(FairMQSink::INIT_DEVICE);
|
|
sink.WaitForEndOfState(FairMQSink::INIT_DEVICE);
|
|
|
|
sink.ChangeState(FairMQSink::INIT_TASK);
|
|
sink.WaitForEndOfState(FairMQSink::INIT_TASK);
|
|
|
|
sink.ChangeState(FairMQSink::RUN);
|
|
sink.WaitForEndOfState(FairMQSink::RUN);
|
|
|
|
sink.ChangeState(FairMQSink::STOP);
|
|
|
|
sink.ChangeState(FairMQSink::RESET_TASK);
|
|
sink.WaitForEndOfState(FairMQSink::RESET_TASK);
|
|
|
|
sink.ChangeState(FairMQSink::RESET_DEVICE);
|
|
sink.WaitForEndOfState(FairMQSink::RESET_DEVICE);
|
|
|
|
sink.ChangeState(FairMQSink::END);
|
|
|
|
return 0;
|
|
}
|