/misc/udptest.cpp

https://github.com/privacore/open-source-search-engine · C++ · 162 lines · 108 code · 20 blank · 34 comment · 22 complexity · c390ace4cbf925245a82127c7398e0f5 MD5 · raw file

  1. // Matt Wells, copyright Sep 2001
  2. #include "gb-include.h"
  3. #include "Mem.h"
  4. #include "Conf.h"
  5. #include "Hostdb.h"
  6. #include "UdpServer.h"
  7. #include "Loop.h"
  8. #include "ip.h"
  9. #include <ctype.h>
  10. #include "Spider.h"
  11. // the default protocol
  12. UdpProtocol g_dp;
  13. // the request handler
  14. static void handleRequest ( UdpSlot *slot , int32_t niceness ) ;
  15. static void gotit ( void *state , UdpSlot *slot ) ;
  16. // where we store the file contents, if any
  17. char s_buf [ 1024*1024 ];
  18. int32_t s_n = 0;
  19. int64_t s_startTime;
  20. int main ( int argc , char *argv[] ) {
  21. // ip/port to ask for the file from
  22. int32_t ip = 0;
  23. uint16_t port = 0;
  24. // . filename may be supplied
  25. // . if so we send that file back to all who ask
  26. if ( argc == 2 && !isdigit(argv[1][0]) ) {
  27. char *fn = argv[1];
  28. log("Reading filename = %s", fn );
  29. int fd = open ( fn , O_RDONLY );
  30. if ( fd < 0 ) {
  31. log("File %s does not exist. exiting.",fn );
  32. return -1;
  33. }
  34. s_n = 0;
  35. while ( s_n < 1024*1024 && read ( fd , &s_buf[s_n] , 1 ) == 1)
  36. s_n++;
  37. close ( fd );
  38. // listen on udp port 2000
  39. port = 2000;
  40. //log("Listening on udp port %hu", port );
  41. }
  42. // otherwise if argc is 2 we ask an ip/port for the file
  43. else if ( argc == 2 && isdigit(argv[1][0]) ) {
  44. // send on udp port 2001
  45. port = 2001;
  46. ip = atoip ( argv[1] , gbstrlen(argv[1]) );
  47. }
  48. // otherwise, print usage
  49. else {
  50. fprintf(stderr,
  51. "udptest <filename>\n"
  52. "\tThis will set up a server on udp port 2000 which\n"
  53. "\twill serve the sepcified filename to all incoming\n"
  54. "\trequests. The first char of the filename must NOT\n"
  55. "\tbe a number.\n\n"
  56. "udptest <ip>\n"
  57. "\tThis will send a request for a file to the\n"
  58. "\tudptest running on the specified ip. ip must be\n"
  59. "\tof form like 1.2.3.4\n"
  60. "\tIt will listen on udp port 2001.\n"
  61. );
  62. return -1;
  63. }
  64. // init our table for doing zobrist hashing
  65. if ( ! hashinit() ) {
  66. log("main::hashinit failed" ); return 1; }
  67. // default conf filename
  68. char *confFilename = "./gigablast.conf";
  69. if ( ! g_conf.init ( confFilename ) ) {
  70. fprintf (stderr,"main::Conf init failed\n" ); return 1; }
  71. // init the memory class after conf since it gets maxMem from Conf
  72. if ( ! g_mem.init ( 1000000000 ) ) {
  73. fprintf (stderr,"main::Mem init failed" ); return 1; }
  74. // start up hostdb
  75. if ( ! g_hostdb.init() ) {
  76. log("main::Hostdb init failed" ); return 1; }
  77. // init the loop
  78. if ( ! g_loop.init() ) {
  79. fprintf ( stderr , "main::Loop init failed\n" );
  80. return 1;
  81. }
  82. // . then our main udp server
  83. // . must pass defaults since g_dns uses it's own port/instance of it
  84. // . server should listen to a socket and register with g_loop
  85. // . the last 1 and 0 are respective niceness levels
  86. if ( ! g_udpServer.init( port , // use 2000 or 2001
  87. &g_dp , // use default proto
  88. 1024*1024 ,
  89. 1024*1024 )){
  90. fprintf ( stderr, "main::UdpServer init failed\n");
  91. return 1;
  92. }
  93. // . otherwise, set up a handler for incoming msgTypes of 0x00
  94. // . register a handler for msgType 0x00
  95. // . we must register this msgType even if we're not going to handle
  96. // these requests, because we may send them...
  97. if ( ! g_udpServer.registerHandler ( 0x00 , handleRequest )) {
  98. fprintf ( stderr , "udp server registration failed\n");
  99. return 1;
  100. }
  101. // if ip is non-zero send a request to it
  102. if ( ip != 0 ) {
  103. log("Sending request to ip=%s port=2000", argv[1] );
  104. s_startTime = gettimeofdayInMilliseconds();
  105. g_udpServer.sendRequest ( NULL , // msg ptr
  106. 0 , // msg size
  107. 0x00 , // msg type
  108. ip , // ip
  109. 2000 , // port
  110. 0 , // hostId (bogus)
  111. NULL , // UdpSlot ptr being used
  112. NULL , // callback state
  113. gotit, // callback
  114. 30*1000 );// timeout in seconds
  115. }
  116. // . now start g_loops main interrupt handling loop
  117. // . it should block forever
  118. // . when it gets a signal it dispatches to a server or db to handle it
  119. if ( ! g_loop.runLoop() ) {
  120. log("main::runLoop failed" );
  121. return 1;
  122. }
  123. // dummy return (0-->normal exit status for the shell)
  124. return 0;
  125. }
  126. void handleRequest ( UdpSlot *slot , int32_t niceness ) {
  127. log("got request of type 0x00");
  128. // send the deisignated file back
  129. g_udpServer.sendReply ( s_buf , // data ptr
  130. s_n , // data size
  131. 0 , // allocated size
  132. slot );
  133. //sendErrorReply ( slot , EBADREQUESTSIZE )
  134. }
  135. void gotit ( void *state , UdpSlot *slot ) {
  136. // compute speed
  137. double size = slot->m_readBufSize;
  138. double elapsed = gettimeofdayInMilliseconds() - s_startTime;
  139. log("got reply. %f Mbps", (size*8/(1024.0*1024.0)) / (elapsed / 1000.0) );
  140. // exit for good
  141. exit(-1);
  142. }