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