ClientFSM Class Reference

#include <Clientfsm.h>

List of all members.

Public Member Functions

 ClientFSM (ConnectionData *data, Server *server, Client *client)
void Start (void)
void Evolve (void)
void Stop (void)
bool getStatus (void)

Protected Member Functions

bool ExecTrans (client_fsm_trans t)
void SendKA_ACK (void)
void Ready (void)
void Login (void)
void Disconnect (void)
void ResetKATime (void)

Protected Attributes

void(ClientFSM::* ExecState )(void)

Private Member Functions

bool authenticate (MessageIn *msg)
void chat (MessageIn *message)
void move (MessageIn *message)

Private Attributes

bool endFSM
ConnectionDatadata
SDLNet_SocketSet set
unsigned char livePtr
Uint32 lastKA
Loggerlogger
ServermyServer
ClientmyClient


Detailed Description

Finished states machine for the client.

Definition at line 25 of file Clientfsm.h.


Constructor & Destructor Documentation

ClientFSM::ClientFSM ConnectionData data,
Server server,
Client client
 

Builds a Client Finished state machine.

Author:
dndvault

stonedz

Since:
pre-alpha
Parameters:
data A ConnectionData pointer.
server Pointer to the server I'm running in.
client Pointer to the client I belong to.

Definition at line 15 of file Clientfsm.cpp.

References ConnectionData::socket.

00016         : endFSM(false),
00017         data(data),
00018         set(SDLNet_AllocSocketSet(1)), // Allocate a new set for the client.
00019         lastKA(SDL_GetTicks()),
00020         logger(Logger::getInstance()),
00021         myServer(server),
00022         myClient(client)
00023 {
00024         if(!set) {
00025                 std::cerr << "SDLNet_AllocSocketSet: " << SDLNet_GetError() << endl;
00026                 exit(0);
00027         }
00028         SDLNet_TCP_AddSocket(this->set, this->data->socket); // Add our TCP socket to the set.
00029         std::cerr <<"Client thread socket id: "<< data->socket << std::endl;
00030 }


Member Function Documentation

bool ClientFSM::authenticate MessageIn msg  )  [private]
 

Authenticate an user from his MSG_LOGIN message. If the authentification is correctly finished an Account instance is created and attacched to the Client this Finished state machine is running in.

Author:
dndvault

stonedz

Since:
pre-alpha
Parameters:
msg Pointer to the MSG_LOGIN message
Returns:
True if ok, false elsewhere.

Definition at line 175 of file Clientfsm.cpp.

References MessageIn::checkCRC(), Logger::log(), logger, myClient, myServer, MessageIn::readString(), and Client::setAccount().

Referenced by Login().

00175                                           {
00176         bool ret = false;
00177         CppSQLite3DB db;
00178 
00179         string login_data = msg->readString();
00180         std::cout << "Received a login request, processing it..." << std::endl;
00181 
00182         if (msg->checkCRC()){
00183         try{
00184             db.open("accounts.db");
00185             string tmpQuery = "SELECT * FROM registered_users WHERE index_hash = '"+login_data+"';";
00186 
00187             CppSQLite3Query q = db.execQuery(tmpQuery.data());
00188             if (!q.eof()){ // found a valid hash.
00189                 this->myClient->setAccount(new Account(myServer, myClient, q.getIntField(0),q.fieldValue(1),q.fieldValue(3),q.fieldValue(4),q.fieldValue(5),q.getIntField(6))); // Attach an account to the Client.
00190                 ret = true;
00191             }
00192 
00193             db.close();
00194             }
00195             catch(CppSQLite3Exception& e){
00196                 logger->log("Authentication failed due to a database problem.", LOGMODE_AUTH);
00197             logger->log(e.errorMessage(), LOGMODE_DB);
00198             }
00199 
00200         }
00201         else{
00202                 logger->log("Failed CRC check during authentification", LOGMODE_AUTH);
00203         }
00204 
00205         return ret;
00206 }

void ClientFSM::chat MessageIn message  )  [private]
 

Manages received chat messages.

Author:
stonedz
Since:
pre-alpha
Parameters:
message Pointer to the received message. No need do delete it.
Todo:
Implements a real MSG_* handler!

Definition at line 208 of file Clientfsm.cpp.

References data, Server::generalChatMsg(), myServer, MessageIn::readString(), and ConnectionData::self.

Referenced by Ready().

00208                                       {
00209     Uint32 tmp = reinterpret_cast<Uint32>(this->data->self); // Client* to an int in order to find the client in mClients map.
00210     // A MSG_CHAT has been received
00211     myServer->generalChatMsg(tmp, message->readString());
00212 
00213 }

void ClientFSM::Disconnect void   )  [protected]
 

Disconnects an user.

Author:
stonedz
Since:
pre-alpha

Definition at line 158 of file Clientfsm.cpp.

References data, endFSM, ConnectionData::running, set, and ConnectionData::socket.

00158                           {
00159         std::cout << "Closing connection with Client " << this->data->socket << "...";
00160         SDLNet_FreeSocketSet(this->set);
00161         this->set = NULL;
00162         SDLNet_TCP_Close(this->data->socket);
00163         this->data->socket = NULL;
00164         this->data->running = false;
00165         this->endFSM = true;
00166         std::cout << "OK"<< std::endl;
00167 }

void ClientFSM::Evolve void   ) 
 

Evolve to the next state of the machine.

Author:
dndvault

stonedz

Since:
pre-alpha

Definition at line 36 of file Clientfsm.cpp.

Referenced by Client::startListen().

00036                       {
00037         (this->*ExecState)();
00038 }

bool ClientFSM::ExecTrans client_fsm_trans  t  )  [protected]
 

Executes translations between states

Author:
dndvault

stonedz

Since:
pre-alpha
Parameters:
t state to evelve to.

Definition at line 43 of file Clientfsm.cpp.

Referenced by Login(), Ready(), ResetKATime(), and SendKA_ACK().

00043                                            {
00044         switch (t)
00045         {
00046         case t_login_ok:
00047                 ExecState = &ClientFSM::Ready;
00048                 break;
00049         case t_ka_received:
00050                 ExecState = &ClientFSM::ResetKATime;
00051                 break;
00052         case t_ack_time:
00053                 ExecState = &ClientFSM::SendKA_ACK;
00054                 break;
00055         case t_ka_ack_sent:
00056                 ExecState = &ClientFSM::Ready;
00057                 break;
00058         case t_ka_timeout:
00059                 ExecState = &ClientFSM::Disconnect;
00060                 break;
00061         case t_disconnect:
00062                 ExecState = &ClientFSM::Disconnect;
00063                 break;
00064         default:
00065                 std::cerr << "Unsupported transition in ClientFSM" << std::endl;
00066         }
00067 
00068 return true;
00069 }

bool ClientFSM::getStatus void   ) 
 

Returns true if the machine is running, false if the machine is stopped.

Author:
stonedz
Since:
pre-alpha
Returns:
True if running, flse if stopped.

Definition at line 230 of file Clientfsm.cpp.

Referenced by Client::startListen().

00230                          {
00231         return this->endFSM;
00232 }

void ClientFSM::Login void   )  [protected]
 

Logs an user.

Author:
stonedz
Since:
pre-alpha
Todo:
This method needs a real implementation.

Add CRC and SHA controls.

Definition at line 73 of file Clientfsm.cpp.

References authenticate(), data, ExecTrans(), Connection::getMessage(), MessageIn::getType(), and ConnectionData::socket.

00073                      {
00074         Sint32 ready = 0;
00075         MessageIn* message = NULL;
00076 
00077         ready = SDLNet_CheckSockets(this->set, 10000);// Check for socket's activity
00078         if(ready==-1){ // An error occurred, disconnect the socket.
00079                 std::cerr << "SDLNet_CheckSockets: " << SDLNet_GetError() << endl;
00080                 ExecTrans(t_disconnect);
00081         }
00082         if(SDLNet_SocketReady(data->socket)){
00083                 message = Connection::getMessage(data->socket);
00084                 if (message->getType() == MSG_LOGIN) // We now proceed with the autentication process
00085                 {
00086                         bool authenticated = false;
00087                         authenticated = authenticate(message);
00088                         if (authenticated == true)
00089                         {
00090                                 ExecTrans(t_login_ok);
00091                         }
00092                         else
00093                         {
00094                                 ExecTrans(t_disconnect);
00095                         }
00096                 }
00097                 else
00098                 {
00099                         ExecTrans(t_disconnect);
00100                 }
00101                 delete message;
00102         }
00103         else
00104         {
00105                 ExecTrans(t_disconnect); // No data to read means no username and pasword, something went wrong.
00106         }
00107 }

void ClientFSM::move MessageIn message  )  [private]
 

Manages received MSG_MOVE messages.

Author:
stonedz
Since:
pre-alpha
Parameters:
message Pointer to the received message. No need do delete it.
Todo:
Implements a real MSG_* handler!

Definition at line 215 of file Clientfsm.cpp.

References Client::getAccount(), Logger::log(), logger, myClient, and MessageIn::read2().

Referenced by Ready().

00215                                       {
00216     Uint16 len, x, y ,z;
00217     len = message->read2();
00218     if (len == 6){
00219         x = message->read2();
00220         y = message->read2();
00221         z = message->read2();
00222         std::cout << "Received a Move message, new coord are x:" << x << " y:" << y << std::endl;
00223         //myServer->getMapManager()->validateMove(ORIG,DEST); <- to be implemented!
00224         myClient->getAccount()->getChar()->setDestination(Location(x,y,z));
00225     }
00226     else
00227         logger->log("Bad lenght in MSG_MOVE message <additional info here>", LOGMODE_ERROR);
00228 }

void ClientFSM::Ready void   )  [protected]
 

Main Client loop.

Author:
stonedz
Since:
pre-alpha
Todo:
implements an extern interpreter to switch message types.

Definition at line 122 of file Clientfsm.cpp.

References chat(), data, ExecTrans(), Connection::getMessage(), MessageIn::getType(), Logger::log(), logger, move(), and ConnectionData::socket.

00122                      {
00123 
00124         if ((SDL_GetTicks() - lastKA) > KEEPALIVE_TIMEOUT){
00125                 std::cerr<<"KEEPALIVE_TIMEOUT occoured\n";
00126                 ExecTrans(t_ka_timeout);
00127         }
00128         else{
00129                 Sint32 ready = 0;
00130                 MessageIn* messagein = NULL;
00131 
00132                 ready = SDLNet_CheckSockets(this->set,0); // Check for socket's activity.
00133                 if(ready==-1){
00134                         std::cerr << "SDLNet_CheckSockets: " << SDLNet_GetError() << std::endl; // An error occurred, disconnect the socket.
00135                         ExecTrans(t_disconnect);
00136                 }
00137                 if(SDLNet_SocketReady(data->socket)){
00138                         messagein = Connection::getMessage(data->socket);
00139                 if (messagein){
00140                     if (messagein->getType() == MSG_KA){
00141                         ExecTrans(t_ka_received);
00142                     }
00143                     else if (messagein->getType() == MSG_CHAT){ // General chat message received.
00144                         chat(messagein);
00145                     }
00146                     else if (messagein->getType() == MSG_MOVE){ // A movement message received.
00147                         move(messagein);
00148                     }
00149                 }
00150                 else{
00151                     logger->log("Error crating a valid MessageIn in ClientFSM ready loop! <Additional client info here>", LOGMODE_ERROR);
00152                 }
00153                         delete messagein;
00154                 }
00155         }
00156 }

void ClientFSM::ResetKATime void   )  [protected]
 

Resets the time elapsed between two KA.

Author:
dndvault

stonedz

Since:
pre-alpha

Definition at line 169 of file Clientfsm.cpp.

References ExecTrans(), and lastKA.

00169                            {
00170         lastKA = SDL_GetTicks();
00171         ExecTrans(t_ack_time);
00172         //std::cerr<<"ResetKATime\n";
00173 }

void ClientFSM::SendKA_ACK void   )  [protected]
 

Sends an ack to the client after the reception of a keep alive message from the client.

Author:
stonedz
Since:
pre-alpha

Definition at line 109 of file Clientfsm.cpp.

References MessageOut::addCRC(), data, ExecTrans(), Connection::putMessage(), ConnectionData::socket, and MessageOut::write2().

00109                           {
00110         MessageOut* messageout;
00111         messageout = new MessageOut(MSG_KA_ACK);
00112 
00113         messageout->write2(0);
00114         messageout->addCRC();
00115         Connection::putMessage(data->socket, messageout);
00116 
00117         delete messageout;
00118 
00119         ExecTrans(t_ka_ack_sent);
00120 }

void ClientFSM::Start void   ) 
 

Starts the FSM.

Author:
dndvault

stonedz

Since:
pre-alpha

Definition at line 32 of file Clientfsm.cpp.

Referenced by Client::startListen().

00032                      {
00033         ExecState = &ClientFSM::Login;
00034 }

void ClientFSM::Stop void   ) 
 

Stops the FSM.

Author:
dndvault

stonedz

Since:
pre-alpha

Definition at line 40 of file Clientfsm.cpp.

00040                     {
00041 }


Member Data Documentation

ConnectionData* ClientFSM::data [private]
 

Useful connection's (Client) data.

Definition at line 182 of file Clientfsm.h.

Referenced by chat(), Disconnect(), Login(), Ready(), and SendKA_ACK().

bool ClientFSM::endFSM [private]
 

Indicates if the FSM is working or if it is stopped.

Definition at line 181 of file Clientfsm.h.

Referenced by Disconnect().

Uint32 ClientFSM::lastKA [private]
 

Time of last received KA.

Definition at line 185 of file Clientfsm.h.

Referenced by ResetKATime().

unsigned char ClientFSM::livePtr [private]
 

Simulate a live prompt.

Definition at line 184 of file Clientfsm.h.

Logger* ClientFSM::logger [private]
 

Logger instance (Singleton no need to free it).

Definition at line 186 of file Clientfsm.h.

Referenced by authenticate(), move(), and Ready().

Client* ClientFSM::myClient [private]
 

Points to the Client that is running this machine.

Definition at line 188 of file Clientfsm.h.

Referenced by authenticate(), and move().

Server* ClientFSM::myServer [private]
 

Points to the server.

Definition at line 187 of file Clientfsm.h.

Referenced by authenticate(), and chat().

SDLNet_SocketSet ClientFSM::set [private]
 

Client's socket set.

Definition at line 183 of file Clientfsm.h.

Referenced by Disconnect().


The documentation for this class was generated from the following files:
Generated on Mon Dec 3 04:30:13 2007 for lo-testserver by  doxygen 1.3.9.1