Commit 6a30d458 authored by Steven Murray's avatar Steven Murray
Browse files

Removed legacy mashalling from tapeserver/castor

parent 8717cfe2
......@@ -209,8 +209,6 @@ Unit tests and system tests with virtual tape drives
%attr(0755,root,root) %{_libdir}/libctaexceptionunittests.so
%attr(0755,root,root) %{_libdir}/libctainmemorycatalogueunittests.so
%attr(0755,root,root) %{_libdir}/libctaiounittests.so
%attr(0755,root,root) %{_libdir}/libctalegacymsgunittests.so
%attr(0755,root,root) %{_libdir}/libctamessagesunittests.so
%attr(0755,root,root) %{_libdir}/libctaobjectstoreunittests.so
%attr(0755,root,root) %{_libdir}/libctardbmsunittests.so
%attr(0755,root,root) %{_libdir}/libctaschedulerunittests.so
......
......@@ -5,23 +5,10 @@ include_directories(${PROJECT_SOURCE_DIR}/tapeserver/h)
add_library (ctalegacymsg
TapeLabelRqstMsgBody.cpp
GenericMarshal.cpp
MessageHeader.cpp
legacymsg.cpp
TapeConfigRequestMsgBody.cpp
TapeMarshal.cpp
CommonMarshal.cpp
TapeStatReplyMsgBody.cpp
TapeStatRequestMsgBody.cpp
GenericErrorReplyMsgBody.cpp
GenericReplyMsgBody.cpp
TapeStatDriveEntry.cpp)
add_library (ctalegacymsgunittests SHARED
CommonMarshalTest.cpp
GenericMarshalTest.cpp
TapeMarshalTest.cpp)
target_link_libraries (ctalegacymsgunittests
ctamessages
ctautils)
install(TARGETS ctalegacymsgunittests DESTINATION usr/${CMAKE_INSTALL_LIBDIR})
/******************************************************************************
*
* 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/io/io.hpp"
#include "castor/legacymsg/CommonMarshal.hpp"
#include <errno.h>
#include <iostream>
#include <string.h>
//-----------------------------------------------------------------------------
// marshal
//-----------------------------------------------------------------------------
size_t castor::legacymsg::marshal(char *const dst, const size_t dstLen,
const MessageHeader &src) {
if(dst == NULL) {
cta::exception::Exception ex;
ex.getMessage() << "Failed to marshal MessageHeader"
<< ": Pointer to destination buffer is NULL";
throw ex;
}
// Calculate the length of the message header
const uint32_t totalLen = 3 * sizeof(uint32_t); // magic + reqType + len
// Check that the message header buffer is big enough
if(totalLen > dstLen) {
cta::exception::Exception ex;
ex.getMessage() << "Failed to marshal MessageHeader"
": Buffer too small : required=" << totalLen << " actual=" << dstLen;
throw ex;
}
// Marshal the message header
char *p = dst;
io::marshalUint32(src.magic , p);
io::marshalUint32(src.reqType , p);
io::marshalUint32(src.lenOrStatus, p);
// Calculate the number of bytes actually marshalled
const size_t nbBytesMarshalled = p - dst;
// Check that the number of bytes marshalled was what was expected
if(totalLen != nbBytesMarshalled) {
cta::exception::Exception ex;
ex.getMessage() << "Failed to marshal MessageHeader"
": Mismatch between expected total length and actual"
": expected=" << totalLen << " actual=" << nbBytesMarshalled;
throw ex;
}
return totalLen;
}
//-----------------------------------------------------------------------------
// unmarshal
//-----------------------------------------------------------------------------
void castor::legacymsg::unmarshal(const char * &src, size_t &srcLen,
MessageHeader &dst) {
io::unmarshalUint32(src, srcLen, dst.magic);
io::unmarshalUint32(src, srcLen, dst.reqType);
io::unmarshalUint32(src, srcLen, dst.lenOrStatus);
}
/******************************************************************************
*
* 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 "castor/legacymsg/MessageHeader.hpp"
#include "common/exception/Exception.hpp"
#include <errno.h>
#include <stdint.h>
#include <string>
#include <time.h>
namespace castor {
namespace legacymsg {
/**
* Marshals the specified source message header structure into the
* specified destination buffer.
*
* @param dst The destination buffer.
* @param dstLen The length of the destination buffer.
* @param src The source structure.
* @return The total length of the header.
*/
size_t marshal(char *const dst, const size_t dstLen,
const MessageHeader &src);
/**
* Marshals the specified source message header structure into the
* specified destination buffer.
*
* @param dst The destination buffer.
* @param src The source structure.
* @return The total length of the header.
*/
template<int n> size_t marshal(char (&dst)[n],
const MessageHeader &src) {
return marshal(dst, n, src);
}
/**
* Unmarshals a message header from the specified source buffer into the
* specified destination message header structure.
*
* @param src In/out parameter, before invocation points to the source
* buffer where the message header should be unmarshalled from
* and on return points to the byte in the source buffer
* immediately after the unmarshalled message header.
* @param srcLen In/out parameter, before invocation is the length of the
* source buffer from where the message header should be
* unmarshalled and on return is the number of bytes remaining in
* the source buffer.
* @param dst The destination structure.
*/
void unmarshal(const char * &src, size_t &srcLen, MessageHeader &dst);
} // namespace legacymsg
} // 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/legacymsg/CommonMarshal.hpp"
#include "castor/utils/utils.hpp"
#include <gtest/gtest.h>
namespace unitTests {
class castor_legacymsg_CommonMarshalTest : public ::testing::Test {
protected:
virtual void SetUp() {
}
virtual void TearDown() {
}
};
TEST_F(castor_legacymsg_CommonMarshalTest, marshalMessageHeader) {
using namespace castor::legacymsg;
char buf[12];
// Marshal message header
{
MessageHeader srcMsgHeader;
srcMsgHeader.magic = 1;
srcMsgHeader.reqType = 2;
srcMsgHeader.lenOrStatus = 3;
size_t bufLen = sizeof(buf);
size_t headerLen = 0;
ASSERT_NO_THROW(headerLen = marshal(buf, bufLen, srcMsgHeader));
ASSERT_EQ((size_t)12, headerLen);
}
// Unmarshall message header
{
MessageHeader dstHeader;
const char *bufPtr = buf;
size_t bufLen = 12; // Length of the message header
ASSERT_NO_THROW(unmarshal(bufPtr, bufLen, dstHeader));
ASSERT_EQ(buf + 12, bufPtr);
ASSERT_EQ((size_t)0, bufLen);
ASSERT_EQ((uint32_t)1, dstHeader.magic);
ASSERT_EQ((uint32_t)2, dstHeader.reqType);
ASSERT_EQ((uint32_t)3, dstHeader.lenOrStatus);
}
}
} // 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/io/io.hpp"
#include "castor/legacymsg/GenericMarshal.hpp"
#include <string.h>
//-----------------------------------------------------------------------------
// marshal
//-----------------------------------------------------------------------------
size_t castor::legacymsg::marshal(char *const dst, const size_t dstLen,
const uint32_t srcMagic, const uint32_t srcReqType,
const GenericReplyMsgBody &srcBody) {
const char *const task = "marshal GenericReplyMsgBody";
if(dst == NULL) {
cta::exception::Exception ex;
ex.getMessage() << "Failed to " << task <<
": Pointer to destination buffer is NULL";
throw ex;
}
// Calculate the length of the message body
const uint32_t bodyLen =
sizeof(srcBody.status) +
strlen(srcBody.errorMessage) + 1;
// Calculate the total length of the message (header + body)
const uint32_t totalLen =
sizeof(uint32_t) + // magic
sizeof(uint32_t) + // reqType
sizeof(uint32_t) + // len
bodyLen;
// Check that the message buffer is big enough
if(totalLen > dstLen) {
cta::exception::Exception ex;
ex.getMessage() << "Failed to " << task <<
": Buffer too small: required=" << totalLen << " actual=" << dstLen;
throw ex;
}
// Marshal message header
char *p = dst;
try {
io::marshalUint32(srcMagic, p);
io::marshalUint32(srcReqType, p);
io::marshalUint32(bodyLen, p);
} catch(cta::exception::Exception &ne) {
cta::exception::Exception ex;
ex.getMessage() << "Failed to " << task << ": Failed to marshal header: "
<< ne.getMessage().str();
throw ex;
}
// Marshal message body
try {
io::marshalUint32(srcBody.status, p);
io::marshalString(srcBody.errorMessage, p);
} catch(cta::exception::Exception &ne) {
cta::exception::Exception ex;
ex.getMessage() << "Failed to " << task << ": Failed to marshal body: "
<< ne.getMessage().str();
throw ex;
}
// Calculate the number of bytes actually marshalled
const size_t nbBytesMarshalled = p - dst;
// Check that the number of bytes marshalled was what was expected
if(totalLen != nbBytesMarshalled) {
cta::exception::Exception ex;
ex.getMessage() << "Failed to " << task <<
": Mismatch between expected total length and actual"
": expected=" << totalLen << " actual=" << nbBytesMarshalled;
throw ex;
}
return totalLen;
}
//-----------------------------------------------------------------------------
// unmarshal
//-----------------------------------------------------------------------------
void castor::legacymsg::unmarshal(const char * &src, size_t &srcLen,
GenericReplyMsgBody &dst) {
try {
io::unmarshalUint32(src, srcLen, dst.status);
io::unmarshalString(src, srcLen, dst.errorMessage);
} catch(cta::exception::Exception &ne) {
cta::exception::Exception ex;
ex.getMessage() << "Failed to unmarshal GenericReplyMsgBody: " <<
ne.getMessage().str();
throw ex;
}
}
//-----------------------------------------------------------------------------
// marshal
//-----------------------------------------------------------------------------
size_t castor::legacymsg::marshal(char *const dst, const size_t dstLen,
const uint32_t srcMagic, const uint32_t srcReqType,
const GenericErrorReplyMsgBody &srcBody) {
const char *const task = "marshal GenericErrorReplyMsgBody";
if(dst == NULL) {
cta::exception::Exception ex;
ex.getMessage() << "Failed to " << task <<
": Pointer to destination buffer is NULL";
throw ex;
}
// Calculate the length of the message body
const uint32_t bodyLen = strlen(srcBody.errorMessage) + 1;
// Calculate the total length of the message (header + body)
const uint32_t totalLen =
sizeof(uint32_t) + // magic
sizeof(uint32_t) + // reqType
sizeof(uint32_t) + // len
bodyLen;
// Check that the message buffer is big enough
if(totalLen > dstLen) {
cta::exception::Exception ex;
ex.getMessage() << "Failed to " << task <<
": Buffer too small: required=" << totalLen << " actual=" << dstLen;
throw ex;
}
// Marshal message header
char *p = dst;
try {
io::marshalUint32(srcMagic, p);
io::marshalUint32(srcReqType, p);
io::marshalUint32(bodyLen, p);
} catch(cta::exception::Exception &ne) {
cta::exception::Exception ex;
ex.getMessage() << "Failed to " << task << ": Failed to marshal header: "
<< ne.getMessage().str();
throw ex;
}
// Marshal message body
try {
io::marshalString(srcBody.errorMessage, p);
} catch(cta::exception::Exception &ne) {
cta::exception::Exception ex;
ex.getMessage() << "Failed to " << task << ": Failed to marshal body: "
<< ne.getMessage().str();
throw ex;
}
// Calculate the number of bytes actually marshalled
const size_t nbBytesMarshalled = p - dst;
// Check that the number of bytes marshalled was what was expected
if(totalLen != nbBytesMarshalled) {
cta::exception::Exception ex;
ex.getMessage() << "Failed to " << task <<
": Mismatch between expected total length and actual"
": expected=" << totalLen << " actual=" << nbBytesMarshalled;
throw ex;
}
return totalLen;
}
//-----------------------------------------------------------------------------
// unmarshal
//-----------------------------------------------------------------------------
void castor::legacymsg::unmarshal(const char * &src, size_t &srcLen,
GenericErrorReplyMsgBody &dst) {
try {
io::unmarshalString(src, srcLen, dst.errorMessage);
} catch(cta::exception::Exception &ne) {
cta::exception::Exception ex;
ex.getMessage() << "Failed to unmarshal GenericErrorReplyMsgBody: " <<
ne.getMessage().str();
throw ex;
}
}
/******************************************************************************
*
* 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/Exception.hpp"
#include "castor/legacymsg/GenericReplyMsgBody.hpp"
#include "castor/legacymsg/GenericErrorReplyMsgBody.hpp"
#include <stdint.h>
namespace castor {
namespace legacymsg {
/**
* Marshals the specified source message into the specified destination buffer.
*
* Please note that this method marshals the length of the message body as the
* third field of the message header (message header = magic + reqType + len).
*
* @param dst The destination message buffer.
* @param dstLen The length of the destination buffer.
* @param srcMagic The magic number of the source message.
* @param srcReqType The request type of the source message.
* @param srcBody The body of the source message.
*
* @return The total length of the message (header + body).
*/
size_t marshal(char *const dst, const size_t dstLen, const uint32_t srcMagic,
const uint32_t srcReqType, const GenericReplyMsgBody &srcBody)
;
/**
* Marshals the specified source message into the specified destination buffer.
*
* Please note that this method marshals the length of the message body as the
* third field of the message header (message header = magic + reqType + len).
*
* @param dst The destination message buffer.
* @param srcMagic The magic number of the source message.
* @param srcReqType The request type of the source message.
* @param srcBody The body of the source message.
* @return The total length of the message (header + body).
*/
template<int n> size_t marshal(char (&dst)[n], const uint32_t srcMagic,
const uint32_t srcReqType, const GenericReplyMsgBody &srcBody)
{
return marshal(dst, n, srcMagic, srcReqType, srcBody);
}
/**
* Unmarshals a message body with the specified destination structure type
* from the specified source buffer.
*
* @param src In/out parameter, before invocation points to the source
* buffer where the message body should be unmarshalled from and on return
* points to the byte in the source buffer immediately after the
* unmarshalled message body.
* @param srcLen In/out parameter, before invocation is the length of the
* source buffer from where the message body should be unmarshalled and on
* return is the number of bytes remaining in the source buffer.
* @param dst The destination message body structure.
*/
void unmarshal(const char * &src, size_t &srcLen, GenericReplyMsgBody &dst) ;
/**
* Marshals the specified source message into the specified destination buffer.
*
* Please note that this method marshals the length of the message body as the
* third field of the message header (message header = magic + reqType + len).
*
* @param dst The destination message buffer.
* @param dstLen The length of the destination buffer.
* @param srcMagic The magic number of the source message.
* @param srcReqType The request type of the source message.
* @param srcBody The body of the source message.
*
* @return The total length of the message (header + body).
*/
size_t marshal(char *const dst, const size_t dstLen, const uint32_t srcMagic,
const uint32_t srcReqType, const GenericErrorReplyMsgBody &srcBody)
;
/**
* Marshals the specified source message into the specified destination buffer.
*
* Please note that this method marshals the length of the message body as the
* third field of the message header (message header = magic + reqType + len).
*
* @param dst The destination message buffer.
* @param srcMagic The magic number of the source message.
* @param srcReqType The request type of the source message.
* @param srcBody The body of the source message.
* @return The total length of the message (header + body).
*/
template<int n> size_t marshal(char (&dst)[n], const uint32_t srcMagic,