acl/lib_acl_cpp/samples/aio/aio_dns/main.cpp

140 lines
2.7 KiB
C++
Raw Normal View History

#include "lib_acl.h"
2014-11-19 00:25:21 +08:00
#ifndef WIN32
#include <getopt.h>
#endif
#include <iostream>
2016-10-27 21:25:14 +08:00
#include "acl_cpp/acl_cpp_init.hpp"
2014-11-19 00:25:21 +08:00
#include "acl_cpp/stream/aio_handle.hpp"
#include "acl_cpp/stdlib/dns_service.hpp"
static void usage(const char* procname)
{
printf("usage: %s -h[help] -t[use thread]\n", procname);
}
static int __ncount = 0;
2019-06-16 20:10:40 +08:00
class dns_result : public acl::dns_result_callback
2014-11-19 00:25:21 +08:00
{
public:
dns_result(const char* domain)
2019-06-16 20:10:40 +08:00
: dns_result_callback(domain) {}
2014-11-19 00:25:21 +08:00
2019-06-16 20:10:40 +08:00
~dns_result(void) {}
2014-11-19 00:25:21 +08:00
2019-06-16 20:10:40 +08:00
// @override
void destroy(void)
2014-11-19 00:25:21 +08:00
{
delete this;
__ncount--;
}
2019-06-16 20:10:40 +08:00
// @override
void on_result(const char* domain, const acl::dns_res& res)
2014-11-19 00:25:21 +08:00
{
std::cout << "result: domain: " << domain;
2019-06-16 20:10:40 +08:00
if (res.ips_.size() == 0) {
2014-11-19 00:25:21 +08:00
std::cout << ": null" << std::endl;
return;
}
std::cout << std::endl;
2019-06-16 20:10:40 +08:00
std::list<acl::string>::const_iterator cit = res.ips_.begin();
for (; cit != res.ips_.end(); cit++) {
2014-11-19 00:25:21 +08:00
std::cout << "\t" << (*cit).c_str();
2019-06-16 20:10:40 +08:00
}
2014-11-19 00:25:21 +08:00
std::cout << std::endl;
}
};
int main(int argc, char* argv[])
{
int ch, nthreads = 2;
2019-06-16 20:10:40 +08:00
while ((ch = getopt(argc, argv, "ht:")) > 0) {
switch (ch) {
2014-11-19 00:25:21 +08:00
case 'h':
usage(argv[0]);
2019-06-16 20:10:40 +08:00
return 0;
2014-11-19 00:25:21 +08:00
case 't':
nthreads = atoi(optarg);
break;
default:
break;
}
}
2019-06-16 20:10:40 +08:00
acl::acl_cpp_init();
2014-11-19 00:25:21 +08:00
2019-06-16 20:10:40 +08:00
acl::aio_handle handle;
2014-11-19 00:25:21 +08:00
2019-06-16 20:10:40 +08:00
acl::dns_service* server = new acl::dns_service(nthreads);
2014-11-19 00:25:21 +08:00
// ʹ<><CAB9>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 127.0.0.1 <20>ĵ<EFBFBD>ַ
2019-06-16 20:10:40 +08:00
if (!server->open(&handle)) {
2014-11-19 00:25:21 +08:00
delete server;
std::cout << "open server error!" << std::endl;
getchar();
2019-06-16 20:10:40 +08:00
return 1;
2014-11-19 00:25:21 +08:00
}
2019-06-16 20:10:40 +08:00
const char* domain = "www.baidu.com";
2014-11-19 00:25:21 +08:00
dns_result* result = new dns_result(domain);
server->lookup(result);
__ncount++;
result = new dns_result(domain);
server->lookup(result);
__ncount++;
result = new dns_result(domain);
server->lookup(result);
__ncount++;
domain = "www.sina.com";
result = new dns_result(domain);
server->lookup(result);
__ncount++;
domain = "www.51iker.com";
result = new dns_result(domain);
server->lookup(result);
__ncount++;
domain = "www.hexun.com";
result = new dns_result(domain);
server->lookup(result);
__ncount++;
domain = "www.com.dummy";
result = new dns_result(domain);
server->lookup(result);
__ncount++;
result = new dns_result(domain);
server->lookup(result);
__ncount++;
result = new dns_result(domain);
server->lookup(result);
__ncount++;
2019-06-16 20:10:40 +08:00
while (true) {
if (handle.check() == false) {
2014-11-19 00:25:21 +08:00
std::cout << "stop now!" << std::endl;
break;
}
2019-06-16 20:10:40 +08:00
if (__ncount == 0) {
2014-11-19 00:25:21 +08:00
break;
2019-06-16 20:10:40 +08:00
}
2014-11-19 00:25:21 +08:00
}
delete server;
handle.check();
2019-06-16 20:10:40 +08:00
std::cout << "server stopped! enter any key to exit ..." << std::endl;
2014-11-19 00:25:21 +08:00
getchar();
2019-06-16 20:10:40 +08:00
return 0;
2014-11-19 00:25:21 +08:00
}