#include <TraitementClient.h>
Inheritance diagram for TraitementClient:
Public Member Functions | |
TraitementClient (Socket *s) | |
Constructeur de la classe. | |
~TraitementClient () | |
void * | Traitement () |
Méthode implémentant le traitement d'un client On attend l'initialisation du protocole et l'authentification du client. | |
Private Member Functions | |
bool | WaitForHello () |
Méthode attendant l'initialisation de la part du client. | |
bool | Authentification () |
Authentification du client (utilisation du singleton BdD). | |
bool | EnvoyerListePartie () |
Envoyer la liste des partie en cours (utilisation du singleton Partie). | |
bool | EnvoyerListeScore () |
Envoyer la liste des scores des utilisateurs enregistrés (utilisation du singleton BdD). | |
bool | CreerOuRejoindrePartie () |
Permet de créer ou de rejoindre une partie. | |
Private Attributes | |
Socket * | mSocket |
Pointeur sur la socket. | |
BdD * | mBdD |
Pointeur sur le singleton gérant les utilisateurs. | |
TraitementComServeurJeu * | mComSJ |
Pointeur sur le thread de communication UDP avec le serveur de jeu. | |
LanceurSJ * | mLSJ |
Pointeur sur le thread démarrant un serveur de jeu. | |
Partie * | mPartie |
Pointeur sur le singleton gérant les parties en cours. |
Definition at line 41 of file TraitementClient.h.
TraitementClient::TraitementClient | ( | Socket * | s | ) |
Constructeur de la classe.
s | Pointeur sur une socket (le résultat d'un Accept()) |
Definition at line 23 of file TraitementClient.cpp.
References Singleton< T >::GetInstance(), mBdD, mPartie, and mSocket.
00023 : Thread(){ 00024 mBdD=BdD::GetInstance(); 00025 mPartie=Partie::GetInstance(); 00026 mSocket=s; 00027 cout<<" SP TraitementClient::Création d'un thread pour traiter le client"<<endl; 00028 }
Here is the call graph for this function:
TraitementClient::~TraitementClient | ( | ) |
void * TraitementClient::Traitement | ( | ) | [virtual] |
Méthode implémentant le traitement d'un client
On attend l'initialisation du protocole et l'authentification du client.
On lui donne ensuite à sa guise la liste des parties en cours ou les highscores.
Il doit ensuite créer ou rejoindre une partie.
Implements Thread.
Definition at line 36 of file TraitementClient.cpp.
References Authentification(), CreerOuRejoindrePartie(), Socket::Ecouter(), Socket::Envoyer(), EnvoyerListePartie(), EnvoyerListeScore(), mSocket, and WaitForHello().
00036 { 00037 //On attend l'initialisation du protocole 00038 if(!WaitForHello()) return NULL; 00039 //Puis l'indentification 00040 if(!Authentification()) return NULL; 00041 //On envoie ensuite la liste des parties en cours ou des scores tant que nécessaire (actualisation du client) 00042 while(1){ 00043 try{ 00044 string cmd=mSocket->Ecouter(); 00045 if(cmd=="LIST") { 00046 if(!EnvoyerListePartie()) return NULL; 00047 } else if(cmd=="SCORE"){ 00048 if(!EnvoyerListeScore()) return NULL; 00049 } else if(cmd=="SUITE") { 00050 mSocket->Envoyer(cmd); 00051 if(!CreerOuRejoindrePartie()) return NULL; 00052 break; 00053 } else mSocket->Envoyer("ERROR LIST ou SCORE attendu"); 00054 } catch(SocketException s){ 00055 cout<<" SP TraitementClient::"; printf( "\e[31m[Erreur] \e[m" ); cout<<"Déconnexion du client: le thread se fait harakiri!"<<endl; 00056 return NULL; 00057 } 00058 } 00059 cout<<" SP TraitementClient::Fin du traitement du client"<<endl; 00060 return NULL; 00061 }
Here is the call graph for this function:
bool TraitementClient::WaitForHello | ( | ) | [private] |
Méthode attendant l'initialisation de la part du client.
Definition at line 63 of file TraitementClient.cpp.
References Socket::Ecouter(), Socket::Envoyer(), and mSocket.
Referenced by Traitement().
00063 { 00064 cout<<" SP TraitementClient::Attente de l'initiation du protocole de la part du client"; 00065 try{ 00066 while(mSocket->Ecouter()!="HELLO"){ 00067 mSocket->Envoyer("ERROR HELLO"); 00068 } 00069 mSocket->Envoyer("HELLO"); 00070 printf( "\e[32m [OK]\e[m\n" ); 00071 //cout<<" SP Protocole initialisé"<<endl; 00072 } catch(SocketException s){ 00073 cout<<" SP TraitementClient::"; printf( "\e[31m[Erreur] \e[m" ); cout<<"Déconnexion du client: le thread se fait harakiri!"<<endl; 00074 return false; 00075 } 00076 return true; 00077 }
Here is the call graph for this function:
bool TraitementClient::Authentification | ( | ) | [private] |
Authentification du client (utilisation du singleton BdD).
Definition at line 79 of file TraitementClient.cpp.
References BdD::AjouterUtilisateur(), Socket::Ecouter(), Socket::Envoyer(), BdD::EstUtilisateur(), mBdD, mSocket, and BdD::Sauvegarder().
Referenced by Traitement().
00079 { 00080 string line, cmd, login, mdp; 00081 do{ 00082 try{ 00083 line=mSocket->Ecouter(); 00084 istringstream iss(line); 00085 getline(iss,cmd,' '); 00086 if(cmd=="LOGIN"){ 00087 getline(iss,login,' '); getline(iss,mdp,' '); //<=> Tokens sur ' ' 00088 cout<<" SP TraitementClient::Tentative de connexion de "<<login<<" avec le mot de passe'"<<mdp<<"'"; 00089 if(mBdD->EstUtilisateur(login,mdp)){ 00090 //cout<<"SP Utilisateur connu"<<endl, 00091 printf( "\e[32m [OK]\e[m\n" ); 00092 mSocket->Envoyer("LOGIN OK"); 00093 break; 00094 } else { 00095 printf( "\e[31m [Echec]\e[m\n" ); 00096 //cout<<"SP Utilisateur inconnu"<<endl, 00097 mSocket->Envoyer("ERROR LOGIN Utilisateur non enregistré ou mot de passe manquant"); 00098 } 00099 } else if(cmd=="REG"){ 00100 getline(iss,login,' '); getline(iss,mdp,' '); 00101 cout<<" SP TraitementClient::Tentative de création du compte "<<login<<" avec le mot de passe'"<<mdp<<"'"; 00102 if((login=="")||(mdp=="")) { 00103 mSocket->Envoyer("ERROR REG manque login ou mot de passe"); 00104 printf( "\e[31m [Echec]\e[m\n" ); 00105 } else { 00106 if(mBdD->AjouterUtilisateur(login,mdp)==-1){ 00107 mSocket->Envoyer("ERROR REG login déjà utilisé") ; 00108 printf( "\e[31m [Echec]\e[m\n" ); 00109 } else { 00110 //cout<<"SP Utilisateur ajouté"<<endl, 00111 printf( "\e[32m [OK]\e[m\n" ); 00112 mSocket->Envoyer("REG OK"); 00113 mBdD->Sauvegarder(); 00114 break; 00115 } 00116 } 00117 } else mSocket->Envoyer("ERROR LOGIN ou REG attendu"); 00118 } catch(SocketException s){ 00119 cout<<" SP TraitementClient::"; printf( "\e[31m[Erreur] \e[m" ); cout<<"Déconnexion du client: le thread se fait harakiri!"<<endl; 00120 return false; 00121 } 00122 } while(1); 00123 return true; 00124 }
Here is the call graph for this function:
bool TraitementClient::EnvoyerListePartie | ( | ) | [private] |
Envoyer la liste des partie en cours (utilisation du singleton Partie).
Definition at line 126 of file TraitementClient.cpp.
References Socket::Envoyer(), mPartie, mSocket, and Partie::ToString().
Referenced by Traitement().
00126 { 00127 cout<<" SP TraitementClient::Envoi de la liste des parties en cours"; 00128 string cmd="LIST "; 00129 cmd+=mPartie->ToString(); 00130 try{ 00131 mSocket->Envoyer(cmd); 00132 } catch(SocketException s){ 00133 cout<<" SP TraitementClient::"; printf( "\e[31m[Erreur] \e[m" ); cout<<"Déconnexion du client: le thread se fait harakiri!"<<endl; 00134 return false; 00135 } 00136 printf( "\e[32m [OK]\e[m\n" ); 00137 return true; 00138 }
Here is the call graph for this function:
bool TraitementClient::EnvoyerListeScore | ( | ) | [private] |
Envoyer la liste des scores des utilisateurs enregistrés (utilisation du singleton BdD).
Definition at line 140 of file TraitementClient.cpp.
References Socket::Envoyer(), BdD::ListeScore(), mBdD, and mSocket.
Referenced by Traitement().
00140 { 00141 cout<<" SP TraitementClient::Envoi de la liste des scores en cours"; 00142 string cmd="SCORE "; 00143 cmd+=mBdD->ListeScore(); 00144 try{ 00145 mSocket->Envoyer(cmd); 00146 } catch(SocketException s){ 00147 cout<<" SP TraitementClient::"; printf( "\e[31m[Erreur] \e[m" ); cout<<"Déconnexion du client: le thread se fait harakiri!"<<endl; 00148 return false; 00149 } 00150 printf( "\e[32m [OK]\e[m\n" ); 00151 return true; 00152 }
Here is the call graph for this function:
bool TraitementClient::CreerOuRejoindrePartie | ( | ) | [private] |
Permet de créer ou de rejoindre une partie.
Pour rejoindre une partie, on donne simplement le port
Dans le cas d'une création, on créé 2 threads, l'un pour lancer le serveur (LanceurSJ) et l'autre pour écouter le serveur de jeu en UDP (TraitementComServeurJeu)
Definition at line 154 of file TraitementClient.cpp.
References Thread::Demarrer(), Socket::Ecouter(), Socket::Envoyer(), TraitementComServeurJeu::EstPret(), mComSJ, mLSJ, mPartie, mSocket, and Partie::Rechercher().
Referenced by Traitement().
00154 { 00155 static unsigned int port=20045; 00156 do{ 00157 try{ 00158 string line=mSocket->Ecouter(), cmd, nbFantomeAuto; 00159 istringstream iss(line); 00160 getline(iss,cmd,' '); 00161 if(cmd=="GAME"){ 00162 getline(iss,nbFantomeAuto,' '); 00163 istringstream iss(nbFantomeAuto); 00164 unsigned int nbFantome; 00165 iss>>nbFantome; 00166 if(nbFantomeAuto=="") mSocket->Envoyer("ERROR GAME nombre de fantomes automatique attendu"); 00167 else { 00168 if((nbFantome>=0)&&(nbFantome<=3)){ 00169 port++; 00170 //On lance un thread de communication udp entre SP et le SJ que l'on va créer... 00171 mComSJ= new TraitementComServeurJeu(port); 00172 mComSJ->Demarrer(); 00173 //On créer la ligne de commande à exécuter pour lancer le serveur de jeu 00174 00175 string cmd="../../../serveurjeu/debug/src/serveurjeu "; 00176 ostringstream oss; 00177 oss<<port; 00178 string p=oss.str(); 00179 cmd+=p; cmd+=" "; cmd+=nbFantomeAuto; cmd+="&"; 00180 //On démarre le serveur de jeu 00181 cout<<" SP TraitementClient::Démarrage du serveur de jeu"<<endl; 00182 mLSJ =new LanceurSJ(cmd); 00183 mLSJ->Demarrer(); 00184 //Attendre com du sj pour envoyer confirmation 00185 while(!(mComSJ->EstPret())){ 00186 sleep(1); 00187 } 00188 cout<<" SP TraitementClient::On donne l'adresse du serveur de jeu au client"<<endl; 00189 string reponse="GAME "; reponse+=p; 00190 mSocket->Envoyer(reponse); 00191 break; 00192 } else mSocket->Envoyer("ERROR GAME le nombre de fantome automatique doit être compris entre 0 et 3"); 00193 } 00194 } else if(cmd=="JOIN"){ 00195 string port; 00196 getline(iss,port,' '); 00197 if(port=="") mSocket->Envoyer("ERROR JOIN le port de la partie a rejoindre est attendu"); 00198 else{ 00199 istringstream iss(port); 00200 unsigned int p; 00201 iss>>p; 00202 if((mPartie->Rechercher(p))==-1) mSocket->Envoyer("ERROR JOIN le port ne correspond a aucune partie en cours"); 00203 else { 00204 string reponse="GAME "; reponse+=port; 00205 mSocket->Envoyer(reponse); 00206 break; 00207 } 00208 } 00209 } 00210 } catch(SocketException s){ 00211 cout<<" SP TraitementClient::"; printf( "\e[31m[Erreur] \e[m" ); cout<<"Déconnexion du client: le thread se fait harakiri!"<<endl; 00212 return false; 00213 } 00214 } while(1); 00215 return true; 00216 }
Here is the call graph for this function:
Socket* TraitementClient::mSocket [private] |
Pointeur sur la socket.
Definition at line 96 of file TraitementClient.h.
Referenced by Authentification(), CreerOuRejoindrePartie(), EnvoyerListePartie(), EnvoyerListeScore(), Traitement(), TraitementClient(), WaitForHello(), and ~TraitementClient().
BdD* TraitementClient::mBdD [private] |
Pointeur sur le singleton gérant les utilisateurs.
Definition at line 101 of file TraitementClient.h.
Referenced by Authentification(), EnvoyerListeScore(), and TraitementClient().
TraitementComServeurJeu* TraitementClient::mComSJ [private] |
Pointeur sur le thread de communication UDP avec le serveur de jeu.
Definition at line 106 of file TraitementClient.h.
Referenced by CreerOuRejoindrePartie(), and ~TraitementClient().
LanceurSJ* TraitementClient::mLSJ [private] |
Pointeur sur le thread démarrant un serveur de jeu.
Definition at line 111 of file TraitementClient.h.
Referenced by CreerOuRejoindrePartie().
Partie* TraitementClient::mPartie [private] |
Pointeur sur le singleton gérant les parties en cours.
Definition at line 116 of file TraitementClient.h.
Referenced by CreerOuRejoindrePartie(), EnvoyerListePartie(), and TraitementClient().