Hepu.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #include "Hepu.hpp"
  2. namespace gsd{
  3. std::shared_ptr<httplib::Client> m_hepu = nullptr;
  4. std::shared_ptr<Hepu> Hepu::CreateNew(){
  5. static shared_ptr<Hepu> hepu = nullptr;
  6. if(hepu == nullptr)
  7. hepu = std::make_shared<Hepu>();
  8. return hepu;
  9. }
  10. int32_t Hepu::Init(std::string username, std::string url, int port){
  11. //cout<<"Hepu username: "<<username<<endl;
  12. //cout<<"Hepu api url: "<<url<<endl;
  13. //cout<<"Hepu port: "<<port<<endl;
  14. this->username_ = username;
  15. m_hepu = std::make_shared<httplib::Client>(url, port);
  16. if(m_hepu == NULL){
  17. ErrorL << "Hepu http client initialization failed.";
  18. return ERR;
  19. }
  20. if(this->getSalt() != OK){
  21. ErrorL << "Failed to get salt from Hepu IPC.";
  22. return ERR;
  23. }
  24. if(this->LoginHepu() != OK){
  25. ErrorL << "Failed to login to Hepu IPC.";
  26. return ERR;
  27. }
  28. return OK;
  29. }
  30. int32_t Hepu::Init2(){
  31. m_hepu = std::make_shared<httplib::Client>(this->url_, this->port_);
  32. if(m_hepu == NULL){
  33. ErrorL << "Hepu http client initialization failed.";
  34. return ERR;
  35. }
  36. if(this->LoginHepu() != OK){
  37. ErrorL << "Failed to login to Hepu IPC.";
  38. return ERR;
  39. }
  40. return OK;
  41. }
  42. int32_t Hepu::LoginHepu(){
  43. if(m_hepu == nullptr) return ERR;
  44. HepuLogin hepuloginMsg;
  45. std::string msg;
  46. hepuloginMsg.token = "";
  47. hepuloginMsg.username = this->username_;
  48. hepuloginMsg.password = md5(this->password_ + this->salt_);
  49. hepuloginMsg.objectToJson(msg);
  50. msg += "\r\n";
  51. //cout<<"login json msg: "<<endl;
  52. //cout<<msg<<endl;
  53. if(auto res = m_hepu->Post("/cgi-bin/proc.cgi", msg,"application/json")){
  54. //cout<<res->body<<endl;
  55. if(res->status != 100){
  56. HepuLoginRe hepuloginReMsg;
  57. if(!hepuloginReMsg.jsonToObject(res->body)){
  58. //cout<<"login res json2object fail."<<endl;
  59. return ERR;
  60. }
  61. if(hepuloginReMsg.ackvalue != 100){
  62. //cout<<"login res ack err: "<<hepuloginReMsg.ackvalue<<endl;
  63. return ERR;
  64. }
  65. if(hepuloginReMsg.token.size()){
  66. this->token_ = hepuloginReMsg.token;
  67. //cout<<"login res token: "<<hepuloginReMsg.token<<endl;
  68. return OK;
  69. }
  70. return ERR;
  71. }
  72. return ERR;
  73. }else{
  74. auto err = res.error();
  75. //cout<<"The Hepu is not avaliable, "<<err<<endl;
  76. ErrorL << "The service is not available,"<<err<<endl;
  77. return ERR;
  78. }
  79. return ERR;
  80. }
  81. bool Hepu::alive(){
  82. return this->token_ == "" ? false : true;
  83. }
  84. int32_t Hepu::getSalt(HepuGetSalt& hepuGetSaltMsg){
  85. if(m_hepu == nullptr) return ERR;
  86. std::string msg;
  87. hepuGetSaltMsg.username = this->username_;
  88. hepuGetSaltMsg.objectToJson(msg);
  89. msg+="\r\n";
  90. //cout<<"get salt json msg: "<<endl;
  91. //cout<<msg<<endl;
  92. if(auto res = m_hepu->Post("/cgi-bin/proc.cgi", msg,"application/json")){
  93. //cout<<res->body<<endl;
  94. if(res->status != 100){
  95. HepuGetSaltRe hepuGetSaltReMsg;
  96. if(!hepuGetSaltReMsg.jsonToObject(res->body)){
  97. //cout<<"get salt res json2object fail."<<endl;
  98. return ERR;
  99. }
  100. this->salt_ = hepuGetSaltReMsg.salt;
  101. return OK;
  102. }
  103. return ERR;
  104. }else{
  105. auto err = res.error();
  106. //cout<<"hepu getsalt failed,"<<err<<endl;
  107. ErrorL <<"Hepu getsalt failed,"<<err<<endl;
  108. return ERR;
  109. }
  110. return ERR;
  111. }
  112. int32_t Hepu::getSalt(){
  113. if(m_hepu == nullptr) return ERR;
  114. HepuGetSalt hepuGetSaltMsg;
  115. std::string msg;
  116. hepuGetSaltMsg.username = this->username_;
  117. hepuGetSaltMsg.objectToJson(msg);
  118. msg+="\r\n";
  119. //cout<<"get salt json msg: "<<endl;
  120. //cout<<msg<<endl;
  121. if(auto res = m_hepu->Post("/cgi-bin/proc.cgi", msg,"application/json")){
  122. //cout<<res->body<<endl;
  123. if(res->status != 100){
  124. HepuGetSaltRe hepuGetSaltReMsg;
  125. if(!hepuGetSaltReMsg.jsonToObject(res->body)){
  126. //cout<<"get salt res json2object fail."<<endl;
  127. return ERR;
  128. }
  129. this->salt_ = hepuGetSaltReMsg.salt;
  130. return OK;
  131. }
  132. return ERR;
  133. }else{
  134. auto err = res.error();
  135. //cout<<"hepu getsalt failed,"<<err<<endl;
  136. ErrorL <<"Hepu getsalt failed,"<<err<<endl;
  137. return ERR;
  138. }
  139. return ERR;
  140. }
  141. int32_t Hepu::sendHepuHB(HepuHB& hepuHBMsg){
  142. if(m_hepu == nullptr) return ERR;
  143. std::string msg;
  144. hepuHBMsg.token = this->token_;
  145. hepuHBMsg.objectToJson(msg);
  146. msg+="\r\n";
  147. //cout<<"hb json msg: "<<endl;
  148. //cout<<msg<<endl;
  149. if(auto res = m_hepu->Post("/cgi-bin/proc.cgi", msg,"application/json")){
  150. //cout<<res->body<<endl;
  151. if(res->status != 100){
  152. HepuHBRe hepuHBReMsg;
  153. if(!hepuHBReMsg.jsonToObject(res->body)){
  154. //cout<<"hb res json2object fail."<<endl;
  155. return ERR;
  156. }
  157. if(hepuHBReMsg.ackvalue != 100){
  158. //cout<<"hb res ack err: "<<hepuHBReMsg.ackvalue<<endl;
  159. return ERR;
  160. }
  161. return OK;
  162. }
  163. return ERR;
  164. }else{
  165. auto err = res.error();
  166. //cout<<"Hepu heartbeat failed,"<<err<<endl;
  167. ErrorL << "Hepu Heartbeat failed,"<<err<<endl;
  168. return ERR;
  169. }
  170. return ERR;
  171. }
  172. int32_t Hepu::sendHepuHB(){
  173. if(m_hepu == nullptr) return ERR;
  174. HepuHB hepuHBMsg;
  175. std::string msg;
  176. hepuHBMsg.token = this->token_;
  177. hepuHBMsg.objectToJson(msg);
  178. msg+="\r\n";
  179. //cout<<"hb json msg: "<<endl;
  180. //cout<<msg<<endl;
  181. if(auto res = m_hepu->Post("/cgi-bin/proc.cgi", msg,"application/json")){
  182. //cout<<res->body<<endl;
  183. if(res->status != 100){
  184. HepuHBRe hepuHBReMsg;
  185. if(!hepuHBReMsg.jsonToObject(res->body)){
  186. //cout<<"hb res json2object fail."<<endl;
  187. return ERR;
  188. }
  189. if(hepuHBReMsg.ackvalue != 100){
  190. //cout<<"hb res ack err: "<<hepuHBReMsg.ackvalue<<endl;
  191. return ERR;
  192. }
  193. return OK;
  194. }
  195. return ERR;
  196. }else{
  197. auto err = res.error();
  198. //cout<<"Hepu heartbeat failed,"<<err<<endl;
  199. ErrorL << "Hepu Heartbeat failed,"<<err<<endl;
  200. return ERR;
  201. }
  202. return ERR;
  203. }
  204. int32_t Hepu::sendHepuTarget(HepuTarget& HepuTargetMsg){
  205. if(m_hepu == nullptr) return ERR;
  206. std::string msg;
  207. HepuTargetMsg.token = this->token_;
  208. HepuTargetMsg.channelid = 1;
  209. HepuTargetMsg.bTracking = true;
  210. HepuTargetMsg.trackingTime = 0;
  211. HepuTargetMsg.objectToJson(msg);
  212. msg+="\r\n";
  213. cout<<"Target json msg: "<<endl;
  214. cout<<msg<<endl;
  215. if(auto res = m_hepu->Post("/cgi-bin/proc.cgi", msg,"application/json")){
  216. cout<<res->body<<endl;
  217. if(res->status != 100){
  218. HepuTargetRe hepuTargetReMsg;
  219. if(!hepuTargetReMsg.jsonToObject(res->body)){
  220. cout<<"target res json2object fail."<<endl;
  221. return ERR;
  222. }
  223. if(hepuTargetReMsg.ackvalue != 100){
  224. cout<<"hb res ack err: "<<hepuTargetReMsg.ackvalue<<endl;
  225. return ERR;
  226. }
  227. return OK;
  228. }
  229. return ERR;
  230. }else{
  231. auto err = res.error();
  232. cout<<"Hepu send target failed,"<<err<<endl;
  233. ErrorL << "Hepu send target failed,"<<err<<endl;
  234. return ERR;
  235. }
  236. return ERR;
  237. }
  238. void Hepu::setUsr(string username)
  239. {
  240. this->username_ = username;
  241. }
  242. void Hepu::setPwd(string password)
  243. {
  244. this->password_ = password;
  245. }
  246. void Hepu::setUrl(string url)
  247. {
  248. this->url_ = url;
  249. }
  250. void Hepu::setPort(int port)
  251. {
  252. this->port_ = port;
  253. }
  254. }