Commit 026ac134 authored by Steven Murray's avatar Steven Murray
Browse files

Where appropriate, replaced calls to sstrerror_r() with zmqErrnoToStr()

parent da9d8b4f
/******************************************************************************
*
* 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
......@@ -22,6 +22,7 @@
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
#include "castor/messages/messages.hpp"
#include "castor/messages/ZmqMsg.hpp"
#include <errno.h>
......@@ -32,11 +33,9 @@
//-----------------------------------------------------------------------------
castor::messages::ZmqMsg::ZmqMsg() throw() {
if(zmq_msg_init(&m_zmqMsg)) {
char message[100];
sstrerror_r(errno, message, sizeof(message));
const int savedErrno = errno;
castor::exception::Exception ex;
ex.getMessage() << "Failed to construct a ZmqMsg: zmq_msg_init() failed: "
<< message;
ex.getMessage() << "zmq_msg_init() failed: " << zmqErrnoToStr(savedErrno);
throw ex;
}
}
......@@ -46,11 +45,10 @@ castor::messages::ZmqMsg::ZmqMsg() throw() {
//-----------------------------------------------------------------------------
castor::messages::ZmqMsg::ZmqMsg(const size_t msgSize) throw() {
if(zmq_msg_init_size(&m_zmqMsg, msgSize)) {
char message[100];
sstrerror_r(errno, message, sizeof(message));
const int savedErrno = errno;
castor::exception::Exception ex;
ex.getMessage() << "Failed to construct a ZmqMsg"
": zmq_msg_init_size() failed: " << message;
ex.getMessage() << "zmq_msg_init_size() failed: " <<
zmqErrnoToStr(savedErrno);
throw ex;
}
}
......
......@@ -22,6 +22,7 @@
*****************************************************************************/
#include "castor/exception/Exception.hpp"
#include "castor/messages/messages.hpp"
#include "castor/messages/ZmqSocketST.hpp"
#include "h/serrno.h"
......@@ -32,11 +33,9 @@ castor::messages::ZmqSocketST::ZmqSocketST(void *const zmqContext,
const int socketType) {
m_zmqSocket = zmq_socket (zmqContext, socketType);
if (NULL == m_zmqSocket) {
char message[100];
sstrerror_r(errno, message, sizeof(message));
const int savedErrno = errno;
castor::exception::Exception ex;
ex.getMessage() << "Failed to create ZMQ socket: "
<< message;
ex.getMessage() << "zmq_socket() failed: " << zmqErrnoToStr(savedErrno);
throw ex;
}
}
......@@ -60,12 +59,10 @@ void castor::messages::ZmqSocketST::close() {
return;
}
if(zmq_close (m_zmqSocket)) {
char message[100];
sstrerror_r(errno, message, sizeof(message));
if(zmq_close(m_zmqSocket)) {
const int savedErrno = errno;
castor::exception::Exception ex;
ex.getMessage() << "Failed to close ZMQ socket: "
<< message;
ex.getMessage() << "zmq_close() failed: " << zmqErrnoToStr(savedErrno);
throw ex;
}
m_zmqSocket = NULL;
......@@ -75,12 +72,10 @@ void castor::messages::ZmqSocketST::close() {
// bind
//------------------------------------------------------------------------------
void castor::messages::ZmqSocketST::bind (const std::string &endpoint) {
if(zmq_bind (m_zmqSocket, endpoint.c_str())) {
char message[100];
sstrerror_r(errno, message, sizeof(message));
if(zmq_bind(m_zmqSocket, endpoint.c_str())) {
const int savedErrno = errno;
castor::exception::Exception ex;
ex.getMessage() << "Failed to bind ZMQ socket: "
<< message;
ex.getMessage() << "zmq_bind failed(): " << zmqErrnoToStr(savedErrno);
throw ex;
}
}
......@@ -90,11 +85,9 @@ void castor::messages::ZmqSocketST::bind (const std::string &endpoint) {
//------------------------------------------------------------------------------
void castor::messages::ZmqSocketST::connect(const std::string &endpoint) {
if(zmq_connect(m_zmqSocket, endpoint.c_str())) {
char message[100];
sstrerror_r(errno, message, sizeof(message));
const int savedErrno = errno;
castor::exception::Exception ex;
ex.getMessage() << "Failed to connect ZMQ socket: "
<< message;
ex.getMessage() << "zmq_connect() failed: " << zmqErrnoToStr(savedErrno);
throw ex;
}
}
......@@ -110,12 +103,10 @@ void castor::messages::ZmqSocketST::send(ZmqMsg &msg, const int flags) {
// send
//------------------------------------------------------------------------------
void castor::messages::ZmqSocketST::send(zmq_msg_t *const msg, const int flags) {
if(-1 == zmq_msg_send (msg, m_zmqSocket, flags)) {
char message[100];
sstrerror_r(errno, message, sizeof(message));
if(-1 == zmq_msg_send(msg, m_zmqSocket, flags)) {
const int savedErrno = errno;
castor::exception::Exception ex;
ex.getMessage() << "Failed to send ZMQ message: "
<< message;
ex.getMessage() << "zmq_msg_send() failed: " << zmqErrnoToStr(savedErrno);
throw ex;
}
}
......@@ -132,11 +123,9 @@ void castor::messages::ZmqSocketST::recv(ZmqMsg &msg, const int flags) {
//------------------------------------------------------------------------------
void castor::messages::ZmqSocketST::recv(zmq_msg_t *const msg, int flags) {
if(-1 == zmq_msg_recv (msg, m_zmqSocket, flags)) {
char message[100];
sstrerror_r(errno, message, sizeof(message));
const int savedErrno = errno;
castor::exception::Exception ex;
ex.getMessage() << "Failed to receive ZMQ message: "
<< message;
ex.getMessage() << "zmq_msg_recv() failed: " << zmqErrnoToStr(savedErrno);
throw ex;
}
}
......
......@@ -23,8 +23,10 @@
#include "castor/messages/messages.hpp"
#include "castor/utils/utils.hpp"
#include "h/strerror_r_wrapper.h"
#include <string.h>
#include <zmq.h>
//------------------------------------------------------------------------------
// sendFrame
......@@ -228,3 +230,51 @@ void castor::messages::recvReplyOrEx(ZmqSocket& socket,
frame.parseBodyIntoProtocolBuffer(body);
}
//------------------------------------------------------------------------------
// zmqErrnoToStr
//------------------------------------------------------------------------------
std::string castor::messages::zmqErrnoToStr(const int zmqErrno) {
switch(zmqErrno) {
// Translate the values that are specific to ZMQ
case EFSM:
return "Operation cannot be accomplished in current state";
case ENOCOMPATPROTO:
return "The protocol is not compatible with the socket type";
case ETERM:
return "Context was terminated";
case EMTHREAD:
return "No thread available";
// Translate the values that are not specific to ZMQ
default:
{
char errorCStr[100];
const int rc = strerror_r_wrapper(zmqErrno, errorCStr, sizeof(errorCStr));
// If strerror_r_wrapper() failed to translate
if(rc) {
std::ostringstream oss;
oss << "Failed to translate ZMQ errno";
switch(errno) {
case EINVAL:
oss << ": Unknown ZMQ errno";
break;
case ERANGE:
oss << ": strerror_r_wrapper() given too small a buffer";
break;
default:
oss << ": Unknown reason";
}
oss << ": zmqErrno=" << zmqErrno;
return oss.str();
}
// strerror_r_wrapper() succeeded to translate
errorCStr[sizeof(errorCStr) -1] = '\0'; // Being paranoid
return errorCStr;
}
}
}
......@@ -156,5 +156,13 @@ void recvReplyOrEx(ZmqSocket& socket,
const uint32_t magic, const uint32_t protocolType,
const uint32_t protocolVersion);
/**
* Returns the string representation of the specified ZMQ errno.
*
* This method does not throw an exception if the specified errno is unknown.
* Instead the method returns a string explaining that the errno is unknown.
*/
std::string zmqErrnoToStr(const int zmqErrno);
} // namespace messages
} // namespace castor
......@@ -129,6 +129,7 @@ add_executable(castorUnitTests
../castor/log/LogContextTest.cpp
../castor/log/StringLoggerTest.cpp
../castor/log/SyslogLoggerTest.cpp
../castor/messages/MessagesTest.cpp
../castor/server/DaemonTest.cpp
../castor/server/SmartCapTest.cpp
../castor/tape/reactor/PollReactorImplTest.cpp
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment