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 m_Name; }
34  XmlRpcServer* GetServer() const { return m_Server; }
35 
36  private:
37  StdString m_Name;
38  XmlRpcServer* m_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) { m_Methods[method->GetName()] = method; }
53  void RemoveServerMethod(const StdString& name) {
54  Methods::iterator it = m_Methods.find(name);
55  if (it != m_Methods.end()) m_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 = m_Methods.find(name);
62  return (it != m_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 m_Methods;
69 };
70 
71 // This class is for the client to receive the result of the procedure.
72 class XmlRpcCallResult {
73  XmlRpcServer* m_Server;
74  std::vector<unsigned char> m_ToServer;
75 
76  public:
77  XmlRpcCallResult() : m_Server(NULL) {}
78  void SetServer(XmlRpcServer* server) { m_Server = server; }
79  void SetData(const std::vector<unsigned char>& rhs) { m_ToServer = 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* m_Server;
88 
89  public:
90  XmlRpcClient() : m_Server(NULL) {}
91  void SetServer(XmlRpcServer* server) { m_Server = server; }
92  bool Execute(const ExiChar* methodName, const XmlRpcValue& params, XmlRpcCallResult* result);
93 };
94 
95 #endif // SAMPLES_XML_RPC_XMLRPC_CLIENTSERVER_H_ // NOLINT
Header that includes all the headers within the nn/nlib/exi directory.
wchar_t ExiChar
A string-type typedef used internally by the XML parser.
Definition: Types.h:23