Responder.h
1/*
2 * Copyright (C) 2020-2026 by Marc Boris Duerner
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27 * MA 02110-1301 USA
28 */
29
30#ifndef PT_JSONRPC_RESPONDER_H
31#define PT_JSONRPC_RESPONDER_H
32
33#include <Pt/JsonRpc/Api.h>
34#include <Pt/JsonRpc/Fault.h>
35#include <Pt/JsonRpc/Formatter.h>
36#include <Pt/Remoting/Responder.h>
37#include <Pt/Json/JsonReader.h>
38#include <Pt/Json/JsonWriter.h>
39#include <Pt/TextStream.h>
40#include <Pt/NonCopyable.h>
41#include <Pt/Utf8Codec.h>
42#include <Pt/Types.h>
43
44namespace Pt {
45
46namespace JsonRpc {
47
50
53class PT_JSONRPC_API Responder : public Remoting::Responder
54{
55 public:
60
63 virtual ~Responder();
64
67 bool isFailed() const;
68
69 protected:
70 // inheritdoc
71 virtual void onReady();
72
73 // inheritdoc
74 virtual void onCancel();
75
81 virtual void onFault(const Fault& fault) = 0;
82
89 virtual void onResult() = 0;
90
91 protected:
94 void beginMessage(std::istream& is);
95
102
106
110
113 void beginResult(std::ostream& os);
114
117 void beginFault(std::ostream& os, const Fault& fault);
118
124
128
131 void setFault(int rc, const char* msg);
132
133 private:
135 bool advance(const Json::Node& node);
136
137 private:
138 enum State
139 {
140 OnBegin,
141 OnRequestObject,
142 OnMethod,
143 OnParams,
144 OnParam,
145 OnNamedParams,
146 OnNamedParam,
147 OnParamsEnd,
148 OnId,
149 OnEnd
150 };
151
152 const ServiceDeclaration* _decl;
153 const ProcedureDeclaration* _procedure;
154
155 Utf8Codec _utf8;
156 TextIStream _tis;
157 Json::JsonReader _reader;
158 Composer** _args;
159 State _state;
160 long long _id;
161 std::string _methodName;
162
163 TextOStream _tos;
164 Json::JsonWriter _writer;
165 Formatter _formatter;
166 Decomposer* _result;
167
168 Fault _fault;
169 bool _isFault;
170 Pt::varint_t _r1;
171 Pt::varint_t _r2;
172};
173
174} // namespace JsonRpc
175
176} // namespace Pt
177
178#endif // PT_JSONRPC_RESPONDER_H
Composes types during serialization.
Definition Composer.h:101
Manages the decomposition of types during serialization.
Definition Decomposer.h:121
JSON-RPC fault exception.
Definition Fault.h:44
Formatter for JSON-RPC message serialization and deserialization.
Definition Formatter.h:57
Maps parameter names to positional indices for named parameters.
Definition ProcedureDeclaration.h:49
Responder(const ServiceDeclaration &decl, Remoting::ServiceDefinition &def)
Construct with ServiceDeclaration and ServiceDefinition.
virtual ~Responder()
Destructor.
virtual void onCancel()
Cancels all operations.
void finishMessage()
Execute the service procedure synchronously.
void beginMessage(std::istream &is)
Begin parsing a JSON-RPC request from a stream.
void beginFault(std::ostream &os, const Fault &fault)
Begin formatting a JSON-RPC error response.
void beginResult(std::ostream &os)
Begin formatting a JSON-RPC success response.
virtual void onReady()
The service procedure has finished.
void setFault(int rc, const char *msg)
Mark the procedure as failed.
bool advanceResult()
Continue formatting the result.
bool parseMessage()
Parse available data from the input stream.
void finishResult()
Finish formatting the result response.
bool isFailed() const
Indicates if the procedure has failed.
virtual void onFault(const Fault &fault)=0
The service procedure has failed.
virtual void onResult()=0
The service procedure has finished.
void finishMessage(System::EventLoop &loop)
Execute the service procedure after parsing completes.
Declares named parameter mappings for JSON-RPC procedures.
Definition ServiceDeclaration.h:49
Reads JSON as a Stream of Nodes.
Definition JsonReader.h:51
Writes JSON to a text stream.
Definition JsonWriter.h:46
JSON document node.
Definition Node.h:52
Dispatches requests to a service procedure.
Definition Responder.h:50
Remote service definition.
Definition ServiceDefinition.h:59
Event loop of a thread or process.
Definition EventLoop.h:83
Convert between unicode and UTF-8.
Definition Utf8Codec.h:44
JSON-RPC 2.0 services and clients.
Core module.
Definition Allocator.h:33