-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.cpp
124 lines (100 loc) · 2.62 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <unistd.h>
#include <fstream>
#include <iterator>
#include <nlohmann/json.hpp>
#include "Client.h"
using json = nlohmann::json;
void help()
{
std::cout
<< "client -a broker_address -s service_name -i [input.json|-] [-o output]"
<< std::endl;
}
int main(int argc, char *const argv[])
{
std::string address;
std::string serviceName;
std::string iname;
std::string oname;
for(int c; -1 != (c = ::getopt(argc, argv, "ha:s:i:"));)
{
switch(c)
{
case 'h':
help();
return EXIT_SUCCESS;
break;
case 'a':
address = optarg ? optarg : "";
break;
case 's':
serviceName = optarg ? optarg : "";
break;
case 'i':
iname = optarg ? optarg : "";
break;
case 'o':
oname = optarg ? optarg : "";
break;
case ':':
case '?':
default:
return EXIT_FAILURE;
break;
}
}
if(address.empty() || serviceName.empty() || iname.empty())
{
help();
return EXIT_FAILURE;
}
try
{
json input;
if("-" == iname) std::cin >> input;
else std::ifstream{iname} >> input;
Client::PayloadSeq payloadSeq;
payloadSeq.emplace_back(input.dump());
Client client;
const auto reply = client.exec(address, serviceName, payloadSeq);
ENSURE(!reply.empty(), RuntimeError);
auto begin = std::begin(reply);
ENSURE(MDP::Broker::Signature::statusSucess == *begin, RuntimeError);
++begin;
if(std::end(reply) == begin) goto exit;
if(oname.empty())
{
std::cout << '[';
for(;begin != std::end(reply);)
{
std::cout << *begin;
++begin;
if(begin != std::end(reply)) std::cout << ',';
}
std::cout << ']';
}
else
{
std::ofstream ofile{oname};
ofile << '[';
for(;begin != std::end(reply);)
{
ofile << *begin;
if(begin != std::end(reply)) ofile << ',';
}
ofile << ']';
}
}
catch(const std::exception &except)
{
TRACE(TraceLevel::Error, except.what());
return EXIT_FAILURE;
}
catch(...)
{
TRACE(TraceLevel::Error, "unsupported exception");
return EXIT_FAILURE;
}
exit:
return EXIT_SUCCESS;
}