-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathRemoteAPIClient.cpp
252 lines (218 loc) · 6.08 KB
/
RemoteAPIClient.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "RemoteAPIClient.h"
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <boost/format.hpp>
#include <boost/optional.hpp>
#include <random>
#include <sstream>
namespace uuid {
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_int_distribution<> dis(0, 15);
static std::uniform_int_distribution<> dis2(8, 11);
std::string generate_uuid_v4() {
std::stringstream ss;
int i;
ss << std::hex;
for (i = 0; i < 8; i++) {
ss << dis(gen);
}
ss << "-";
for (i = 0; i < 4; i++) {
ss << dis(gen);
}
ss << "-4";
for (i = 0; i < 3; i++) {
ss << dis(gen);
}
ss << "-";
ss << dis2(gen);
for (i = 0; i < 3; i++) {
ss << dis(gen);
}
ss << "-";
for (i = 0; i < 12; i++) {
ss << dis(gen);
};
return ss.str();
}
}
json bin(const char *s, int size)
{
if(size == -1) size = int(strlen(s));
return bin(reinterpret_cast<const uint8_t *>(s), size);
}
json bin(const uint8_t *b, int size)
{
return bin(std::vector<uint8_t>{b, b + size});
}
json bin(const std::string &s)
{
return bin(s.data(), int(s.length()));
}
json bin(const std::vector<uint8_t> &v)
{
return json{byte_string_arg, v};
}
RemoteAPIClient::RemoteAPIClient(const std::string host, int rpcPort, int cntPort, int verbose_)
: rpcSocket(ctx, zmq::socket_type::req),
verbose(verbose_)
{
if(verbose == -1)
{
if(const char* verboseStr = std::getenv("VERBOSE"))
verbose = std::atoi(verboseStr);
else
verbose = 0;
}
uuid = uuid::generate_uuid_v4();
VERSION = 2;
auto rpcAddr = (boost::format("tcp://%s:%d") % host % rpcPort).str();
rpcSocket.connect(rpcAddr);
}
RemoteAPIClient::~RemoteAPIClient()
{
json req;
req["func"] = "_*end*_";
req["args"] = json::array();
send(req);
recv();
}
json RemoteAPIClient::call(const std::string &func, std::initializer_list<json> args)
{
return call(func, json::make_array(args));
}
json RemoteAPIClient::call(const std::string &_func, const json &_args)
{ // call function with specified arguments. Is reentrant
std::string func(_func);
json args = _args;
json req;
req["func"] = func;
req["args"] = args;
send(req);
json resp = recv();
while (resp.contains("func"))
{ // We have a callback or a wait:
if (resp["func"].as<std::string>().compare("_*wait*_")==0)
{
func = "_*executed*_";
args = json::array();
json req2;
req2["func"] = func;
req2["args"] = args;
send(req2);
}
else if (resp["func"].as<std::string>().compare("_*repeat*_")==0)
{
json req2;
req2["func"] = func;
req2["args"] = args;
send(req2);
}
else
{ // call a callback
auto funcToRun = _getFunctionPointerByName(resp["func"].as<std::string>());
json rep;
func = "_*executed*_";
if (funcToRun)
{
auto r = funcToRun(resp["args"]);
if (!r.is_array())
{
auto arr = json::array();
arr.push_back(r);
args = arr;
}
else
args = r;
}
else
args = json::array();
rep["func"] = func;
rep["args"] = args;
send(rep);
}
resp = recv();
}
if (resp.contains("err"))
throw std::runtime_error(resp["err"].as<std::string>().c_str());
const auto &ret = resp["ret"];
return ret;
}
json RemoteAPIClient::getObject(const std::string &name)
{
return call("zmqRemoteApi.info", json(json_array_arg, {name}));
}
void RemoteAPIClient::require(const std::string &name)
{
call("zmqRemoteApi.require", json(json_array_arg, {name}));
}
void RemoteAPIClient::setVerbose(int level)
{
verbose = level;
}
void RemoteAPIClient::setStepping(bool enable)
{ // for backw. comp., now via sim.setStepping
call("sim.setStepping", {enable});
}
void RemoteAPIClient::step(bool wait)
{ // for backw. comp., now via sim.step
call("sim.step", {wait});
}
void RemoteAPIClient::send(json &j)
{
if(verbose > 0)
std::cout << "Sending: " << pretty_print(j) << std::endl;
j["uuid"] = uuid;
j["ver"] = VERSION;
j["lang"] = "c++";
if (j.contains("args"))
{
auto a = j["args"];
j["argsL"] = a.size();
}
std::vector<uint8_t> data;
cbor::encode_cbor(j, data);
if(verbose > 1)
{
std::cout << "Sending (raw):";
for(size_t i = 0; i < data.size(); i++)
std::cout << " " << std::hex << std::setw(2) << std::setfill('0') << int(data[i]);
std::cout << std::endl;
}
zmq::message_t msg(data.data(), data.size());
rpcSocket.send(msg, zmq::send_flags::dontwait);
}
json RemoteAPIClient::recv()
{
zmq::message_t msg;
rpcSocket.recv(msg);
auto data = reinterpret_cast<const uint8_t*>(msg.data());
if(verbose > 1)
{
std::cout << "Received (raw):";
for(size_t i = 0; i < msg.size(); i++)
std::cout << " " << std::hex << std::setw(2) << std::setfill('0') << int(data[i]);
std::cout << std::endl;
}
json j = cbor::decode_cbor<json>(data, data + msg.size());
if(verbose > 0)
std::cout << "Received: " << pretty_print(j) << std::endl;
return j;
}
void RemoteAPIClient::registerCallback(const std::string &funcName, CallbackType callback)
{
callbacks[funcName] = callback;
}
RemoteAPIClient::CallbackType RemoteAPIClient::_getFunctionPointerByName(const std::string &funcName)
{
auto it = callbacks.find(funcName);
if(it != callbacks.end())
return it->second;
return nullptr;
}
#ifdef SIM_REMOTEAPICLIENT_OBJECTS
#include "RemoteAPIObjects.cpp"
#endif // SIM_REMOTEAPICLIENT_OBJECTS