ServerRegistration.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <time.h>
00022 #include <server/ServerRegistration.h>
00023 #include <server/ScorchedServer.h>
00024 #include <common/Logger.h>
00025 #include <common/OptionsMasterListServer.h>
00026 #include <common/OptionsScorched.h>
00027 #include <common/Defines.h>
00028 
00029 ServerRegistration *ServerRegistration::instance_ = 0;
00030 
00031 ServerRegistration *ServerRegistration::instance()
00032 {
00033         if (!instance_)
00034         {
00035                 instance_ = new ServerRegistration();
00036         }
00037         return instance_;
00038 }
00039 
00040 ServerRegistration::ServerRegistration() :
00041         mainServer_(
00042                 OptionsMasterListServer::instance()->getMasterListServer(),
00043                 OptionsMasterListServer::instance()->getMasterListServerURI()),
00044         backupServer_(
00045                 OptionsMasterListServer::instance()->getMasterListBackupServer(),
00046                 OptionsMasterListServer::instance()->getMasterListBackupServerURI())
00047 {
00048 }
00049 
00050 ServerRegistration::~ServerRegistration()
00051 {
00052 }
00053 
00054 void ServerRegistration::start()
00055 {
00056         mainServer_.start();
00057         backupServer_.start();
00058 }
00059 
00060 ServerRegistrationEntry::ServerRegistrationEntry(
00061         const char *masterListServer, 
00062         const char *masterListServerURI) : 
00063         netServer_(new NetServerHTTPProtocolSend),
00064         masterListServer_(masterListServer)
00065 {
00066         std::string buffer = S3D::formatStringBuffer(
00067                 "GET %s/servers.php?register=%s&port=%i HTTP/1.0\r\n"
00068                 "User-Agent: Scorched3D\r\n"
00069                 "Host: %s\r\n"
00070                 "Connection: close\r\n"
00071                 "\r\n"
00072                 "\r\n",
00073                 masterListServerURI,
00074                 ScorchedServer::instance()->getOptionsGame().getPublishAddress(),
00075                 ScorchedServer::instance()->getOptionsGame().getPortNo(),
00076                 masterListServer);
00077         sendNetBuffer_.addDataToBuffer(buffer.c_str(), (int) buffer.size()); // Note no null
00078 
00079         netServer_.setMessageHandler(this);
00080 }
00081 
00082 ServerRegistrationEntry::~ServerRegistrationEntry()
00083 {
00084 }
00085 
00086 void ServerRegistrationEntry::start()
00087 {
00088         SDL_CreateThread(ServerRegistrationEntry::threadFunc, (void *) this);
00089 }
00090 
00091 int ServerRegistrationEntry::threadFunc(void *param)
00092 {
00093         ServerRegistrationEntry *entry = 
00094                 (ServerRegistrationEntry *) param;
00095 
00096         entry->actualThreadFunc();
00097         return 0;
00098 }
00099 
00100 void ServerRegistrationEntry::actualThreadFunc()
00101 {
00102         const int TimeBetweenRegistrations = 540;
00103 
00104         for (;;)
00105         {
00106                 // Ensure no connections are hanging around
00107                 netServer_.disconnectAllClients();
00108                 netServer_.processMessages(); // Get rid of disconnect messages
00109 
00110                 // Register this game on the web
00111                 success_ = false;
00112                 finished_ = false;
00113 
00114                 Logger::log(S3D::formatStringBuffer(
00115                         "Connecting to registration server %s...", masterListServer_));
00116                 if (registerGame())
00117                 {
00118                         time_t lastTime = time(0);
00119                         for (;;)
00120                         {
00121                                 // Check for any replies or timeout every 1 seconds
00122                                 SDL_Delay(1000);
00123                                 netServer_.processMessages();
00124 
00125                                 // We have recieved a disconnect
00126                                 if (finished_) break;
00127 
00128                                 // Check for timeout
00129                                 int timeOut = 
00130                                         OptionsMasterListServer::instance()->getMasterListServerTimeout();
00131                                 time_t currentTime = time(0);
00132                                 if (currentTime - lastTime > timeOut)
00133                                 {
00134                                         break;
00135                                 }
00136                         }
00137 
00138                         Logger::log(S3D::formatStringBuffer("Registration to %s %s.", 
00139                                 masterListServer_,
00140                                 (success_?"was successfull":"failed")));
00141                 }
00142                 else
00143                 {
00144                         Logger::log(S3D::formatStringBuffer(
00145                                 "Failed to connect to registration server %s", masterListServer_));
00146                 }
00147 
00148                 // Wait for TimeBetweenRegistrations seconds before registering again
00149                 // unless we have had an error, in which case try again in 30 seconds
00150                 int waitTime = (success_?TimeBetweenRegistrations:30);
00151                 SDL_Delay(1000 * waitTime);
00152         }
00153 }
00154 
00155 bool ServerRegistrationEntry::registerGame()
00156 {
00157         // Connect to the web server
00158         if (!netServer_.connect(masterListServer_, 80)) return false;
00159 
00160         // Send the web request
00161         netServer_.sendMessageServer(sendNetBuffer_);
00162         return true;
00163 }
00164 
00165 void ServerRegistrationEntry::processMessage(NetMessage &message)
00166 {
00167         // We have received a reply from the web server
00168         if (message.getMessageType() == NetMessage::BufferMessage)
00169         {
00170                 message.getBuffer().addToBuffer("");
00171                 success_ = (strstr(message.getBuffer().getBuffer(), "success") != 0);
00172         }
00173         else if (message.getMessageType() == NetMessage::DisconnectMessage)
00174         {
00175                 finished_ = true;
00176         }
00177 }
00178 

Generated on Mon Feb 16 15:14:53 2009 for Scorched3D by  doxygen 1.5.3