-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrpcserver.h
More file actions
186 lines (161 loc) · 5.62 KB
/
rpcserver.h
File metadata and controls
186 lines (161 loc) · 5.62 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
//
// 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.
//
#ifndef __RPC_SERVER_H__
#define __RPC_SERVER_H__
#include <condition_variable>
#include <functional>
#include <map>
#include <memory>
#include <mutex>
#include <queue>
#include <thread>
struct cJSON;
class RpcService;
using RpcDataHandler = std::function<void (char const* buff, int n)>;
using RpcNotificationFunction = std::function<void (cJSON const* json)>;
using RpcMethod = std::function<cJSON* (cJSON const* req)>;
using RpcMethodMap = std::map< std::string, RpcMethod >;
using RpcServiceConstructor = std::function<RpcService* ()>;
struct DeviceInfoProvider
{
std::function< std::string () > GetSystemId;
std::function< std::string () > GetModelNumber;
std::function< std::string () > GetSerialNumber;
std::function< std::string () > GetFirmwareRevision;
std::function< std::string () > GetHardwareRevision;
std::function< std::string () > GetSoftwareRevision;
std::function< std::string () > GetManufacturerName;
};
class RpcConnectedClient
{
public:
RpcConnectedClient() { }
virtual ~RpcConnectedClient() { }
virtual void init(DeviceInfoProvider const& deviceInfoProvider) = 0;
virtual void enqueueForSend(char const* buff, int n) = 0;
virtual void run() = 0;
virtual void setDataHandler(RpcDataHandler const& handler) = 0;
};
class RpcService
{
public:
RpcService();
virtual ~RpcService();
virtual void init(cJSON const* conf, RpcNotificationFunction const& callback) = 0;
virtual std::string name() const = 0;
virtual std::vector<std::string> methodNames() const = 0;
virtual cJSON* invokeMethod(std::string const& name, cJSON const* req) = 0;
public:
static void registerServiceConstructor(std::string const& name, RpcServiceConstructor const& ctor);
static RpcService* createServiceByName(std::string const& name);
};
class RpcServiceRegistrar
{
public:
RpcServiceRegistrar(std::string const& name, RpcServiceConstructor const& ctor);
};
#define JSONRPC_SERVICE_DEFINE(NAME, CTOR) static RpcServiceRegistrar __ ## NAME(#NAME, CTOR)
class BasicRpcService : public RpcService
{
public:
BasicRpcService(std::string const& name);
virtual ~BasicRpcService();
virtual std::string name() const override;
virtual std::vector<std::string> methodNames() const override;
virtual cJSON* invokeMethod(std::string const& name, cJSON const* req) override;
virtual void init(cJSON const* conf, RpcNotificationFunction const& callback) override;
protected:
void registerMethod(std::string const& name, RpcMethod const& method);
void notifyAndDelete(cJSON* json);
protected:
cJSON* m_config;
private:
RpcMethodMap m_methods;
std::string m_name;
RpcNotificationFunction m_notify;
};
class RpcListener
{
public:
RpcListener() { }
virtual ~RpcListener() { }
virtual void init(cJSON const* conf) = 0;
virtual std::shared_ptr<RpcConnectedClient>
accept(DeviceInfoProvider const& deviceInfoProvider) = 0;
public:
static std::shared_ptr<RpcListener> create();
};
class RpcServer
{
public:
RpcServer(std::string const& configFile, cJSON const* config);
~RpcServer();
private:
class RpcSystemService : public BasicRpcService
{
public:
RpcSystemService(RpcServer* parent);
virtual ~RpcSystemService();
virtual void init(cJSON const* conf, RpcNotificationFunction const& callback) override;
private:
cJSON* listServices(cJSON const* req);
cJSON* listMethods(cJSON const* req);
cJSON* getServerPublicKey(cJSON const* req);
cJSON* setClientPublicKey(cJSON const* req);
private:
RpcServer* m_server;
};
struct RpcMethodInfo
{
RpcMethodInfo() { }
RpcMethodInfo(std::string const& service, std::string const& method)
: ServiceName(service)
, MethodName(method) { }
std::string const ServiceName;
std::string const MethodName;
std::string toString() const;
static RpcMethodInfo parseMethod(char const* s);
};
friend class RpcSystemService;
public:
void setClient(std::shared_ptr<RpcConnectedClient> const& tport);
void registerService(std::shared_ptr<RpcService> const& service);
void stop();
void run();
void enqueueAsyncMessage(cJSON const* json);
void onIncomingMessage(const char* buff, int n);
void setLastChanceHandler(RpcMethod const& lastChanceHandler);
private:
void processIncomingQueue();
void processRequest(cJSON const* req);
cJSON* processJsonRpcRequest(cJSON const* req);
cJSON* processNonJsonRpcRequest(cJSON const* req);
cJSON* invokeMethod(RpcMethodInfo const& methodInfo, cJSON const* req);
private:
std::shared_ptr<RpcConnectedClient> m_client;
std::mutex m_mutex;
std::shared_ptr<std::thread> m_dispatch_thread;
std::queue<cJSON *> m_incoming_queue;
std::condition_variable m_cond;
std::map< std::string, std::shared_ptr<RpcService> > m_services;
cJSON* m_config;
std::string m_config_file;
RpcMethod m_last_chance;
bool m_running;
};
// not sure where to put these
std::string chomp(char const* s);
#endif