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
mbed/mod/ModJsonRpc.cpp
- Committer:
- nyatla
- Date:
- 2014-06-13
- Revision:
- 72:c118a7aa37a3
- Child:
- 73:8c7dd6fd462e
File content as of revision 72:c118a7aa37a3:
#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]; } }