-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathClient.cpp
62 lines (54 loc) · 1.66 KB
/
Client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "Client.h"
#include "Except.h"
auto Client::exec(
const std::string &address,
const std::string &serviceName,
const PayloadSeq &payloadSeq) -> PayloadSeq
{
auto zmqContext = ZMQContext{ZMQIdentity::unique(), address};
try
{
onRequest(MDP::Client::makeReq(serviceName, payloadSeq), zmqContext);
return onReply(onMessage(zmqContext.recv(), zmqContext, serviceName));
}
catch(const std::exception &except)
{
TRACE(TraceLevel::Error, this, " ", except.what(), " aborting");
return {};
}
catch(...)
{
TRACE(TraceLevel::Error, this, " Unsupported exception, aborting");
return {};
}
return {};
}
void Client::onRequest(Message message, ZMQContext &zmqContext)
{
TRACE(TraceLevel::Debug, this, " ", message);
zmqContext.send(std::move(message));
}
auto Client::onMessage(
Message msg,
const ZMQContext &,
const std::string &serviceName) -> Message
{
ENSURE(4 <= msg.parts(), MessageFormatInvalid);
/* Frame 0: empty */
ENSURE(0 == msg.size(0), MessageFormatInvalid);
/* Frame 1: six byte signature (client) */
ENSURE(6 == msg.size(1), MessageFormatInvalid);
ENSURE(MDP::Client::Signature::self == msg.get(1), MessageFormatInvalid);
/* Frame 2: service name */
ENSURE(serviceName == msg.get(2), MessageFormatInvalid);
/* Frame 3+: payload */
ENSURE(0 < msg.size(3), MessageFormatInvalid);
return msg;
}
auto Client::onReply(Message message) ->PayloadSeq
{
TRACE(TraceLevel::Debug, this, " ", message);
PayloadSeq seq;
for(size_t i = 3; i < message.parts(); ++i) seq.push_back(message.get(i));
return seq;
}