Skip to content
Snippets Groups Projects
Commit 3ab8d7f4 authored by Steven Murray's avatar Steven Murray
Browse files

Improved type safety of castor::messages by removing templated methods

parent c5145c90
Branches
Tags
No related merge requests found
......@@ -124,6 +124,16 @@ std::string castor::messages::computeSHA1Base64(const std::string &data) {
return computeSHA1Base64(data.c_str(), data.length());
}
//------------------------------------------------------------------------------
// computeSHA1Base64
//------------------------------------------------------------------------------
std::string castor::messages::computeSHA1Base64(
const google::protobuf::Message& msg) {
std::string buffer;
msg.SerializeToString(&buffer);
return computeSHA1Base64(buffer.c_str(),buffer.size());
}
//------------------------------------------------------------------------------
// computeSHA1Base64
//------------------------------------------------------------------------------
......@@ -161,3 +171,60 @@ std::string castor::messages::computeSHA1Base64(void const* const data,
return ret;
}
//-----------------------------------------------------------------------------
// recvReplyOrEx
//-----------------------------------------------------------------------------
void castor::messages::recvTapeReplyOrEx(ZmqSocket& socket,
google::protobuf::Message &body) {
recvReplyOrEx(socket, body, TPMAGIC, PROTOCOL_TYPE_TAPE, PROTOCOL_VERSION_1);
}
//-----------------------------------------------------------------------------
// recvReplyOrEx
//-----------------------------------------------------------------------------
void castor::messages::recvReplyOrEx(ZmqSocket& socket,
google::protobuf::Message &body,
const uint32_t magic, const uint32_t protocolType,
const uint32_t protocolVersion) {
const Frame frame = recvFrame(socket);
// Check the magic number
if(magic != frame.header.magic()) {
castor::exception::Exception ex;
ex.getMessage() << "Failed to receive message"
": Unexpected magic number: excpected=" << magic << " actual= " <<
frame.header.magic();
throw ex;
}
// Check the protocol type
if(protocolType != frame.header.protocoltype()) {
castor::exception::Exception ex;
ex.getMessage() << "Failed to receive message"
": Unexpected protocol type: excpected=" << protocolType << " actual= "
<< frame.header.protocoltype();
throw ex;
}
// Check the protocol version
if(protocolVersion != frame.header.protocolversion()) {
castor::exception::Exception ex;
ex.getMessage() << "Failed to receive message"
": Unexpected protocol version: excpected=" << protocolVersion <<
" actual= " << frame.header.protocolversion();
throw ex;
}
// If an exception message was received
if(messages::MSG_TYPE_EXCEPTION == frame.header.msgtype()) {
// Convert it into a C++ exception and throw it
messages::Exception exMsg;
frame.parseBodyIntoProtocolBuffer(exMsg);
castor::exception::Exception ex(exMsg.code());
ex.getMessage() << exMsg.message();
throw ex;
}
frame.parseBodyIntoProtocolBuffer(body);
}
......@@ -65,6 +65,14 @@ Frame recvFrame(ZmqSocket &socket);
*/
std::string computeSHA1Base64(const std::string &data);
/**
* Function to compute inn Base64 the SHA1 of a given ProtoBuff message.
* All fields must be set because we are going to serialize the message.
* @param msg
* @return the base64 sha1 of the serialized body
*/
std::string computeSHA1Base64(const google::protobuf::Message& msg);
/**
* Function to compute inn Base64 the SHA1 of a given buffer
* @param data The data
......@@ -73,18 +81,6 @@ std::string computeSHA1Base64(const std::string &data);
*/
std::string computeSHA1Base64(void const* const data, const int len);
/**
* Template function to compute inn Base64 the SHA1 of a given ProtoBuff message
* All fields have to be set because we are going to serialize the message
* @param msg
* @return the base64 sha1 of the serialized body
*/
template <class T> std::string computeSHA1Base64(const T& msg) {
std::string buffer;
msg.SerializeToString(&buffer);
return computeSHA1Base64(buffer.c_str(),buffer.size());
}
/**
* Connects the specified ZMQ socket to localhost on the given TCP/IP port.
*
......@@ -136,9 +132,7 @@ Header protoTapePreFillHeader();
* @param body Output parameter: The body of the good-day reply-message in the
* form of a Google protocol-buffer.
*/
template<class ZS, class PB> void recvTapeReplyOrEx(ZS& socket, PB &body) {
recvReplyOrEx(socket, body, TPMAGIC, PROTOCOL_TYPE_TAPE, PROTOCOL_VERSION_1);
}
void recvTapeReplyOrEx(ZmqSocket& socket, google::protobuf::Message &body);
/**
* Receives either a good-day reply-message or an exception message from the
......@@ -157,50 +151,10 @@ template<class ZS, class PB> void recvTapeReplyOrEx(ZS& socket, PB &body) {
* @param protocolType The expected protocol type of the message.
* @param protocolVersion The expected protocol version of the message.
*/
template<class ZS, class PB> void recvReplyOrEx(ZS& socket, PB &body,
void recvReplyOrEx(ZmqSocket& socket,
google::protobuf::Message &body,
const uint32_t magic, const uint32_t protocolType,
const uint32_t protocolVersion) {
const Frame frame = recvFrame(socket);
// Check the magic number
if(magic != frame.header.magic()) {
castor::exception::Exception ex;
ex.getMessage() << "Failed to receive message"
": Unexpected magic number: excpected=" << magic << " actual= " <<
frame.header.magic();
throw ex;
}
// Check the protocol type
if(protocolType != frame.header.protocoltype()) {
castor::exception::Exception ex;
ex.getMessage() << "Failed to receive message"
": Unexpected protocol type: excpected=" << protocolType << " actual= "
<< frame.header.protocoltype();
throw ex;
}
// Check the protocol version
if(protocolVersion != frame.header.protocolversion()) {
castor::exception::Exception ex;
ex.getMessage() << "Failed to receive message"
": Unexpected protocol version: excpected=" << protocolVersion <<
" actual= " << frame.header.protocolversion();
throw ex;
}
// If an exception message was received
if(messages::MSG_TYPE_EXCEPTION == frame.header.msgtype()) {
// Convert it into a C++ exception and throw it
messages::Exception exMsg;
frame.parseBodyIntoProtocolBuffer(exMsg);
castor::exception::Exception ex(exMsg.code());
ex.getMessage() << exMsg.message();
throw ex;
}
frame.parseBodyIntoProtocolBuffer(body);
}
const uint32_t protocolVersion);
} // namespace messages
} // namespace castor
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment