mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-15 09:31:45 +00:00
Refactor the transport interface
- give transport Initialize() method with access to device config. - avoid using global context in the transport. - simplify shutdown procedure (no need for extra thread).
This commit is contained in:
committed by
Mohammad Al-Turany
parent
d7eb692951
commit
5aaf27bf02
@@ -15,16 +15,48 @@
|
||||
#include "zmq.h"
|
||||
|
||||
#include "FairMQTransportFactoryZMQ.h"
|
||||
#include "../options/FairMQProgOptions.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
FairMQ::Transport FairMQTransportFactoryZMQ::fTransportType = FairMQ::Transport::ZMQ;
|
||||
|
||||
FairMQTransportFactoryZMQ::FairMQTransportFactoryZMQ()
|
||||
: fContext(zmq_ctx_new())
|
||||
{
|
||||
int major, minor, patch;
|
||||
zmq_version(&major, &minor, &patch);
|
||||
LOG(DEBUG) << "Transport: Using ZeroMQ library, version: " << major << "." << minor << "." << patch;
|
||||
|
||||
if (!fContext)
|
||||
{
|
||||
LOG(ERROR) << "failed creating context, reason: " << zmq_strerror(errno);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void FairMQTransportFactoryZMQ::Initialize(const FairMQProgOptions* config)
|
||||
{
|
||||
int numIoThreads = 1;
|
||||
if (config)
|
||||
{
|
||||
numIoThreads = config->GetValue<int>("io-threads");
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(WARN) << "zeromq: FairMQProgOptions not available! Using defaults.";
|
||||
}
|
||||
|
||||
if (zmq_ctx_set(fContext, ZMQ_IO_THREADS, numIoThreads) != 0)
|
||||
{
|
||||
LOG(ERROR) << "failed configuring context, reason: " << zmq_strerror(errno);
|
||||
}
|
||||
|
||||
// Set the maximum number of allowed sockets on the context.
|
||||
if (zmq_ctx_set(fContext, ZMQ_MAX_SOCKETS, 10000) != 0)
|
||||
{
|
||||
LOG(ERROR) << "failed configuring context, reason: " << zmq_strerror(errno);
|
||||
}
|
||||
}
|
||||
|
||||
FairMQMessagePtr FairMQTransportFactoryZMQ::CreateMessage() const
|
||||
@@ -42,9 +74,10 @@ FairMQMessagePtr FairMQTransportFactoryZMQ::CreateMessage(void* data, const size
|
||||
return unique_ptr<FairMQMessage>(new FairMQMessageZMQ(data, size, ffn, hint));
|
||||
}
|
||||
|
||||
FairMQSocketPtr FairMQTransportFactoryZMQ::CreateSocket(const string& type, const string& name, const int numIoThreads, const string& id /*= ""*/) const
|
||||
FairMQSocketPtr FairMQTransportFactoryZMQ::CreateSocket(const string& type, const string& name, const string& id /*= ""*/) const
|
||||
{
|
||||
return unique_ptr<FairMQSocket>(new FairMQSocketZMQ(type, name, numIoThreads, id));
|
||||
assert(fContext);
|
||||
return unique_ptr<FairMQSocket>(new FairMQSocketZMQ(type, name, id, fContext));
|
||||
}
|
||||
|
||||
FairMQPollerPtr FairMQTransportFactoryZMQ::CreatePoller(const vector<FairMQChannel>& channels) const
|
||||
@@ -66,3 +99,34 @@ FairMQ::Transport FairMQTransportFactoryZMQ::GetType() const
|
||||
{
|
||||
return fTransportType;
|
||||
}
|
||||
|
||||
void FairMQTransportFactoryZMQ::Shutdown()
|
||||
{
|
||||
if (zmq_ctx_shutdown(fContext) != 0)
|
||||
{
|
||||
LOG(ERROR) << "zeromq: failed shutting down context, reason: " << zmq_strerror(errno);
|
||||
}
|
||||
}
|
||||
|
||||
void FairMQTransportFactoryZMQ::Terminate()
|
||||
{
|
||||
if (fContext)
|
||||
{
|
||||
if (zmq_ctx_term(fContext) != 0)
|
||||
{
|
||||
if (errno == EINTR)
|
||||
{
|
||||
LOG(ERROR) << " failed closing context, reason: " << zmq_strerror(errno);
|
||||
}
|
||||
else
|
||||
{
|
||||
fContext = nullptr;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(ERROR) << "shmem: Terminate(): context now available for shutdown";
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user