You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #include <windows.h>
  2. #include <iostream>
  3. #include <string>
  4. #include "snfmultidll.h"
  5. #include "../CodeDweller/timing.hpp"
  6. #include "../CodeDweller/threading.hpp"
  7. class Pounder : private codedweller::Thread { // A Pounder _REALLY_ scans its msg.
  8. private:
  9. std::string MessageToTest; // This is the message to test.
  10. bool TimeToStop; // True when time to stop.
  11. volatile int ScansAccomplished; // Number completed so far.
  12. volatile int HighestHandleSeen; // Highest handle so far.
  13. void myTask(); // How it is done.
  14. public:
  15. Pounder(std::string Message); // Ctor takes the message to scan.
  16. ~Pounder(); // Dtor shuts things down.
  17. int NumberOfScans(); // Returns number of scans.
  18. int HighestHandle(); // Returns the highest handle.
  19. void stop(); // Stops.
  20. };
  21. Pounder::Pounder(std::string Message) : // Ctor for a pounder.
  22. MessageToTest(Message), // Capture the message to test
  23. TimeToStop(false), // and initialize the variables.
  24. ScansAccomplished(0),
  25. HighestHandleSeen(0) {
  26. run(); // When ready, run the task.
  27. }
  28. Pounder::~Pounder() { // When being destroyed stop
  29. stop(); // the thread if it's not stopped.
  30. }
  31. int Pounder::NumberOfScans() { // Return the number of scans.
  32. return ScansAccomplished; // Simple.
  33. }
  34. int Pounder::HighestHandle() { // Return the hightest handle.
  35. return HighestHandleSeen; // Simple.
  36. }
  37. void Pounder::stop() { // To stop the thread:
  38. if(!TimeToStop) { // Skip if already stopped.
  39. TimeToStop = true; // If not stopped, set the stop
  40. join(); // flag and wait for the end.
  41. }
  42. }
  43. void Pounder::myTask() { // This is how it is done.
  44. while(!TimeToStop) { // Until it is time to stop:
  45. int ScanHandle = // Perform a scan and get
  46. scanBuffer( // a handle to it's results.
  47. (unsigned char*)MessageToTest.c_str(), // Scan the message buffer,
  48. MessageToTest.length(), // provide the buffer length,
  49. (char *) "nobigdeal", // name the message,
  50. 0 // and say no setup time.
  51. );
  52. if(1 > ScanHandle) { // If the scan failed, then
  53. TimeToStop = true; // it is time to stop.
  54. break;
  55. }
  56. if(ScanHandle > HighestHandleSeen) { // Keep track of the highest
  57. HighestHandleSeen = ScanHandle; // handle we've seen.
  58. }
  59. char* HeadersString = 0; // Setp to get the results as
  60. int HeadersStringLength = 0; // an xheader string.
  61. int ScanResult =
  62. getScanXHeaders(ScanHandle, &HeadersString, &HeadersStringLength); // Get the results.
  63. std::string XHeaders = HeadersString; // Form a string.
  64. closeScan(ScanHandle); // Got our data, close the scan.
  65. if(0 > ScanResult) { TimeToStop = true; break; } // If we got an error, stop.
  66. if(0 == HeadersStringLength) { TimeToStop = true; break; } // If there is no length, stop.
  67. if(HeadersStringLength != XHeaders.length()) { // If the string seems corrupt
  68. TimeToStop = true; break; // then stop.
  69. }
  70. ++ScansAccomplished; // All went well, count the scan.
  71. }
  72. }
  73. std::string BigGiantUglyMessage =
  74. "Received: from [213.93.213.84] (HELO uw-4b58d8528225.arnhem.chello.nl)\r\n"
  75. " by inbound.appriver.com (CommuniGate Pro SMTP 5.0.6)\r\n"
  76. "Received: from [13.93.213.84] (HELO uw-4b58d8528225.arnhem.chello.nl)\r\n"
  77. " by inbound.appriver.com (CommuniGate Pro SMTP 5.0.6)\r\n"
  78. "Received: from [23.93.213.84] (HELO uw-4b58d8528225.arnhem.chello.nl)\r\n"
  79. " by inbound.appriver.com (CommuniGate Pro SMTP 5.0.6)\r\n"
  80. "Received: from [33.93.213.84] (HELO uw-4b58d8528225.arnhem.chello.nl)\r\n"
  81. " by inbound.appriver.com (CommuniGate Pro SMTP 5.0.6)\r\n"
  82. "Received: from [43.93.213.84] (HELO uw-4b58d8528225.arnhem.chello.nl)\r\n"
  83. " by inbound.appriver.com (CommuniGate Pro SMTP 5.0.6)\r\n"
  84. "Received: from [53.93.213.84] (HELO uw-4b58d8528225.arnhem.chello.nl)\r\n"
  85. " by inbound.appriver.com (CommuniGate Pro SMTP 5.0.6)\r\n"
  86. "Received: from [63.93.213.84] (HELO uw-4b58d8528225.arnhem.chello.nl)\r\n"
  87. " by inbound.appriver.com (CommuniGate Pro SMTP 5.0.6)\r\n"
  88. "From: \"somebody\" <somebody@someplace.com>\r\n"
  89. "Subject: Nothing to see here\r\n"
  90. "\r\n"
  91. "So, this is a message eh?\r\n"
  92. "Don't you have something special to say?\r\n"
  93. "I suppose you could say something else too.\r\n"
  94. "This is just more filler text. No. Really.\r\n"
  95. "Nothing to see here. Move along. Move along.\r\n"
  96. "If ever a whiz of a whiz there was the wizard of oz is one because,\r\n"
  97. "because, because, because, because, because..... \r\n"
  98. "Because of the wonderful things he does.\r\n"
  99. "\r\n"
  100. "I repeat myself when under stress. I repeat myself when under stress.\r\n"
  101. "I repeat myself when under stress. I repeat myself when under stress.\r\n"
  102. "I repeat myself when under stress. I repeat myself when under stress.\r\n"
  103. "I repeat myself when under stress. I repeat...\r\n"
  104. "\r\n"
  105. "I think that's enough craziness for now.\r\n"
  106. ".\r\n";
  107. int main()
  108. {
  109. std::cout << "Hello world!" << std::endl;
  110. int LastResult;
  111. std::cout << "Throttling not set" << std::endl;
  112. //setThrottle(3);
  113. std::cout << "startupSNF(): ";
  114. LastResult = startupSNF((char *) "prescale.xml");
  115. std::cout << LastResult << std::endl;
  116. std::cout << "Scanning ugly text: ";
  117. std::string UglyText1 =
  118. "Received: from [213.93.213.84] (HELO uw-4b58d8528225.arnhem.chello.nl)\r\n"
  119. " by inbound.appriver.com (CommuniGate Pro SMTP 5.0.6)\r\n"
  120. "From: \"somebody\" <somebody@someplace.com>\r\n"
  121. "Subject: Nothing to see here\r\n"
  122. "\r\n"
  123. "So, this is a message eh?\r\n"
  124. ".\r\n";
  125. std::string UglyText2 =
  126. "Received: from [11.93.213.84] (HELO uw-4b58d8528225.arnhem.chello.nl)\r\n"
  127. " by inbound.appriver.com (CommuniGate Pro SMTP 5.0.6)\r\n"
  128. "From: \"somebody\" <somebody@someplace.com>\r\n"
  129. "Subject: Nothing to see here\r\n"
  130. "\r\n"
  131. "So, this is a message eh?\r\n"
  132. ".\r\n";
  133. int HandleOne = scanBuffer((unsigned char*)UglyText1.c_str(), UglyText1.length(), (char *) "", 0);
  134. char* Result1; int Result1Length;
  135. LastResult = getScanXHeaders(HandleOne, &Result1, &Result1Length);
  136. std::cout << HandleOne << ", " << LastResult << std::endl;
  137. std::cout << Result1;
  138. LastResult = closeScan(HandleOne);
  139. std::cout << "Closed handle one: " << LastResult << std::endl;
  140. // DEBUG.
  141. // cout << "scanFile('testFile', 33): " << scanFile("testFile", 33) << std::endl;
  142. // exit(0);
  143. // END OF DEBUG.
  144. HandleOne = scanBuffer((unsigned char*)UglyText1.c_str(), UglyText1.length(), (char *) "", 0);
  145. LastResult = getScanXHeaders(HandleOne, &Result1, &Result1Length);
  146. std::cout << HandleOne << ", " << LastResult << std::endl;
  147. std::cout << Result1;
  148. int HandleTwo = scanBuffer((unsigned char*)UglyText1.c_str(), UglyText1.length(), (char *) "", 0);
  149. char* Result2; int Result2Length;
  150. LastResult = getScanXHeaders(HandleTwo, &Result2, &Result2Length);
  151. std::cout << HandleTwo << ", " << LastResult << std::endl;
  152. std::cout << Result2;
  153. std::cout << "Closing Scanners: ";
  154. LastResult = closeScan(HandleOne);
  155. std::cout << LastResult << ", ";
  156. LastResult = closeScan(HandleTwo);
  157. std::cout << LastResult << std::endl;
  158. std::cout << "Scanners Closed" << std::endl;
  159. //// Heavy multi-thread testing now...
  160. codedweller::Sleeper WaitATic(1000);
  161. std::cout << "Starting Pounder 1" << std::endl;
  162. Pounder Pounder1(BigGiantUglyMessage);
  163. for(int a = 0; a < 10; a++) {
  164. WaitATic();
  165. std::cout
  166. << "Pounder 1: " << Pounder1.NumberOfScans()
  167. << ", " << Pounder1.HighestHandle() << std::endl;
  168. }
  169. std::cout << "Starting Pounder 2" << std::endl;
  170. Pounder Pounder2(BigGiantUglyMessage);
  171. for(int a = 0; a < 10; a++) {
  172. WaitATic();
  173. std::cout << "Pounder 1: " << Pounder1.NumberOfScans()
  174. << ", " << Pounder1.HighestHandle() << std::endl;
  175. std::cout << "Pounder 2: " << Pounder2.NumberOfScans()
  176. << ", " << Pounder2.HighestHandle() << std::endl;
  177. }
  178. std::cout << "Starting Pounder 3" << std::endl;
  179. Pounder Pounder3(BigGiantUglyMessage);
  180. for(int a = 0; a < 10; a++) {
  181. WaitATic();
  182. std::cout << "Pounder 1: " << Pounder1.NumberOfScans()
  183. << ", " << Pounder1.HighestHandle() << std::endl;
  184. std::cout << "Pounder 2: " << Pounder2.NumberOfScans()
  185. << ", " << Pounder2.HighestHandle() << std::endl;
  186. std::cout << "Pounder 3: " << Pounder3.NumberOfScans()
  187. << ", " << Pounder3.HighestHandle() << std::endl;
  188. }
  189. std::cout << "Starting Pounder 4" << std::endl;
  190. Pounder Pounder4(BigGiantUglyMessage);
  191. for(int a = 0; a < 10; a++) {
  192. WaitATic();
  193. std::cout << "Pounder 1: " << Pounder1.NumberOfScans()
  194. << ", " << Pounder1.HighestHandle() << std::endl;
  195. std::cout << "Pounder 2: " << Pounder2.NumberOfScans()
  196. << ", " << Pounder2.HighestHandle() << std::endl;
  197. std::cout << "Pounder 3: " << Pounder3.NumberOfScans()
  198. << ", " << Pounder3.HighestHandle() << std::endl;
  199. std::cout << "Pounder 4: " << Pounder4.NumberOfScans()
  200. << ", " << Pounder4.HighestHandle() << std::endl;
  201. }
  202. std::cout << "Starting Pounder 5" << std::endl;
  203. Pounder Pounder5(BigGiantUglyMessage);
  204. for(int a = 0; a < 10; a++) {
  205. WaitATic();
  206. std::cout << "Pounder 1: " << Pounder1.NumberOfScans()
  207. << ", " << Pounder1.HighestHandle() << std::endl;
  208. std::cout << "Pounder 2: " << Pounder2.NumberOfScans()
  209. << ", " << Pounder2.HighestHandle() << std::endl;
  210. std::cout << "Pounder 3: " << Pounder3.NumberOfScans()
  211. << ", " << Pounder3.HighestHandle() << std::endl;
  212. std::cout << "Pounder 4: " << Pounder4.NumberOfScans()
  213. << ", " << Pounder4.HighestHandle() << std::endl;
  214. std::cout << "Pounder 5: " << Pounder5.NumberOfScans()
  215. << ", " << Pounder5.HighestHandle() << std::endl;
  216. }
  217. std::cout << "Starting Pounder 6" << std::endl;
  218. Pounder Pounder6(BigGiantUglyMessage);
  219. for(int a = 0; a < 30; a++) {
  220. WaitATic();
  221. std::cout << "Pounder 1: " << Pounder1.NumberOfScans()
  222. << ", " << Pounder1.HighestHandle() << std::endl;
  223. std::cout << "Pounder 2: " << Pounder2.NumberOfScans()
  224. << ", " << Pounder2.HighestHandle() << std::endl;
  225. std::cout << "Pounder 3: " << Pounder3.NumberOfScans()
  226. << ", " << Pounder3.HighestHandle() << std::endl;
  227. std::cout << "Pounder 4: " << Pounder4.NumberOfScans()
  228. << ", " << Pounder4.HighestHandle() << std::endl;
  229. std::cout << "Pounder 5: " << Pounder5.NumberOfScans()
  230. << ", " << Pounder5.HighestHandle() << std::endl;
  231. std::cout << "Pounder 6: " << Pounder6.NumberOfScans()
  232. << ", " << Pounder6.HighestHandle() << std::endl;
  233. }
  234. std::cout << "Stopping All Pounders" << std::endl;
  235. Pounder1.stop(); std::cout << "1 stopped." << std::endl;
  236. Pounder2.stop(); std::cout << "2 stopped." << std::endl;
  237. Pounder3.stop(); std::cout << "3 stopped." << std::endl;
  238. Pounder4.stop(); std::cout << "4 stopped." << std::endl;
  239. Pounder5.stop(); std::cout << "5 stopped." << std::endl;
  240. Pounder6.stop(); std::cout << "6 stopped." << std::endl;
  241. std::cout << "shutdownSNF(): ";
  242. LastResult = shutdownSNF();
  243. std::cout << LastResult << std::endl;
  244. std::cout << "That's All Folks";
  245. return 0;
  246. }