123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- #include "Hepu.hpp"
- namespace gsd{
- std::shared_ptr<httplib::Client> m_hepu = nullptr;
- std::shared_ptr<Hepu> Hepu::CreateNew(){
- static shared_ptr<Hepu> hepu = nullptr;
- if(hepu == nullptr)
- hepu = std::make_shared<Hepu>();
- return hepu;
- }
- int32_t Hepu::Init(std::string username, std::string url, int port){
- //cout<<"Hepu username: "<<username<<endl;
- //cout<<"Hepu api url: "<<url<<endl;
- //cout<<"Hepu port: "<<port<<endl;
- this->username_ = username;
- m_hepu = std::make_shared<httplib::Client>(url, port);
- if(m_hepu == NULL){
- ErrorL << "Hepu http client initialization failed.";
- return ERR;
- }
- if(this->getSalt() != OK){
- ErrorL << "Failed to get salt from Hepu IPC.";
- return ERR;
- }
- if(this->LoginHepu() != OK){
- ErrorL << "Failed to login to Hepu IPC.";
- return ERR;
- }
- return OK;
- }
- int32_t Hepu::Init2(){
- m_hepu = std::make_shared<httplib::Client>(this->url_, this->port_);
- if(m_hepu == NULL){
- ErrorL << "Hepu http client initialization failed.";
- return ERR;
- }
- if(this->LoginHepu() != OK){
- ErrorL << "Failed to login to Hepu IPC.";
- return ERR;
- }
- return OK;
- }
- int32_t Hepu::LoginHepu(){
- if(m_hepu == nullptr) return ERR;
- HepuLogin hepuloginMsg;
- std::string msg;
- hepuloginMsg.token = "";
- hepuloginMsg.username = this->username_;
- hepuloginMsg.password = md5(this->password_ + this->salt_);
- hepuloginMsg.objectToJson(msg);
- msg += "\r\n";
- //cout<<"login json msg: "<<endl;
- //cout<<msg<<endl;
- if(auto res = m_hepu->Post("/cgi-bin/proc.cgi", msg,"application/json")){
- //cout<<res->body<<endl;
- if(res->status != 100){
- HepuLoginRe hepuloginReMsg;
- if(!hepuloginReMsg.jsonToObject(res->body)){
- //cout<<"login res json2object fail."<<endl;
- return ERR;
- }
- if(hepuloginReMsg.ackvalue != 100){
- //cout<<"login res ack err: "<<hepuloginReMsg.ackvalue<<endl;
- return ERR;
- }
- if(hepuloginReMsg.token.size()){
- this->token_ = hepuloginReMsg.token;
- //cout<<"login res token: "<<hepuloginReMsg.token<<endl;
- return OK;
- }
- return ERR;
- }
- return ERR;
- }else{
- auto err = res.error();
- //cout<<"The Hepu is not avaliable, "<<err<<endl;
- ErrorL << "The service is not available,"<<err<<endl;
- return ERR;
- }
- return ERR;
- }
- bool Hepu::alive(){
- return this->token_ == "" ? false : true;
- }
- int32_t Hepu::getSalt(HepuGetSalt& hepuGetSaltMsg){
- if(m_hepu == nullptr) return ERR;
- std::string msg;
- hepuGetSaltMsg.username = this->username_;
- hepuGetSaltMsg.objectToJson(msg);
- msg+="\r\n";
- //cout<<"get salt json msg: "<<endl;
- //cout<<msg<<endl;
- if(auto res = m_hepu->Post("/cgi-bin/proc.cgi", msg,"application/json")){
- //cout<<res->body<<endl;
- if(res->status != 100){
- HepuGetSaltRe hepuGetSaltReMsg;
- if(!hepuGetSaltReMsg.jsonToObject(res->body)){
- //cout<<"get salt res json2object fail."<<endl;
- return ERR;
- }
- this->salt_ = hepuGetSaltReMsg.salt;
- return OK;
- }
- return ERR;
- }else{
- auto err = res.error();
- //cout<<"hepu getsalt failed,"<<err<<endl;
- ErrorL <<"Hepu getsalt failed,"<<err<<endl;
- return ERR;
- }
- return ERR;
- }
- int32_t Hepu::getSalt(){
- if(m_hepu == nullptr) return ERR;
- HepuGetSalt hepuGetSaltMsg;
- std::string msg;
- hepuGetSaltMsg.username = this->username_;
- hepuGetSaltMsg.objectToJson(msg);
- msg+="\r\n";
- //cout<<"get salt json msg: "<<endl;
- //cout<<msg<<endl;
- if(auto res = m_hepu->Post("/cgi-bin/proc.cgi", msg,"application/json")){
- //cout<<res->body<<endl;
- if(res->status != 100){
- HepuGetSaltRe hepuGetSaltReMsg;
- if(!hepuGetSaltReMsg.jsonToObject(res->body)){
- //cout<<"get salt res json2object fail."<<endl;
- return ERR;
- }
- this->salt_ = hepuGetSaltReMsg.salt;
- return OK;
- }
- return ERR;
- }else{
- auto err = res.error();
- //cout<<"hepu getsalt failed,"<<err<<endl;
- ErrorL <<"Hepu getsalt failed,"<<err<<endl;
- return ERR;
- }
- return ERR;
- }
- int32_t Hepu::sendHepuHB(HepuHB& hepuHBMsg){
- if(m_hepu == nullptr) return ERR;
- std::string msg;
- hepuHBMsg.token = this->token_;
- hepuHBMsg.objectToJson(msg);
- msg+="\r\n";
-
- //cout<<"hb json msg: "<<endl;
- //cout<<msg<<endl;
- if(auto res = m_hepu->Post("/cgi-bin/proc.cgi", msg,"application/json")){
- //cout<<res->body<<endl;
- if(res->status != 100){
- HepuHBRe hepuHBReMsg;
- if(!hepuHBReMsg.jsonToObject(res->body)){
- //cout<<"hb res json2object fail."<<endl;
- return ERR;
- }
- if(hepuHBReMsg.ackvalue != 100){
- //cout<<"hb res ack err: "<<hepuHBReMsg.ackvalue<<endl;
- return ERR;
- }
- return OK;
- }
- return ERR;
- }else{
- auto err = res.error();
- //cout<<"Hepu heartbeat failed,"<<err<<endl;
- ErrorL << "Hepu Heartbeat failed,"<<err<<endl;
- return ERR;
- }
- return ERR;
- }
- int32_t Hepu::sendHepuHB(){
- if(m_hepu == nullptr) return ERR;
- HepuHB hepuHBMsg;
- std::string msg;
- hepuHBMsg.token = this->token_;
- hepuHBMsg.objectToJson(msg);
- msg+="\r\n";
-
- //cout<<"hb json msg: "<<endl;
- //cout<<msg<<endl;
- if(auto res = m_hepu->Post("/cgi-bin/proc.cgi", msg,"application/json")){
- //cout<<res->body<<endl;
- if(res->status != 100){
- HepuHBRe hepuHBReMsg;
- if(!hepuHBReMsg.jsonToObject(res->body)){
- //cout<<"hb res json2object fail."<<endl;
- return ERR;
- }
- if(hepuHBReMsg.ackvalue != 100){
- //cout<<"hb res ack err: "<<hepuHBReMsg.ackvalue<<endl;
- return ERR;
- }
- return OK;
- }
- return ERR;
- }else{
- auto err = res.error();
- //cout<<"Hepu heartbeat failed,"<<err<<endl;
- ErrorL << "Hepu Heartbeat failed,"<<err<<endl;
- return ERR;
- }
- return ERR;
- }
- int32_t Hepu::sendHepuTarget(HepuTarget& HepuTargetMsg){
- if(m_hepu == nullptr) return ERR;
- std::string msg;
- HepuTargetMsg.token = this->token_;
- HepuTargetMsg.channelid = 1;
- HepuTargetMsg.bTracking = true;
- HepuTargetMsg.trackingTime = 0;
- HepuTargetMsg.objectToJson(msg);
- msg+="\r\n";
-
- cout<<"Target json msg: "<<endl;
- cout<<msg<<endl;
- if(auto res = m_hepu->Post("/cgi-bin/proc.cgi", msg,"application/json")){
- cout<<res->body<<endl;
- if(res->status != 100){
- HepuTargetRe hepuTargetReMsg;
- if(!hepuTargetReMsg.jsonToObject(res->body)){
- cout<<"target res json2object fail."<<endl;
- return ERR;
- }
- if(hepuTargetReMsg.ackvalue != 100){
- cout<<"hb res ack err: "<<hepuTargetReMsg.ackvalue<<endl;
- return ERR;
- }
- return OK;
- }
- return ERR;
- }else{
- auto err = res.error();
- cout<<"Hepu send target failed,"<<err<<endl;
- ErrorL << "Hepu send target failed,"<<err<<endl;
- return ERR;
- }
- return ERR;
- }
- void Hepu::setUsr(string username)
- {
- this->username_ = username;
- }
- void Hepu::setPwd(string password)
- {
- this->password_ = password;
- }
- void Hepu::setUrl(string url)
- {
- this->url_ = url;
- }
- void Hepu::setPort(int port)
- {
- this->port_ = port;
- }
- }
|