00001
00002
00003
00004
00005
00006
00007 #include "Server.h"
00008
00009
00010
00011
00012
00013
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
00027
00028
00029 Server::~Server(void) {
00030 connHandPool.clear();
00031 activeGames.clear();
00032 }
00033
00034
00035
00036
00037
00038
00039
00040
00041
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
00050
00051
00052
00053
00054
00055
00056
00057 void Server::run(const timeout_t pendingTime) {
00058
00059 loadPlugInGames();
00060
00061
00062
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
00075
00076 cout << "<Server msg> Socket Error. Throwed." << endl;
00077 throw;
00078 }
00079 }
00080
00081
00082
00083
00084
00085
00086
00087 int Server::SharedObjectLibrarySelector(const struct dirent *dir) {
00088 string fileName = dir->d_name;
00089 string extension;
00090 if( fileName.size() < 3 )
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
00101
00102
00103
00104 void Server::loadPlugInGames(void) {
00105 struct dirent **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
00116 numFoundedFile = scandir(pluginGameDirPath.data(), &directory,
00117 Server::SharedObjectLibrarySelector, alphasort);
00118
00119
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
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
00135 gamePointer = factoryPointer->build();
00136
00137
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 }