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