From ec06f5b0296a946f168cb316607f750b7b42da99 Mon Sep 17 00:00:00 2001 From: Franklin_Zhang Date: Wed, 28 Mar 2018 22:24:50 +0800 Subject: [PATCH 1/2] update data configuration interface to support more functions Signed-off-by: Franklin_Zhang --- include/INxDataConfiguration.h | 2 +- include/INxModelServiceInstance.h | 18 ++++++++++++++++-- src/NxDataConfiguration.cpp | 2 +- src/NxDataConfiguration.h | 4 +++- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/include/INxDataConfiguration.h b/include/INxDataConfiguration.h index e2e9023..4321c0b 100644 --- a/include/INxDataConfiguration.h +++ b/include/INxDataConfiguration.h @@ -13,7 +13,7 @@ namespace NJGIS public: virtual int getCount() = 0; - virtual int insertData(const char* state, const char* event, const char* dataid, bool destoryed = false) = 0; + virtual int insertData(const char* state, const char* event, const char* dataid, bool destoryed = false, bool requested = false, bool optional = false) = 0; virtual int getData(int index, std::string &state, std::string &event, std::string &dataid, bool &destoryed) = 0; diff --git a/include/INxModelServiceInstance.h b/include/INxModelServiceInstance.h index 4cdce01..78ccb65 100644 --- a/include/INxModelServiceInstance.h +++ b/include/INxModelServiceInstance.h @@ -5,6 +5,8 @@ #include #include "INxService.h" +#include "INxRunningLog.h" +#include "INxDataConfiguration.h" namespace NJGIS { @@ -13,11 +15,23 @@ namespace NJGIS class INjServiceServiceInstance : public virtual INjService { public: - virtual int getCurrentState(std::string& state) = 0; + virtual const char* getCurrentState() = 0; - virtual int getLogs() = 0; + virtual const char* getCurrentEvent() = 0; + + virtual const char* getGUID() = 0; + + virtual const char* getStartTime() = 0; + + virtual const char* getModelServiceRecordID() = 0; + + virtual const char* getModelServiceID() = 0; + + virtual std::vector* getLogs() = 0; virtual int kill() = 0; + + virtual int refresh() = 0; }; } } diff --git a/src/NxDataConfiguration.cpp b/src/NxDataConfiguration.cpp index 6c206e7..6399d09 100644 --- a/src/NxDataConfiguration.cpp +++ b/src/NxDataConfiguration.cpp @@ -10,7 +10,7 @@ int NJGIS::SERVICE::NjDataConfiguration::getCount() return this->_items.size(); } -int NJGIS::SERVICE::NjDataConfiguration::insertData( const char* state, const char* event, const char* dataid, bool destoryed ) +int NJGIS::SERVICE::NjDataConfiguration::insertData( const char* state, const char* event, const char* dataid, bool destoryed, bool requested, bool optional) { for(int i = 0; i < this->_items.size(); i++) { diff --git a/src/NxDataConfiguration.h b/src/NxDataConfiguration.h index 1b46cce..1d327a1 100644 --- a/src/NxDataConfiguration.h +++ b/src/NxDataConfiguration.h @@ -16,6 +16,8 @@ namespace NJGIS const char* event; const char* data; bool destoryed; + bool requested; + bool optional; }NjDataConfigItem; class NjDataConfiguration : public virtual INjDataConfiguration @@ -25,7 +27,7 @@ namespace NJGIS virtual int getCount(); - virtual int insertData( const char* state, const char* event, const char* dataid, bool destoryed = false ); + virtual int insertData( const char* state, const char* event, const char* dataid, bool destoryed = false, bool requested = false, bool optional = false); virtual int getData( int index, std::string &state, std::string &event, std::string &dataid, bool &destoryed ); -- Gitee From fe617a1901cc1e06e37af8e91d823c04321e683a Mon Sep 17 00:00:00 2001 From: Franklin_Zhang Date: Mon, 2 Apr 2018 21:56:42 +0800 Subject: [PATCH 2/2] update model instance Signed-off-by: Franklin_Zhang --- include/INxModelServiceInstance.h | 18 +++- include/INxServiceAccess.h | 4 + src/NxModelServiceInstance.cpp | 119 ++++++++++++++++++++++++++ src/NxModelServiceInstance.h | 95 ++++++++++++++++++++ src/NxModelServiceInstanceFactory.cpp | 15 ++++ src/NxModelServiceInstanceFactory.h | 18 ++++ src/NxService.h | 1 + src/NxServiceAccess.cpp | 22 ++++- src/NxServiceAccess.h | 2 + test/test.cpp | 3 + utils/NxHttp.cpp | 48 +++++++++++ utils/NxHttp.h | 8 +- 12 files changed, 348 insertions(+), 5 deletions(-) create mode 100644 src/NxModelServiceInstance.cpp create mode 100644 src/NxModelServiceInstance.h create mode 100644 src/NxModelServiceInstanceFactory.cpp create mode 100644 src/NxModelServiceInstanceFactory.h diff --git a/include/INxModelServiceInstance.h b/include/INxModelServiceInstance.h index 78ccb65..4818875 100644 --- a/include/INxModelServiceInstance.h +++ b/include/INxModelServiceInstance.h @@ -12,7 +12,15 @@ namespace NJGIS { namespace SERVICE { - class INjServiceServiceInstance : public virtual INjService + typedef enum + { + INSTA_UNKNOWN = 0, + INSTA_RUNNING = 1, + INSTA_REQUESTING = 2, + INSTA_HANGING = 3 + }NjModelInstanceStatus; + + class INjModelServiceInstance : public virtual INjService { public: virtual const char* getCurrentState() = 0; @@ -23,14 +31,18 @@ namespace NJGIS virtual const char* getStartTime() = 0; - virtual const char* getModelServiceRecordID() = 0; - virtual const char* getModelServiceID() = 0; virtual std::vector* getLogs() = 0; + virtual NjModelInstanceStatus getStatus() = 0; + virtual int kill() = 0; + virtual int pause() = 0; + + virtual int restart() = 0; + virtual int refresh() = 0; }; } diff --git a/include/INxServiceAccess.h b/include/INxServiceAccess.h index 61f0908..cac7ffe 100644 --- a/include/INxServiceAccess.h +++ b/include/INxServiceAccess.h @@ -4,6 +4,7 @@ #include "INxModelService.h" #include "INxDataConfiguration.h" #include "INxModelServiceRecord.h" +#include "INxModelServiceInstance.h" #include namespace NJGIS @@ -25,6 +26,9 @@ namespace NJGIS //! get model service record by id virtual INjModelServiceRecord* getModelServiceRecordByID(const char* msrid) = 0; + //! get model service instance by id + virtual INjModelServiceInstance* getModelServiceInstanceByGUID(const char* guid) = 0; + //! get a data service virtual NJGIS::SERVICE::INjData* getDataServiceByID(const char* id) = 0; diff --git a/src/NxModelServiceInstance.cpp b/src/NxModelServiceInstance.cpp new file mode 100644 index 0000000..aec73b1 --- /dev/null +++ b/src/NxModelServiceInstance.cpp @@ -0,0 +1,119 @@ +#include "NxModelServiceInstance.h" +#include "../utils/NxHttp.h" +#include "json/json.h" + +const char* NJGIS::SERVICE::NjModelServiceInstance::getCurrentState() +{ + return this->_state.c_str(); +} + +const char* NJGIS::SERVICE::NjModelServiceInstance::getCurrentEvent() +{ + return this->_event.c_str(); +} + +const char* NJGIS::SERVICE::NjModelServiceInstance::getGUID() +{ + return this->_guid.c_str(); +} + +const char* NJGIS::SERVICE::NjModelServiceInstance::getStartTime() +{ + return this->_guid.c_str(); +} + +const char* NJGIS::SERVICE::NjModelServiceInstance::getModelServiceID() +{ + return this->_serviceID.c_str(); +} + +std::vector* NJGIS::SERVICE::NjModelServiceInstance::getLogs() +{ + return this->_logs; +} + +NJGIS::SERVICE::NjModelInstanceStatus NJGIS::SERVICE::NjModelServiceInstance::getStatus() +{ + return this->_status; +} + +int NJGIS::SERVICE::NjModelServiceInstance::kill() +{ + Json::Value jsResponse; + std::string url; + this->getBaseUrl(url); + url += "modelins/"; + url += this->getGUID(); + url += "?ac=kill"; + NJGIS::SERVICE::NjHttpHelper::request_put_json_sync(url.c_str(), jsResponse); + if(jsResponse["result"].asString() == "suc") + { + return 1; + } + else if(jsResponse["result"].asString() == "fail") + { + return -2; + } + return -1; +} + +int NJGIS::SERVICE::NjModelServiceInstance::pause() +{ + Json::Value jsResponse; + std::string url; + this->getBaseUrl(url); + url += "modelins/"; + url += this->getGUID(); + url += "?ac=pause"; + NJGIS::SERVICE::NjHttpHelper::request_put_json_sync(url.c_str(), jsResponse); + if(jsResponse["result"].asString() == "suc") + { + return 1; + } + else if(jsResponse["result"].asString() == "fail") + { + return -2; + } + return -1; +} + +int NJGIS::SERVICE::NjModelServiceInstance::restart() +{ + Json::Value jsResponse; + std::string url; + this->getBaseUrl(url); + url += "modelins/"; + url += this->getGUID(); + url += "?ac=restart"; + NJGIS::SERVICE::NjHttpHelper::request_put_json_sync(url.c_str(), jsResponse); + if(jsResponse["result"].asString() == "suc") + { + return 1; + } + else if(jsResponse["result"].asString() == "fail") + { + return -2; + } + return -1; +} + +int NJGIS::SERVICE::NjModelServiceInstance::refresh() +{ + Json::Value jsResponse; + std::string url; + this->getBaseUrl(url); + url += "modelins/json/"; + url += this->getGUID(); + NJGIS::SERVICE::NjHttpHelper::request_get_json_sync(url.c_str(), jsResponse); + if(jsResponse["result"].asString() == "suc") + { + jsResponse[""].asString(); + return 1; + } + else if(jsResponse["result"].asString() == "err") + { + return -2; + } + return -1; +} + diff --git a/src/NxModelServiceInstance.h b/src/NxModelServiceInstance.h new file mode 100644 index 0000000..70e5d31 --- /dev/null +++ b/src/NxModelServiceInstance.h @@ -0,0 +1,95 @@ +#ifndef __NJGIS_NJMOLDESERVICEINSTANCE_H__ +#define __NJGIS_NJMOLDESERVICEINSTANCE_H__ + +#include "../include/INxModelServiceInstance.h" +#include "NxService.h" + +namespace NJGIS +{ + namespace SERVICE + { + class NjModelServiceInstance : public virtual INjModelServiceInstance, public virtual NjService + { + public: + + NjModelServiceInstance( + const char* state, + const char* eventname, + const char* guid, + const char* startDT, + const char* serviceID, + const char* ip, + int port, + std::vector* logs = NULL, + NjModelInstanceStatus status = NJGIS::SERVICE::INSTA_UNKNOWN + ): + _state(state), + _event(eventname), + _guid(guid), + _startDT(startDT), + _serviceID(serviceID), + _status(status), + NjService(ip, port) + { + this->_logs = new std::vector(); + if(logs != NULL) + { + for (int i = 0; i < logs->size(); i++) + { + this->_logs->push_back((*logs)[i]); + } + } + }; + + ~NjModelServiceInstance() + { + delete this->_logs; + }; + + virtual const char* getCurrentState(); + + virtual const char* getCurrentEvent(); + + virtual const char* getGUID(); + + virtual const char* getStartTime(); + + virtual const char* getModelServiceID(); + + virtual std::vector* getLogs(); + + virtual NjModelInstanceStatus getStatus(); + + virtual int kill(); + + virtual int pause(); + + virtual int restart(); + + virtual int refresh(); + + private: + + std::string _startDT; + + std::string _serviceID; + + //! dynamic + std::string _state; + + //! dynamic + std::string _event; + + //! dynamic + std::string _guid; + + //! dynamic + std::vector* _logs; + + //! dynamic + NjModelInstanceStatus _status; + }; + } +} + +#endif \ No newline at end of file diff --git a/src/NxModelServiceInstanceFactory.cpp b/src/NxModelServiceInstanceFactory.cpp new file mode 100644 index 0000000..457980d --- /dev/null +++ b/src/NxModelServiceInstanceFactory.cpp @@ -0,0 +1,15 @@ +#include "NxModelServiceInstanceFactory.h" + +NJGIS::SERVICE::INjModelServiceInstance* NJGIS::SERVICE::NjModelServiceInstanceFactory::createModelServiceInstacnce(Json::Value jsData, const char* ip, int port) +{ + NJGIS::SERVICE::NjModelServiceInstance* pInstance = new NJGIS::SERVICE::NjModelServiceInstance( + jsData["state"].asString().c_str(), + jsData["event"].asString().c_str(), + jsData["guid"].asString().c_str(), + jsData["start"].asString().c_str(), + jsData["ms"]["_id"].asString().c_str(), + ip, + port + ); + return pInstance; +} \ No newline at end of file diff --git a/src/NxModelServiceInstanceFactory.h b/src/NxModelServiceInstanceFactory.h new file mode 100644 index 0000000..17cb69f --- /dev/null +++ b/src/NxModelServiceInstanceFactory.h @@ -0,0 +1,18 @@ +#ifndef __NJGIS_NJMODELSERVICEINSTANCEFACTORY_H__ +#define __NJGIS_NJMODELSERVICEINSTANCEFACTORY_H__ +#include "NxModelServiceInstance.h" +#include "json/json.h" + +namespace NJGIS +{ + namespace SERVICE + { + class NjModelServiceInstanceFactory + { + public: + static INjModelServiceInstance* createModelServiceInstacnce(Json::Value jsData, const char* ip, int port); + }; + } +} + +#endif \ No newline at end of file diff --git a/src/NxService.h b/src/NxService.h index 0aa1732..18f8fa9 100644 --- a/src/NxService.h +++ b/src/NxService.h @@ -25,6 +25,7 @@ namespace NJGIS protected: + //! !!!With '/' in the end virtual int getBaseUrl( std::string &url ); virtual int setIP( const char* ip ); diff --git a/src/NxServiceAccess.cpp b/src/NxServiceAccess.cpp index dca1cb8..bea2ab1 100644 --- a/src/NxServiceAccess.cpp +++ b/src/NxServiceAccess.cpp @@ -3,6 +3,8 @@ #include "NxModelServiceFactory.h" #include "NxDataFactory.h" #include "NxModelServiceRecordFactory.h" +#include "NxModelServiceInstance.h" +#include "NxModelServiceInstanceFactory.h" #include "../utils/NxHttp.h" int NJGIS::SERVICE::NjServiceAccess::getModelServicesList( std::vector &list ) @@ -138,6 +140,24 @@ NJGIS::SERVICE::INjModelServiceRecord* NJGIS::SERVICE::NjServiceAccess::getModel }else{ return NULL; } +} + +NJGIS::SERVICE::INjModelServiceInstance* NJGIS::SERVICE::NjServiceAccess::getModelServiceInstanceByGUID( const char* guid ) +{ + INjModelServiceInstance* pInstance; + + std::string url; + this->getBaseUrl(url); + url += "modelserins/json/" + std::string(guid); + //NJGIS::SERVICE::INjModelServiceRecord* pRecord = NULL; - + Json::Value jResponse; + NJGIS::SERVICE::NjHttpHelper::request_get_json_sync(url.c_str(), jResponse); + if(jResponse["result"].asString() == "suc") + { + Json::Value jMis = jResponse["data"]; + return NJGIS::SERVICE::NjModelServiceInstanceFactory::createModelServiceInstacnce(jMis, this->getIP(), this->getPort()); + }else{ + return NULL; + } } diff --git a/src/NxServiceAccess.h b/src/NxServiceAccess.h index acbfdd5..986036d 100644 --- a/src/NxServiceAccess.h +++ b/src/NxServiceAccess.h @@ -31,6 +31,8 @@ namespace NJGIS virtual INjModelServiceRecord* getModelServiceRecordByID( const char* msrid ); + virtual INjModelServiceInstance* getModelServiceInstanceByGUID( const char* guid ); + }; } } diff --git a/test/test.cpp b/test/test.cpp index 80ff3f5..5168824 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -14,6 +14,9 @@ int main() { std::cout <<"Hello My SDK!" << std::endl; NJGIS::SERVICE::INjServiceAccess* pServiceAccess = pServer->getServiceAccess(); + + + std::vector list_ms; pServiceAccess->getModelServicesList(list_ms); for(int i = 0; i < list_ms.size(); i++) diff --git a/utils/NxHttp.cpp b/utils/NxHttp.cpp index c6a08b2..cd5bc44 100644 --- a/utils/NxHttp.cpp +++ b/utils/NxHttp.cpp @@ -119,6 +119,39 @@ int NJGIS::SERVICE::NjHttpHelper::request_post_withform_sync( const char* url, N return 1; } +int NJGIS::SERVICE::NjHttpHelper::request_put_sync( const char* url, std::string &responsebody ) +{ + // init curl + CURL *curl = curl_easy_init(); + // res code + CURLcode res; + // head + std::string head; + + if (curl) + { + // set params + curl_easy_setopt(curl, CURLOPT_URL, url); // url + curl_easy_setopt(curl, CURLOPT_PUT, 1L); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); // if want to use https + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); // set peer and host verify false + // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); + curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NJGIS::SERVICE::NjHttpHelper::req_reply); + curl_easy_setopt(curl, CURLOPT_HEADERDATA, (void *)&head); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&responsebody); + curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); + curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3); // set transport and time out time + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3); + // start req + res = curl_easy_perform(curl); + } + // release curl + curl_easy_cleanup(curl); + + return 1; +} + int NJGIS::SERVICE::NjHttpHelper::request_get_json_sync( const char* url, Json::Value &value ) { std::string body; @@ -149,3 +182,18 @@ int NJGIS::SERVICE::NjHttpHelper::request_post_withform_json_sync( const char* u } } + +int NJGIS::SERVICE::NjHttpHelper::request_put_json_sync( const char* url, Json::Value &value ) +{ + std::string body; + int res = NJGIS::SERVICE::NjHttpHelper::request_put_sync(url, body); + Json::Reader reader; + if (reader.parse(body, value)) + { + return 1; + } + else + { + return -2; + } +} \ No newline at end of file diff --git a/utils/NxHttp.h b/utils/NxHttp.h index f57dc2f..3126ca4 100644 --- a/utils/NxHttp.h +++ b/utils/NxHttp.h @@ -25,13 +25,19 @@ namespace NJGIS //! Request -POST static int request_post_withform_sync( const char* url, INjFormParam* &list_form, INjLocalFiles* &list_files, std::string &responsebody ); + //! Request -PUT + static int request_put_sync(const char* url, std::string &responsebody); + /* JSON */ //! Request -GET static int request_get_json_sync(const char* url, Json::Value &value); - //! Request -Post + //! Request -POST static int request_post_withform_json_sync(const char* url, INjFormParam* &list_form, INjLocalFiles* &list_files, Json::Value &value); + //! Request -PUT + static int request_put_json_sync(const char* url, Json::Value &value); + private: //! reply static size_t req_reply(void *ptr, size_t size, size_t nmemb, void *stream); -- Gitee