FairMQ/fairmq/zeromq/FairMQMessageZMQ.cxx
Florian Uhlig 865c0e010f Add license file LICENSE with LPGL license text copied from https://www.gnu.org/licenses/lgpl.html. Add license text to most of files of the project.
Add license text to all header files

Add license text to all c++ source files

Add license text to all C macro files

Converting file to UNIX format.

Add license text to all build system files

Add license text to all c source files

Rename file LICENCE to LICENSE. Change preamble in all files.
2014-06-06 14:57:56 +02:00

136 lines
3.7 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" *
********************************************************************************/
/**
* FairMQMessageZMQ.cxx
*
* @since 2012-12-05
* @author D. Klein, A. Rybalchenko, N. Winckler
*/
#include <cstring>
#include <cstdlib>
#include "FairMQMessageZMQ.h"
#include "FairMQLogger.h"
FairMQMessageZMQ::FairMQMessageZMQ()
{
int rc = zmq_msg_init(&fMessage);
if (rc != 0)
{
LOG(ERROR) << "failed initializing message, reason: " << zmq_strerror(errno);
}
}
FairMQMessageZMQ::FairMQMessageZMQ(size_t size)
{
int rc = zmq_msg_init_size(&fMessage, size);
if (rc != 0)
{
LOG(ERROR) << "failed initializing message with size, reason: " << zmq_strerror(errno);
}
}
FairMQMessageZMQ::FairMQMessageZMQ(void* data, size_t size)
{
int rc = zmq_msg_init_data(&fMessage, data, size, &CleanUp, NULL); // TODO: expose the cleanup function part in the interface?
if (rc != 0)
{
LOG(ERROR) << "failed initializing message with data, reason: " << zmq_strerror(errno);
}
}
void FairMQMessageZMQ::Rebuild()
{
CloseMessage();
int rc = zmq_msg_init(&fMessage);
if (rc != 0)
{
LOG(ERROR) << "failed initializing message, reason: " << zmq_strerror(errno);
}
}
void FairMQMessageZMQ::Rebuild(size_t size)
{
CloseMessage();
int rc = zmq_msg_init_size(&fMessage, size);
if (rc != 0)
{
LOG(ERROR) << "failed initializing message with size, reason: " << zmq_strerror(errno);
}
}
void FairMQMessageZMQ::Rebuild(void* data, size_t size)
{
CloseMessage();
int rc = zmq_msg_init_data(&fMessage, data, size, &CleanUp, NULL); // TODO: expose the cleanup function part in the interface?
if (rc != 0)
{
LOG(ERROR) << "failed initializing message with data, reason: " << zmq_strerror(errno);
}
}
void* FairMQMessageZMQ::GetMessage()
{
return &fMessage;
}
void* FairMQMessageZMQ::GetData()
{
return zmq_msg_data(&fMessage);
}
size_t FairMQMessageZMQ::GetSize()
{
return zmq_msg_size(&fMessage);
}
void FairMQMessageZMQ::SetMessage(void* data, size_t size)
{
// dummy method to comply with the interface. functionality not allowed in zeromq.
}
void FairMQMessageZMQ::Copy(FairMQMessage* msg)
{
// Shares the message buffer between msg and this fMessage.
int rc = zmq_msg_copy(&fMessage, (zmq_msg_t*)msg->GetMessage());
if (rc != 0)
{
LOG(ERROR) << "failed copying message, reason: " << zmq_strerror(errno);
}
// Alternatively, following code does a hard copy of the message, which allows to modify the original after making a copy, without affecting the new msg.
// CloseMessage();
// size_t size = msg->GetSize();
// zmq_msg_init_size(&fMessage, size);
// std::memcpy(zmq_msg_data(&fMessage), msg->GetData(), size);
}
inline void FairMQMessageZMQ::CloseMessage()
{
int rc = zmq_msg_close(&fMessage);
if (rc != 0)
{
LOG(ERROR) << "failed closing message, reason: " << zmq_strerror(errno);
}
}
void FairMQMessageZMQ::CleanUp(void* data, void* hint)
{
free(data);
}
FairMQMessageZMQ::~FairMQMessageZMQ()
{
int rc = zmq_msg_close(&fMessage);
if (rc != 0)
{
LOG(ERROR) << "failed closing message with data, reason: " << zmq_strerror(errno);
}
}