src/CommandManager.cpp

00001 #include <string>
00002 #include "CommandManager.h"
00003 #include "Logger.h"
00004 #include "cppsqlite3.h"
00005 
00006 #include "CommandScheduler.h"
00007 #include "commands/Cmd_test.h"
00008 #include "commands/Cmd_exit.h"
00009 #include "commands/Cmd_chat.h"
00010 #include "commands/Cmd_accounts.h"
00011 
00012 CommandManager* CommandManager::pUniqueInstance = NULL;         // Reference to the unique instance.
00013 SDL_mutex* CommandManager::mxInstance = SDL_CreateMutex();      // Reference to instance mutex.
00014 
00015 CommandManager* CommandManager::getInstance(){
00016         SDL_LockMutex(mxInstance);      // Locks the mutex...
00017         if (pUniqueInstance == NULL){ // No instance, create one.
00018         pUniqueInstance = new CommandManager();
00019     }
00020     SDL_UnlockMutex(mxInstance);        // Unlocks the mutex...
00021     return pUniqueInstance;
00022 }
00023 
00024 void CommandManager::freeInstance(){
00025         SDL_LockMutex(mxInstance);      // Locks the mutex...
00026         if (pUniqueInstance != NULL) {
00027         delete pUniqueInstance;
00028         pUniqueInstance = NULL;
00029     }
00030         SDL_UnlockMutex(mxInstance);    // Unlocks the mutex...
00031 }
00032 
00033 void CommandManager::setServer(Server* myServer){
00034         SDL_LockMutex(mxServer);
00035         this->myServer = myServer;              // Sets the CommandManager Server.
00036         this->myScheduler->setServer(myServer);
00037         SDL_UnlockMutex(mxServer);
00038 }
00039 
00040 void CommandManager::processCommand(std::string &cmd){
00041         std::string tmp;
00042         Uint16 epos;
00043         ICommand* cmdObj = NULL;    // This will be deleted by CommandScheduler.
00044 
00045         // Finds the command
00046         tmp = cmd;
00047         epos = tmp.find(" ");
00048         tmp.assign(tmp,0,epos);
00049 
00050         // Commands switch. This could be delegated to a parser class.
00051         // Please keep alphabetical order.
00052         if (tmp.size() == 0) // Empty message or white space at the beginning.
00053                 std::cout << "Please enter a command or clear white spaces before your command." << std::endl;
00054     else if ( tmp == "accounts" ){ // Account manager.
00055         cmdObj = new Cmd_accounts(cmd, myServer);
00056     }
00057     else if ( tmp == "chat" ){ // Basic chat interface for the console.
00058         cmdObj = new Cmd_chat(cmd, myServer);
00059     }
00060     else if ( tmp == "exit" || tmp == "quit" ){ // Quit command sent.
00061         cmdObj = new Cmd_exit(cmd, myServer);
00062     }
00063         else if (tmp == "test"){
00064                 cmdObj = new Cmd_test(cmd, myServer);
00065         }
00066         else{ // Invalid or non-existent command.
00067                 cmdObj = NULL;
00068                 std::cout << "Command not found." << std::endl;
00069         }
00070 
00071         if(cmdObj != NULL){
00072                 this->myScheduler->queue(cmdObj); // Queue the command.
00073         }
00074 }
00075 
00076 CommandManager::CommandManager()
00077         :mxServer(SDL_CreateMutex()),
00078         myScheduler(CommandScheduler::getInstance())
00079 {
00080 }
00081 
00082 CommandManager::~CommandManager()
00083 {
00084         SDL_DestroyMutex(mxServer);
00085 }

Generated on Mon Dec 3 04:30:12 2007 for lo-testserver by  doxygen 1.3.9.1