|
|
@@ -1,6 +1,6 @@ |
|
|
|
// SNFServer/main.cpp
|
|
|
|
//
|
|
|
|
// (C) Copyright 2006 - 2009 ARM Research Labs, LLC
|
|
|
|
// (C) Copyright 2006 - 2020 ARM Research Labs, LLC
|
|
|
|
// See www.armresearch.com for the copyright terms.
|
|
|
|
//
|
|
|
|
|
|
|
@@ -33,18 +33,19 @@ |
|
|
|
#include "../CodeDweller/base64codec.hpp"
|
|
|
|
|
|
|
|
//#include "../nvwa-0.6/nvwa/debug_new.h"
|
|
|
|
using namespace std; // Introduce standard namespace.
|
|
|
|
|
|
|
|
namespace cd = codedweller;
|
|
|
|
|
|
|
|
const char* SERVER_VERSION_INFO = "SNF Server Version 3.2.0 Build: " __DATE__ " " __TIME__;
|
|
|
|
|
|
|
|
static const string XCIShutdownResponse =
|
|
|
|
static const std::string XCIShutdownResponse =
|
|
|
|
"<snf><xci><server><response message=\'shutdown in progress\' code=\'0\'/></server></xci></snf>\n";
|
|
|
|
|
|
|
|
class XCIShutdownWatcher : public snfXCIServerCommandHandler { // Shutdown watcher.
|
|
|
|
public:
|
|
|
|
XCIShutdownWatcher():TimeToStop(false){} // Construct with shutdown flag false.
|
|
|
|
bool TimeToStop; // Here is the flag.
|
|
|
|
string processXCIRequest(snf_xci& X) { // Here is how we process requests.
|
|
|
|
std::string processXCIRequest(snf_xci& X) { // Here is how we process requests.
|
|
|
|
if(0 == X.xci_server_command.find("shutdown")) { // If we find shutdown then
|
|
|
|
TimeToStop = true; // set the shutdown flag
|
|
|
|
return XCIShutdownResponse; // and let them know we got it.
|
|
|
@@ -56,23 +57,23 @@ class XCIShutdownWatcher : public snfXCIServerCommandHandler { |
|
|
|
// Thread Status Analysis For Debugging.
|
|
|
|
|
|
|
|
void ThreadStatusToCout() { // Produce a thread status list.
|
|
|
|
ThreadStatusReport R = Threads.StatusReport(); // Get a report from Threads.
|
|
|
|
cout << endl; // Break the line.
|
|
|
|
cd::ThreadStatusReport R = cd::Threads.StatusReport(); // Get a report from Threads.
|
|
|
|
std::cout << std::endl; // Break the line.
|
|
|
|
for(
|
|
|
|
ThreadStatusReport::iterator iR = R.begin(); // Loop through the report.
|
|
|
|
cd::ThreadStatusReport::iterator iR = R.begin(); // Loop through the report.
|
|
|
|
iR != R.end(); iR++
|
|
|
|
) {
|
|
|
|
ThreadStatusRecord& S = (*iR); // Take each status report and
|
|
|
|
cout // send it to cout on it's own line.
|
|
|
|
cd::ThreadStatusRecord& S = (*iR); // Take each status report and
|
|
|
|
std::cout // send it to cout on it's own line.
|
|
|
|
<< S.getName() << " (" << S.getPointer() << "), "
|
|
|
|
<< S.getType().Name << ", "
|
|
|
|
<< S.getState().Name << ", "
|
|
|
|
<< ((S.getRunning()) ? "Running, " : "Not Running, ")
|
|
|
|
<< ((S.getBad()) ? "Broken, " : "Ok, ")
|
|
|
|
<< S.getFault()
|
|
|
|
<< endl;
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
cout << endl; // Leave a blank line at the end.
|
|
|
|
std::cout << std::endl; // Leave a blank line at the end.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here in the main thread is where we get executive tasks done.
|
|
|
@@ -82,42 +83,42 @@ int go(int argc, char* argv[]) { //// go() stands in for main(). main() catches |
|
|
|
// Check for debug mode.
|
|
|
|
|
|
|
|
bool DebugMode = false; // This will be our debug mode.
|
|
|
|
string argv0(argv[0]); // Capture how we were called.
|
|
|
|
std::string argv0(argv[0]); // Capture how we were called.
|
|
|
|
if(
|
|
|
|
string::npos != argv0.find("Debug") || // If we find "Debug" or
|
|
|
|
string::npos != argv0.find("debug") // "debug" in our command path
|
|
|
|
std::string::npos != argv0.find("Debug") || // If we find "Debug" or
|
|
|
|
std::string::npos != argv0.find("debug") // "debug" in our command path
|
|
|
|
) { // then we are in DebugMode.
|
|
|
|
DebugMode = true; // Set the flag and tell the
|
|
|
|
cout << "Debug Mode" << endl; // watchers.
|
|
|
|
std::cout << "Debug Mode" << std::endl; // watchers.
|
|
|
|
}
|
|
|
|
|
|
|
|
// DebugMode = true; // Force it when needed.
|
|
|
|
|
|
|
|
// Announce Version / Build Info.
|
|
|
|
|
|
|
|
cout << SERVER_VERSION_INFO << endl; // Shout out our version.
|
|
|
|
cout << SNF_ENGINE_VERSION << endl; // Shout out the engine version.
|
|
|
|
std::cout << SERVER_VERSION_INFO << std::endl; // Shout out our version.
|
|
|
|
std::cout << SNF_ENGINE_VERSION << std::endl; // Shout out the engine version.
|
|
|
|
|
|
|
|
// Sanity checks before we get going.
|
|
|
|
|
|
|
|
if(2 != argc) { // Check the command line args.
|
|
|
|
cout << "Use: SNFServer <path-to-config-file>" << endl; // If wrong, say how we work.
|
|
|
|
std::cout << "Use: SNFServer <path-to-config-file>" << std::endl; // If wrong, say how we work.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(0 != access(argv[1], R_OK)) { // Check the config file path.
|
|
|
|
cout << "Can't read " << argv[1] << endl; // If it's not accessible, punt.
|
|
|
|
std::cout << "Can't read " << argv[1] << std::endl; // If it's not accessible, punt.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
cout << "Launching with " << argv[1] << endl; // Tell them we're going.
|
|
|
|
std::cout << "Launching with " << argv[1] << std::endl; // Tell them we're going.
|
|
|
|
snf_RulebaseHandler MyRulebase; // Create a rulebase manager.
|
|
|
|
MyRulebase.PlatformVersion(SERVER_VERSION_INFO); // Set the Platform version string.
|
|
|
|
XCIShutdownWatcher ShutdownWatcher; // Make a server shutdown processor
|
|
|
|
MyRulebase.XCIServerCommandHandler(ShutdownWatcher); // and register it with the engine.
|
|
|
|
MyRulebase.open(argv[1], "", ""); // Open a configured rulebase.
|
|
|
|
|
|
|
|
Sleeper WaitATic(1000); // Learn to wait a second.
|
|
|
|
cd::Sleeper WaitATic(1000); // Learn to wait a second.
|
|
|
|
|
|
|
|
cout << "Running." << endl << endl; // Tell them we're running.
|
|
|
|
std::cout << "Running." << std::endl << std::endl; // Tell them we're running.
|
|
|
|
char Tic = '\\'; // Tic/Toc indicator.
|
|
|
|
while(false == ShutdownWatcher.TimeToStop) { // While running, update the screen.
|
|
|
|
WaitATic(); // One second between updates.
|
|
|
@@ -134,12 +135,12 @@ int go(int argc, char* argv[]) { //// go() stands in for main(). main() catches |
|
|
|
// Format and output the screen update. At the end post a \r so that
|
|
|
|
// the line appears to update in place.
|
|
|
|
|
|
|
|
cout
|
|
|
|
<< "M/min: " << setw(4) << (int) MyRulebase.MyLOGmgr.MessagesPerMinute() << " "
|
|
|
|
<< "SP: " << setw(6) << setprecision(2) << setiosflags(ios::fixed) <<
|
|
|
|
std::cout
|
|
|
|
<< "M/min: " << std::setw(4) << (int) MyRulebase.MyLOGmgr.MessagesPerMinute() << " "
|
|
|
|
<< "SP: " << std::setw(6) << std::setprecision(2) << std::setiosflags(std::ios::fixed) <<
|
|
|
|
((0 < MyRulebase.MyLOGmgr.MessagesPerMinute()) ?
|
|
|
|
(100 * MyRulebase.MyLOGmgr.SpamPerMinute() / MyRulebase.MyLOGmgr.MessagesPerMinute()) : 0.0) << "% "
|
|
|
|
<< "LR:" << setw(7) << MyRulebase.MyLOGmgr.LatestRuleID()
|
|
|
|
<< "LR:" << std::setw(7) << MyRulebase.MyLOGmgr.LatestRuleID()
|
|
|
|
<< " ["
|
|
|
|
<< MyRulebase.MyXCImgr.pollClientCount() << "/"
|
|
|
|
<< MyRulebase.MyXCImgr.pollLoopCount() << " "
|
|
|
@@ -149,23 +150,23 @@ int go(int argc, char* argv[]) { //// go() stands in for main(). main() catches |
|
|
|
<< "B:" << (int) MyRulebase.MyLOGmgr.BlackPerMinute() << " "
|
|
|
|
<< "T:" << (int) MyRulebase.MyLOGmgr.TruncatePerMinute() << " "
|
|
|
|
<< "S:" << (int) MyRulebase.MyLOGmgr.SamplePerMinute()
|
|
|
|
<< " \r" << flush;
|
|
|
|
<< " \r" << std::flush;
|
|
|
|
|
|
|
|
if(DebugMode) ThreadStatusToCout(); // Debug? Show Thread Status Report.
|
|
|
|
|
|
|
|
}
|
|
|
|
cout << endl << endl << "Shutdown Received." << endl;
|
|
|
|
std::cout << std::endl << std::endl << "Shutdown Received." << std::endl;
|
|
|
|
|
|
|
|
// When this loop fails it is time to shut down.
|
|
|
|
// All the rest happens via XCI now.
|
|
|
|
|
|
|
|
cout << "Closing Rulebase Handler..." << endl;
|
|
|
|
std::cout << "Closing Rulebase Handler..." << std::endl;
|
|
|
|
|
|
|
|
MyRulebase.close();
|
|
|
|
|
|
|
|
// All done...
|
|
|
|
|
|
|
|
cout << "Bye bye." << endl;
|
|
|
|
std::cout << "Bye bye." << std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
@@ -187,11 +188,11 @@ int main(int argc, char* argv[]) { |
|
|
|
try {
|
|
|
|
go(argc, argv);
|
|
|
|
}
|
|
|
|
catch(exception& e) {
|
|
|
|
cout << "Unhandled Exception: " << e.what() << " Thrown!" << endl;
|
|
|
|
catch(const std::exception& e) {
|
|
|
|
std::cout << "Unhandled Exception: " << e.what() << " Thrown!" << std::endl;
|
|
|
|
}
|
|
|
|
catch(...) {
|
|
|
|
cout << "Unknown, Unhandled Exception Discovered!" << endl;
|
|
|
|
std::cout << "Unknown, Unhandled Exception Discovered!" << std::endl;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|