From: <eg...@us...> - 2007-07-07 08:32:59
|
Revision: 624 http://svn.sourceforge.net/opengate/?rev=624&view=rev Author: egore Date: 2007-07-07 01:32:59 -0700 (Sat, 07 Jul 2007) Log Message: ----------- Add a working version check. It's only not working if we login before. We need to reset something in ASIO I'm missing right now. I think we need to close and reopen the socket. Modified Paths: -------------- branches/ogsector/src/Makefile.am branches/ogsector/src/Makefile.in branches/ogsector/src/metaserver.cpp branches/ogsector/src/metaserver.h branches/ogsector/src/opengateclient.cpp Modified: branches/ogsector/src/Makefile.am =================================================================== --- branches/ogsector/src/Makefile.am 2007-07-06 22:22:34 UTC (rev 623) +++ branches/ogsector/src/Makefile.am 2007-07-07 08:32:59 UTC (rev 624) @@ -74,7 +74,9 @@ networkServer.h \ networkServer.cpp \ networkServerUser.h \ - networkServerUser.cpp + networkServerUser.cpp \ + metaserver.h \ + metaserver.cpp testopenalmanager_SOURCES = \ testopenalmanager.cpp \ Modified: branches/ogsector/src/Makefile.in =================================================================== --- branches/ogsector/src/Makefile.in 2007-07-06 22:22:34 UTC (rev 623) +++ branches/ogsector/src/Makefile.in 2007-07-07 08:32:59 UTC (rev 624) @@ -66,7 +66,7 @@ am_opengateserver_OBJECTS = opengateserver.$(OBJEXT) common.$(OBJEXT) \ LogManager.$(OBJEXT) InputManager.$(OBJEXT) \ networkProtocol.$(OBJEXT) networkServer.$(OBJEXT) \ - networkServerUser.$(OBJEXT) + networkServerUser.$(OBJEXT) metaserver.$(OBJEXT) opengateserver_OBJECTS = $(am_opengateserver_OBJECTS) opengateserver_LDADD = $(LDADD) am_testopenalmanager_OBJECTS = testopenalmanager.$(OBJEXT) \ @@ -255,7 +255,9 @@ networkServer.h \ networkServer.cpp \ networkServerUser.h \ - networkServerUser.cpp + networkServerUser.cpp \ + metaserver.h \ + metaserver.cpp testopenalmanager_SOURCES = \ testopenalmanager.cpp \ Modified: branches/ogsector/src/metaserver.cpp =================================================================== --- branches/ogsector/src/metaserver.cpp 2007-07-06 22:22:34 UTC (rev 623) +++ branches/ogsector/src/metaserver.cpp 2007-07-07 08:32:59 UTC (rev 624) @@ -20,6 +20,7 @@ #include "metaserver.h" #include <boost/regex.hpp> +#include "config.h" namespace OpenGate{ @@ -57,50 +58,15 @@ int MetaConnection::login( const std::string & userName, const std::string & passwd ) { - std::cout << userName << ":" << passwd << std::endl; - try { - // Form the request. We specify the "Connection: close" header so that the - // server will close the socket after transmitting the response. This will - // allow us to treat all data up until the EOF as the content. - asio::streambuf request; - std::ostream request_stream(&request); - request_stream << "GET " << "/script/schnittstelle/?action=login&username=" << userName << "&password=" << passwd << " HTTP/1.0\r\n"; - request_stream << "Host: " << hostname_ << "\r\n"; - request_stream << "Accept: */*\r\n"; - request_stream << "Connection: close\r\n\r\n"; - // Send the request. - asio::write(socket_, request); - - // Read the response status line. asio::streambuf response; - asio::read_until(socket_, response, boost::regex("\r\n")); - - // Check that response is OK. - std::istream response_stream(&response); - std::string http_version; - response_stream >> http_version; - unsigned int status_code; - response_stream >> status_code; - std::string status_message; - std::getline(response_stream, status_message); - if ( !response_stream || http_version.substr(0, 5) != "HTTP/" ) { - std::cout << "Invalid response\n"; - return -1; + std::string parameters( "login&username=" + userName + "&password=" + passwd); + int retval = request(parameters, response); + if (retval < 0) { + return retval; } - if (status_code != 200) { - std::cout << "Response returned with status code " << status_code << "\n"; - return -2; - } - // Read the response headers, which are terminated by a blank line. - asio::read_until(socket_, response, boost::regex("\r\n\r\n")); - - // Process the response headers. (to get the crap of the response before reading the body!) - std::string header; - while (std::getline(response_stream, header) && header != "\r"); - // We got a usefull body. The body is an int, lets use it. if (response.size() > 0) { std::ostringstream stream; @@ -124,14 +90,6 @@ } } - asio::error error = asio::error::eof; - // Read until EOF, writing data to output as we go. - while (asio::read(socket_, response, - asio::transfer_at_least(1), - asio::assign_error(error))) - std::cout << &response; - if (error != asio::error::eof) - throw error; } catch ( asio::error & e) { log_->fatal( e.what() ); @@ -146,4 +104,105 @@ return 1; } +int MetaConnection::check_version( int user_id ) { + + try { + + asio::streambuf response; + std::string parameters("check_version&version=" + std::string(PACKAGE_VERSION) + "&user_id=" + toStr(user_id)); + 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 -3: log_->warn ( std::string ("user_id doesn't exist") ); return -3; + case -6: log_->warn ( std::string ("no version given") ); return -3; + case -7: log_->warn ( std::string ("version given but it isn't the newest (the same version as set in the admin interface of the project website)") ); return -3; + default: + if (returncode > 0) { + return returncode; + } else { + log_->warn ( std::string ("unkown return code for version check ") + toStr( returncode ) ); + return -4; + } + } + } + + } catch ( asio::error & e) { + log_->fatal( std::string( "check_version: ASIO: " ) + e.what() ); + return -5; + } catch ( std::exception & e) { + log_->fatal( std::string( "check_version: Exception: " ) + e.what() ); + return -5; + } catch (...) { + log_->fatal( "check_version: 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 + // allow us to treat all data up until the EOF as the content. + asio::streambuf request; + std::ostream request_stream(&request); + request_stream << "GET " << "/script/schnittstelle/?action=" << parameters << " HTTP/1.0\r\n"; + request_stream << "Host: " << hostname_ << "\r\n"; + request_stream << "Accept: */*\r\n"; + request_stream << "Connection: close\r\n\r\n"; + + // Send the request. + asio::write(socket_, request); + + // Read the response status line. + asio::read_until(socket_, response, boost::regex("\r\n")); + + // Check that response is OK. + std::istream response_stream(&response); + std::string http_version; + response_stream >> http_version; + unsigned int status_code; + response_stream >> status_code; + std::string status_message; + std::getline(response_stream, status_message); + if ( !response_stream || http_version.substr(0, 5) != "HTTP/" ) { + std::cout << "Invalid response\n"; + return -51; + } + if (status_code != 200) { + std::cout << "Response returned with status code " << status_code << "\n"; + return -52; + } + + // Read the response headers, which are terminated by a blank line. + asio::read_until(socket_, response, boost::regex("\r\n\r\n")); + + // Process the response headers. (to get the crap of the response before reading the body!) + std::string header; + while (std::getline(response_stream, header) && header != "\r"); + + asio::error error = asio::error::eof; + // Read until EOF, writing data to output as we go. + while (asio::read(socket_, response, + asio::transfer_at_least(1), + asio::assign_error(error))) + std::cout << &response; + if (error != asio::error::eof) + throw error; + + return 1; +} + + } // namespace OpenGate Modified: branches/ogsector/src/metaserver.h =================================================================== --- branches/ogsector/src/metaserver.h 2007-07-06 22:22:34 UTC (rev 623) +++ branches/ogsector/src/metaserver.h 2007-07-07 08:32:59 UTC (rev 624) @@ -34,6 +34,7 @@ ~MetaConnection(); int login( const std::string & userName, const std::string & passwd = "" ); + int check_version( int user_id ); private: @@ -44,6 +45,8 @@ tcp::socket socket_; tcp::resolver resolver_; + int request( const std::string & parameters, asio::streambuf & response ); + }; } Modified: branches/ogsector/src/opengateclient.cpp =================================================================== --- branches/ogsector/src/opengateclient.cpp 2007-07-06 22:22:34 UTC (rev 623) +++ branches/ogsector/src/opengateclient.cpp 2007-07-07 08:32:59 UTC (rev 624) @@ -37,10 +37,11 @@ int main( int argc, char * argv[ ] ) { int option_dialog = 1; + int option_version = 0; std::string username = "testuser"; std::string hostname = "localhost"; std::string password = "dummy"; - bool withMetaServer = false; + int withMetaServer = 0; OpenGate::LogManager *log = new OpenGate::LogManager(); log->setLogFile( "OpenGate.log" ); @@ -49,20 +50,22 @@ while (1) { static struct option long_options[] = { /* These options set a flag. */ - {"config", no_argument, &option_dialog, 1}, - {"no-config", no_argument, &option_dialog, 0}, + {"config", no_argument, &option_dialog, 1}, + {"no-config", no_argument, &option_dialog, 0}, + {"checkversion", no_argument, &option_version, 1}, + {"Metaserver", no_argument, &withMetaServer, 1}, /* These options don't set a flag. We distinguish them by their indices. */ - {"help", no_argument, 0, 'h'}, - {"Metaserver", no_argument, 0, 'M'}, - {"username", required_argument, 0, 'u'}, - {"password", required_argument, 0, 'p'}, - {"server", required_argument, 0, 's'}, + {"help", no_argument, 0, 'h'}, + {"username", required_argument, 0, 'u'}, + {"password", required_argument, 0, 'p'}, + {"server", required_argument, 0, 's'}, + {"version", no_argument, 0, 'v'}, {0, 0, 0, 0} }; /* getopt_long stores the option index here. */ int option_index = 0; - int c = getopt_long (argc, argv, "cnhMu:p:s:", long_options, &option_index); + int c = getopt_long (argc, argv, "cnhMVvu:p:s:", long_options, &option_index); /* Detect the end of the options. */ if ( c == -1 ) break; @@ -70,28 +73,31 @@ switch ( c ) { case 0: // If this option set a flag, do nothing else now if ( long_options[option_index].flag != 0 ) break; - + printf ("option %s", long_options[option_index].name); if ( optarg ) printf (" with arg %s", optarg); printf ("\n"); break; case 'c': option_dialog = true; break; + case 'V': option_version = true; break; + case 'M': withMetaServer = true; break; case 'h': #ifdef PACKAGE_NAME std::cout << PACKAGE_NAME << std::endl; #endif std::cout << std::endl << "Usage instructions" << std::endl; std::cout << " -h --help Show this information" << std::endl; + std::cout << " -v --version Show version information" << std::endl; std::cout << " -M --with-metaserver Connect to metaserver" << std::endl; std::cout << " -u name --username=name Set username to 'name'" << std::endl; std::cout << " -p pass --password=pass Set password to 'pass'" << std::endl; std::cout << " -s server --server=server Set server hostname to 'server'" << std::endl; + std::cout << " -V --checkversion See if a new version of opengate was released" << std::endl; std::cout << std::endl; std::cout << " -c --config Show config dialog" << std::endl; std::cout << " -n --no-config Disable config dialog, reuse last settings" << std::endl; delete log; return EXIT_SUCCESS; - case 'M': withMetaServer = true; break; case 'u': log->debug ( std::string ("Setting username to ") + optarg); username = optarg; @@ -101,15 +107,19 @@ password = optarg; break; case 's': - log->debug ( std::string ("Setting server to ") + optarg); - hostname = optarg; - break; - case '?': - // getopt_long already printed an error message. - break; - default: - // Fail on unkown options - abort(); + log->debug ( std::string ("Setting server to ") + optarg); + hostname = optarg; + break; + case 'v': + log->info ( std::string (PACKAGE_STRING) ); + delete log; + return EXIT_SUCCESS; + case '?': + // getopt_long already printed an error message. + break; + default: + // Fail on unkown options + abort(); } } @@ -125,7 +135,14 @@ if ( withMetaServer ){ OpenGate::MetaConnection meta( io_service ); - if ( meta.login( username, password ) > 0) { + int user_id = meta.login( username, password ); + if (( user_id > 0 ) && ( option_version == 1)) { + int response = meta.check_version( user_id ); + if (response == 1) { + log->debug( "Version is ok" ); + } else { + log->warn( "Version is not ok" ); + } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |