git-svn-id: https://svn.microneil.com/svn/SNF4CGP/trunk@33 59e8e3e7-56fa-483b-b4b4-fa6ab0af3dfcmaster
public: | public: | ||||
enum CommandType { // Commands have these types: | enum CommandType { // Commands have these types: | ||||
NOTGOOD, // cin.good() has become false. | |||||
UNKNOWN, // We don't know this command. | UNKNOWN, // We don't know this command. | ||||
QUIT, // It is time to stop the app. | QUIT, // It is time to stop the app. | ||||
WAKE, // Report the app is now awake. | WAKE, // Report the app is now awake. |
dispatchWakeupCommand(); // First job is to announce ourselves. | dispatchWakeupCommand(); // First job is to announce ourselves. | ||||
for(;;) { // Then process all jobs but QUIT. | for(;;) { // Then process all jobs but QUIT. | ||||
Command C = Input.getCommand(); | Command C = Input.getCommand(); | ||||
if(Command::QUIT != C.Type) dispatchCommand(C); | |||||
else { | |||||
QuitJobNumber = C.Number; | |||||
cout << "* SNF4CGP[" << C.Number << "] Received QUIT, shutting down..." << endl; | |||||
cout.flush(); | |||||
return; | |||||
switch(C.Type) { | |||||
case Command::QUIT: { | |||||
QuitJobNumber = C.Number; | |||||
cout << "* SNF4CGP[" << C.Number << "] Received QUIT, shutting down..." << endl; | |||||
cout.flush(); | |||||
return; | |||||
} | |||||
case Command::NOTGOOD: { | |||||
cout << "* SNF4CGP Input stream has gone bad, shutting down..." << endl; | |||||
cout.flush(); | |||||
return; | |||||
} | |||||
default: { | |||||
dispatchCommand(C); | |||||
} | |||||
} | } | ||||
} | } | ||||
} | } | ||||
shutdownOutput(); | shutdownOutput(); | ||||
shutdownJobPool(); | shutdownJobPool(); | ||||
cout << "* SNF4CGP Shutdown completed." << endl; | cout << "* SNF4CGP Shutdown completed." << endl; | ||||
cout << QuitJobNumber << " OK" << endl; | |||||
if(0 < QuitJobNumber) { | |||||
cout << QuitJobNumber << " OK" << endl; | |||||
} | |||||
cout.flush(); | cout.flush(); | ||||
} | } |
} | } | ||||
Command InputProcessor::getCommand() { // How to get/parse a command | Command InputProcessor::getCommand() { // How to get/parse a command | ||||
Command NewCommand; | |||||
string InputLine; | string InputLine; | ||||
getline(cin, InputLine); | getline(cin, InputLine); | ||||
istringstream InputLineStream(InputLine); | istringstream InputLineStream(InputLine); | ||||
Command NewCommand; | |||||
parseCommandNumber(InputLineStream, NewCommand); // First thing is always a number. | |||||
parseCommandType(InputLineStream, NewCommand); // Next is always a command name. | |||||
parseCommandData(InputLineStream, NewCommand); // The rest is data for the command. | |||||
if(cin.good()) { | |||||
parseCommandNumber(InputLineStream, NewCommand); // First thing is always a number. | |||||
parseCommandType(InputLineStream, NewCommand); // Next is always a command name. | |||||
parseCommandData(InputLineStream, NewCommand); // The rest is data for the command. | |||||
if(Command::UNKNOWN == NewCommand.Type) NewCommand.Data = InputLine; // Capture unknown inputs for later. | |||||
if(Command::UNKNOWN == NewCommand.Type) NewCommand.Data = InputLine; // Capture unknown inputs for later. | |||||
} else { | |||||
NewCommand.Type = Command::NOTGOOD; | |||||
} | |||||
return NewCommand; | return NewCommand; | ||||
} | } |
using namespace std; // Introduce standard namespace. | using namespace std; // Introduce standard namespace. | ||||
const string SNF4CGP_VERSION_INFO = "SNF4CGP Version 0.0.1 Build: " __DATE__ " " __TIME__; | |||||
const string SNF4CGP_VERSION_INFO = "SNF4CGP Version 0.0.2 Build: " __DATE__ " " __TIME__; | |||||
const int ConfigPathArgNumber = 1; | const int ConfigPathArgNumber = 1; | ||||
const int CorrectArgcNumber = 2; | const int CorrectArgcNumber = 2; |