Commit 8717cfe2 authored by Steven Murray's avatar Steven Murray
Browse files

Removed Zmq classes from tapeserver/castor

parent 6658237a
......@@ -39,10 +39,6 @@ set(CtaMessageSrcs
Frame.cpp
messages.cpp
../../../tapeserver/daemon/TapedProxy.cpp
ZmqMsg.cpp
ZmqSocket.cpp
ZmqSocketMT.cpp
ZmqSocketST.cpp
)
add_library(ctamessages SHARED
......@@ -57,11 +53,4 @@ target_link_libraries(ctamessages
ctautils
ctatapesession
protobuf
ssl
zmq)
add_library(ctamessagesunittests SHARED
MessagesTest.cpp)
install(TARGETS ctamessagesunittests DESTINATION usr/${CMAKE_INSTALL_LIBDIR})
add_dependencies(ctamessagesunittests ctamessagesprotobuf)
ssl)
......@@ -40,50 +40,6 @@ void castor::messages::Frame::checkHashValueOfBody() const {
}
}
//------------------------------------------------------------------------------
// serialiseHeaderToZmqMsg
//------------------------------------------------------------------------------
void castor::messages::Frame::serializeHeaderToZmqMsg(ZmqMsg &msg) const {
try {
if(!header.IsInitialized()) {
cta::exception::Exception ex;
ex.getMessage() << "Frame header is not initialized";
throw ex;
}
if(header.ByteSize() != (int)msg.size()) {
cta::exception::Exception ex;
ex.getMessage() << "Size of frame header does not match that of ZMQ"
" message: header.ByteSize()=" << header.ByteSize() << " msg.size()="
<< msg.size();
throw ex;
}
if(!header.SerializeToArray(msg.getData(), header.ByteSize())) {
cta::exception::Exception ex;
ex.getMessage() << "header.SerializeToArray() returned false";
throw ex;
}
} catch(cta::exception::Exception &ne) {
cta::exception::Exception ex;
ex.getMessage() << "Failed to serialize frame header to ZMQ message: " <<
ne.getMessage().str();
throw ex;
}
}
//------------------------------------------------------------------------------
// parseZmqMsgIntoHeader
//------------------------------------------------------------------------------
void castor::messages::Frame::parseZmqMsgIntoHeader(const ZmqMsg &msg) {
if(!header.ParseFromArray(msg.getData(), msg.size())) {
cta::exception::Exception ex;
ex.getMessage() << "Failed to parse ZMQ message into frame header: "
"header.ParseFromArray() returned false";
throw ex;
}
}
//------------------------------------------------------------------------------
// serializeProtocolBufferIntoBody
//------------------------------------------------------------------------------
......
......@@ -24,7 +24,6 @@
#pragma once
#include "castor/messages/Header.pb.h"
#include "ZmqMsg.hpp"
#include "tapeserver/castor/utils/utils.hpp"
#include <google/protobuf/message.h>
......@@ -53,23 +52,6 @@ struct Frame {
*/
void checkHashValueOfBody() const;
/**
* Serializes the frame header to the specified ZMQ message.
*
* Please note that the specified size of the specified ZMQ message must
* match that of the header.
*
* @param msg Output parameter: The ZMQ message.
*/
void serializeHeaderToZmqMsg(ZmqMsg &msg) const;
/**
* Parses the specified ZMQ message into the frame header.
*
* @param msg The ZMQ message.
*/
void parseZmqMsgIntoHeader(const ZmqMsg &msg);
/**
* Serializes the specified protocol buffer into the frame body, calculates
* it hash value and stores the has value in the frame header.
......
/******************************************************************************
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
*
* Copyright (C) 2003 CERN
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
#include "castor/messages/messages.hpp"
#include <gtest/gtest.h>
namespace unitTests {
class castor_messages : public ::testing::Test {
protected:
virtual void SetUp() {
}
virtual void TearDown() {
}
};
/**
* Tests the translation of ZMQ specific errno values.
*/
TEST_F(castor_messages, zmqSpecificErrnoValues) {
using namespace castor::messages;
ASSERT_EQ(std::string("Operation cannot be accomplished in current state"),
zmqErrnoToStr(EFSM));
ASSERT_EQ(std::string("The protocol is not compatible with the socket type"),
zmqErrnoToStr(ENOCOMPATPROTO));
ASSERT_EQ(std::string("Context was terminated"),
zmqErrnoToStr(ETERM));
ASSERT_EQ(std::string("No thread available"),
zmqErrnoToStr(EMTHREAD));
}
/**
* Tests the translation of non-ZMQ errno values.
*/
TEST_F(castor_messages, nonZmqrrnoValues) {
using namespace castor::messages;
ASSERT_EQ(std::string("Invalid argument"),
zmqErrnoToStr(EINVAL));
}
} // namespace unitTests
/******************************************************************************
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
*
* Copyright (C) 2003 CERN
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
*
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
#include "castor/messages/messages.hpp"
#include "castor/messages/ZmqMsg.hpp"
#include <errno.h>
#include <unistd.h>
//-----------------------------------------------------------------------------
// constructor
//-----------------------------------------------------------------------------
castor::messages::ZmqMsg::ZmqMsg() throw() {
if(zmq_msg_init(&m_zmqMsg)) {
const int savedErrno = errno;
cta::exception::Exception ex;
ex.getMessage() << "zmq_msg_init() failed: " << zmqErrnoToStr(savedErrno);
throw ex;
}
}
//-----------------------------------------------------------------------------
// constructor
//-----------------------------------------------------------------------------
castor::messages::ZmqMsg::ZmqMsg(const size_t msgSize) throw() {
if(zmq_msg_init_size(&m_zmqMsg, msgSize)) {
const int savedErrno = errno;
cta::exception::Exception ex;
ex.getMessage() << "zmq_msg_init_size() failed: " <<
zmqErrnoToStr(savedErrno);
throw ex;
}
}
//-----------------------------------------------------------------------------
// destructor
//-----------------------------------------------------------------------------
castor::messages::ZmqMsg::~ZmqMsg() throw() {
zmq_msg_close(&m_zmqMsg);
}
//-----------------------------------------------------------------------------
// getZmqMsg
//-----------------------------------------------------------------------------
zmq_msg_t &castor::messages::ZmqMsg::getZmqMsg() throw() {
return m_zmqMsg;
}
//-----------------------------------------------------------------------------
// getData
//-----------------------------------------------------------------------------
const void* castor::messages::ZmqMsg::getData() const {
return zmq_msg_data(const_cast<zmq_msg_t*>(&m_zmqMsg));
}
//-----------------------------------------------------------------------------
// getData
//-----------------------------------------------------------------------------
void* castor::messages::ZmqMsg::getData() {
return zmq_msg_data(&m_zmqMsg);
}
//-----------------------------------------------------------------------------
// size
//-----------------------------------------------------------------------------
size_t castor::messages::ZmqMsg::size() const {
return zmq_msg_size(const_cast<zmq_msg_t*>(&m_zmqMsg));
}
//-----------------------------------------------------------------------------
// more
//-----------------------------------------------------------------------------
bool castor::messages::ZmqMsg::more() const {
return zmq_msg_more(const_cast<zmq_msg_t*>(&m_zmqMsg));
}
/******************************************************************************
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
*
* Copyright (C) 2003 CERN
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
#pragma once
#include "common/exception/NotAnOwner.hpp"
#include <stdio.h>
#include <zmq.h>
namespace castor {
namespace messages {
/**
* C++ wrapper around a ZMQ message.
*/
class ZmqMsg {
public:
/**
* Constructor.
*/
ZmqMsg() throw();
/**
* Constructor.
*
* @param msgSize The size of the ZMQ message.
*/
ZmqMsg(const size_t msgSize) throw();
/**
* Destructor.
*
* Calls zmq_msg_close().
*/
~ZmqMsg() throw();
/**
* Returns the enclosed ZMQ message.
*
* @return The underlying ZMQ message.
*/
zmq_msg_t &getZmqMsg() throw();
/**
* Gives read access to the data of the enclosed ZMQ message.
*
* @return A pointer to the beginning of the data
*/
const void* getData() const;
/**
* Gives read/write access to the data of the enclosed ZMQ message.
*
* @return A pointer to the beginning of the data
*/
void* getData();
/**
* Gets the size of the enclosed ZMQ message.
*
* @return The size of the ZMQ message.
*/
size_t size() const;
/**
* Returns true if the enclosed ZMQ message is part of a multi-part message
* and there are more parts to receive.
*/
bool more() const;
private:
/**
* The enclosed ZMQ message.
*/
zmq_msg_t m_zmqMsg;
/**
* Private copy-constructor to prevent users from trying to create a new
* copy of an object of this class.
*
* Not implemented so that it cannot be called
*/
ZmqMsg(const ZmqMsg &obj) throw();
}; // class ZmqMsg
} // namespace messages
} // namespace castor
/******************************************************************************
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
*
* Copyright (C) 2003 CERN
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
#include "castor/messages/ZmqSocket.hpp"
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
castor::messages::ZmqSocket::ZmqSocket() {
}
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
castor::messages::ZmqSocket::~ZmqSocket() throw() {
}
/******************************************************************************
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
*
* Copyright (C) 2003 CERN
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
#pragma once
#include "ZmqMsg.hpp"
#include <string>
#include <zmq.h>
namespace castor {
namespace messages {
/**
* Abstract C++ class that defines the interace of a wrapper around a ZMQ
* socket.
*/
class ZmqSocket {
public:
/**
* Constructor.
*/
ZmqSocket();
/**
* Destructor.
*/
virtual ~ZmqSocket() throw() = 0;
/**
* Closes the ZMQ socket.
*/
virtual void close() = 0;
/**
* Binds the ZMQ socket to the specified endpoint.
*
* @param endpoint The endpoint to bind to.
*/
virtual void bind(const std::string &endpoint) = 0;
/**
* Connects the socket to the spedicied endpoint.
*
* @param endpoint The endpoint to connect to.
*/
virtual void connect(const std::string &endpoint) = 0;
/**
* Sends the specified ZMQ message over the socket.
*
* @param msg The ZMQ messge to be sent.
* @param flags See manual page of zmq_msg_send().
*/
virtual void send(ZmqMsg &msg, const int flags = 0) = 0;
/**
* Sends the specified ZMQ message over the socket.
*
* @param msg The ZMQ messge to be sent.
* @param flags See manual page of zmq_msg_send().
*/
virtual void send(zmq_msg_t *const msg, const int flags = 0) = 0;
/**
* Receives a ZMQ mesage from the socket.
*
* @param msg Output parameter: The received ZMQ messge.
* @param flags See manual page of zmq_msg_send().
*/
virtual void recv(ZmqMsg &msg, const int flags = 0) = 0;
/**
* Receives a ZMQ mesage from the socket.
*
* @param msg Output parameter: The received ZMQ messge.
* @param flags See manual page of zmq_msg_send().
*/
virtual void recv(zmq_msg_t *const msg, const int flags = 0) = 0;
/**
* Returns the ZMQ socket wrappeed by this class.
*
* @return The ZMQ socket wrappeed by this class.
*/
virtual void *getZmqSocket() const throw() = 0;
private:
/**
* Copy constructor made private to prevent copies.
*/
ZmqSocket(const ZmqSocket&);
/**
* Assignment operator made private to prevent assignments.
*/
void operator=(const ZmqSocket &);
}; // class ZmqSocket
} // namespace messages
} // namespace castor
/******************************************************************************
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
*
* Copyright (C) 2003 CERN
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
#include "common/threading/MutexLocker.hpp"
#include "castor/messages/ZmqSocketMT.hpp"
#include "common/exception/Exception.hpp"
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
castor::messages::ZmqSocketMT::ZmqSocketMT(void *const zmqContext,
const int socketType): m_socket(zmqContext, socketType) {
}
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
castor::messages::ZmqSocketMT::~ZmqSocketMT() throw() {
try {
close();
} catch(...) {
// Ignore any exceptions because this is a destructor.
}
}
//------------------------------------------------------------------------------
// close
//------------------------------------------------------------------------------
void castor::messages::ZmqSocketMT::close() {
cta::threading::MutexLocker lock(m_mutex);
m_socket.close();
}
//------------------------------------------------------------------------------
// bind
//------------------------------------------------------------------------------
void castor::messages::ZmqSocketMT::bind (const std::string &endpoint) {
cta::threading::MutexLocker lock(m_mutex);
m_socket.bind(endpoint);
}
//------------------------------------------------------------------------------
// connect
//------------------------------------------------------------------------------
void castor::messages::ZmqSocketMT::connect(const std::string &endpoint) {
cta::threading::MutexLocker lock(m_mutex);
m_socket.connect(endpoint);
}
//------------------------------------------------------------------------------
// send