Riferimenti per la classe ConnectionHandler

#include <ConnectionHandler.h>

Diagramma delle classi per ConnectionHandler

Inheritance graph
[legenda]
Diagramma di collaborazione per ConnectionHandler:

Collaboration graph
[legenda]
Lista di tutti i membri.

Descrizione Dettagliata

Handler delle Connessioni istanziato da Server.

Questa classe e' un Thread che hai il compito di gestire tutta l'interazione con l'utente che si connette:

Questa Classe eredita da TCPSession, che a sua volta eredita da Thread e da Socket: cio' rende questa Classe un "flusso di controllo a se stante", che opera, per ovvie esigenze di concorrenza, in maniera asincrona rispetto al Server principale.

Definizione alla linea 45 del file ConnectionHandler.h.

Membri pubblici

 ConnectionHandler (TCPSocket &, vector< Game * > &, vector< pair< string, TFactory< Game * > * > * > &)
 Costruttore della Classe ConnectionHandler.

virtual ~ConnectionHandler (void)
 Distruttore ConnectionHandler.


Membri privati

void run (void)
 Attivazione del ConnectionHandler.

void final (void)
 Chiusura del ConnectionHandler.

void printActiveGameList (void)
 Stampa la lista dei Game attivi verso lo stream dell'utente connesso.

bool joinPlayerToActiveGame (Player *, const char)
void printAvailableGameList (void)
 Stampa la lista dei Game disponibili su MGS, verso lo stream dell'utente.

bool joinPlayerToAvailableGame (Player *, const char)

Attributi privati

Playerplayer
 Puntatore al Player gestito da questo ConnectionHandler.

vector< Game * > & activeGames
 Vettore dei Game in esecuzione su MGS.

vector< pair< string, TFactory<
Game * > * > * > & 
availableGames
 Vettore dei Game disponibili su MGS.


Attributi privati statici

Mutex mutex
 Mutex (anche detto Semaforo) per le Sezioni Critiche


Documentazione dei costruttori e dei distruttori

ConnectionHandler::ConnectionHandler TCPSocket server,
vector< Game * > &  activeG,
vector< pair< string, TFactory< Game * > * > * > &  availableG
 

Costruttore della Classe ConnectionHandler.

Parametri:
server Server (TCPSocket</code) da cui prende la Connessione da Gestire
activeG Vettore dei Giochi in esecuzione su MGS

Definizione alla linea 18 del file ConnectionHandler.cc.

00020                                                              : 
00021 TCPSession(server), activeGames(activeG), availableGames(availableG) {
00022    cout << "<ConnHand msg> Creating New ConnectionHandler." << endl;
00023    cout << "<ConnHand msg> " << getIPV4Local() << " is now Handled." << endl;
00024 }

ConnectionHandler::~ConnectionHandler void   )  [virtual]
 

Distruttore ConnectionHandler.

Definizione alla linea 27 del file ConnectionHandler.cc.

00027 { exit(); }


Documentazione delle funzioni membro

void ConnectionHandler::final void   )  [private]
 

Chiusura del ConnectionHandler.

Questo metodo viene richiamato automaticamente al termine del metodo run(). Permette di gestire al meglio la chiusura del Thread. Ha il compito di deallocare il Player creato.

Definizione alla linea 125 del file ConnectionHandler.cc.

Riferimenti player.

00125                                   {
00126    delete player;
00127    delete this;
00128 }

bool ConnectionHandler::joinPlayerToActiveGame Player ,
const   char
[private]
 

Referenziato da run().

bool ConnectionHandler::joinPlayerToAvailableGame Player ,
const   char
[private]
 

Referenziato da run().

void ConnectionHandler::printActiveGameList void   )  [private]
 

Stampa la lista dei Game attivi verso lo stream dell'utente connesso.

Definizione alla linea 131 del file ConnectionHandler.cc.

Riferimenti activeGames, e mutex.

Referenziato da run().

00131                                                 {
00132    int activeGamesNumber;
00133    mutex.enterMutex();
00134    
00135    activeGamesNumber = activeGames.size();
00136    *tcp() << "\n------------------------" << endl;
00137    *tcp() << "--> Active Game List <--" << endl;
00138    *tcp() << "------------------------\n" << endl;
00139    *tcp() << "Active Games: " << activeGamesNumber << endl;
00140    *tcp() << "(<num>)\t- [<game name>]" << endl;
00141    
00142    for ( int i = 0; i < activeGamesNumber; i++ ) {
00143       *tcp() << "( " << (i+1) 
00144          << " )\t- [" << activeGames[i]->getName() << "]" << endl;
00145       activeGames[i]->printPlayersList(tcp());
00146    }
00147 
00148    mutex.leaveMutex();
00149 }

void ConnectionHandler::printAvailableGameList void   )  [private]
 

Stampa la lista dei Game disponibili su MGS, verso lo stream dell'utente.

Sfrutta le peculiarita' della variabile privata availableGames.

Definizione alla linea 179 del file ConnectionHandler.cc.

Riferimenti availableGames.

Referenziato da run().

00179                                                    {
00180    *tcp() << "\n---------------------------" << endl;
00181    *tcp() << "--> Available Game List <--" << endl;
00182    *tcp() << "---------------------------" << endl;
00183    *tcp() << "(<num>)\t- [<game name>]" << endl;
00184    for ( int i = 0; i < availableGames.size(); i++ ) {
00185          *tcp() << "( " << (i+1) << " )\t- [" <<
00186             availableGames[i]->first << "]" << endl;
00187    }
00188 }

void ConnectionHandler::run void   )  [private]
 

Attivazione del ConnectionHandler.

Questo metodo, overlodato rispetto alla Classe Thread, e' il corpo principale del flusso di controllo di questa Classe. Qui' avviene tutta l'interazione con l'utente (Player).

Definizione alla linea 34 del file ConnectionHandler.cc.

Riferimenti Player::getName(), joinPlayerToActiveGame(), joinPlayerToAvailableGame(), NAME_MAX_LENGTH, player, printActiveGameList(), printAvailableGameList(), e Player::suspendConnHandler().

00034                                 {
00035    char *buffer;
00036    buffer = new char[NAME_MAX_LENGTH];
00037    char selection;
00038    char gameSelection;
00039    
00040    InetAddress address = getIPV4Local();
00041    /* "tcp()" e' ereditato dalla classe TCPStream;
00042     * e' importante perche' attraverso il puntatore che ritorna
00043     * avviene tutta la comunicazione con l'utente/giocatore */
00044    *tcp() << ">>> MGS - Multithreaded Game Server <<<" << endl;
00045    *tcp() << "<ConnHand msg> Welcome to MGS on " 
00046       << address.getHostname() << " from socket " 
00047       << so << endl << endl;
00048    
00049    *tcp() << "<ConnHand msg> Please, Insert your nick (only one word): ";
00050    TCPStream::sync();
00051 
00052    if( isPending(Socket::pendingInput, 30000) ) {
00053       *tcp() >> buffer;
00054       /* Creazione giocatore */
00055       player = new Player (buffer , *this, this);
00056    
00057       do {
00058          /* Stampa del Menu per l'utente */
00059          *tcp() << "\n\n\n\n\n";
00060          *tcp() << "--- --- --- --- ---" << endl;
00061          *tcp() << "(n)\t- Create New Game" << endl;
00062          *tcp() << "(j)\t- Join Active Game" << endl;
00063          *tcp() << "(q)\t- Quit from MGS" << endl;
00064          *tcp() << "--- --- --- --- ---" << endl;
00065          *tcp() << "--- "<< player->getName() << 
00066             ", please make your selection (case sensitive): ";
00067          TCPStream::sync();
00068          *tcp() >> selection;
00069          /* Elimina dallo stream il carattere "\n" */
00070          tcp()->ignore(256,'\n');
00071       
00072          switch (selection) {
00073             case 'n':
00074                /* Menu Creazione Nuovo Game */
00075                *tcp() << "\n\n\n\n\n";
00076                printAvailableGameList();
00077                *tcp() << "--- "<< player->getName() << 
00078                   ", please make your selection: ";
00079                TCPStream::sync();
00080                *tcp() >> gameSelection;
00081                tcp()->ignore(256,'\n');
00082                if ( joinPlayerToAvailableGame(player, gameSelection) )
00083                   player->suspendConnHandler();
00084                else
00085                   *tcp() << "--- ERROR: Game not found!" << endl;
00086                break;
00087             case 'j':
00088                /* Menu Join Game attivo */
00089                *tcp() << "\n\n\n\n\n";
00090                printActiveGameList();
00091                *tcp() << "--- "<< player->getName() << 
00092                   ", please make your selection: ";
00093                TCPStream::sync();
00094                *tcp() >> gameSelection;
00095                tcp()->ignore(256,'\n');
00096                if ( joinPlayerToActiveGame(player, gameSelection) )
00097                   player->suspendConnHandler();
00098                else
00099                   *tcp() << "--- ERROR: Game not found!" << endl;
00100                break;
00101             case 'q':
00102                /* Quit */
00103                break;
00104             default:
00105                /* Errore di selezione */
00106                *tcp() << "\n\n\n\n\n";
00107                *tcp() << "Input error!" << endl;
00108                break;
00109          }
00110       }while ( selection != 'q' );
00111    }
00112    else {
00113       *tcp() << "\n<ConnHand msg> 30 Seconds without type your name." << endl;
00114       *tcp() << "<ConnHand msg> Connection TimeOut!" << endl;
00115    }
00116    *tcp() << "<ConnHand msg> Bye bye, from MGS" << endl;
00117 }

Questo è il grafo delle chiamate per questa funzione:


Documentazione dei dati membri

vector<Game *>& ConnectionHandler::activeGames [private]
 

Vettore dei Game in esecuzione su MGS.

Definizione alla linea 61 del file ConnectionHandler.h.

Referenziato da printActiveGameList().

vector< pair< string, TFactory<Game*> *> *>& ConnectionHandler::availableGames [private]
 

Vettore dei Game disponibili su MGS.

Ha la peculitarita' di permettere un accesso istantaneo ai Name dei Game e alle relative Factory, ottimizzando cosi' le prestazioni.

Definizione alla linea 67 del file ConnectionHandler.h.

Referenziato da printAvailableGameList().

Mutex ConnectionHandler::mutex [static, private]
 

Mutex (anche detto Semaforo) per le Sezioni Critiche

Definizione alla linea 10 del file ConnectionHandler.cc.

Referenziato da printActiveGameList().

Player* ConnectionHandler::player [private]
 

Puntatore al Player gestito da questo ConnectionHandler.

Definizione alla linea 59 del file ConnectionHandler.h.

Referenziato da final(), e run().


La documentazione per questa classe è stata generata a partire dai seguenti file:
Generato il Sun Nov 28 13:27:27 2004 per MGS - Multithreaded Game Server da doxygen 1.3.4