nlib
xmlrpc_clientserver.h
1 
2 #ifndef SAMPLES_XML_RPC_XMLRPC_CLIENTSERVER_H_ // NOLINT
3 #define SAMPLES_XML_RPC_XMLRPC_CLIENTSERVER_H_
4 
5 #include <map>
6 #include <string>
7 #include <vector>
8 
9 #include "nn/nlib/exi/exi.h"
10 
11 // Read http://www.xmlrpc.com/spec for XML-RPC
12 // (In Japanese, http://lowlife.jp/yasusii/stories/9.html)
13 class XmlRpcServer;
14 class XmlRpcValue;
15 
16 #ifdef NLIB_EXI_WCHAR_INTERFACE
17 typedef std::wstring StdString;
18 #else
19 typedef std::string StdString;
20 #endif
21 
23 
24 // Server Method:
25 // Implements the server method to inherit this class.
26 // The constructor registers the method with the server(XmlRpcServer),
27 // and the destructor unregisters it.
28 class XmlRpcServerMethod {
29  public:
30  XmlRpcServerMethod(const StdString& name, XmlRpcServer* server);
31  virtual ~XmlRpcServerMethod();
32  virtual void Execute(const XmlRpcValue& params, XmlRpcValue* result) = 0;
33  const StdString& GetName() const { return name_; }
34  XmlRpcServer* GetServer() const { return server_; }
35 
36  private:
37  StdString name_;
38  XmlRpcServer* server_;
39 
40  private:
41  XmlRpcServerMethod(const XmlRpcServerMethod&);
42  XmlRpcServerMethod& operator=(const XmlRpcServerMethod&);
43 };
44 
45 // Server:
46 // Receives XML and converts it into the XmlRpcValue object,
47 // then calls the server method corresponding to the name of the function.
48 class XmlRpcServer {
49  public:
50  XmlRpcServer() {}
51  ~XmlRpcServer() {}
52  void AddServerMethod(XmlRpcServerMethod* method) { methods_[method->GetName()] = method; }
53  void RemoveServerMethod(const StdString& name) {
54  Methods::iterator it = methods_.find(name);
55  if (it != methods_.end()) methods_.erase(it);
56  }
57  void RemoveServerMethod(XmlRpcServerMethod* method) {
58  this->RemoveServerMethod(method->GetName());
59  }
60  XmlRpcServerMethod* FindServerMethod(const StdString& name) const {
61  Methods::const_iterator it = methods_.find(name);
62  return (it != methods_.end()) ? it->second : NULL;
63  }
64  bool Process(nlib_ns::exi::XmlStreamReader* reader, nlib_ns::exi::XmlStreamWriter* writer);
65 
66  private:
67  typedef std::map<StdString, XmlRpcServerMethod*> Methods;
68  Methods methods_;
69 };
70 
71 // This class is for the client to receive the result of the procedure.
72 class XmlRpcCallResult {
73  XmlRpcServer* server_;
74  std::vector<unsigned char> to_server_;
75 
76  public:
77  XmlRpcCallResult() : server_(NULL) {}
78  void SetServer(XmlRpcServer* server) { server_ = server; }
79  void SetData(const std::vector<unsigned char>& rhs) { to_server_ = rhs; }
80  bool GetResult(XmlRpcValue* result);
81 };
82 
83 // Client:
84 // Sends the method name and the parameters to the server,
85 // and receives the result via XmlRpcCallResult.
86 class XmlRpcClient {
87  XmlRpcServer* server_;
88 
89  public:
90  XmlRpcClient() : server_(NULL) {}
91  void SetServer(XmlRpcServer* server) { server_ = server; }
92  bool Execute(const ExiChar* methodName, const XmlRpcValue& params, XmlRpcCallResult* result);
93 };
94 
95 #endif // SAMPLES_XML_RPC_XMLRPC_CLIENTSERVER_H_ // NOLINT
ディレクトリnn/nlib/exi内のヘッダを全てインクルードするためのヘッダです。
wchar_t ExiChar
XMLパーサーの内部文字列型のtypedefです。
Definition: Types.h:23