Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of libMiMic by
ModJsonRpc.cpp
00001 #include "ModJsonRpc.h" 00002 #include "../net/httpd/mod/NyLPC_cModWebSocket_protected.h" 00003 #include "HttpdConnection.h" 00004 00005 namespace MiMic 00006 { 00007 #define NUM_OF_OBJECTS 16 00008 ModJsonRpc::BasicRpcObject** newObjectArray() 00009 { 00010 ModJsonRpc::BasicRpcObject** r=new ModJsonRpc::BasicRpcObject*[NUM_OF_OBJECTS]; 00011 memset(r,0,sizeof(ModJsonRpc::BasicRpcObject*)*NUM_OF_OBJECTS); 00012 return r; 00013 } 00014 void deleteObjectArray(ModJsonRpc::BasicRpcObject** v) 00015 { 00016 for(int i=0;i<NUM_OF_OBJECTS;i++){ 00017 if(v[i]!=NULL){ 00018 delete v[i]; 00019 } 00020 } 00021 delete[] v; 00022 } 00023 00024 ModJsonRpc::ModJsonRpc(const char* i_path,const struct NyLPC_TJsonRpcClassDef** i_rpc_table):ModBaseClass(i_path) 00025 { 00026 this->_mod=NULL; 00027 this->_rpc_table=i_rpc_table; 00028 } 00029 ModJsonRpc::ModJsonRpc() 00030 { 00031 this->_mod=NULL; 00032 } 00033 ModJsonRpc::~ModJsonRpc() 00034 { 00035 if(this->_mod!=NULL){ 00036 NyLPC_cModJsonRpc_finalize(&(this->_mod->super)); 00037 free(this->_mod); 00038 deleteObjectArray(this->_objects); 00039 this->_mod=NULL; 00040 } 00041 } 00042 void ModJsonRpc::setParam(const char* i_path,const struct NyLPC_TJsonRpcClassDef** i_rpc_table) 00043 { 00044 ModBaseClass::setParam(i_path); 00045 this->_rpc_table=i_rpc_table; 00046 } 00047 bool ModJsonRpc::isStarted(){ 00048 return this->_mod!=NULL; 00049 } 00050 bool ModJsonRpc::execute(HttpdConnection& i_connection) 00051 { 00052 i_connection.lockHttpd(); 00053 if(this->_mod!=NULL){ 00054 i_connection.unlockHttpd(); 00055 return false; 00056 } 00057 this->_mod=(TcJsonRpcEx_t*)malloc(sizeof(TcJsonRpcEx_t)); 00058 i_connection.unlockHttpd(); 00059 if(this->_mod==NULL){ 00060 return false; 00061 } 00062 this->_mod->cppmod_ptr=this; 00063 00064 //initialize websocket 00065 NyLPC_cModJsonRpc_initialize(&(this->_mod->super),this->_path,this->_rpc_table); 00066 if(NyLPC_cModJsonRpc_canHandle(&(this->_mod->super),i_connection._ref_inst)){ 00067 if(NyLPC_cModJsonRpc_execute(&(this->_mod->super),i_connection._ref_inst)){ 00068 //initialize object array 00069 this->_objects=newObjectArray(); 00070 return true; 00071 } 00072 } 00073 NyLPC_cModJsonRpc_finalize(&(this->_mod->super)); 00074 free(this->_mod); 00075 i_connection.lockHttpd(); 00076 this->_mod=NULL; 00077 i_connection.unlockHttpd(); 00078 return false; 00079 } 00080 00081 void ModJsonRpc::dispatchRpc() 00082 { 00083 const union NyLPC_TJsonRpcParserResult* rpc_result; 00084 if(this->_mod==NULL){ 00085 return; 00086 } 00087 for(;;){ 00088 if(!NyLPC_cModJsonRpc_processRpcMessage(&(this->_mod->super))){ 00089 break; 00090 } 00091 //メッセージ取得を試行 00092 rpc_result=NyLPC_cModJsonRpc_getMessage(&(this->_mod->super)); 00093 if(rpc_result==NULL){ 00094 //nothing 00095 continue; 00096 } 00097 if(NyLPC_TJsonRpcParserResult_hasMethodHandler(rpc_result)){ 00098 if(NyLPC_TJsonRpcParserResult_callMethodHandler(rpc_result,this->_mod)){ 00099 continue; 00100 }else{ 00101 //function failed. 00102 break; 00103 } 00104 }else{ 00105 //no handler 00106 break; 00107 } 00108 } 00109 NyLPC_cModJsonRpc_close(&(this->_mod->super),1000); 00110 NyLPC_cModJsonRpc_finalize(&(this->_mod->super)); 00111 deleteObjectArray(this->_objects); 00112 free(this->_mod); 00113 this->_mod=NULL; 00114 return; 00115 } 00116 bool ModJsonRpc::putResult(unsigned int i_id) 00117 { 00118 return this->putResult(i_id,""); 00119 } 00120 bool ModJsonRpc::putResult(unsigned int i_id,const char* i_params_fmt,...) 00121 { 00122 bool ret; 00123 va_list a; 00124 va_start(a,i_params_fmt); 00125 ret=NyLPC_cModJsonRpc_putResultV(&(this->_mod->super),i_id,i_params_fmt,a)?true:false; 00126 va_end(a); 00127 return ret; 00128 } 00129 bool ModJsonRpc::putError(unsigned int i_id,int i_code) 00130 { 00131 return NyLPC_cModJsonRpc_putError(&(this->_mod->super),i_id,i_code)?true:false; 00132 } 00133 00134 int ModJsonRpc::addObject(BasicRpcObject* i_object) 00135 { 00136 for(int i=0;i<NUM_OF_OBJECTS;i++){ 00137 if(this->_objects[i]==NULL){ 00138 this->_objects[i]=i_object; 00139 return i; 00140 } 00141 } 00142 return -1; 00143 } 00144 bool ModJsonRpc::removeObject(int i_id) 00145 { 00146 if(0<=i_id && i_id<NUM_OF_OBJECTS){ 00147 if(this->_objects[i_id]!=NULL){ 00148 delete this->_objects[i_id]; 00149 this->_objects[i_id]=NULL; 00150 return true; 00151 } 00152 } 00153 return false; 00154 } 00155 void* ModJsonRpc::getObject(int i_oid) 00156 { 00157 return this->_objects[i_oid]->obj; 00158 } 00159 00160 }
Generated on Tue Jul 12 2022 16:22:56 by
