CommandManager Class Reference

#include <CommandManager.h>

List of all members.

Public Member Functions

void setServer (Server *myServer)
void processCommand (std::string &cmd)

Static Public Member Functions

CommandManagergetInstance ()
void freeInstance ()

Protected Member Functions

 CommandManager ()
virtual ~CommandManager ()

Private Attributes

SDL_mutex * mxServer
ServermyServer
CommandSchedulermyScheduler

Static Private Attributes

CommandManagerpUniqueInstance = NULL
SDL_mutex * mxInstance = SDL_CreateMutex()


Detailed Description

Manages server commands.

CommandManager takes care of creating ICommand objects that will be sent to the CommandScheduler for execution. In order to be fully functional, the CommandManager should have a pointer to a valid server. This class implements Singleton pattern and should be thread-safe.

Definition at line 21 of file CommandManager.h.


Constructor & Destructor Documentation

CommandManager::CommandManager  )  [protected]
 

Constructor.

Definition at line 76 of file CommandManager.cpp.

Referenced by getInstance().

00077         :mxServer(SDL_CreateMutex()),
00078         myScheduler(CommandScheduler::getInstance())
00079 {
00080 }

CommandManager::~CommandManager  )  [protected, virtual]
 

Virtual destructor.

Definition at line 82 of file CommandManager.cpp.

References mxServer.

00083 {
00084         SDL_DestroyMutex(mxServer);
00085 }


Member Function Documentation

void CommandManager::freeInstance  )  [static]
 

Deletes the class's instance.

Author:
stonedz
Since:
pre-alpha

Definition at line 24 of file CommandManager.cpp.

References mxInstance, and pUniqueInstance.

00024                                  {
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 }

CommandManager * CommandManager::getInstance  )  [static]
 

Returns class' instance.

Author:
stonedz
Since:
pre-alpha
Returns:
Class' instance.

Definition at line 15 of file CommandManager.cpp.

References CommandManager(), mxInstance, and pUniqueInstance.

00015                                            {
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 }

void CommandManager::processCommand std::string &  cmd  ) 
 

Processes a command received as a string.

It takes the whole command string, creates a new ICommand command and then sends it to the CommandScheduler.See the dedicated documentation for further informations.

Author:
stonedz
Since:
pre-alpha
Parameters:
cmd The whole command string (parameters included).

Definition at line 40 of file CommandManager.cpp.

References myScheduler, myServer, and CommandScheduler::queue().

Referenced by Console::startConsole().

00040                                                  {
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 }

void CommandManager::setServer Server myServer  ) 
 

Sets the Server the CommandManager refers to.

Author:
stonedz
Since:
pre-alpha
Parameters:
myServer A valid Server pointer.

Definition at line 33 of file CommandManager.cpp.

References mxServer, myScheduler, and CommandScheduler::setServer().

Referenced by Console::Console(), and Console::setServer().

00033                                               {
00034         SDL_LockMutex(mxServer);
00035         this->myServer = myServer;              // Sets the CommandManager Server.
00036         this->myScheduler->setServer(myServer);
00037         SDL_UnlockMutex(mxServer);
00038 }


Member Data Documentation

SDL_mutex * CommandManager::mxInstance = SDL_CreateMutex() [static, private]
 

Instance mutex, for thread safetyness.

Definition at line 13 of file CommandManager.cpp.

Referenced by freeInstance(), and getInstance().

SDL_mutex* CommandManager::mxServer [private]
 

Server's mutex.

Definition at line 77 of file CommandManager.h.

Referenced by setServer(), and ~CommandManager().

CommandScheduler* CommandManager::myScheduler [private]
 

Pointer of a CommandScheduler.

Definition at line 79 of file CommandManager.h.

Referenced by processCommand(), and setServer().

Server* CommandManager::myServer [private]
 

Points to a valid Server.

Definition at line 78 of file CommandManager.h.

Referenced by processCommand().

CommandManager * CommandManager::pUniqueInstance = NULL [static, private]
 

Reference to unique CommandManager instance.

Definition at line 12 of file CommandManager.cpp.

Referenced by freeInstance(), and getInstance().


The documentation for this class was generated from the following files:
Generated on Mon Dec 3 04:30:13 2007 for lo-testserver by  doxygen 1.3.9.1