Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

CommandProcessor.cpp

Go to the documentation of this file.
00001 #include "headers.h"
00002 
00013 
00014 
00015 CommandProcessor::CommandProcessor() {
00016     setParser(NULL);
00017     m_commandsProcessed = 0;
00018 } // ctor
00019 
00020 
00024 void
00025 CommandProcessor::startThread()
00026 {
00027     pthread_attr_t at;
00028     pthread_attr_init(&at);
00029     pthread_create(&m_daemonThreadId, &at, CommandProcessor::daemonThread, this);
00030 } // fn startThread
00031 
00032 
00033 void 
00034 CommandProcessor::stopThread() 
00035 {
00036     void* statusp;
00037     pthread_cancel(m_daemonThreadId);
00038     pthread_join(m_daemonThreadId, &statusp);
00039 } // fn stopThread
00040 
00041 
00042 void*
00043 CommandProcessor::daemonThread(void* processorPtr) {
00044     int lastType, lastState;
00045     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &lastType);
00046     pthread_setcanceltype(PTHREAD_CANCEL_ENABLE, &lastState);
00047         CommandProcessor* processor = (CommandProcessor*)processorPtr;
00048         processor->processCommands();
00049         return NULL;
00050 } // fn daemonThread
00051 
00052 
00053 void 
00054 CommandProcessor::setParser(CommandParser* parser) {
00055     m_parser = parser;
00056 } // fn setParser
00057 
00058 
00062 void 
00063 CommandProcessor::addCommand(Command* command) {
00064     if (command != NULL) {
00065         m_commandList.push_back(command);
00066     }
00067 } // fn addCommand
00068 
00069 
00075 Command*
00076 CommandProcessor::matchCommand(string commandWord) {
00077     // iterate through command list
00078     // return the command if found
00079     vector<Command*>::iterator cmdIterator;
00080 
00081     for (cmdIterator = m_commandList.begin(); cmdIterator != m_commandList.end(); cmdIterator++) {
00082         Command* cmd = (Command*)(*cmdIterator);
00083         if (cmd->getCommandString() == commandWord) {
00084             cmd->clear();
00085             return cmd;
00086         }
00087     }
00088     return NULL;
00089 } // fn matchCommand
00090 
00091 
00095 void 
00096 CommandProcessor::init() {
00097     return;
00098 } // fn init
00099 
00100 
00104 void 
00105 CommandProcessor::processCommands() {
00106     string input;
00107     stringstream output;
00108     if (m_parser == NULL) {
00109         debug(DEBUG_CMD, "No parser defined for CommandProcessor!!!");
00110         return;
00111     }
00112     Command* command;
00113     init();
00114     while (1) {
00115         readCommand(&input);
00116         command = m_parser->parse(input);
00117         if (command != NULL) {
00118             command->run(output);
00119             m_commandsProcessed++;
00120         }
00121         else {
00122             generateError(&input, output);
00123         }
00124         displayResponse(&(output.str()));
00125         // clear the output stream
00126         output.str("");
00127     }
00128 } // fn processCommands
00129 
00130 
00131 vector<Command*> 
00132 CommandProcessor::getCommandList() {
00133     return m_commandList;
00134 } // fn getCommandList
00135 
00136 
00137 void
00138 CommandProcessor::generateError(string* input, std::ostream& output) {
00139     output << "\n\nERROR: Command not recognized\n\n  Input: " << *input << "\n\n";
00140 }
00141 
00142 
00143 void
00144 CommandProcessor::toStream(std::ostream& out) {
00145     out << "Commands Processed: " << m_commandsProcessed << "\n";
00146 }

Generated at Thu Jul 11 13:31:49 2002 for Peekabooty by doxygen1.2.9 written by Dimitri van Heesch, © 1997-2001