Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
cta
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
dCache
cta
Commits
3ab8d7f4
Commit
3ab8d7f4
authored
10 years ago
by
Steven Murray
Browse files
Options
Downloads
Patches
Plain Diff
Improved type safety of castor::messages by removing templated methods
parent
c5145c90
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
castor/messages/messages.cpp
+67
-0
67 additions, 0 deletions
castor/messages/messages.cpp
castor/messages/messages.hpp
+12
-58
12 additions, 58 deletions
castor/messages/messages.hpp
with
79 additions
and
58 deletions
castor/messages/messages.cpp
+
67
−
0
View file @
3ab8d7f4
...
...
@@ -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
);
}
This diff is collapsed.
Click to expand it.
castor/messages/messages.hpp
+
12
−
58
View file @
3ab8d7f4
...
...
@@ -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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment