Responder.h
1 /* Copyright (C) 2020 Marc Boris Duerner
2 
3  This library is free software; you can redistribute it and/or
4  modify it under the terms of the GNU Lesser General Public
5  License as published by the Free Software Foundation; either
6  version 2.1 of the License, or (at your option) any later version.
7 
8  As a special exception, you may use this file as part of a free
9  software library without restriction. Specifically, if other files
10  instantiate templates or use macros or inline functions from this
11  file, or you compile this file and link it with other files to
12  produce an executable, this file does not by itself cause the
13  resulting executable to be covered by the GNU General Public
14  License. This exception does not however invalidate any other
15  reasons why the executable file might be covered by the GNU Library
16  General Public License.
17 
18  This library is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  Lesser General Public License for more details.
22 
23  You should have received a copy of the GNU Lesser General Public
24  License along with this library; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26  MA 02110-1301 USA
27 */
28 
29 #ifndef Pt_JsonRpc_Responder_h
30 #define Pt_JsonRpc_Responder_h
31 
32 #include <Pt/JsonRpc/Api.h>
33 #include <Pt/JsonRpc/Fault.h>
34 //#include <Pt/XmlRpc/Formatter.h>
35 #include <Pt/Remoting/Responder.h>
36 //#include <Pt/Xml/InputSource.h>
37 #include <Pt/Json/JsonReader.h>
38 #include <Pt/Json/JsonWriter.h>
39 #include <Pt/Json/JsonFormatter.h>
40 #include <Pt/TextStream.h>
41 #include <Pt/NonCopyable.h>
42 #include <Pt/Utf8Codec.h>
43 #include <Pt/Types.h>
44 
45 namespace Pt {
46 
47 namespace JsonRpc {
48 
51 class PT_JSONRPC_API Responder : public Remoting::Responder
52 {
53  public:
57 
60  virtual ~Responder();
61 
64  bool isFailed() const;
65 
66  protected:
67  // inheritdoc
68  virtual void onReady();
69 
70  // inheritdoc
71  virtual void onCancel();
72 
80  virtual void onFault(const Fault& fault) = 0;
81 
89  virtual void onResult() = 0;
90 
91  protected:
97  void beginMessage(std::istream& is);
98 
107  bool parseMessage();
108 
116 
122  void beginResult(std::ostream& os);
123 
124  void beginFault(std::ostream& os, const Fault& fault);
125 
133 
140  void finishResult();
141 
148  void setFault(int rc, const char* msg);
149 
150  private:
152  bool advance(const Pt::Json::Node& node);
153 
154  private:
155  enum State
156  {
157  OnBegin,
158  OnMethodCallBegin,
159  OnMethodNameBegin,
160  OnMethodName,
161  OnMethodNameEnd,
162  OnParams,
163  OnParam,
164  OnParamsEnd,
165  OnMethodCallEnd
166  };
167 
168  Utf8Codec _utf8;
169  TextIStream _tis;
170  Json::JsonReader _reader;
171  Composer** _args;
172  State _state;
173  long long _id;
174 
175  TextOStream _tos;
176  Json::JsonWriter _writer;
177  Json::JsonFormatter _formatter;
178  Decomposer* _result;
179 
180  Fault _fault;
181  bool _isFault;
182  Pt::varint_t _r1;
183  Pt::varint_t _r2;
184 };
185 
186 } // namespace XmlRpc
187 
188 } // namespace Pt
189 
190 #endif // Pt_XmlRpc_Responder_h
Core module.
Definition: Allocator.h:33
Manages the decomposition of types during serialization.
Definition: Decomposer.h:44
void finishMessage(System::EventLoop &loop)
Parses the XML-RPC message.
virtual void onReady()
The service procedure has finished.
bool isFailed() const
Indicates if the procedure has failed.
void beginResult(std::ostream &os)
Formats the XML-RPC result.
Convert between unicode and UTF-8.
Definition: Utf8Codec.h:44
void setFault(int rc, const char *msg)
Fails the service procedure.
Responder(Remoting::ServiceDefinition &service)
Construct with Service.
JSON Formatter.
Definition: JsonFormatter.h:52
Remote service definition.
Definition: Api-ServiceDefinition.h:22
JSON document node.
Definition: Node.h:52
Reads JSON as a Stream of Nodes.
Definition: JsonReader.h:51
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)
Parses the XML-RPC message.
Dispatches requests to a service procedure.
Definition: Responder.h:52
virtual void onCancel()
Cancels all operations.
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: EventLoop.h:74
Composes types during serialization.
Definition: Composer.h:43
bool parseMessage()
Parses the XML-RPC message.
virtual ~Responder()
Destructor.
JSON-RPC fault exception.
Definition: Fault.h:44
void finishResult()
Formats the XML-RPC message.
virtual void onResult()=0
The service procedure has finished.
bool advanceResult()
Formats the XML-RPC message.
Writes JSON to a text stream.
Definition: JsonWriter.h:46