Page principale | Liste des namespaces | Hiérarchie des classes | Liste par ordre alphabétique | Liste des composants | Liste des fichiers | Membres des namespaces | Composants | Déclarations

# ExManagerNetwork.cpp

Aller à la documentation de ce fichier.

    00001 /*
    00002  * ExNihilo 3D Engine
    00003  *
    00004  *  This program is free software; you can redistribute it and/or modify
    00005  *  it under the terms of the GNU General Public License as published by
    00006  *  the Free Software Foundation; either version 2 of the License, or
    00007  *  (at your option) any later version.
    00008  *
    00009  *  This program is distributed in the hope that it will be useful,
    00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00012  *  GNU Library General Public License for more details.
    00013  *
    00014  *  You should have received a copy of the GNU General Public License
    00015  *  along with this program; if not, write to the Free Software
    00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    00017  *
    00018  * Please read AUTHORS file !!!
    00019  *
    00020  * $Id: ExManagerNetwork.cpp,v 1.3 2002/11/24 11:58:58 data Exp $
    00021  *
    00022  */
    00023
    00024 #include "ExManagerNetwork.h"
    00025
    00026 bool ExManagerNetwork::m_flag = false;
    00027 ExManagerNetwork* ExManagerNetwork::m_instance = NULL;
    00028
    00029 ExManagerNetwork* ExManagerNetwork::CreateSingleton(void){
    00030 Guard(ExManagerNetwork* ExManagerNetwork::CreateSingleton(void))
    00031         if(!m_flag)
    00032         {
    00033                 m_flag = true; // We are creating the error log now, so set flag to true
    00034                 m_instance = new ExManagerNetwork; // Create the error log
    00035         }else
    00036         {
    00037                 std::cout<<"Error singleton already created"<<std::endl;
    00038         }
    00039         return m_instance;
    00040 UnGuard
    00041 }
    00042
    00043 ExManagerNetwork::ExManagerNetwork(void)
    00044 {
    00045
    00046 }
    00047
    00048 ExManagerNetwork::~ExManagerNetwork(void)
    00049 {
    00050 }
    00051
    00052 void ExManagerNetwork::Init(void)
    00053 {
    00054         m_ShowNetStat=false;
    00055         m_ShowMessage=false;
    00056         m_ShowSayBar=false;
    00057         m_ServerIp="No Connexion";
    00058         m_Connected=false;
    00059         m_LastPing=0.0f;
    00060         SetAskForCommand(false);
    00061         SetAskForExclusifCommand(false);
    00062
    00063 }
    00064
    00065 void ExManagerNetwork::Draw(void)
    00066 {
    00067         if(m_ShowNetStat)DrawNetStat();
    00068         if(m_ShowMessage)DrawMessage();
    00069
    00070
    00071 }
    00072
    00073 void ExManagerNetwork::DrawNetStat(void)
    00074 {
    00075         //ExNihilo::DrawCadre(10,glutGet(GLUT_WINDOW_HEIGHT)-135,140,50,0.2f,0.0f,0.6f,0.9f);
    00076
    00077         ExNihilo::DrawCadre(glutGet(GLUT_WINDOW_WIDTH)-135,     glutGet(GLUT_WINDOW_HEIGHT)-80, 130,50,0.2f,0.0f,0.6f,0.9f);
    00078         ExNihilo::WriteToScreen(glutGet(GLUT_WINDOW_WIDTH)-130,glutGet(GLUT_WINDOW_HEIGHT)-65,1,1,1,"NetStat");
    00079         std::string buffserver("Server :");
    00080         buffserver=buffserver+m_ServerIp;
    00081         ExNihilo::WriteToScreen(glutGet(GLUT_WINDOW_WIDTH)-130,glutGet(GLUT_WINDOW_HEIGHT)-50,1,1,1,buffserver);
    00082         char buffping[255];
    00083         Ping();
    00084         sprintf(buffping,"Ping :%4.4f",m_LastPing);
    00085         ExNihilo::WriteToScreen(glutGet(GLUT_WINDOW_WIDTH)-130,glutGet(GLUT_WINDOW_HEIGHT)-35,1,1,1,buffping);
    00086 }
    00087
    00088 void ExManagerNetwork::DrawMessage(void)
    00089 {
    00090         //draw old message
    00091         ExNihilo::DrawCadre(10,glutGet(GLUT_WINDOW_HEIGHT)-80,glutGet(GLUT_WINDOW_WIDTH)-150,50,0.8f,0.0f,0.0f,0.9f);
    00092         ExNihilo::WriteToScreen(15,glutGet(GLUT_WINDOW_HEIGHT)-65,1,1,1,m_BuffOldSay3);
    00093         ExNihilo::WriteToScreen(15,glutGet(GLUT_WINDOW_HEIGHT)-50,1,1,1,m_BuffOldSay2);
    00094         ExNihilo::WriteToScreen(15,glutGet(GLUT_WINDOW_HEIGHT)-35,1,1,1,m_BuffOldSay1);
    00095         //draw current phrase
    00096         if(m_ShowSayBar)
    00097         {
    00098                 ExNihilo::DrawCadre(10,glutGet(GLUT_WINDOW_HEIGHT)-25,glutGet(GLUT_WINDOW_WIDTH)-15,20,0.7f,0.1f,0.0f,0.9f);
    00099                 ExNihilo::WriteToScreen(15,glutGet(GLUT_WINDOW_HEIGHT)-10,1,1,1,m_BuffSay);
    00100         }
    00101 }
    00102
    00103 void  ExManagerNetwork::Ping(void)
    00104 {
    00105         if(glutGet(GLUT_ELAPSED_TIME)%100==0)
    00106         {
    00107                 if(m_Connected)
    00108                 {
    00109                         int time;
    00110                         ExCMessage buffmsg;
    00111                         time=glutGet(GLUT_ELAPSED_TIME);
    00112                         buffmsg.SetType(MESSAGE_PING);
    00113                         m_SocketClient.SendMessage(buffmsg);
    00114                         m_SocketClient.RecvMessage(buffmsg);
    00115                         m_LastPing=((float)(glutGet(GLUT_ELAPSED_TIME)-time)/100);
    00116                 }
    00117         }
    00118 }
    00119
    00120 void ExManagerNetwork::Reset(void)
    00121 {
    00122 Guard(void ExManagerNetwork::Reset(void))
    00123 UnGuard
    00124 }
    00125
    00126
    00127 void ExManagerNetwork::Connect(void)
    00128 {
    00129 Guard(void ExManagerNetwork::Connect(void))
    00130         m_SocketClient.Init();
    00131         if(m_SocketClient.Connect()==0)
    00132         {
    00133                 m_Connected=true;
    00134                 m_ServerIp="192.168.0.1";
    00135                 m_ShowNetStat=true;
    00136                 ExCMessage buffmsg;
    00137
    00138                 //w8 msg from server
    00139                 m_SocketClient.RecvMessage(buffmsg);
    00140         //      *Consol<<buffmsg<<std::endl;
    00141                 //send login
    00142
    00143                 buffmsg.SetType(MESSAGE_LOGIN);
    00144                 buffmsg.SetMessage("Client");
    00145                 m_SocketClient.SendMessage(buffmsg);
    00146         //      *Consol<<"connected"<<std::endl;
    00147         }else
    00148         {
    00149         //      *Consol<<"Socket error"<<std::endl;
    00150                 m_SocketClient.Close();
    00151         }
    00152 UnGuard
    00153 }
    00154
    00155 void ExManagerNetwork::Disconnect(void)
    00156 {
    00157 Guard(void ExManagerNetwork::Disconnect(void))
    00158         ExCMessage buffmsg;
    00159         buffmsg.SetType(MESSAGE_QUIT);
    00160         m_SocketClient.SendMessage(buffmsg);
    00161         m_SocketClient.Close();
    00162         m_Connected=false;
    00163         m_ServerIp="No Connexion";
    00164
    00165 UnGuard
    00166 }
    00167
    00168 void ExManagerNetwork::Say(void)
    00169 {
    00170 Guard(void ExManagerNetwork::Say(void))
    00171         SetAskForCommand(true);
    00172         SetAskForExclusifCommand(true);
    00173         SetSayBarState(true);
    00174 UnGuard
    00175 }
    00176
    00177 ExCAction ExManagerNetwork::InputAction(ExCAction Action)
    00178 {
    00179 Guard(ExCAction ExManagerNetwork::InputAction(ExCAction Action))
    00180         switch(Action.m_Action)
    00181         {
    00182         case SHOW_NET_STAT:
    00183                 SetNetstatState(true);
    00184                 break;
    00185         case HIDE_NET_STAT:
    00186                 SetNetstatState(false);
    00187                 break;
    00188         case SHOW_CHAT:
    00189                 SetMessageState(true);
    00190                 break;
    00191         case HIDE_CHAT:
    00192                 SetMessageState(false);
    00193                 break;
    00194         case CONNECT:
    00195                 Connect();
    00196                 break;
    00197         case DISCONNECT:
    00198                 Disconnect();
    00199                 break;
    00200         case SAY:
    00201                 Say();
    00202                 break;
    00203         }
    00204         if(Action.m_NextAction){return *Action.m_NextAction;}
    00205         else{return NOTHING;}
    00206 UnGuard
    00207 }
    00208
    00209 ExCAction ExManagerNetwork::InputCommand(ExCCommand Command)
    00210 {
    00211 Guard(ExCAction ExManagerNetwork::InputCommand(ExCCommand Command))
    00212         //std::cout<<"ExManagerNetwork::Input Command network:"<<Command.m_Command<<std::endl;
    00213         //custom action
    00214         switch(Command.m_Command)
    00215         {
    00216         case KEYBOARD_UP_ARROW_LEFT:
    00217                 if(m_BuffSay.size()>0)
    00218                 {
    00219                         m_BuffSay.erase(m_BuffSay.end()-1,m_BuffSay.end());
    00220                 }
    00221                 return NOTHING;
    00222
    00223         case KEYBOARD_UP_BACKSPACE:
    00224                 if(m_BuffSay.size()>0)
    00225                 {
    00226                         m_BuffSay.erase(m_BuffSay.end()-1,m_BuffSay.end());
    00227                 }
    00228                 return NOTHING;
    00229
    00230         case KEYBOARD_DOWN_ENTER:
    00231                 if(m_Connected)
    00232                 {
    00233                         ExCMessage buffmsg;
    00234                         buffmsg.SetType(MESSAGE_SAY);
    00235                         buffmsg.SetMessage(m_BuffSay);
    00236                         m_SocketClient.SendMessage(buffmsg);
    00237                 }
    00238                 SetAskForCommand(false);
    00239                 SetAskForExclusifCommand(false);
    00240                 m_BuffOldSay3=m_BuffOldSay2;
    00241                 m_BuffOldSay2=m_BuffOldSay1;
    00242                 m_BuffOldSay1=m_BuffSay;
    00243                 SetSayBarState(false);
    00244                 m_BuffSay.erase(m_BuffSay.begin(),m_BuffSay.end());
    00245
    00246                 return NOTHING;
    00247         }
    00248         //simple text action
    00249         ExNihilo::AddKeyToBuffer(Command.m_Command,m_BuffSay);
    00250         return NOTHING;
    00251 UnGuard
    00252 }