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
/
cancellableReceiver.h
108 lines (102 loc) · 3.44 KB
/
cancellableReceiver.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
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
/* 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_CANCELLABLE_RECEIVER_H
#define ROCKETS_JSONRPC_CANCELLABLE_RECEIVER_H
#include <rockets/jsonrpc/asyncReceiver.h>
namespace rockets
{
namespace jsonrpc
{
/**
* Extends the asynchronous receiver by providing cancelable requests, which
* will provide progress updates during their execution.
*
* The cancel notification needs to have the following payload:
*
* @code{.json}
* {
* "jsonrpc": "2.0",
* "method": "cancel",
* "params": { "id": <request_to_cancel> }
* }
* @endcode
*
* The progress notification will have the following payload:
*
* @code{.json}
* {
* "jsonrpc": "2.0",
* "method": "progress",
* "params": {
* "id": <request_id>
* "amount": float
* "string": operation
* }
* }
* @endcode
*/
class CancellableReceiver : public AsyncReceiver
{
public:
/** Constructor. */
explicit CancellableReceiver(SendTextCallback sendTextCb);
/**
* Bind a cancellable method to an async response callback.
*
* @param method to register.
* @param action to perform that will notify the caller upon completion.
* @throw std::invalid_argument if the method name starts with "rpc." or
* "cancel"
*/
void bindAsync(const std::string& method,
CancellableResponseCallback action);
/**
* Bind a cancellable method to an async response callback with templated
* parameters.
*
* The parameters object must be deserializable by a free function:
* from_json(Params& object, const std::string& json).
*
* @param method to register.
* @param action to perform that will notify the caller upon completion.
* @throw std::invalid_argument if the method name starts with "rpc." or
* "cancel"
*/
template <typename Params>
void bindAsync(
const std::string& method,
std::function<CancelRequestCallback(Params, uintptr_t, AsyncResponse,
ProgressUpdateCallback)>
action)
{
bindAsync(method,
[action](const Request& request, AsyncResponse callback,
ProgressUpdateCallback progressUpdate) {
Params params;
if (from_json(params, request.message))
return action(std::move(params), request.clientID,
callback, progressUpdate);
callback(Response::invalidParams());
return CancelRequestCallback{};
});
}
};
}
}
#endif