/*Software License Copyright(C) 2024[liuyingjie] License Terms Usage Rights 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. 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. Future Versions Updates 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. Users must comply with the terms of the new license issued in future releases. Liability and Disclaimer 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. Contact Information If you have any questions, please contact us: 1585346868@qq.com Or visit our website fwlua.com. */ #include "net/http_cdn.h" #include "net/http_website.h" #include "net/http_router.h" #include "net/http_reqpack.h" #include "net/http_response.h" #include "net/http_request.h" #include "net/http_subscribe.h" network::http::cnode::cnode(const cdn_config::node_config& config){ m_reply_wait_msec = 0; m_send_bytes_sec = 0; m_recv_bytes_sec = 0; m_update_sec = 0; m_net_status = false; m_max_band = 0; m_mang_url = "http://"+ config.mang_domain+"/info"; } network::http::cnode::~cnode(){} int network::http::cnode::score(){ if(m_net_status == false) return 0; return (int)((double)100 - ((double)m_send_bytes_sec / (double)m_max_band * 100)); } bool network::http::cnode::update(){ timestamp start_msec = time::now_msec(); m_client.headers_request().set("key",m_config.key); if(m_client.get(m_mang_url) == false){ m_net_status = false; m_net_errormsg = "connect failed"; m_lastErrorDesc = m_net_errormsg; return false; } m_reply_wait_msec = time::now_msec() - start_msec; ylib::json rep = ylib::json::from(m_client.response().to_string()); //m_client.response().to_string().println(); // std::cout<() == false){ m_net_status = false; m_net_errormsg = rep["msg"].to(); m_lastErrorDesc = m_net_errormsg; return false; } m_recv_bytes_sec = rep["down_sec_byte"].to(); m_send_bytes_sec = rep["up_sec_byte"].to(); m_max_band = rep["max_band"].to(); m_update_sec = time::now_sec(); m_net_status = true; return true; } network::http::cdn::cdn() { m_cdn_node_max_band = 0; } network::http::cdn::~cdn() { ::ithread::stop(); ::ithread::wait(); for(size_t i=0;irouter()->subscribe()->add("/info",network::http::GET,[&](network::http::request* request,network::http::response* response,void* extra){ ylib::json rep; std::string value_key; if(request->header("key",value_key) == false || m_cdn_node_key != value_key){ rep["result"] = false; rep["msg"] = "key is empty or incorrect"; response->send(rep); return; } rep["result"] = true; rep["down_all"] = m_cdn_node_bandinfo.down_all; rep["down_sec_byte"] = m_cdn_node_bandinfo.down_sec_byte; rep["up_all"] = m_cdn_node_bandinfo.up_all; rep["up_sec_byte"] = m_cdn_node_bandinfo.up_sec_byte; rep["max_band"] = m_cdn_node_max_band; response->send(rep); });*/ } ::ithread::start(); return true; } void ylib::network::http::cdn::close() { ::ithread::stop(); ::ithread::wait(); m_nodes.clear(); } std::string network::http::cdn::host(){ int32 score = 0; std::string result_host; for(size_t i=0;iscore() > score){ result_host = m_nodes[i]->config().host; score = m_nodes[i]->score(); } } return result_host; } bool network::http::cdn::run(){ if(m_config.manager){ for(size_t i=0;iupdate() == false){ std::cout<last_error()<