PageRenderTime 18ms CodeModel.GetById 9ms app.highlight 7ms RepoModel.GetById 0ms app.codeStats 0ms

/dnstest.cpp

https://github.com/gigablast/open-source-search-engine
C++ | 152 lines | 78 code | 21 blank | 53 comment | 19 complexity | b516653354f8cb0c21de8263503a7f10 MD5 | raw file
Possible License(s): Apache-2.0
  1// Matt Wells, copyright Jan 2002
  2
  3// program to test Rdb
  4
  5#include "gb-include.h"
  6#include "Rdb.h"
  7#include "Conf.h"
  8#include <pthread.h>
  9#include "Dns.h"
 10#include "Url.h"
 11
 12bool allExit ( ) { return true; }
 13bool closeAll ( void *state , void (* callback)(void *state) ) {return true;}
 14
 15static void timeWrapper ( int fd , void *state ) ;
 16static void dnsWrapper ( void *state , int32_t ip ) ;
 17
 18static int32_t s_max = 10;
 19
 20bool mainShutdown ( bool urgent ) { return true; }
 21
 22int main ( int argc , char *argv[] ) {
 23
 24	// init our table for doing zobrist hashing
 25	if ( ! hashinit() ) {
 26		log("main::hashinit failed" ); return 1; }
 27	if ( argc < 2 ) {
 28		//fprintf(stderr,"dnstest <# threads> [conf filename]\n");
 29		fprintf(stderr,"dnstest <# threads> < FILEOFURLS\n");
 30		return -1;
 31	}
 32	g_mem.m_maxMem  = 1000000000LL; // 1G
 33	// default conf filename
 34	// char *confFilename = "./gb.conf";
 35	char *confFilename = "./";
 36	// set threads
 37	s_max = atoi(argv[1]);
 38	// use the command line parm as the full conf filename
 39	//if ( argc == 3 ) confFilename = argv[2];
 40	// use default
 41	//if ( argc <  3 ) confFilename = "/gigablast/gigablast.conf";
 42	// start up log file
 43	/*
 44	if ( ! g_log.init( "/tmp/log" )        ) {
 45		fprintf (stderr,"main::Log init failed\n" ); return 1; }
 46	*/
 47	// make a new conf
 48	// makeNewConf ( 0 , confFilename );
 49	// read in the conf file
 50	if ( ! g_conf.init ( confFilename ) ) {
 51		fprintf (stderr,"main::Conf init failed\n" ); return 1; }
 52	// debug the udp traffic
 53	//g_conf.m_logDebugUdp = true;
 54	g_conf.m_logDebugDns = true;
 55	// init the memory class after conf since it gets maxMem from Conf
 56	
 57	// if ( ! g_mem.init ( 1024*1024*30 ) ) {
 58	//	fprintf (stderr,"main::Mem init failed\n" ); return 1; }
 59
 60
 61	// . set up shared mem now, only on udpServer2
 62	// . will only set it up if we're the lowest hostId on this ip
 63	//if ( ! g_udpServer2.setupSharedMem() ) {
 64	//	log("main::SharedMem init failed" ); return 1; }
 65	// plotter test
 66	//g_stats.dumpGIF ();
 67	//exit (0);
 68	// init the loop
 69	if ( ! g_loop.init() ) {
 70		log("main::Loop init failed" ); return 1; }
 71
 72	//if( ! g_threads.init()){
 73	//	log("main:Init failed."); return 1;}
 74
 75	// start up hostdb
 76	if ( ! g_hostdb.init("./hosts.conf" , 0 )     ) {
 77		log("main::Hostdb init failed" ); return 1; }
 78	// . then dns client
 79	// . server should listen to a socket and register with g_loop
 80	if ( ! g_dns.init(8855)        ) {
 81		log("main::Dns client init failed" ); return 1; }
 82
 83	// every .1 seconds launch a dns request
 84	if (!g_loop.registerSleepCallback(100,NULL,timeWrapper))
 85		return false;	
 86	
 87	if ( ! g_loop.runLoop()    ) {
 88		log("main::runLoop failed" ); return 1; }
 89
 90	return 0;
 91}
 92
 93class StateT {
 94public:
 95	int32_t m_ip;
 96	char m_buf[1024];
 97	int64_t m_time;
 98};
 99
100static int32_t s_count = 0;
101
102void timeWrapper ( int fd , void *state ) { 
103 top:
104	// bail if too many launched
105	if ( s_count >= s_max ) return;
106	// new state
107	StateT *st = (StateT *)mmalloc ( sizeof(StateT) , "dnstest" );
108	// get url from stdin into buf
109	char *p = st->m_buf;
110	if ( ! fgets ( p , 1023 , stdin ) ) exit ( 0 );
111	// trim tail
112	while ( p[0] && !isalnum ( p [ gbstrlen(p) - 1] ) ) p [gbstrlen(p)-1]='\0';
113	// time it
114	st->m_time = gettimeofdayInMilliseconds();
115	// then look it up
116	Url url;
117	url.set ( p , gbstrlen(p) );
118	int32_t status = g_dns.getIp( url.getHost() , 
119			url.getHostLen() , &st->m_ip , st , dnsWrapper );
120	logf(LOG_INFO, "dnstest: Looking up %s", url.getHost());
121	if(g_errno) {
122		logf(LOG_INFO,"dns: %s.",mstrerror(g_errno));
123	}
124
125	// return on error
126	if ( status == -1 ) { log("ipWrapper: error"); return; }
127	// handle return if did not block
128	if ( status != 0 ) {
129		s_count++; 
130		//log(LOG_INFO,"dns: status is not 0, calling dnsWrapper");
131		dnsWrapper ( st , st->m_ip );
132	}
133	// otherwise count it
134	if ( status ==  0 ) {
135		s_count++; 
136		//log(LOG_INFO,"dns: status is 0, increment s_count to %i",
137		//		s_count);
138	}
139	// loop to do more
140	goto top;
141}
142
143void dnsWrapper ( void *state , int32_t ip ) {
144	StateT *st = (StateT *)state;
145	int64_t time = gettimeofdayInMilliseconds() - st->m_time ;
146	fprintf ( stderr,"Response: %"INT64"ms %s %s (%s)\n", time, 
147			st->m_buf , iptoa(ip) , mstrerror(g_errno));
148	//if ( g_errno == ETRYAGAIN )
149	//	log("hey");
150	mfree ( st , sizeof(StateT), "dnstest" );
151	s_count--;
152}