TraitementComServeurPrincipal.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 "TraitementComServeurPrincipal.h"
00022 
00023 
00024 TraitementComServeurPrincipal::TraitementComServeurPrincipal(unsigned int port){
00025         mJeu=Jeu::GetInstance();
00026         //On établit une connexion vers le serveur principal 
00027         cout<<endl<<"    SJ TraitementComServeurPrincipal::Création d'un thread de communication UDP avec le serveur principal sur le port "<<port<<endl;
00028         //On établit une connexion avec le serveur principal
00029         try{
00030                 mSocket=new SocketUDP("localhost",(unsigned int)port);
00031         } catch(SocketException s){ s.Afficher(); }
00032 }
00033 
00034 TraitementComServeurPrincipal::~TraitementComServeurPrincipal(){
00035         cout<<"      SJ TraitementComServeurPrincipal::Destruction du thread de communication avec le serveur principal"<<endl;
00036         if(mSocket!=NULL) delete mSocket;
00037 }
00038 
00039 void *TraitementComServeurPrincipal::Traitement(){
00040         HandShake();
00041         IndiquerAttenteJoueurs();
00042         IndiquerConnexions();
00043         IndiquerDebutPartie();
00044         IndiquerStatusPartie();
00045         IndiquerFinPartie();
00046 }
00047 
00048 void TraitementComServeurPrincipal::HandShake(){
00049         mSocket->Envoyer("HELLO");
00050         if(mSocket->Ecouter()=="HELLO"){
00051                 cout<<"    SJ TraitementComServeurPrincipal::Le serveur principal a répondu à la poignée de main UDP"<<endl;
00052         }
00053 }
00054 
00055 void TraitementComServeurPrincipal::IndiquerAttenteJoueurs(){
00056         //On indique au serveur principal que l'on est en attente des joueurs 
00057         string cmd;
00058         cmd="WAITING ";
00059         ostringstream oss;
00060         oss<<mJeu->GetNbFantomeAuto();
00061         cmd+=oss.str();
00062         cout<<"    SJ TraitementComServeurPrincipal::On indique au serveur de jeu que le jeu n'est pas pret "<<endl;
00063         mSocket->Envoyer(cmd);
00064         mSocket->Ecouter();
00065 }
00066 
00067 void TraitementComServeurPrincipal::IndiquerConnexions(){
00068         //On attend que tout les joueurs soient prets
00069         string cmd;
00070         bool pacman=false, f[3];
00071         for(unsigned int i=0; i<3; i++) f[i]=false;
00072         while(1){
00073                 //On cherche si des nouvelles places ont été choisies et on l'en informe le serveur de jeu
00074                 if(!pacman) if(mJeu->GetPacmanConnecte()){
00075                         cout<<"    SJ TraitementComServeurPrincipal::On indique au serveur de jeu que pacman vient de se connecter"<<endl;
00076                         cmd="SET PACMAN "; cmd+=mJeu->GetLoginPacman();
00077                         mSocket->Envoyer(cmd);
00078                         pacman=true;
00079                 }
00080                 for(unsigned int i=0; i<3; i++){
00081                         if(!f[i]) if(mJeu->GetFConnecte(i)){
00082                                 cout<<"    SJ TraitementComServeurPrincipal::On indique au serveur de jeu que le fantome "<<i<<" vient de se connecter"<<endl;  
00083                                 ostringstream oss;
00084                                 oss<<i;
00085                                 string no=oss.str();
00086                                 cmd="SET F "; cmd+=mJeu->GetLoginF(i);  cmd+=" ";  cmd+=no;
00087                                 mSocket->Envoyer(cmd);
00088                                 f[i]=true;
00089                         }
00090                 }
00091                 if(pacman){
00092                         bool tousConnecte=true;
00093                         for(unsigned int i=0; i<3; i++){
00094                                 if(!f[i]) tousConnecte=false;
00095                         }
00096                         if(tousConnecte){
00097                                 //Tous les joueurs sont connectés ET on l'a bien annoncé au serveur principal 
00098                                 break;
00099                         }
00100                 }
00101                 sleep(1);
00102         }
00103 }
00104 
00105 void TraitementComServeurPrincipal::IndiquerDebutPartie(){
00106         cout<<"    SJ TraitementComServeurPrincipal::On indique au serveur de jeu que tous les joueurs sont connectés"<<endl;
00107         mSocket->Envoyer("READY");
00108         mSocket->Ecouter();     
00109 }
00110 
00111 void TraitementComServeurPrincipal::IndiquerStatusPartie(){
00112         while(!mJeu->GameOver()){
00113                 string envoi="STATUS "; envoi+=mJeu->EtatPartie();
00114                 mSocket->Envoyer(envoi);
00115                 sleep(1);
00116         }
00117 }
00118 
00119 void TraitementComServeurPrincipal::IndiquerFinPartie(){
00120         //La partie est terminée, on le signifie au serveur principal 
00121         //On envoi le status du jeu final 
00122         string envoi="STATUS "; envoi+=mJeu->EtatPartie();
00123         mSocket->Envoyer(envoi);
00124         cout<<"    SJ TraitementComServeurPrincipal::On indique au serveur principal que la partie est terminée"<<endl;
00125         mSocket->Envoyer("GAME OVER");
00126 }
00127 

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