diff --git a/include/INxData.h b/include/INxData.h index 94931db59f77e2d11f67cb41cfe75e9088d0db77..25ad728883b26643bd87281e3bfb983bd05626ad 100644 --- a/include/INxData.h +++ b/include/INxData.h @@ -34,7 +34,7 @@ namespace NJGIS virtual int getValue(std::string& value) = 0; virtual int save(const char* path) = 0; - } + }; } } diff --git a/include/INxDataConfiguration.h b/include/INxDataConfiguration.h index 874894249408d0890cdd828d0889d5526028c642..e4584fcbf0141a563f32bc2ec025f8944ff6a04b 100644 --- a/include/INxDataConfiguration.h +++ b/include/INxDataConfiguration.h @@ -16,7 +16,7 @@ namespace NJGIS virtual int getCount() = 0; virtual int getData(std::string& state, std::string& event, INjData* &data) = 0; - } + }; } } diff --git a/include/INxServer.h b/include/INxServer.h index 890fdaaa78db24a9292b3c045ff8d0539b214b36..b4bbe7ef83db17edb980f004f5eb469ae278cea4 100644 --- a/include/INxServer.h +++ b/include/INxServer.h @@ -11,24 +11,25 @@ namespace NJGIS { class INjServer : public virtual INxUnknown { + public: //! get IP "xxxx.xxxx.xxxx.xxxx" virtual int getIP(std::string &ip) = 0; //! get Port - virtual int getPort(int &port) = 0; + virtual int getPort() = 0; //! set IP and Port virtual int setIPAndPort(const char* ip, const int port = 8060) = 0; //! connect - virtual int connect() = 0; + virtual int connect() = 0; - //! check is the server online - virtual bool isOnline() = 0; + //! connect with params + virtual int connect(const char* ip, const int port = 8060) = 0; //! get service access virtual INjServiceAccess* getServiceAccess() = 0; - } + }; } } diff --git a/include/INxServerFactory.h b/include/INxServerFactory.h new file mode 100644 index 0000000000000000000000000000000000000000..0f485cec4cc6d57589de56591fac54c42efc7d02 --- /dev/null +++ b/include/INxServerFactory.h @@ -0,0 +1,19 @@ +#ifndef __NJGIS_INXSERVERFACTORY_H__ +#define __NJGIS_INXSERVERFACTORY_H__ + +#include "INxServer.h" + +namespace NJGIS +{ + namespace SERVICE + { + class INjServerFactory : public virtual INxUnknown + { + public: + //! create a server instance + virtual INjServer* createServer(const char* ip = "127.0.0.1", const int port = 8060) = 0; + }; + } +} + +#endif \ No newline at end of file diff --git a/include/INxService.h b/include/INxService.h index 1e4db1bc1688f31c0b4a0519a2840e7fb21eb2da..72a1a050d98978af68130c2f9d593202945facbb 100644 --- a/include/INxService.h +++ b/include/INxService.h @@ -81,7 +81,7 @@ namespace NJGIS virtual int getServiceDeployorInfo() = 0; //! get status - virtual int getServiceStatus(NJGIS::SERVICE::NjServiceStatus& status) = 0 + virtual int getServiceStatus(NJGIS::SERVICE::NjServiceStatus& status) = 0; //! get limitaion virtual int getServiceLimitation(NJGIS::SERVICE::NjServiceLimitation& limitaion) = 0; @@ -91,7 +91,7 @@ namespace NJGIS //! invoke service virtual int invoke(const INjDataConfiguration* config, std::string& recordid) = 0; - } + }; } } diff --git a/include/INxServiceAccess.h b/include/INxServiceAccess.h index 870bf054d7be2453070860d06b2420327a9942b3..aa8249e1996b973e8d7a7671f0acf07d0fd82015 100644 --- a/include/INxServiceAccess.h +++ b/include/INxServiceAccess.h @@ -16,11 +16,11 @@ namespace NJGIS virtual int getServicesList(std::vector &list) = 0; //! get model service by OID - virtual int getServiceByOID(const char* oid) = 0; + virtual NJGIS::SERVICE::INjService* getServiceByOID(const char* oid) = 0; //! get model servcie by name virtual int getServicesByName(const char* name, std::vector &list) = 0; - } + }; } } diff --git a/include/INxServiceInstance.h b/include/INxServiceInstance.h index 780d764e96dbc37ba911ad91fc079fd54850a2bf..e9571b2c8e8cd7921a95ca7e51d2202e2bf23a45 100644 --- a/include/INxServiceInstance.h +++ b/include/INxServiceInstance.h @@ -18,7 +18,7 @@ namespace NJGIS virtual int getLogs() = 0; virtual int kill() = 0; - } + }; } } diff --git a/include/INxServiceRecord.h b/include/INxServiceRecord.h index b165d58fd3dc7fa9301a2a416924294a6520cc4b..9664694a3c40906f85da312e762cf95081ffe2e3 100644 --- a/include/INxServiceRecord.h +++ b/include/INxServiceRecord.h @@ -42,7 +42,7 @@ namespace NJGIS virtual int getLogs() = 0; virtual int refresh() = 0; - } + }; } } diff --git a/include/INxServiceAPI.h b/include/NxServiceAPI.h similarity index 66% rename from include/INxServiceAPI.h rename to include/NxServiceAPI.h index 0db7d7b9d3a15ffd36fbf70da0a5d82ee3badf43..b9f9538b96b08bab7b63099481bef7880333caa2 100644 --- a/include/INxServiceAPI.h +++ b/include/NxServiceAPI.h @@ -1,12 +1,12 @@ #ifndef __NJGIS_INXSERVICEAPI_H__ #define __NJGIS_INXSERVICEAPI_H__ -#include "INxServer.h" +#include "INxServerFactory.h" //! Windows #ifdef _WINDOWS //! Static Lib -#ifndef _NJGIS_STATIC_LIB_ +#ifndef _NJGIS_STATIC_LIB #ifdef _NJGIS_EXPORTS #define NJGIS_SERVICE __declspec(dllexport) #else @@ -15,22 +15,22 @@ #endif //! Linux OSX(UNIX) #else -#ifndef _NJGIS_STATIC_LIB_ +#ifndef _NJGIS_STATIC_LIB #ifdef _NJGIS_EXPORTS -#define NJGIS_SERVICE __declspec(dllexport) -#else #define NJGIS_SERVICE __attribute ((visibility("default"))) +#else +#define NJGIS_SERVICE #endif #endif #endif -namespace NSGIS +namespace NJGIS { namespace SERVICE { extern "C" NJGIS_SERVICE const char* getVersion(); - extern "C" NJGIS_SERVICE INjServer* createServer(const char* ip = "127.0.0.1", const int port = 8060); + extern "C" NJGIS_SERVICE NJGIS::SERVICE::INjServerFactory* createServerFactory(); } } diff --git a/src/NxServer.cpp b/src/NxServer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f4039f8a32669e2c760d0fd3ac280fdc5c56998 --- /dev/null +++ b/src/NxServer.cpp @@ -0,0 +1,53 @@ +#include "NxServer.h" +#include "../utils/NxHttp.h" +#include + +int NJGIS::SERVICE::NjServer::getIP( std::string &ip ) +{ + ip = this->_ip; + return 1; +} + +int NJGIS::SERVICE::NjServer::getPort( ) +{ + return this->_port; +} + +int NJGIS::SERVICE::NjServer::setIPAndPort( const char* ip, const int port ) +{ + this->_ip = ip; + if(port > 65535 || port < 0) { return -1; } + this->_port = port; + return 1; +} + +int NJGIS::SERVICE::NjServer::connect() +{ + std::stringstream ss; + ss << this->_port; + std::string strport; + ss >> strport; + + std::string url = "http://" + this->_ip + ":" + strport + "/ping"; + std::string body = ""; + NjHttpHelper::request_get_sync(url.c_str(), body); + if (body == "OK") + { + return 1; + } + return 0; +} + +int NJGIS::SERVICE::NjServer::connect( const char* ip, const int port ) +{ + this->_ip = ip; + if(port > 65535 || port < 0) { return -1; } + this->_port = port; + return this->connect(); +} + +NJGIS::SERVICE::INjServiceAccess* NJGIS::SERVICE::NjServer::getServiceAccess() +{ + throw std::exception("The method or operation is not implemented."); +} + diff --git a/src/NxServer.h b/src/NxServer.h new file mode 100644 index 0000000000000000000000000000000000000000..4dc961e2185f02eae7bc7e037c87a465e50c8104 --- /dev/null +++ b/src/NxServer.h @@ -0,0 +1,37 @@ +#ifndef __NJGIS_NXSERVER_H__ +#define __NJGIS_NXSERVER_H__ + +#include "../include/INxServer.h" + +namespace NJGIS +{ + namespace SERVICE + { + class NjServer : public virtual INjServer + { + public: + NjServer(){}; + + NjServer(const char* ip, const int port):_ip(ip),_port(port){}; + + virtual int getIP( std::string &ip ); + + virtual int getPort(); + + virtual int setIPAndPort( const char* ip, const int port = 8060 ); + + virtual int connect(); + + virtual int connect( const char* ip, const int port = 8060 ); + + virtual INjServiceAccess* getServiceAccess(); + private: + std::string _ip; + + int _port; + + }; + } +} + +#endif \ No newline at end of file diff --git a/src/NxServerFactory.cpp b/src/NxServerFactory.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c9401c3c1d4882ebcc84b1121a96153106cf9e6 --- /dev/null +++ b/src/NxServerFactory.cpp @@ -0,0 +1,7 @@ +#include "NxServerFactory.h" +#include "NxServer.h" + +NJGIS::SERVICE::INjServer* NJGIS::SERVICE::NjServerFactory::createServer( const char* ip, const int port ) +{ + return new NJGIS::SERVICE::NjServer(ip, port); +} \ No newline at end of file diff --git a/src/NxServerFactory.h b/src/NxServerFactory.h new file mode 100644 index 0000000000000000000000000000000000000000..bd2028ce2be65a63db93d0a3e954836312272ac6 --- /dev/null +++ b/src/NxServerFactory.h @@ -0,0 +1,19 @@ +#ifndef __NJGSI_NXSERVERFACYORY_H__ +#define __NJGSI_NXSERVERFACYORY_H__ + +#include "../include/INxServerFactory.h" + +namespace NJGIS +{ + namespace SERVICE + { + class NjServerFactory : public virtual INjServerFactory + { + public: + virtual INjServer* createServer( const char* ip, const int port ); + + }; + } +} + +#endif \ No newline at end of file diff --git a/src/NxServiceAPI.cpp b/src/NxServiceAPI.cpp new file mode 100644 index 0000000000000000000000000000000000000000..03287560ebf8b03804ec514c6fae4429e317c13d --- /dev/null +++ b/src/NxServiceAPI.cpp @@ -0,0 +1,12 @@ +#include "../include/NxServiceAPI.h" +#include "NxServerFactory.h" + +extern "C" NJGIS_SERVICE const char* NJGIS::SERVICE::getVersion() +{ + return "0.1"; +} + +extern "C" NJGIS_SERVICE NJGIS::SERVICE::INjServerFactory* NJGIS::SERVICE::createServerFactory() +{ + return new NJGIS::SERVICE::NjServerFactory(); +} \ No newline at end of file diff --git a/test/test.cpp b/test/test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1a1fea45d6ac156478e4be0044930769e745355 --- /dev/null +++ b/test/test.cpp @@ -0,0 +1,17 @@ +#include "NxServiceAPI.h" +#include + +int main() +{ + NJGIS::SERVICE::INjServerFactory* pServerFactory = NJGIS::SERVICE::createServerFactory(); + NJGIS::SERVICE::INjServer* pServer = pServerFactory->createServer(); + if(pServer != NULL) + { + if(pServer->connect() == 1) + { + printf("Hello My SDK"); + } + } + + return 1; +} \ No newline at end of file diff --git a/utils/NxHttp.cpp b/utils/NxHttp.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e39661118a7b63edfd0a9715859f4ba3a06e404 --- /dev/null +++ b/utils/NxHttp.cpp @@ -0,0 +1,68 @@ +#include "NxHttp.h" +#include "curl/curl.h" + +int NJGIS::SERVICE::NjHttpHelper::init() +{ + curl_global_init(CURL_GLOBAL_ALL); + return 1; +} + +int NJGIS::SERVICE::NjHttpHelper::request_get_sync( const char* url, std::string &responesbody ) +{ + curl_global_init(CURL_GLOBAL_ALL); + // 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_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 *)&responesbody); + 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_post_sync( const char* url ) +{ + return 0; +} + +int NJGIS::SERVICE::NjHttpHelper::request_get_json_sync( const char* url, Json::Value &value ) +{ + std::string body; + int res = NJGIS::SERVICE::NjHttpHelper::request_get_sync(url, body); + Json::Reader reader; + if (reader.parse(body, value)) + { + return 1; + } + else + { + return -2; + } +} + +size_t NJGIS::SERVICE::NjHttpHelper::req_reply( void *ptr, size_t size, size_t nmemb, void *stream ) +{ + std::string *str = (std::string*)stream; + (*str).append((char*)ptr, size*nmemb); + return size * nmemb; +} diff --git a/utils/NxHttp.h b/utils/NxHttp.h new file mode 100644 index 0000000000000000000000000000000000000000..c41428300ada480a3dcaf3f3f0abd7d7b3ebbf01 --- /dev/null +++ b/utils/NxHttp.h @@ -0,0 +1,37 @@ +#ifndef __NJGIS_NXHTTP_H__ +#define __NJGIS_NXHTTP_H__ + +#include + +#include "curl/curl.h" +#include "json/json.h" + +namespace NJGIS +{ + namespace SERVICE + { + class NjHttpHelper + { + public: + //! init + static int init(); + + /* TEST */ + //! Request -GET + static int request_get_sync(const char* url, std::string &responesbody); + + //! Request -POST + static int request_post_sync(const char* url); + + /* JSON */ + //! Request -GET + static int request_get_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); + }; + } +} + +#endif \ No newline at end of file diff --git a/utils/README.md b/utils/README.md new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391