]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Now, json-rpc _ONLY_ supports member function pointers. An example is given in
authorpippijn <pippijn@e03df62e-2008-0410-955e-edbf42e46eb7>
Tue, 17 Jul 2007 14:39:18 +0000 (14:39 +0000)
committerpippijn <pippijn@e03df62e-2008-0410-955e-edbf42e46eb7>
Tue, 17 Jul 2007 14:39:18 +0000 (14:39 +0000)
ModuleRpcJson::ModuleRpcJson. I must admit that it is kind of ugly but it is
the only way I can see right now.

git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@7464 e03df62e-2008-0410-955e-edbf42e46eb7

src/modules/json.h
src/modules/m_rpc_json.cpp

index 385b7c559eba57670a34cd369f2c77c7c3db15e1..ca9ed8d3f862d642cb659e011795672ca90eed5f 100644 (file)
@@ -532,9 +532,18 @@ namespace json
 {
   namespace rpc
   {
-    typedef void (*method) (HTTPRequest *http, Value &request, Value &response);
-    void init (void);
-    void add_method (char *name, method mth);
+    typedef void (Module::*method) (HTTPRequest *http, Value &request, Value &response);
+
+    struct mfp
+    {
+      Module const *mod;
+      method mth;
+    };
+
+    typedef std::map<std::string, mfp> method_map;
+    extern method_map methods;
+  
+    void add_method (char *name, Module const *mod, method mth);
     void service (HTTPRequest *http, Value &request, Value &response);
     void process (HTTPRequest *http, std::string &response, char const *request);
   }
index 5977eb36e5e0130161a1d68456485dc09d60e570..960a06b9404e6072b40876dd4cb3fca380334ddf 100644 (file)
 
 class ModuleRpcJson : public Module
 {
+        void MthModuleVersion (HTTPRequest *http, json::Value &request, json::Value &response)
+        {
+                std::string result = "GetVersion().ToString()";
+                response["result"] = result;
+        }
+
+        void system_list_methods (HTTPRequest *http, json::Value &request, json::Value &response)
+        {
+                unsigned i = 0;
+                json::Value method_list (json::arrayValue);
+                
+                json::rpc::method_map::iterator it;
+                for (it = json::rpc::methods.begin(); it != json::rpc::methods.end(); ++it)
+                {
+                        method_list[i] = json::Value (it->first);
+                        i++;
+                }
+                
+                response["result"] = method_list;
+        }
+
  public:
        ModuleRpcJson(InspIRCd* Me) : Module(Me)
        {
-               json::rpc::init ();
+                json::rpc::add_method ("system.listMethods", (Module *)this, (void (Module::*)(HTTPRequest*, json::Value&, json::Value&))&ModuleRpcJson::system_list_methods);
+                json::rpc::add_method ("ircd.moduleVersion", (Module *)this, (void (Module::*)(HTTPRequest*, json::Value&, json::Value&))&ModuleRpcJson::MthModuleVersion);
        }
 
        void OnEvent(Event* event)
@@ -2048,36 +2070,13 @@ namespace json
 {
   namespace rpc
   {
-    typedef std::map<std::string, void (*) (HTTPRequest *, Value &, Value &)> method_map;
-  
     method_map methods;
   
     void
-    add_method (char *name, method mth)
-    {
-      methods[name] = mth;
-    }
-  
-    void
-    system_list_methods (HTTPRequest *http, Value &request, Value &response)
-    {
-      unsigned i = 0;
-      Value method_list (arrayValue);
-    
-      method_map::iterator it;
-      for (it = methods.begin(); it != methods.end(); ++it)
-       {
-         method_list[i] = Value (it->first);
-         i++;
-       }
-    
-      response["result"] = method_list;
-    }
-
-    void
-    init ()
+    add_method (char *name, Module const *mod, method mth)
     {
-      add_method ("system.listMethods", &system_list_methods);
+      mfp m = { mod, mth };
+      methods[name] = m;
     }
   
     void
@@ -2085,9 +2084,15 @@ namespace json
     {
       char const *methodName = static_cast<char const *> (request["method"]);
       
-      method_map::iterator mth = methods.find (methodName);
-      if (mth != methods.end ())
-       (*mth->second) (http, request, response);
+      method_map::iterator mthit = methods.find (methodName);
+      if (mthit != methods.end ())
+        {
+          mfp m = mthit->second;
+          Module *mod = new Module (*m.mod);
+          method mth = m.mth;
+          (mod->*mth) (http, request, response);
+          delete mod;
+        }
     }
     
     void