TraitementClient.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Pierre Schmitt, Ludovic Giacomello, Nhi Ly      *
00003  *                                                                         *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "TraitementClient.h"
00022 
00023 TraitementClient::TraitementClient(Socket *s) : 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 }
00029 
00030 TraitementClient::~TraitementClient(){
00031         cout<<"  SP TraitementClient::~TraitementClient()"<<endl;
00032         delete mSocket;
00033         if(mComSJ!=NULL) delete mComSJ;
00034 }
00035 
00036 void* TraitementClient::Traitement(){
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 }
00062 
00063 bool TraitementClient::WaitForHello(){
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 }
00078 
00079 bool TraitementClient::Authentification(){
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 }
00125 
00126 bool TraitementClient::EnvoyerListePartie(){
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 }
00139 
00140 bool TraitementClient::EnvoyerListeScore(){
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 }
00153 
00154 bool TraitementClient::CreerOuRejoindrePartie(){
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 }

Generated on Wed Jan 2 14:01:41 2008 for Pacman by  doxygen 1.5.1