forked from joncol/jcon-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
270 lines (227 loc) · 8.4 KB
/
main.cpp
File metadata and controls
270 lines (227 loc) · 8.4 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
#include "example_service.h"
#include "other_service.h"
#include "notification_service.h"
#include <jcon/json_rpc_tcp_client.h>
#include <jcon/json_rpc_tcp_server.h>
#include <jcon/json_rpc_websocket_client.h>
#include <jcon/json_rpc_websocket_server.h>
#include <QCoreApplication>
#include <QUrl>
#include <ctime>
#include <iostream>
#include <memory>
enum class SocketType {tcp, websocket};
jcon::JsonRpcServer* startServer(QObject* parent,
SocketType socket_type = SocketType::tcp,
bool allow_notifications = false)
{
jcon::JsonRpcServer* rpc_server;
if (socket_type == SocketType::tcp) {
qDebug() << "Starting TCP server";
rpc_server = new jcon::JsonRpcTcpServer(parent);
} else {
qDebug() << "Starting WebSocket server";
rpc_server = new jcon::JsonRpcWebSocketServer(parent);
}
if (allow_notifications)
rpc_server->enableSendNotification(true);
auto service1 = new ExampleService;
auto service2 = new NotificationService;
rpc_server->registerServices({ service1, service2 });
rpc_server->listen(6002);
return rpc_server;
}
jcon::JsonRpcServer* startNamespacedServer(
QObject* parent,
SocketType socket_type = SocketType::tcp,
bool allow_notifications = false)
{
jcon::JsonRpcServer* rpc_server;
if (socket_type == SocketType::tcp) {
qDebug() << "Starting TCP server";
rpc_server = new jcon::JsonRpcTcpServer(parent);
} else {
qDebug() << "Starting WebSocket server";
rpc_server = new jcon::JsonRpcWebSocketServer(parent);
}
if (allow_notifications)
rpc_server->enableSendNotification(true);
auto service1 = new ExampleService;
auto service2 = new OtherService;
auto service3 = new NotificationService;
rpc_server->registerServices(
{
{ service1, "ex/myFirstNamespace" },
{ service2, "ex/myOtherNamespace" },
{ service3, "ex/myNotificationNamespace" }
}, "/");
rpc_server->listen(6002);
return rpc_server;
}
jcon::JsonRpcClient* startClient(QObject* parent,
SocketType socket_type = SocketType::tcp,
bool allow_notifications = false)
{
jcon::JsonRpcClient* rpc_client;
if (socket_type == SocketType::tcp) {
rpc_client = new jcon::JsonRpcTcpClient(parent);
rpc_client->connectToServer("127.0.0.1", 6002);
} else {
rpc_client = new jcon::JsonRpcWebSocketClient(parent);
// This is just to illustrate the fact that connectToServer also accepts
// a QUrl argument.
rpc_client->connectToServer(QUrl("ws://127.0.0.1:6002"));
}
if (allow_notifications)
rpc_client->enableReceiveNotification(true);
return rpc_client;
}
void invokeMethodAsync(jcon::JsonRpcClient* rpc_client)
{
auto req = rpc_client->callAsync("getRandomInt", 10);
req->connect(req.get(), &jcon::JsonRpcRequest::result,
[](const QVariant& result) {
qDebug() << "result of asynchronous RPC call:" << result;
});
req->connect(req.get(), &jcon::JsonRpcRequest::error,
[](int code, const QString& message) {
qDebug() << "RPC error:" << message
<< " (" << code << ")";
});
}
void invokeMethodSync(jcon::JsonRpcClient* rpc_client)
{
auto result = rpc_client->call("getRandomInt", 100);
if (result->isSuccess()) {
qDebug() << "result of synchronous RPC call:" << result->result();
} else {
qDebug() << "RPC error:" << result->toString();
}
}
void invokeStringMethodAsync(jcon::JsonRpcClient* rpc_client)
{
auto req = rpc_client->callAsync("printMessage", "hello, world");
req->connect(req.get(), &jcon::JsonRpcRequest::result,
[](const QVariant& result) {
qDebug() << "result of asynchronous RPC call:" << result;
});
req->connect(req.get(), &jcon::JsonRpcRequest::error,
[](int code, const QString& message) {
qDebug() << "RPC error:" << message
<< " (" << code << ")";
});
}
void invokeNamedParamsMethodAsync(jcon::JsonRpcClient* rpc_client)
{
auto req = rpc_client->callAsyncNamedParams("namedParams",
QVariantMap{
{"msg", "hello, world"},
{"answer", 42}
});
req->connect(req.get(), &jcon::JsonRpcRequest::result,
[](const QVariant& result) {
qDebug() << "result of asynchronous RPC call:" << result;
});
req->connect(req.get(), &jcon::JsonRpcRequest::error,
[](int code, const QString& message) {
qDebug() << "RPC error:" << message
<< " (" << code << ")";
});
}
void invokeStringMethodSync(jcon::JsonRpcClient* rpc_client)
{
auto result = rpc_client->call("printMessage", "hello, world");
if (result->isSuccess()) {
qDebug() << "result of synchronous RPC call:" << result->result();
} else {
qDebug() << "RPC error:" << result->toString();
}
}
void invokeNotification(jcon::JsonRpcClient* rpc_client)
{
rpc_client->notification("printNotification", "hello, notification");
}
void invokeNamespacedMethods(jcon::JsonRpcClient* rpc_client)
{
auto result = rpc_client->call("ex/myFirstNamespace/getRandomInt", 100);
if (result->isSuccess()) {
qDebug() << "result of first namespaced synchronous RPC call:"
<< result->result();
} else {
qDebug() << "RPC error:" << result->toString();
}
result = rpc_client->call("ex/myOtherNamespace/getRandomInt", 100);
if (result->isSuccess()) {
qDebug() << "result of second namespaced synchronous RPC call:"
<< result->result();
} else {
qDebug() << "RPC error:" << result->toString();
}
}
void waitForOutstandingRequests(jcon::JsonRpcClient* rpc_client)
{
if (rpc_client->outstandingRequestCount() > 0) {
qDebug().noquote() << QString("Waiting for %1 outstanding requests")
.arg(rpc_client->outstandingRequestCount());
while (rpc_client->outstandingRequestCount() > 0) {
qDebug() << "Calling QCoreApplication::processEvents()";
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
}
} else {
qDebug() << "No outstanding requests, quitting";
}
}
/**
* Example code of running both a server and a client and making some requests
* from the client to the server.
*/
void runServerAndClient(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
{
auto server = startServer(nullptr, SocketType::tcp);
auto rpc_client = startClient(&app, SocketType::tcp);
invokeNotification(rpc_client);
invokeMethodAsync(rpc_client);
invokeMethodSync(rpc_client);
invokeStringMethodSync(rpc_client);
invokeStringMethodAsync(rpc_client);
invokeNamedParamsMethodAsync(rpc_client);
waitForOutstandingRequests(rpc_client);
delete server;
}
{
auto server = startNamespacedServer(nullptr);
auto rpc_client = startClient(&app, SocketType::tcp);
invokeNamespacedMethods(rpc_client);
waitForOutstandingRequests(rpc_client);
delete server;
}
{
auto server = startServer(nullptr, SocketType::tcp, true);
auto rpc_client = startClient(&app, SocketType::tcp, true);
QObject::connect(rpc_client, &jcon::JsonRpcClient::notificationReceived,
&app, [](const QString& key, const QVariant& value){
qDebug() << "Received notification:"
<< "Key:" << key
<< "Value:" << value;
});
app.exec();
delete server;
}
}
/**
* Example of starting a server and keeping it running indefinitely.
*/
void runServer(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
auto server = startServer(nullptr, SocketType::websocket);
app.exec();
}
int main(int argc, char* argv[])
{
runServerAndClient(argc, argv);
// runServer(argc, argv);
return 0;
}