http_cdn.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*Software License
  2. Copyright(C) 2024[liuyingjie]
  3. License Terms
  4. Usage Rights
  5. Any individual or entity is free to use, copy, and distribute the binary form of this software without modification to the source code, without the need to disclose the source code.
  6. If the source code is modified, the modifications must be open - sourced under the same license.This means that the modifications must be disclosed and accompanied by a copy of this license.
  7. Future Versions Updates
  8. From this version onwards, all future releases will be governed by the terms of the latest version of the license.This license will automatically be nullified and replaced by the new version.
  9. Users must comply with the terms of the new license issued in future releases.
  10. Liability and Disclaimer
  11. This software is provided “as is”, without any express or implied warranties, including but not limited to the warranties of merchantability, fitness for a particular purpose, and non - infringement.In no event shall the author or copyright holder be liable for any claims, damages, or other liabilities, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software.
  12. Contact Information
  13. If you have any questions, please contact us: 1585346868@qq.com Or visit our website fwlua.com.
  14. */
  15. #include "net/http_cdn.h"
  16. #include "net/http_website.h"
  17. #include "net/http_router.h"
  18. #include "net/http_reqpack.h"
  19. #include "net/http_response.h"
  20. #include "net/http_request.h"
  21. #include "net/http_subscribe.h"
  22. network::http::cnode::cnode(const cdn_config::node_config& config){
  23. m_reply_wait_msec = 0;
  24. m_send_bytes_sec = 0;
  25. m_recv_bytes_sec = 0;
  26. m_update_sec = 0;
  27. m_net_status = false;
  28. m_max_band = 0;
  29. m_mang_url = "http://"+ config.mang_domain+"/info";
  30. }
  31. network::http::cnode::~cnode(){}
  32. int network::http::cnode::score(){
  33. if(m_net_status == false)
  34. return 0;
  35. return (int)((double)100 - ((double)m_send_bytes_sec / (double)m_max_band * 100));
  36. }
  37. bool network::http::cnode::update(){
  38. timestamp start_msec = time::now_msec();
  39. m_client.headers_request().set("key",m_config.key);
  40. if(m_client.get(m_mang_url) == false){
  41. m_net_status = false;
  42. m_net_errormsg = "connect failed";
  43. m_lastErrorDesc = m_net_errormsg;
  44. return false;
  45. }
  46. m_reply_wait_msec = time::now_msec() - start_msec;
  47. ylib::json rep = ylib::json::from(m_client.response().to_string());
  48. //m_client.response().to_string().println();
  49. // std::cout<<rep.to_string(true).c_str()<<std::endl;
  50. if(rep["result"].to<bool>() == false){
  51. m_net_status = false;
  52. m_net_errormsg = rep["msg"].to<std::string>();
  53. m_lastErrorDesc = m_net_errormsg;
  54. return false;
  55. }
  56. m_recv_bytes_sec = rep["down_sec_byte"].to<uint64>();
  57. m_send_bytes_sec = rep["up_sec_byte"].to<uint64>();
  58. m_max_band = rep["max_band"].to<uint64>();
  59. m_update_sec = time::now_sec();
  60. m_net_status = true;
  61. return true;
  62. }
  63. network::http::cdn::cdn()
  64. {
  65. m_cdn_node_max_band = 0;
  66. }
  67. network::http::cdn::~cdn()
  68. {
  69. ::ithread::stop();
  70. ::ithread::wait();
  71. for(size_t i=0;i<m_nodes.size();i++)
  72. {
  73. delete m_nodes[i];
  74. }
  75. }
  76. bool network::http::cdn::start(const cdn_config& config){
  77. m_config = config;
  78. if(m_config.enable == false)
  79. return true;
  80. if(m_config.manager)
  81. {
  82. std::cout<<"CDN:Manager start"<<std::endl;
  83. for(size_t i=0;i< m_config.node.size();i++)
  84. {
  85. auto n = new cnode(m_config.node[i]);
  86. m_nodes.push_back(n);
  87. }
  88. }
  89. else
  90. {
  91. std::cout<<"CDN:Node start"<<std::endl;
  92. /* website()->router()->subscribe()->add("/info",network::http::GET,[&](network::http::request* request,network::http::response* response,void* extra){
  93. ylib::json rep;
  94. std::string value_key;
  95. if(request->header("key",value_key) == false || m_cdn_node_key != value_key){
  96. rep["result"] = false;
  97. rep["msg"] = "key is empty or incorrect";
  98. response->send(rep);
  99. return;
  100. }
  101. rep["result"] = true;
  102. rep["down_all"] = m_cdn_node_bandinfo.down_all;
  103. rep["down_sec_byte"] = m_cdn_node_bandinfo.down_sec_byte;
  104. rep["up_all"] = m_cdn_node_bandinfo.up_all;
  105. rep["up_sec_byte"] = m_cdn_node_bandinfo.up_sec_byte;
  106. rep["max_band"] = m_cdn_node_max_band;
  107. response->send(rep);
  108. });*/
  109. }
  110. ::ithread::start();
  111. return true;
  112. }
  113. void ylib::network::http::cdn::close()
  114. {
  115. ::ithread::stop();
  116. ::ithread::wait();
  117. m_nodes.clear();
  118. }
  119. std::string network::http::cdn::host(){
  120. int32 score = 0;
  121. std::string result_host;
  122. for(size_t i=0;i<m_nodes.size();i++)
  123. {
  124. if(m_nodes[i]->score() > score){
  125. result_host = m_nodes[i]->config().host;
  126. score = m_nodes[i]->score();
  127. }
  128. }
  129. return result_host;
  130. }
  131. bool network::http::cdn::run(){
  132. if(m_config.manager){
  133. for(size_t i=0;i<m_nodes.size();i++)
  134. {
  135. if(m_nodes[i]->update() == false){
  136. std::cout<<m_nodes[i]->last_error()<<std::endl;
  137. }
  138. }
  139. }
  140. else
  141. {
  142. #ifndef _WIN32
  143. //m_cdn_node_bandinfo = system::bandinfo();
  144. #endif
  145. }
  146. system::sleep_msec(1000);
  147. return true;
  148. }