This is Webservice SDK for mbed. LPCXpresso1769/LPC1768/FRDM-K64F/LPC4088

Fork of libMiMic by Ryo Iizuka

Revision:
72:c118a7aa37a3
Child:
73:8c7dd6fd462e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed/mod/ModJsonRpc.cpp	Fri Jun 13 11:06:33 2014 +0000
@@ -0,0 +1,118 @@
+#include "ModJsonRpc.h"
+#include "../net/httpd/mod/NyLPC_cModWebSocket_protected.h"
+#include "HttpdConnection.h"
+
+namespace MiMic
+{
+    #define NUM_OF_OBJECTS 32
+    
+    ModJsonRpc::ModJsonRpc(const char* i_path,const struct NyLPC_TJsonRpcClassDef** i_rpc_table):ModBaseClass(i_path)
+    {
+        this->_mod=NULL;
+        this->_rpc_table=i_rpc_table;
+    }
+    ModJsonRpc::ModJsonRpc()
+    {
+        this->_mod=NULL;
+    }
+    ModJsonRpc::~ModJsonRpc()
+    {
+        if(this->_mod!=NULL){
+            NyLPC_cModJsonRpc_finalize(this->_mod);
+            this->_mod=NULL;
+        }
+    }
+    void ModJsonRpc::setParam(const char* i_path,const struct NyLPC_TJsonRpcClassDef** i_rpc_table)
+    {
+        ModBaseClass::setParam(i_path);
+        this->_rpc_table=i_rpc_table;
+    }
+    bool ModJsonRpc::execute(HttpdConnection& i_connection)
+    {
+        if(this->_mod!=NULL){
+            return false;
+        }
+        this->_mod=(NyLPC_TcModJsonRpc_t*)malloc(sizeof(NyLPC_TcModJsonRpc_t));
+        if(this->_mod==NULL){
+            return false;
+        }
+        bool ret=false;
+        //initialize table
+        this->_objects=new void*[NUM_OF_OBJECTS];
+        memset(this->_objects,sizeof(void*)*NUM_OF_OBJECTS,0);
+        
+        //initialize websocket
+        NyLPC_cModJsonRpc_initialize(this->_mod,this->_path,this->_rpc_table);        
+        if(NyLPC_cModJsonRpc_canHandle(this->_mod,i_connection._ref_inst)){
+            ret=NyLPC_cModJsonRpc_execute(this->_mod,i_connection._ref_inst)?true:false;
+        }
+        NyLPC_cModJsonRpc_finalize(this->_mod);
+        free(this->_mod);
+        this->_mod=NULL;
+        
+        for(int i=0;i<NUM_OF_OBJECTS;i++){
+            if(this->_objects[i]!=NULL){
+                delete this->_objects[i];
+            }
+        }
+        return ret;
+    }
+
+    void ModJsonRpc::dispatchRpcCall()
+    {
+        const union NyLPC_TJsonRpcParserResult* rpc_result;
+        for(;;){
+            if(!NyLPC_cModJsonRpc_processRpcMessage(this->_mod)){
+                break;
+            }
+            //メッセージ取得を試行
+            rpc_result=NyLPC_cModJsonRpc_getMessage(this->_mod);
+            if(rpc_result==NULL){
+                //nothing
+                continue;
+            }
+            if(NyLPC_TJsonRpcParserResult_hasMethodHandler(rpc_result)){
+                if(NyLPC_TJsonRpcParserResult_callMethodHandler(rpc_result,this)){
+                    continue;
+                }else{
+                    //function failed.
+                    break;
+                }
+            }else{
+                //no handler
+                break;
+            }
+        }
+        NyLPC_cModJsonRpc_close(this->_mod,1000);
+        return;
+    }
+    bool ModJsonRpc::putResult(unsigned int i_id,const char* i_params_fmt,...)
+    {
+        bool ret;
+        va_list a;
+        va_start(a,i_params_fmt);
+        ret=NyLPC_cModJsonRpc_putResultV(this->_mod,i_id,i_params_fmt,a)?true:false;
+        va_end(a);
+        return ret;
+    }
+    bool ModJsonRpc::putError(unsigned int i_id,int i_code)
+    {
+        return NyLPC_cModJsonRpc_putError(this->_mod,i_id,i_code)?true:false;
+    }
+    
+    int ModJsonRpc::addObject(void* i_object)
+    {
+        for(int i=0;i<NUM_OF_OBJECTS;i++){
+            if(this->_objects[i]==NULL){
+                this->_objects[i]=i_object;
+                return i;
+            }
+        }
+        return -1;
+    }
+    void* ModJsonRpc::getObject(int i_oid)
+    {
+        return this->_objects[i_oid];
+    }    
+
+}
\ No newline at end of file