From: <eg...@us...> - 2007-09-24 20:40:38
|
Revision: 792 http://opengate.svn.sourceforge.net/opengate/?rev=792&view=rev Author: egore Date: 2007-09-24 13:40:42 -0700 (Mon, 24 Sep 2007) Log Message: ----------- 2007-09-24 Christoph Brill <eg...@us...> * src/common.cpp, src/common.h: add method to split strings with a given delimiter * src/metaserver.cpp, src/metaserver.h: add API for the metaserver * src/opengateserver.cpp: register and unregister game server against the metaserver Modified Paths: -------------- trunk/ChangeLog trunk/src/common.cpp trunk/src/common.h trunk/src/metaserver.cpp trunk/src/metaserver.h trunk/src/opengateserver.cpp Modified: trunk/ChangeLog =================================================================== --- trunk/ChangeLog 2007-09-24 20:03:30 UTC (rev 791) +++ trunk/ChangeLog 2007-09-24 20:40:42 UTC (rev 792) @@ -0,0 +1,6 @@ +2007-09-24 Christoph Brill <eg...@us...> + * src/common.cpp, src/common.h: add method to split strings with a + given delimiter + * src/metaserver.cpp, src/metaserver.h: add API for the metaserver + * src/opengateserver.cpp: register and unregister game server against + the metaserver Modified: trunk/src/common.cpp =================================================================== --- trunk/src/common.cpp 2007-09-24 20:03:30 UTC (rev 791) +++ trunk/src/common.cpp 2007-09-24 20:40:42 UTC (rev 792) @@ -99,3 +99,24 @@ dumpSceneNodes( ss, n, 0 ); return ss.str(); } + +int split(const std::string& input, const std::string& delimiter, std::list<std::string>& results) { + int pos = 0; + int offset = 0; + int lengthDelimiter = (int)delimiter.size(); + int lengthInput = (int)input.size(); + + if ( ( lengthInput == 0 ) || ( lengthDelimiter == 0 ) ) { + return 0; + } + + while (pos = input.find (delimiter, offset)) { + results.push_back(input.substr(offset, pos-offset)); + if (pos == (int)std::string::npos) { + break; + } + offset = pos+1; + } + + return results.size(); +} Modified: trunk/src/common.h =================================================================== --- trunk/src/common.h 2007-09-24 20:03:30 UTC (rev 791) +++ trunk/src/common.h 2007-09-24 20:40:42 UTC (rev 792) @@ -215,4 +215,6 @@ enum watchstate {undefined,halted,running} state; }; +int split(const std::string& input, const std::string& delimiter, std::list<std::string>& results); + #endif // _OPENGATE_COMMON__H Modified: trunk/src/metaserver.cpp =================================================================== --- trunk/src/metaserver.cpp 2007-09-24 20:03:30 UTC (rev 791) +++ trunk/src/metaserver.cpp 2007-09-24 20:40:42 UTC (rev 792) @@ -48,9 +48,9 @@ socket_.connect( *endpoint_iterator++, asio::assign_error( error ) ); } } catch ( asio::error & e) { - log_->fatal( e.what() ); + log_->fatal( std::string( "asio::error: " ) + e.what() ); } catch ( std::exception & e) { - log_->fatal( std::string( "Exception: " ) + e.what() ); + log_->fatal( std::string( "std::exception: " ) + e.what() ); } catch (...) { log_->fatal( "Unkown exception occured while resolving the metaserver" ); } @@ -88,7 +88,7 @@ if (returncode > 0) { return returncode; } else { - log_->warn ( std::string ("unkown return code for login ") + toStr( returncode ) ); + log_->warn ( std::string ("unkown return code for") + parameters + toStr( returncode ) ); return -4; } } @@ -96,10 +96,10 @@ } catch ( asio::error & e) { - log_->fatal( e.what() ); + log_->fatal( std::string( "asio::error: " ) + e.what() ); return -5; } catch ( std::exception & e) { - log_->fatal( std::string( "Exception: " ) + e.what() ); + log_->fatal( std::string( "std::exception: " ) + e.what() ); return -5; } catch (...) { log_->fatal( "Unkown exception occured while resolving the server" ); @@ -136,17 +136,17 @@ if (returncode > 0) { return returncode; } else { - log_->warn ( std::string ("unkown return code for version check ") + toStr( returncode ) ); + log_->warn ( std::string ("unkown return code for") + parameters + toStr( returncode ) ); return -4; } } } } catch ( asio::error & e) { - log_->fatal( std::string( "check_version: ASIO: " ) + e.what() ); + log_->fatal( std::string( "asio::error: " ) + e.what() ); return -5; } catch ( std::exception & e) { - log_->fatal( std::string( "check_version: Exception: " ) + e.what() ); + log_->fatal( std::string( "std::exception: " ) + e.what() ); return -5; } catch (...) { log_->fatal( "check_version: Unkown exception occured while resolving the server" ); @@ -155,6 +155,192 @@ return 1; } +int MetaConnection::logout( int user_id ) { + + try { + + asio::streambuf response; + std::string parameters( "logout&user_id=" + toStr(user_id) + "&user_ip=" + "123.123.123.123"); // FIXME: determine IP or think of other way to handle this + int retval = request(parameters, response); + if (retval < 0) { + return retval; + } + + // We got a usefull body. The body is an int, lets use it. + if (response.size() > 0) { + std::ostringstream stream; + stream << &response; + int returncode = toInt( stream.str() ); + switch (returncode) { + case -101: log_->warn ( std::string ("no action given") ); return -3; + case -102: log_->warn ( std::string ("action given but unknown") ); return -3; + case -103: log_->warn ( std::string ("database is not available") ); return -3; + case -1: log_->warn ( std::string ("no user_id given") ); return -3; + case -2: log_->warn ( std::string ("no user_ip given") ); return -3; + case -3: log_->warn ( std::string ("user_id doesn't exist") ); return -3; + case -4: log_->warn ( std::string ("password incorrect") ); return -3; + case -5: log_->warn ( std::string ("user was already offline") ); return -3; + default: + if (returncode > 0) { + return returncode; + } else { + log_->warn ( std::string ("unkown return code for") + parameters + toStr( returncode ) ); + return -4; + } + } + } + + + } catch ( asio::error & e) { + log_->fatal( std::string( "asio::error: " ) + e.what() ); + return -5; + } catch ( std::exception & e) { + log_->fatal( std::string( "std::exception: " ) + e.what() ); + return -5; + } catch (...) { + log_->fatal( "Unkown exception occured while resolving the server" ); + return -5; + } + return 1; +} + +int MetaConnection::add_game_server() { + + try { + + asio::streambuf response; + std::string parameters( "add_game_server" ); + int retval = request(parameters, response); + if (retval < 0) { + return retval; + } + + // We got a usefull body. The body is an int, lets use it. + if (response.size() > 0) { + std::ostringstream stream; + stream << &response; + int returncode = toInt( stream.str() ); + switch (returncode) { + case -101: log_->warn ( std::string ("no action given") ); return -3; + case -102: log_->warn ( std::string ("action given but unknown") ); return -3; + case -103: log_->warn ( std::string ("database is not available") ); return -3; + case -12: log_->warn ( std::string ("no additional server possible") ); return -3; + case -13: log_->warn ( std::string ("ip is already a server") ); return -3; + default: + if (returncode > 0) { + return returncode; + } else { + log_->warn ( std::string ("unkown return code for") + parameters + toStr( returncode ) ); + return -4; + } + } + } + + + } catch ( asio::error & e) { + log_->fatal( std::string( "asio::error: " ) + e.what() ); + return -5; + } catch ( std::exception & e) { + log_->fatal( std::string( "std::exception: " ) + e.what() ); + return -5; + } catch (...) { + log_->fatal( "Unkown exception occured while resolving the server" ); + return -5; + } + return 1; +} + +std::list<std::string> MetaConnection::list_game_server() { + + std::list<std::string> mylist; + try { + + asio::streambuf response; + std::string parameters( "list_game_server" ); + int retval = request(parameters, response); + if (retval < 0) { + return mylist; + } + + // We got a usefull body. The body is an int, lets use it. + if (response.size() > 0) { + std::ostringstream stream; + stream << &response; + int returncode = toInt( stream.str() ); + switch (returncode) { + case -101: log_->warn ( std::string ("no action given") ); return mylist; + case -102: log_->warn ( std::string ("action given but unknown") ); return mylist; + case -103: log_->warn ( std::string ("database is not available") ); return mylist; + case -15: log_->warn ( std::string ("no game server available") ); return mylist; + default: + + if (stream.str().length() > 5) { + split(stream.str(), " ", mylist); + } else { + log_->warn ( std::string ("unkown return code for ") + parameters + toStr( returncode ) ); + return mylist; + } + } + } + + } catch ( asio::error & e) { + log_->fatal( std::string( "asio::error: " ) + e.what() ); + return mylist; + } catch ( std::exception & e) { + log_->fatal( std::string( "std::exception: " ) + e.what() ); + return mylist; + } catch (...) { + log_->fatal( "Unkown exception occured while resolving the server" ); + return mylist; + } + return mylist; +} + +int MetaConnection::remove_game_server() { + + try { + + asio::streambuf response; + std::string parameters( "remove_game_server" ); + int retval = request(parameters, response); + if (retval < 0) { + return retval; + } + + // We got a usefull body. The body is an int, lets use it. + if (response.size() > 0) { + std::ostringstream stream; + stream << &response; + int returncode = toInt( stream.str() ); + switch (returncode) { + case -101: log_->warn ( std::string ("no action given") ); return -3; + case -102: log_->warn ( std::string ("action given but unknown") ); return -3; + case -103: log_->warn ( std::string ("database is not available") ); return -3; + case -14: log_->warn ( std::string ("ip was not a server") ); return -3; + default: + if (returncode > 0) { + return returncode; + } else { + log_->warn ( std::string ("unkown return code for ") + parameters + toStr( returncode ) ); + return -4; + } + } + } + + + } catch ( asio::error & e) { + log_->fatal( std::string( "asio::error: " ) + e.what() ); + return -5; + } catch ( std::exception & e) { + log_->fatal( std::string( "std::exception: " ) + e.what() ); + return -5; + } catch (...) { + log_->fatal( "Unkown exception occured while resolving the server" ); + return -5; + } + return 1; +} + int MetaConnection::request( const std::string & parameters, asio::streambuf & response ) { // Form the request. We specify the "Connection: close" header so that the // server will close the socket after transmitting the response. This will Modified: trunk/src/metaserver.h =================================================================== --- trunk/src/metaserver.h 2007-09-24 20:03:30 UTC (rev 791) +++ trunk/src/metaserver.h 2007-09-24 20:40:42 UTC (rev 792) @@ -35,7 +35,12 @@ int login( const std::string & userName, const std::string & passwd = "" ); int check_version( int user_id ); + int logout( int user_id ); + int add_game_server(); + std::list<std::string> list_game_server(); + int remove_game_server(); + private: LogManager *log_; Modified: trunk/src/opengateserver.cpp =================================================================== --- trunk/src/opengateserver.cpp 2007-09-24 20:03:30 UTC (rev 791) +++ trunk/src/opengateserver.cpp 2007-09-24 20:40:42 UTC (rev 792) @@ -29,6 +29,7 @@ #include "common.h" #include "networkServer.h" #include "LogManager.h" +#include "metaserver.h" using namespace OpenGate; @@ -38,12 +39,18 @@ LogManager *logMgr = NULL; asio::io_service *io_servicePtr = NULL; Server *serverPtr = NULL; +OpenGate::MetaConnection *metaPtr = NULL; void server_shutdown() { - serverPtr->shutdown(); + if ( metaPtr ) { + logMgr->info("Remove server from server list!"); + int error = 0; + if ((error = metaPtr->remove_game_server()) < 0) { + logMgr->warn( std::string("Could not remove game server " + toStr (error)) ); + } + } if ( serverPtr ) { - //** no new operator so we need no delete. ;) - // delete( serverPtr ); + serverPtr->shutdown(); } if ( io_servicePtr ) { io_servicePtr->interrupt(); @@ -63,6 +70,7 @@ } int main( int argc, char *argv[] ) { + logMgr = new LogManager( ); logMgr->setLogFile( "OGserver.log" ); logMgr->setChatLogFile( "OGserverChat.log" ); @@ -74,6 +82,13 @@ try { asio::io_service io_service; io_servicePtr = &io_service; + + OpenGate::MetaConnection meta( io_service ); + metaPtr = &meta; + int error = 0; + if ((error = meta.add_game_server()) < 0) { + logMgr->warn( std::string("Could not add game server " + toStr (error) ) ); + } tcp::endpoint endpoint( tcp::v4(), OG_PORT ); Server server( io_service, endpoint ); serverPtr = &server; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |