-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrpcserver.cc
More file actions
597 lines (514 loc) · 13.1 KB
/
rpcserver.cc
File metadata and controls
597 lines (514 loc) · 13.1 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
//
// Copyright [2018] [Comcast, Corp]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "defs.h"
#include "rpcserver.h"
#include "rpclogger.h"
#include "jsonrpc.h"
#include <sstream>
#ifdef WITH_BLUEZ
#include "bluez/gattServer.h"
#endif
#include <string.h>
#include <stdarg.h>
#include <sys/stat.h>
namespace
{
class JsonDeleter
{
public:
JsonDeleter(cJSON*& json) : m_json(json) { }
~JsonDeleter()
{
if (m_json)
cJSON_Delete(m_json);
}
private:
cJSON*& m_json;
};
bool fileExists(char const* s)
{
struct stat buf;
memset(&buf, 0, sizeof(buf));
return stat(s, &buf) == 0;
}
std::map< std::string, RpcServiceConstructor > serviceConstructors;
}
std::string
RpcServer::RpcMethodInfo::toString() const
{
std::stringstream buff;
buff << ServiceName;
buff << '-';
buff << MethodName;
return buff.str();
}
RpcServer::RpcMethodInfo
RpcServer::RpcMethodInfo::parseMethod(char const* s)
{
std::string service;
std::string method;
char const* p = strchr(s, '-');
if (p)
{
service = std::string(s, (p-s));
method = std::string(p + 1);
}
return RpcMethodInfo(service, method);
}
std::shared_ptr<RpcListener>
RpcListener::create()
{
return std::shared_ptr<RpcListener>(
#ifdef WITH_BLUEZ
new GattServer()
#endif
);
}
RpcService*
RpcService::createServiceByName(std::string const& name)
{
RpcService* service = nullptr;
auto itr = serviceConstructors.find(name);
if (itr != serviceConstructors.end())
service = itr->second();
return service;
}
RpcServiceRegistrar::RpcServiceRegistrar(std::string const& name, RpcServiceConstructor const& ctor)
{
RpcService::registerServiceConstructor(name, ctor);
}
void
RpcService::registerServiceConstructor(std::string const& name, RpcServiceConstructor const& ctor)
{
serviceConstructors.insert(std::make_pair(name, ctor));
}
RpcService::RpcService()
{
}
RpcService::~RpcService()
{
}
BasicRpcService::BasicRpcService(std::string const& name)
: RpcService()
, m_config(nullptr)
, m_name(name)
{
}
BasicRpcService::~BasicRpcService()
{
if (m_config)
cJSON_Delete(m_config);
}
std::string
BasicRpcService::name() const
{
return m_name;
}
std::vector<std::string>
BasicRpcService::methodNames() const
{
std::vector<std::string> names;
for (auto itr : m_methods)
names.push_back(itr.first);
return names;
}
void
BasicRpcService::registerMethod(std::string const& name, RpcMethod const& method)
{
m_methods.insert(std::make_pair(name, method));
}
void
BasicRpcService::init(cJSON const* config, RpcNotificationFunction const& callback)
{
m_notify = callback;
m_config = cJSON_Duplicate(config, true);
}
void
BasicRpcService::notifyAndDelete(cJSON* json)
{
if (!json)
return;
if (m_notify)
m_notify(json);
cJSON_Delete(json);
}
cJSON*
BasicRpcService::invokeMethod(std::string const& name, cJSON const* req)
{
cJSON* res = nullptr;
XLOG_INFO("invoke method:%s-%s", m_name.c_str(), name.c_str());
if (!req)
{
XLOG_ERROR("cannot invoke method %s-%s with null request",
m_name.c_str(), name.c_str());
res = JsonRpc::makeError(-1, "no request object");
}
else
{
auto itr = m_methods.find(name);
if (itr == m_methods.end())
{
XLOG_WARN("method %s-%s not found", m_name.c_str(), name.c_str());
res = JsonRpc::makeError(-1, "method %s-%s not found", m_name.c_str(), name.c_str());
}
else
{
// actually invoke the method
res = itr->second(req);
}
}
return res;
}
RpcServer::RpcServer(std::string const& configFile, cJSON const* config)
: m_config_file(configFile)
, m_running(false)
{
if (config)
m_config = cJSON_Duplicate(config, true);
else
m_config = nullptr;
std::shared_ptr<RpcService> s(new RpcSystemService(this));
registerService(s);
if (m_config)
{
cJSON const* services = cJSON_GetObjectItem(config, "services");
if (services)
{
for (int i = 0, n = cJSON_GetArraySize(services); i < n; ++i)
{
cJSON const* service = cJSON_GetArrayItem(services, i);
cJSON const* name = cJSON_GetObjectItem(service, "name");
std::shared_ptr<RpcService> s(RpcService::createServiceByName(name->valuestring));
if (s)
registerService(s);
else
XLOG_WARN("failed to create service:%s", name->valuestring);
}
}
}
m_dispatch_thread.reset(new std::thread([this] { this->processIncomingQueue(); }));
}
RpcServer::~RpcServer()
{
if (m_config)
cJSON_Delete(m_config);
{
std::lock_guard<std::mutex> lock(m_mutex);
m_running = false;
}
m_cond.notify_one();
m_dispatch_thread->join();
}
void
RpcServer::setClient(std::shared_ptr<RpcConnectedClient> const& client)
{
std::lock_guard<std::mutex> guard(m_mutex);
m_client = client;
}
void
RpcServer::stop()
{
std::lock_guard<std::mutex> guard(m_mutex);
m_client.reset();
}
void
RpcServer::run()
{
m_client->run();
stop();
}
void
RpcServer::enqueueAsyncMessage(cJSON const* json)
{
if (!json)
return;
char* s = cJSON_PrintUnformatted(json);
int n = static_cast<int>(strlen(s));
XLOG_INFO("notify:%s", s);
{
std::lock_guard<std::mutex> guard(m_mutex);
if (m_client)
m_client->enqueueForSend(s, n);
free(s);
}
}
void
RpcServer::onIncomingMessage(char const* s, int UNUSED_PARAM(n))
{
if (!s || strlen(s) == 0)
return;
XLOG_INFO("enqueue new incoming request");
std::lock_guard<std::mutex> guard(m_mutex);
cJSON* req = cJSON_Parse(s);
if (req)
{
m_incoming_queue.push(req);
m_cond.notify_all();
}
else
{
XLOG_ERROR("failed to parse incoming json request");
XLOG_ERROR("\"%s\"", s);
}
}
void
RpcServer::processIncomingQueue()
{
m_running = true;
while (true)
{
cJSON* req = nullptr;
XLOG_INFO("processing incoming queue");
{
std::unique_lock<std::mutex> guard(m_mutex);
m_cond.wait(guard, [this] { return !this->m_incoming_queue.empty() || !this->m_running; });
if (!m_running)
{
XLOG_INFO("worker thread got shutdown signal");
return;
}
if (!m_incoming_queue.empty())
{
req = m_incoming_queue.front();
m_incoming_queue.pop();
}
}
if (req)
{
JsonDeleter requestDeleter(req);
processRequest(req);
}
}
}
cJSON*
RpcServer::invokeMethod(RpcMethodInfo const& methodInfo, cJSON const* req)
{
cJSON* res = nullptr;
auto service = m_services.find(methodInfo.ServiceName);
if (service == m_services.end())
{
res = JsonRpc::makeError(ENOENT, "service %s not found",
methodInfo.ServiceName.c_str());
}
else
{
res = service->second->invokeMethod(methodInfo.MethodName, req);
if (!res)
res = JsonRpc::makeError(-1, "%s.%s returned null?",
methodInfo.ServiceName.c_str(),
methodInfo.MethodName.c_str());
}
return res;
}
void
RpcServer::processRequest(cJSON const* req)
{
cJSON* res = nullptr;
XLOG_INFO("processing new incoming request");
{
char* s = cJSON_Print(req);
if (s)
{
XLOG_INFO("req:%s", s);
free(s);
}
}
// ensure json-rpc request
if (!JsonRpc::getString(req, "jsonrpc", false, nullptr))
res = processNonJsonRpcRequest(req);
else
res = processJsonRpcRequest(req);
char* s = cJSON_Print(res);
if (s)
{
XLOG_INFO("res:%s", s);
{
std::lock_guard<std::mutex> guard(m_mutex);
if (m_client)
m_client->enqueueForSend(s, strlen(s));
}
free(s);
}
else
{
XLOG_ERROR("failed to serialize JSON response to string");
}
cJSON_Delete(res);
}
cJSON*
RpcServer::processNonJsonRpcRequest(cJSON const* req)
{
cJSON* res = nullptr;
XLOG_INFO("incoming request is not jsonrpc, dispatching to last chance");
if (m_last_chance)
{
try
{
res = m_last_chance(req);
}
catch (std::exception const& err)
{
res = JsonRpc::makeError(-1, "unhandled exception:%s", err.what());
}
}
else
{
XLOG_WARN("invalid non-jsonrpc request with no last chance handler registered");
res = JsonRpc::makeError(-1, "only jsonrpc is supported");
}
return res;
}
cJSON*
RpcServer::processJsonRpcRequest(cJSON const* req)
{
cJSON* res = nullptr;
cJSON* method = cJSON_GetObjectItem(req, "method");
if (!method)
{
XLOG_ERROR("request doesn't contain method");
res = JsonRpc::makeError(-1, "request doesn't contain a 'method'");
}
int requestId = -1;
if (!res)
{
cJSON* id = cJSON_GetObjectItem(req, "id");
if (!id)
{
XLOG_ERROR("request doesn't contain id");
res = JsonRpc::makeError(-1, "request doesn't contain an 'id'");
}
else
{
requestId = id->valueint;
}
}
if (!res)
{
try
{
res = invokeMethod(RpcMethodInfo::parseMethod(method->valuestring), req);
}
catch (std::exception const& err)
{
res = JsonRpc::makeError(-1, "unhandled exception:%s", err.what());
}
}
// if function returned { "code": 1234, ... } where code != 0, then
// it's an error, else it was ok. This is handled by the wrapResponse
int code = JsonRpc::getInt(res, "code", false, 0);
return JsonRpc::wrapResponse(code, res, requestId);
}
void
RpcServer::registerService(std::shared_ptr<RpcService> const& service)
{
XLOG_INFO("registering service:%s", service->name().c_str());
RpcNotificationFunction callback = std::bind(&RpcServer::enqueueAsyncMessage, this,
std::placeholders::_1);
m_services.insert(std::make_pair(service->name(), service));
// TODO: someone update JsonRpc::search to handle lists so we can do
// cJSON* conf = JsonRpc::search(m_conf, "/services/name/[@name='wifi']");
cJSON* conf = nullptr;
cJSON* serviceConfig = nullptr;
if (m_config)
serviceConfig = cJSON_GetObjectItem(m_config, "services");
if (serviceConfig)
{
for (int i = 0, n = cJSON_GetArraySize(serviceConfig); i < n; ++i)
{
cJSON* temp = cJSON_GetArrayItem(serviceConfig, i);
cJSON* name = cJSON_GetObjectItem(temp, "name");
if (name && strcmp(name->valuestring, service->name().c_str()) == 0)
{
conf = temp;
break;
}
}
}
if (conf == nullptr)
XLOG_WARN("service %s is missing configuration", service->name().c_str());
service->init(conf, callback);
}
RpcServer::RpcSystemService::RpcSystemService(RpcServer* parent)
: BasicRpcService("rpc")
, m_server(parent)
{
}
RpcServer::RpcSystemService::~RpcSystemService()
{
}
void
RpcServer::RpcSystemService::init(cJSON const* UNUSED_PARAM(config),
RpcNotificationFunction const& UNUSED_PARAM(callback))
{
// openssl genpkey -algorithm Ec -pkeyopt ec_paramgen_curve:P-256 -pkeyopt ec_param_enc:named_curve > /tmp/bootstrap_private.pem
// openssl pkey -pubout -in /tmp/bootstrap_private.pem > /tmp/bootstrap_public.pem
registerMethod("list-services", [this](cJSON const* req) -> cJSON* { return this->listServices(req); });
registerMethod("list-methods", [this](cJSON const* req) -> cJSON* { return this->listMethods(req); });
registerMethod("get-server-pubkey", [this](cJSON const* req) -> cJSON* { return this->getServerPublicKey(req); });
registerMethod("set-client-pubkey", [this](cJSON const* req) -> cJSON* { return this->setClientPublicKey(req); });
}
cJSON*
RpcServer::RpcSystemService::getServerPublicKey(cJSON const* req)
{
return JsonRpc::notImplemented(__FUNCTION__);
}
cJSON*
RpcServer::RpcSystemService::setClientPublicKey(cJSON const* req)
{
return JsonRpc::notImplemented(__FUNCTION__);
}
cJSON*
RpcServer::RpcSystemService::listServices(cJSON const* UNUSED_PARAM(req))
{
cJSON* res = cJSON_CreateObject();
cJSON* names = cJSON_CreateArray();
//cJSON* names = cJSON_AddArrayToObject(res, "services");
for (auto const& kv : m_server->m_services)
{
cJSON_AddItemToArray(names, cJSON_CreateString(kv.first.c_str()));
}
cJSON_AddItemToObject(res, "services", names);
return res;
}
cJSON*
RpcServer::RpcSystemService::listMethods(cJSON const* req)
{
cJSON* res = cJSON_CreateObject();
cJSON const* service = JsonRpc::search(req, "/params/service", true);
if (service)
{
// cJSON* names = cJSON_AddArrayToObject(res, "methods");
cJSON* names = cJSON_CreateArray();
auto itr = m_server->m_services.find(service->valuestring);
for (std::string const& s : itr->second->methodNames())
{
RpcMethodInfo methodInfo(service->valuestring, s);
cJSON_AddItemToArray(names, cJSON_CreateString(methodInfo.toString().c_str()));
}
cJSON_AddItemToObject(res, "methods", names);
}
return res;
}
std::string chomp(char const* s)
{
std::string t(s);
size_t n = strlen(s);
if (s[n - 1] == '\r' || s[n - 1] == '\n')
t = std::string(s, n - 1);
else
t = s;
return t;
}