Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

main.cpp 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <ctime>
  4. #include <cstring>
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <fstream>
  8. #include <sstream>
  9. #include <string>
  10. #include <queue>
  11. #include <cmath>
  12. #include <exception>
  13. #include <stdexcept>
  14. #include "unistd.h"
  15. #include "timing.hpp"
  16. #include "threading.hpp"
  17. #include "networking.hpp"
  18. #include "SNFMulti.hpp"
  19. #include "snf_xci.hpp"
  20. #include "snf_sync.hpp"
  21. #include "config.h"
  22. // temporary - proving base64codec
  23. #include "base64codec.hpp"
  24. //#include "../nvwa-0.6/nvwa/debug_new.h"
  25. using namespace std; // Introduce standard namespace.
  26. const char* SERVER_VERSION_INFO = "SNF Server Version " PACKAGE_VERSION " Build: " __DATE__ " " __TIME__;
  27. static const string XCIShutdownResponse =
  28. "<snf><xci><server><response message=\'shutdown in progress\' code=\'0\'/></server></xci></snf>\n";
  29. class XCIShutdownWatcher : public snfXCIServerCommandHandler { // Shutdown watcher.
  30. public:
  31. XCIShutdownWatcher():TimeToStop(false){} // Construct with shutdown flag false.
  32. bool TimeToStop; // Here is the flag.
  33. string processXCIRequest(snf_xci& X) { // Here is how we process requests.
  34. if(0 == X.xci_server_command.find("shutdown")) { // If we find shutdown then
  35. TimeToStop = true; // set the shutdown flag
  36. return XCIShutdownResponse; // and let them know we got it.
  37. } // If we get some other request
  38. return XCIErrorResponse; // return the error response.
  39. }
  40. };
  41. // Thread Status Analysis For Debugging.
  42. void ThreadStatusToCout() { // Produce a thread status list.
  43. ThreadStatusReport R = Threads.StatusReport(); // Get a report from Threads.
  44. cout << endl; // Break the line.
  45. for(
  46. ThreadStatusReport::iterator iR = R.begin(); // Loop through the report.
  47. iR != R.end(); iR++
  48. ) {
  49. ThreadStatusRecord& S = (*iR); // Take each status report and
  50. cout // send it to cout on it's own line.
  51. << S.getName() << " (" << S.getPointer() << "), "
  52. << S.getType().Name << ", "
  53. << S.getState().Name << ", "
  54. << ((S.getRunning()) ? "Running, " : "Not Running, ")
  55. << ((S.getBad()) ? "Broken, " : "Ok, ")
  56. << S.getFault()
  57. << endl;
  58. }
  59. cout << endl; // Leave a blank line at the end.
  60. }
  61. // Here in the main thread is where we get executive tasks done.
  62. int go(int argc, char* argv[]) { //// go() stands in for main(). main() catches any unhandled exceptions.
  63. // Check for debug mode.
  64. bool DebugMode = false; // This will be our debug mode.
  65. string argv0(argv[0]); // Capture how we were called.
  66. if(
  67. string::npos != argv0.find("Debug") || // If we find "Debug" or
  68. string::npos != argv0.find("debug") // "debug" in our command path
  69. ) { // then we are in DebugMode.
  70. DebugMode = true; // Set the flag and tell the
  71. cout << "Debug Mode" << endl; // watchers.
  72. }
  73. // DebugMode = true; // Force it when needed.
  74. // Announce Version / Build Info.
  75. cout << SERVER_VERSION_INFO << endl; // Shout out our version.
  76. cout << SNF_ENGINE_VERSION << endl; // Shout out the engine version.
  77. // Sanity checks before we get going.
  78. if(2 != argc) { // Check the command line args.
  79. cout << "Use: SNFServer <path-to-config-file>" << endl; // If wrong, say how we work.
  80. return 0;
  81. }
  82. if(0 != access(argv[1], R_OK)) { // Check the config file path.
  83. cout << "Can't read " << argv[1] << endl; // If it's not accessible, punt.
  84. return 0;
  85. }
  86. cout << "Launching with " << argv[1] << endl; // Tell them we're going.
  87. snf_RulebaseHandler MyRulebase; // Create a rulebase manager.
  88. MyRulebase.PlatformVersion(SERVER_VERSION_INFO); // Set the Platform version string.
  89. XCIShutdownWatcher ShutdownWatcher; // Make a server shutdown processor
  90. MyRulebase.XCIServerCommandHandler(ShutdownWatcher); // and register it with the engine.
  91. MyRulebase.open(argv[1], "", ""); // Open a configured rulebase.
  92. Sleeper WaitATic(1000); // Learn to wait a second.
  93. cout << "Running." << endl << endl; // Tell them we're running.
  94. char Tic = '\\'; // Tic/Toc indicator.
  95. while(false == ShutdownWatcher.TimeToStop) { // While running, update the screen.
  96. WaitATic(); // One second between updates.
  97. // Animate the Tick/Toc Indicator
  98. switch(Tic) {
  99. case '\\': Tic = '|'; break;
  100. case '|': Tic = '/'; break;
  101. case '/': Tic = '-'; break;
  102. default: Tic = '\\'; break;
  103. }
  104. // Format and output the screen update. At the end post a \r so that
  105. // the line appears to update in place.
  106. cout
  107. << "M/min: " << setw(4) << (int) MyRulebase.MyLOGmgr.MessagesPerMinute() << " "
  108. << "SP: " << setw(6) << setprecision(2) << setiosflags(ios::fixed) <<
  109. ((0 < MyRulebase.MyLOGmgr.MessagesPerMinute()) ?
  110. (100 * MyRulebase.MyLOGmgr.SpamPerMinute() / MyRulebase.MyLOGmgr.MessagesPerMinute()) : 0.0) << "% "
  111. << "LR:" << setw(7) << MyRulebase.MyLOGmgr.LatestRuleID()
  112. << " ["
  113. << MyRulebase.MyXCImgr.pollClientCount() << "/"
  114. << MyRulebase.MyXCImgr.pollLoopCount() << " "
  115. << Tic << " " << (int) MyRulebase.MyXCImgr.TotalQueue() << "] "
  116. << "W:" << (int) MyRulebase.MyLOGmgr.WhitePerMinute() << " "
  117. << "C:" << (int) MyRulebase.MyLOGmgr.CautionPerMinute() << " "
  118. << "B:" << (int) MyRulebase.MyLOGmgr.BlackPerMinute() << " "
  119. << "T:" << (int) MyRulebase.MyLOGmgr.TruncatePerMinute() << " "
  120. << "S:" << (int) MyRulebase.MyLOGmgr.SamplePerMinute()
  121. << " \r" << flush;
  122. if(DebugMode) ThreadStatusToCout(); // Debug? Show Thread Status Report.
  123. }
  124. cout << endl << endl << "Shutdown Received." << endl;
  125. // When this loop fails it is time to shut down.
  126. // All the rest happens via XCI now.
  127. cout << "Closing Rulebase Handler..." << endl;
  128. MyRulebase.close();
  129. // All done...
  130. cout << "Bye bye." << endl;
  131. return 0;
  132. }
  133. /*
  134. class DebugExceptionHandler { // Hand wrapper for exception handler.
  135. public:
  136. DebugExceptionHandler() {
  137. LoadLibrary("exchndl.dll");
  138. }
  139. };
  140. static DebugExceptionHandler TheDebugExceptionHandler; // Global exception handler.
  141. */
  142. int main(int argc, char* argv[]) {
  143. try {
  144. go(argc, argv);
  145. }
  146. catch(exception& e) {
  147. cout << "Unhandled Exception: " << e.what() << " Thrown!" << endl;
  148. }
  149. catch(...) {
  150. cout << "Unknown, Unhandled Exception Discovered!" << endl;
  151. }
  152. return 0;
  153. }