Remved StartServer protocol, and used executeNow() in ServerLobby instead.

This commit is contained in:
hiker 2015-10-28 08:01:46 +11:00
parent e212276dd2
commit 60bbfe3ab3
5 changed files with 62 additions and 158 deletions

View File

@ -1,5 +1,5 @@
# Modify this file to change the last-modified date when you add/remove a file.
# This will then trigger a new cmake run automatically.
# This will then trigger a new cmake run automatically.
file(GLOB_RECURSE STK_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.hpp")
file(GLOB_RECURSE STK_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.cpp")
file(GLOB_RECURSE STK_SHADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "data/shaders/*")

View File

@ -26,7 +26,6 @@
#include "network/protocols/get_public_address.hpp"
#include "network/protocols/show_public_address.hpp"
#include "network/protocols/connect_to_peer.hpp"
#include "network/protocols/start_server.hpp"
#include "network/protocols/start_game_protocol.hpp"
#include "network/protocol_manager.hpp"
#include "network/stk_host.hpp"
@ -38,28 +37,33 @@
#include "utils/time.hpp"
/** This is the central game setup protocol running in the server.
* It starts with detecting the public ip address and port of this
* host (GetPublicIpAddress).
*/
ServerLobbyRoomProtocol::ServerLobbyRoomProtocol() : LobbyRoomProtocol(NULL)
{
}
} // ServerLobbyRoomProtocol
//-----------------------------------------------------------------------------
/** Destructor.
*/
ServerLobbyRoomProtocol::~ServerLobbyRoomProtocol()
{
}
} // ~ServerLobbyRoomProtocol
//-----------------------------------------------------------------------------
void ServerLobbyRoomProtocol::setup()
{
m_setup = STKHost::get()->setupNewGame(); // create a new setup
m_setup->getRaceConfig()->setPlayerCount(16); //FIXME : this has to be moved to when logging into the server
m_next_id = 0;
m_state = NONE;
m_setup = STKHost::get()->setupNewGame();
m_next_id = 0;
m_state = NONE;
m_selection_enabled = false;
m_in_race = false;
m_in_race = false;
m_current_protocol = NULL;
Log::info("ServerLobbyRoomProtocol", "Starting the protocol.");
}
} // setup
//-----------------------------------------------------------------------------
@ -99,15 +103,19 @@ bool ServerLobbyRoomProtocol::notifyEventAsynchronous(Event* event)
kartDisconnected(event);
} // if (event->getType() == EVENT_TYPE_DISCONNECTED)
return true;
}
} // notifyEventAsynchronous
//-----------------------------------------------------------------------------
/** Simple finite state machine. First get the public ip address. Once this
* is known, register the server and its address with the stk server so that
* client can find it.
*/
void ServerLobbyRoomProtocol::update()
{
switch (m_state)
{
case NONE:
// Start the protocol to find the public ip address.
m_current_protocol = new GetPublicAddress();
m_current_protocol->requestStart();
m_state = GETTING_PUBLIC_ADDRESS;
@ -115,17 +123,14 @@ void ServerLobbyRoomProtocol::update()
case GETTING_PUBLIC_ADDRESS:
if (m_current_protocol->getState() == PROTOCOL_STATE_TERMINATED)
{
m_current_protocol = new StartServer();
m_current_protocol->requestStart();
m_state = LAUNCHING_SERVER;
Log::debug("ServerLobbyRoomProtocol", "Public address known.");
}
break;
case LAUNCHING_SERVER:
if (m_current_protocol->getState() == PROTOCOL_STATE_TERMINATED)
{
m_state = WORKING;
Log::info("ServerLobbyRoomProtocol", "Server setup");
// Free GetPublicAddress protocol
delete m_current_protocol;
// Register this server with the STK server. The callback in the
// xml request will update the state to WORKING.
registerServer();
Log::info("ServerLobbyRoomProtocol", "Server registered.");
}
break;
case WORKING:
@ -146,7 +151,37 @@ void ServerLobbyRoomProtocol::update()
case EXITING:
break;
}
}
} // update
//-----------------------------------------------------------------------------
/** Register this server (i.e. its public address) with the STK server
* so that clients can find it. It blocks till a responsce from the
* stk server is received (this function is executed from the
* ProtocolManager thread).
*/
void ServerLobbyRoomProtocol::registerServer()
{
Online::XMLRequest *request = new Online::XMLRequest();
const TransportAddress& addr = STKHost::get()->getPublicAddress();
PlayerManager::setUserDetails(request, "start", Online::API::SERVER_PATH);
request->addParameter("address", addr.getIP());
request->addParameter("port", addr.getPort());
request->addParameter("private_port", STKHost::get()->getPort());
request->addParameter("max_players",
UserConfigParams::m_server_max_players);
Log::info("RegisterServer", "Showing addr %s", addr.toString().c_str());
request->executeNow();
const XMLNode * result = request->getXMLData();
std::string rec_success;
if (result->get("success", &rec_success) && rec_success == "yes")
Log::info("RegisterServer", "Server is now online.");
else
Log::error("RegisterServer", "Fail to start server.");
} // registerServer
//-----------------------------------------------------------------------------

View File

@ -25,18 +25,18 @@ private:
void playerTrackVote(Event* event);
void playerReversedVote(Event* event);
void playerLapsVote(Event* event);
void registerServer();
enum STATE
{
NONE,
GETTING_PUBLIC_ADDRESS,
LAUNCHING_SERVER,
WORKING,
DONE,
EXITING
};
STATE m_state; public:
STATE m_state;
public:
ServerLobbyRoomProtocol();
virtual ~ServerLobbyRoomProtocol();

View File

@ -1,91 +0,0 @@
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2013-2015 SuperTuxKart-Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "network/protocols/start_server.hpp"
#include "config/player_manager.hpp"
#include "config/user_config.hpp"
#include "network/stk_host.hpp"
#include "online/request_manager.hpp"
StartServer::StartServer() : Protocol(NULL, PROTOCOL_SILENT)
{
} // StartServer
// ----------------------------------------------------------------------------
StartServer::~StartServer()
{
} // ~StartServer
// ----------------------------------------------------------------------------
void StartServer::setup()
{
m_state = NONE;
} // setup
// ----------------------------------------------------------------------------
void StartServer::asynchronousUpdate()
{
if (m_state == NONE)
{
const TransportAddress& addr = STKHost::get()->getPublicAddress();
m_request = new Online::XMLRequest();
PlayerManager::setUserDetails(m_request, "start", Online::API::SERVER_PATH);
m_request->addParameter("address", addr.getIP());
m_request->addParameter("port", addr.getPort());
m_request->addParameter("private_port", STKHost::get()->getPort());
m_request->addParameter("max_players", UserConfigParams::m_server_max_players);
Log::info("ShowPublicAddress", "Showing addr %s", addr.toString().c_str());
Online::RequestManager::get()->addRequest(m_request);
m_state = REQUEST_PENDING;
}
else if (m_state == REQUEST_PENDING && m_request->isDone())
{
const XMLNode * result = m_request->getXMLData();
std::string rec_success;
if(result->get("success", &rec_success))
{
if(rec_success == "yes")
{
Log::info("StartServer", "Server is now online.");
}
else
{
Log::error("StartServer", "Fail to start server.");
}
}
else
{
Log::error("StartServer", "Fail to start server.");
}
m_state = DONE;
}
else if (m_state == DONE)
{
m_state = EXITING;
delete m_request;
m_request = NULL;
requestTerminate();
}
} // asynchronousUpdate
// ----------------------------------------------------------------------------

View File

@ -1,40 +0,0 @@
#ifndef START_SERVER_HPP
#define START_SERVER_HPP
#include "network/protocol.hpp"
namespace Online { class XMLRequest; }
/*!
* This protocol tells to the database that the server is up and running,
* and shows online the public IP:port that stores the NetworkManager.
*/
class StartServer : public Protocol
{
public:
StartServer();
virtual ~StartServer();
virtual void setup();
virtual void asynchronousUpdate();
// --------------------------------------------------------------------
virtual bool notifyEvent(Event* event) { return true; }
// --------------------------------------------------------------------
virtual bool notifyEventAsynchronous(Event* event) { return true; }
// --------------------------------------------------------------------
virtual void update() {}
// --------------------------------------------------------------------
protected:
Online::XMLRequest* m_request;
enum STATE
{
NONE,
REQUEST_PENDING,
DONE,
EXITING
};
STATE m_state;
}; // class StartServer
#endif // START_SERVER_HPP