|
@@ -0,0 +1,150 @@
|
|
|
+#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){
|
|
|
+ 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->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 = this->password;
|
|
|
+ hepuloginMsg.objectToJson(msg);
|
|
|
+ msg += "\r\n";
|
|
|
+
|
|
|
+ if(auto res = m_hepu->Post("/cgi-bin/proc.cgi", msg,"application/json")){
|
|
|
+ if(res->status != 100){
|
|
|
+ HepuLoginRe hepuloginReMsg;
|
|
|
+ if(!hepuloginReMsg.jsonToObject(res->body)){
|
|
|
+ return ERR;
|
|
|
+ }
|
|
|
+ if(hepuloginReMsg.ackvalue != 100){
|
|
|
+ return ERR;
|
|
|
+ }
|
|
|
+ if(hepuloginReMsg.token.size()){
|
|
|
+ this->token = hepuloginReMsg.token;
|
|
|
+ return OK;
|
|
|
+ }
|
|
|
+ return ERR;
|
|
|
+ }
|
|
|
+ return ERR;
|
|
|
+ }else{
|
|
|
+ auto err = res.error();
|
|
|
+ ErrorL << "The service is not available,"<<err<<endl;
|
|
|
+ return ERR;
|
|
|
+ }
|
|
|
+ return ERR;
|
|
|
+ }
|
|
|
+
|
|
|
+ bool Hepu::alive(){
|
|
|
+ return this->token == "" ? false : true;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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";
|
|
|
+
|
|
|
+ if(auto res = m_hepu->Post("/cgi-bin/proc.cgi", msg,"application/json")){
|
|
|
+ if(res->status != 100){
|
|
|
+ HepuHBRe hepuHBReMsg;
|
|
|
+ if(!hepuHBReMsg.jsonToObject(res->body)){
|
|
|
+ return ERR;
|
|
|
+ }
|
|
|
+ if(hepuHBReMsg.ackvalue != 100){
|
|
|
+ return ERR;
|
|
|
+ }
|
|
|
+ return OK;
|
|
|
+ }
|
|
|
+ return ERR;
|
|
|
+ }else{
|
|
|
+ auto err = res.error();
|
|
|
+ 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";
|
|
|
+
|
|
|
+ if(auto res = m_hepu->Post("/cgi-bin/proc.cgi", msg,"application/json")){
|
|
|
+ if(res->status != 100){
|
|
|
+ HepuHBRe hepuHBReMsg;
|
|
|
+ if(!hepuHBReMsg.jsonToObject(res->body)){
|
|
|
+ return ERR;
|
|
|
+ }
|
|
|
+ if(hepuHBReMsg.ackvalue != 100){
|
|
|
+ return ERR;
|
|
|
+ }
|
|
|
+ return OK;
|
|
|
+ }
|
|
|
+ return ERR;
|
|
|
+ }else{
|
|
|
+ auto err = res.error();
|
|
|
+ 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.objectToJson(msg);
|
|
|
+ msg+="\r\n";
|
|
|
+
|
|
|
+ if(auto res = m_hepu->Post("/cgi-bin/proc.cgi", msg,"application/json")){
|
|
|
+ if(res->status != 100){
|
|
|
+ HepuTargetRe hepuTargetReMsg;
|
|
|
+ if(!hepuTargetReMsg.jsonToObject(res->body)){
|
|
|
+ return ERR;
|
|
|
+ }
|
|
|
+ if(hepuTargetReMsg.ackvalue != 100){
|
|
|
+ return ERR;
|
|
|
+ }
|
|
|
+ return OK;
|
|
|
+ }
|
|
|
+ return ERR;
|
|
|
+ }else{
|
|
|
+ auto err = res.error();
|
|
|
+ ErrorL << "Hepu Heartbeat failed,"<<err<<endl;
|
|
|
+ return ERR;
|
|
|
+ }
|
|
|
+ return ERR;
|
|
|
+ }
|
|
|
+}
|