/Desktop/MGS-MultithreadedGameServer/Server.cc

Vai alla documentazione di questo file.
00001 //
00002 // File: Server.cc
00003 // Created by: <Detro /> aka Ivan De Marino 
00004 // <detro@mandolinux.org, demarino@studenti.unina.it, demarino@na.astro.it>
00005 // Created on: Sun Oct 24 13:14:11 2004
00006 //
00007 #include "Server.h"
00008 
00009 /**
00010  * Costruttore della Classe Server.
00011  *
00012  * @param iAddress Indirizzo su cui il Server deve mettersi in ascolto
00013  * @param port Porta su cui il Server deve mettersi in ascolto */
00014 Server::Server(const InetAddress &iAddress, 
00015    const tpport_t port, 
00016    const string path) : 
00017 TCPSocket(iAddress, port), pluginGameDirPath(path) {
00018    cout << ">>> MGS - Multithreaded Game Server <<<" << endl
00019       << "---------------------------------------" << endl << endl;
00020    cout << "<Server msg> Server UP and Running on " 
00021       << iAddress.getHostname() 
00022       << ":" << port << endl;
00023 };
00024 
00025 /**
00026  * Distruttore della Classe Server.
00027  * Si occupa di Eliminare tutti i @c ConnectionHandler
00028  * e tutti i @c Game istanziati. */
00029 Server::~Server(void) {
00030    connHandPool.clear();
00031    activeGames.clear();
00032 }
00033 
00034 /**
00035  * Accettazione della Connessione.
00036  * Questo metodo protetto viene eseguito all'atto dell'accettazione
00037  * (chiamata ad @c accept()) di una connessione in ingresso.
00038  *
00039  * @param iAddress Indirizzo da cui proviene la Connessione in ingresso
00040  * @param port Porta da cui proviene la Connessione 
00041  * @return true quando il Server riceve una richiesta di Connessione */
00042 bool Server::onAccept(const InetHostAddress &iAddress, const tpport_t port) {
00043    cout << "<Server msg> Server has Accepted Connection from: " 
00044       << iAddress.getHostname() << ":" << port << endl;;
00045    return true;
00046 }
00047 
00048 /**
00049  * Esecuzione del Server.
00050  * Questo metodo e' il corpo principale del Server.
00051  * Il Server si mette in ascolto per il tempo richiesto
00052  * e, come gia' detto, accetta connessioni in ingresso e le
00053  * gira ad un nuovo Handler.
00054  *
00055  * @param pendingTime Tempo di attesa del Server - 
00056  *    <code>TIMEOUT_INF</code> per attendere indeterminatamente */
00057 void Server::run(const timeout_t pendingTime) {
00058    /* Carica i Plug-In Game */
00059    loadPlugInGames();
00060 
00061    /* Prova a rimanere in attesa di connessioni
00062     * per il tempo richiesto ("pendingTime") */
00063    try {
00064       while(isPendingConnection(pendingTime)) {
00065          ConnectionHandler *newConnHand;
00066          newConnHand = new ConnectionHandler(*this, 
00067             activeGames, 
00068             availableGames);
00069          connHandPool.push_back(newConnHand);
00070          newConnHand->start();
00071       }
00072    }
00073    catch(Socket *socket) {
00074       /* In caso di problemi, l'eccezione viene rilanciata a chi
00075        * a livello superiore */
00076       cout << "<Server msg> Socket Error. Throwed." << endl;
00077       throw;
00078    }
00079 }
00080 
00081 /**
00082  * Metodo per la selezione delle Shared Object Library da una Directory.
00083  * E' una funzione utilizzata da @c scandir (inclusa in @c dirent.h )
00084  * per trovare tutte le librerie @c ".so" di una directory data.
00085  * @param dir Prende in input un puntatore a @c struct @c dirent
00086  * @return Restituisce il numero di SO trovate */
00087 int Server::SharedObjectLibrarySelector(const struct dirent *dir) {
00088    string fileName = dir->d_name;
00089    string extension;
00090    if( fileName.size() < 3 ) /* Esclude anche i file ".", ".." */ 
00091       return 0;
00092    extension = fileName.substr( fileName.size()-3, fileName.size() );
00093    if( extension != ".so" ) 
00094       return 0;
00095    else
00096       return 1;
00097 }
00098 
00099 /**
00100  * Caricamento delle Classi Plug-In per i Game.
00101  * Cerca e Carica le Shared Object Library dei Game disponibili.
00102  * La Directory in cui cercare i Plug-In sara' quella specificata
00103  * all'istanziazione del Server */
00104 void Server::loadPlugInGames(void) {
00105    struct dirent **directory; // Struttura per la lettura della directory
00106    string fileName;
00107    int numFoundedFile;
00108    TFactory<Game *> *factoryPointer;
00109    Game *gamePointer;
00110    pair< string, TFactory<Game *> *> *pairPointer;
00111    
00112    cout << "<Server msg> Searching for SO Plug-In Game in \"" <<
00113       pluginGameDirPath << "\"" << endl;
00114    
00115    /* Ricerca ".so" */
00116    numFoundedFile = scandir(pluginGameDirPath.data(), &directory, 
00117       Server::SharedObjectLibrarySelector, alphasort);
00118    
00119    /* Per ogni ".so" trovata istanziamo la relativa TFactory */
00120    for( int i = 0; i < numFoundedFile; i++ ) {
00121       fileName = directory[i]->d_name;
00122       cout << "<Server msg> Founded SO Plug-In Game: " << fileName << endl;
00123       
00124       /* Creazione TFactory per il Plug-In */
00125       try {
00126          factoryPointer = new TFactory<Game *> (string(pluginGameDirPath + "/" + fileName));
00127       }
00128       catch ( TFactoryException &e ) {
00129          cout << "<Server msg> Error during Plug-In Loading: " << 
00130             e.getExceptionMessage() << endl;
00131          exit (1);
00132       }
00133    
00134       /* Creazione Oggetto Game per leggere il relativo Name */
00135       gamePointer = factoryPointer->build();
00136    
00137       /* Caricamento del Game tra quelli disponibili */
00138       pairPointer = new pair< string, TFactory<Game *> *> ( 
00139          gamePointer->getName(), 
00140          factoryPointer );
00141       availableGames.push_back( pairPointer );
00142       
00143       delete gamePointer;
00144    }
00145    cout << "<Server msg> Founded " << numFoundedFile 
00146       << " Plug-In Game(s)" << endl;
00147 }

Generato il Sun Nov 28 13:27:03 2004 per MGS - Multithreaded Game Server da doxygen 1.3.4