This repository has been archived by the owner on Oct 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.h
72 lines (66 loc) · 2.13 KB
/
server.h
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
/* Copyright (c) 2017-2018, EPFL/Blue Brain Project
*
* This file is part of Rockets <https://github.com/BlueBrain/Rockets>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3.0 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef ROCKETS_JSONRPC_SERVER_H
#define ROCKETS_JSONRPC_SERVER_H
#include <rockets/jsonrpc/cancellableReceiver.h>
#include <rockets/jsonrpc/notifier.h>
#include <rockets/ws/types.h>
namespace rockets
{
namespace jsonrpc
{
/**
* JSON-RPC server.
*
* The server can be used over any communication channel that provides the
* following methods:
*
* - void broadcastText(std::string message);
* Used for sending notifications to connected clients.
*
* - void handleText(ws::MessageCallback callback);
* Used to register a callback for processing the requests and notifications
* coming from the client(s).
*/
template <typename CommunicatorT>
class Server : public Notifier, public CancellableReceiver
{
public:
Server(CommunicatorT& server)
: CancellableReceiver([&server](std::string json, uintptr_t client) {
server.sendText(std::move(json), client);
})
, communicator{server}
{
communicator.handleText(
[this](ws::Request request, ws::ResponseCallback callback) {
process(std::move(request), callback);
});
}
private:
/** Notifier::_send */
void _send(std::string json) final
{
communicator.broadcastText(std::move(json));
}
CommunicatorT& communicator;
};
}
}
#endif