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 
44 namespace Pt {
45 
46 namespace JsonRpc {
47 
48 class ProcedureDeclaration;
49 class ServiceDeclaration;
50 
53 class 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 
101  bool parseMessage();
102 
106 
110 
113  void beginResult(std::ostream& os);
114 
117  void beginFault(std::ostream& os, const Fault& fault);
118 
124 
127  void finishResult();
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
Core module.
Definition: Allocator.h:33
Manages the decomposition of types during serialization.
Definition: Decomposer.h:44
void finishMessage(System::EventLoop &loop)
Execute the service procedure after parsing completes.
virtual void onReady()
The service procedure has finished.
Formatter for JSON-RPC message serialization and deserialization.
Definition: Formatter.h:57
bool isFailed() const
Indicates if the procedure has failed.
void beginResult(std::ostream &os)
Begin formatting a JSON-RPC success response.
Convert between unicode and UTF-8.
Definition: Utf8Codec.h:44
void setFault(int rc, const char *msg)
Mark the procedure as failed.
Maps parameter names to positional indices for named parameters.
Definition: ProcedureDeclaration.h:49
Declares named parameter mappings for JSON-RPC procedures.
Definition: ServiceDeclaration.h:49
Remote service definition.
Definition: Api-ServiceDefinition.h:22
JSON document node.
Definition: Node.h:52
void finishMessage()
Execute the service procedure synchronously.
Reads JSON as a Stream of Nodes.
Definition: JsonReader.h:51
void beginFault(std::ostream &os, const Fault &fault)
Begin formatting a JSON-RPC error response.
virtual void onFault(const Fault &fault)=0
The service procedure has failed.
Dispatches requests to a service procedure.
Definition: Responder.h:50
void beginMessage(std::istream &is)
Begin parsing a JSON-RPC request from a stream.
Dispatches JSON-RPC requests to a service procedure.
Definition: Responder.h:54
virtual void onCancel()
Cancels all operations.
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: EventLoop.h:83
Responder(const ServiceDeclaration &decl, Remoting::ServiceDefinition &def)
Construct with ServiceDeclaration and ServiceDefinition.
Composes types during serialization.
Definition: Composer.h:43
bool parseMessage()
Parse available data from the input stream.
virtual ~Responder()
Destructor.
JSON-RPC fault exception.
Definition: Fault.h:44
void finishResult()
Finish formatting the result response.
virtual void onResult()=0
The service procedure has finished.
bool advanceResult()
Continue formatting the result.
Writes JSON to a text stream.
Definition: JsonWriter.h:46