29.1 Report设计与实现
report client主要是实现thread_queue的回调业务,udp server会定期的上传上报数据到reporter,那么请求对于report client就是透传给reporter serivce即可。
lars_loadbalance_agent/src/reporter_client.cpp
#include "lars_reactor.h"#include "main_server.h"#include <string>#include <pthread.h>//typedef void io_callback(event_loop *loop, int fd, void *args);//只要thread_queue有数据,loop就会触发此回调函数来处理业务void new_report_request(event_loop *loop, int fd, void *args){tcp_client *client = (tcp_client*)args;//1. 将请求数据从thread_queue中取出,std::queue<lars::ReportStatusRequest> msgs;//2. 将数据放在queue队列中report_queue->recv(msgs);//3. 遍历队列,通过client依次将每个msg发送给reporter servicewhile (!msgs.empty()) {lars::ReportStatusRequest req = msgs.front();msgs.pop();std::string requestString;req.SerializeToString(&requestString);//client 发送数据client->send_message(requestString.c_str(), requestString.size(), lars::ID_ReportStatusRequest);}}void *report_client_thread(void* args){printf("report client thread start\n");event_loop loop;//1 加载配置文件得到repoter ip + portstd::string ip = config_file::instance()->GetString("reporter", "ip", "");short port = config_file::instance()->GetNumber("reporter", "port", 0);//2 创建客户端tcp_client client(&loop, ip.c_str(), port, "reporter client");//3 将 thread_queue消息回调事件,绑定到loop中report_queue->set_loop(&loop);report_queue->set_callback(new_report_request, &client);//4 启动事件监听loop.event_process();return NULL;}void start_report_client(){//开辟一个线程pthread_t tid;//启动线程业务函数int ret = pthread_create(&tid, NULL, report_client_thread, NULL);if (ret == -1) {perror("pthread_create");exit(1);}//设置分离模式pthread_detach(tid);}
29.2 Dns Client设计与实现
dns client 和report client的业务十分相似,只是针对的协议不同了。dns client的thread_queue 回调业务主要是透传`lars::GetRouteRequest`数据包。
lars_loadbalance_agent/src/dns_client.cpp
#include "lars_reactor.h"#include "main_server.h"#include <pthread.h>//typedef void io_callback(event_loop *loop, int fd, void *args);//只要thread_queue有数据,loop就会触发此回调函数来处理业务void new_dns_request(event_loop *loop, int fd, void *args){tcp_client *client = (tcp_client*)args;//1. 将请求数据从thread_queue中取出,std::queue<lars::GetRouteRequest> msgs;//2. 将数据放在queue队列中dns_queue->recv(msgs);//3. 遍历队列,通过client依次将每个msg发送给reporter servicewhile (!msgs.empty()) {lars::GetRouteRequest req = msgs.front();msgs.pop();std::string requestString;req.SerializeToString(&requestString);//client 发送数据client->send_message(requestString.c_str(), requestString.size(), lars::ID_GetRouteRequest);}}void *dns_client_thread(void* args){printf("dns client thread start\n");event_loop loop;//1 加载配置文件得到dns service ip + portstd::string ip = config_file::instance()->GetString("dnsserver", "ip", "");short port = config_file::instance()->GetNumber("dnsserver", "port", 0);//2 创建客户端tcp_client client(&loop, ip.c_str(), port, "dns client");//3 将thread_queue消息回调事件,绑定到loop中dns_queue->set_loop(&loop);dns_queue->set_callback(new_dns_request, &client);//4 启动事件监听loop.event_process();return NULL;}void start_dns_client(){//开辟一个线程pthread_t tid;//启动线程业务函数int ret = pthread_create(&tid, NULL, dns_client_thread, NULL);if (ret == -1) {perror("pthread_create");exit(1);}//设置分离模式pthread_detach(tid);}
