#include <ConnectionHandler.h>
Diagramma delle classi per ConnectionHandler
Server
.
Questa classe e' un Thread
che hai il compito di gestire tutta l'interazione con l'utente che si connette:
Game
attualmente in esecuzione Game
disponibili
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 | |
Player * | player |
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 |
|
Costruttore della Classe ConnectionHandler.
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 } |
|
Distruttore ConnectionHandler.
Definizione alla linea 27 del file ConnectionHandler.cc.
00027 { exit(); } |
|
Chiusura del ConnectionHandler.
Questo metodo viene richiamato automaticamente al termine del metodo Definizione alla linea 125 del file ConnectionHandler.cc. Riferimenti player.
00125 { 00126 delete player; 00127 delete this; 00128 } |
|
Referenziato da run(). |
|
Referenziato da run(). |
|
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 } |
|
Stampa la lista dei Game disponibili su MGS, verso lo stream dell'utente.
Sfrutta le peculiarita' della variabile privata 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 } |
|
Attivazione del ConnectionHandler.
Questo metodo, overlodato rispetto alla Classe 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:
|
Vettore dei
Definizione alla linea 61 del file ConnectionHandler.h. Referenziato da printActiveGameList(). |
|
Vettore dei 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 (anche detto Semaforo) per le Sezioni Critiche
Definizione alla linea 10 del file ConnectionHandler.cc. Referenziato da printActiveGameList(). |
|
Puntatore al
Definizione alla linea 59 del file ConnectionHandler.h. |