2013-07-12 19:12:31 -04:00
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
|
|
|
// Copyright (C) 2013 Glenn De Jonghe
|
|
|
|
//
|
|
|
|
// 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 "online/servers_manager.hpp"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <irrString.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include "config/user_config.hpp"
|
|
|
|
#include "utils/translation.hpp"
|
2013-07-21 13:41:02 -04:00
|
|
|
#include "utils/time.hpp"
|
|
|
|
|
|
|
|
#define SERVER_REFRESH_INTERVAL 5.0f
|
2013-07-12 19:12:31 -04:00
|
|
|
|
2013-07-19 17:20:32 -04:00
|
|
|
namespace Online{
|
2013-07-12 19:12:31 -04:00
|
|
|
|
2013-07-24 22:22:53 -04:00
|
|
|
static ServersManager* manager_singleton(NULL);
|
2013-07-12 19:12:31 -04:00
|
|
|
|
2013-07-24 22:22:53 -04:00
|
|
|
ServersManager* ServersManager::get()
|
2013-07-18 19:02:18 -04:00
|
|
|
{
|
2013-07-24 22:22:53 -04:00
|
|
|
if (manager_singleton == NULL)
|
|
|
|
manager_singleton = new ServersManager();
|
|
|
|
return manager_singleton;
|
2013-07-21 13:41:02 -04:00
|
|
|
}
|
2013-07-12 19:12:31 -04:00
|
|
|
|
2013-07-18 19:02:18 -04:00
|
|
|
void ServersManager::deallocate()
|
|
|
|
{
|
2013-07-24 22:22:53 -04:00
|
|
|
delete manager_singleton;
|
|
|
|
manager_singleton = NULL;
|
2013-07-18 19:02:18 -04:00
|
|
|
} // deallocate
|
2013-07-12 19:12:31 -04:00
|
|
|
|
2013-07-18 19:02:18 -04:00
|
|
|
// ============================================================================
|
|
|
|
ServersManager::ServersManager(){
|
2013-07-24 22:22:53 -04:00
|
|
|
m_last_load_time.setAtomic(0.0f);
|
|
|
|
m_joined_server.setAtomic(NULL);
|
2013-07-22 16:00:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ServersManager::~ServersManager(){
|
2013-07-23 12:34:38 -04:00
|
|
|
cleanUpServers();
|
2013-07-25 10:01:20 -04:00
|
|
|
MutexLocker(m_joined_server);
|
2013-07-24 22:22:53 -04:00
|
|
|
delete m_joined_server.getData();
|
2013-07-18 19:02:18 -04:00
|
|
|
}
|
2013-07-12 19:12:31 -04:00
|
|
|
|
2013-07-23 12:34:38 -04:00
|
|
|
// ============================================================================
|
|
|
|
void ServersManager::cleanUpServers()
|
|
|
|
{
|
2013-07-24 22:22:53 -04:00
|
|
|
m_sorted_servers.lock();
|
|
|
|
m_sorted_servers.getData().clearAndDeleteAll();
|
|
|
|
m_sorted_servers.unlock();
|
|
|
|
m_mapped_servers.lock();
|
|
|
|
m_mapped_servers.getData().clear();
|
|
|
|
m_mapped_servers.unlock();
|
2013-07-23 12:34:38 -04:00
|
|
|
}
|
|
|
|
|
2013-07-18 19:02:18 -04:00
|
|
|
// ============================================================================
|
2013-07-24 22:22:53 -04:00
|
|
|
const ServersManager::RefreshRequest * ServersManager::refreshRequest() const
|
2013-07-12 19:12:31 -04:00
|
|
|
{
|
2013-07-21 13:41:02 -04:00
|
|
|
RefreshRequest * request = NULL;
|
2013-07-24 22:22:53 -04:00
|
|
|
if(Time::getRealTime() - m_last_load_time.getAtomic() > SERVER_REFRESH_INTERVAL)
|
2013-07-21 13:41:02 -04:00
|
|
|
{
|
|
|
|
request = new RefreshRequest();
|
|
|
|
request->setURL((std::string)UserConfigParams::m_server_multiplayer + "client-user.php");
|
|
|
|
request->setParameter("action",std::string("get_server_list"));
|
|
|
|
HTTPManager::get()->addRequest(request);
|
|
|
|
}
|
|
|
|
return request;
|
|
|
|
}
|
2013-07-18 19:02:18 -04:00
|
|
|
|
2013-07-22 21:37:49 -04:00
|
|
|
void ServersManager::refresh(RefreshRequest * input)
|
2013-07-21 13:41:02 -04:00
|
|
|
{
|
|
|
|
if (input->isSuccess())
|
2013-07-12 19:12:31 -04:00
|
|
|
{
|
2013-07-21 13:41:02 -04:00
|
|
|
const XMLNode * servers_xml = input->getResult()->getNode("servers");
|
2013-07-23 12:34:38 -04:00
|
|
|
cleanUpServers();
|
2013-07-21 13:41:02 -04:00
|
|
|
for (unsigned int i = 0; i < servers_xml->getNumNodes(); i++)
|
2013-07-12 19:12:31 -04:00
|
|
|
{
|
2013-07-23 12:34:38 -04:00
|
|
|
addServer(new Server(*servers_xml->getNode(i)));
|
2013-07-12 19:12:31 -04:00
|
|
|
}
|
2013-07-28 01:22:02 -04:00
|
|
|
m_last_load_time.setAtomic((float)Time::getRealTime());
|
2013-07-12 19:12:31 -04:00
|
|
|
}
|
2013-07-18 19:02:18 -04:00
|
|
|
//FIXME error message
|
2013-07-12 19:12:31 -04:00
|
|
|
}
|
2013-07-14 20:25:56 -04:00
|
|
|
|
2013-07-21 13:41:02 -04:00
|
|
|
void ServersManager::RefreshRequest::callback()
|
|
|
|
{
|
2013-07-24 22:22:53 -04:00
|
|
|
ServersManager::get()->refresh(this);
|
2013-07-21 13:41:02 -04:00
|
|
|
}
|
|
|
|
|
2013-07-18 19:02:18 -04:00
|
|
|
// ============================================================================
|
2013-07-24 22:22:53 -04:00
|
|
|
const Server * ServersManager::getQuickPlay() const
|
2013-07-18 19:02:18 -04:00
|
|
|
{
|
2013-07-24 22:22:53 -04:00
|
|
|
/*if(m_sorted_servers.size() > 0)
|
2013-07-23 12:34:38 -04:00
|
|
|
return getServerBySort(0);
|
2013-07-28 01:22:02 -04:00
|
|
|
*/
|
|
|
|
return NULL;
|
2013-07-18 19:02:18 -04:00
|
|
|
}
|
2013-07-23 12:34:38 -04:00
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
void ServersManager::setJoinedServer(uint32_t id)
|
|
|
|
{
|
2013-07-25 10:01:20 -04:00
|
|
|
MutexLocker(m_joined_server);
|
2013-07-24 22:22:53 -04:00
|
|
|
delete m_joined_server.getData();
|
2013-07-23 12:34:38 -04:00
|
|
|
//It's a copy!
|
2013-07-24 22:22:53 -04:00
|
|
|
m_joined_server.getData() = new Server(*getServerByID(id));
|
2013-07-23 12:34:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
void ServersManager::unsetJoinedServer()
|
|
|
|
{
|
2013-07-25 10:01:20 -04:00
|
|
|
MutexLocker(m_joined_server);
|
2013-07-24 22:22:53 -04:00
|
|
|
delete m_joined_server.getData();
|
|
|
|
m_joined_server.getData() = NULL;
|
2013-07-23 12:34:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
void ServersManager::addServer(Server * server)
|
|
|
|
{
|
2013-07-24 22:22:53 -04:00
|
|
|
m_sorted_servers.lock();
|
|
|
|
m_sorted_servers.getData().push_back(server);
|
|
|
|
m_sorted_servers.unlock();
|
|
|
|
m_mapped_servers.lock();
|
|
|
|
m_mapped_servers.getData()[server->getServerId()] = server;
|
|
|
|
m_mapped_servers.unlock();
|
2013-07-23 12:34:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ============================================================================
|
2013-07-24 22:22:53 -04:00
|
|
|
int ServersManager::getNumServers () const
|
2013-07-23 12:34:38 -04:00
|
|
|
{
|
2013-07-25 10:01:20 -04:00
|
|
|
MutexLocker(m_sorted_servers);
|
2013-07-24 22:22:53 -04:00
|
|
|
return m_sorted_servers.getData().size();
|
2013-07-23 12:34:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ============================================================================
|
2013-07-24 22:22:53 -04:00
|
|
|
const Server * ServersManager::getServerBySort (int index) const
|
2013-07-23 12:34:38 -04:00
|
|
|
{
|
2013-07-25 10:01:20 -04:00
|
|
|
MutexLocker(m_sorted_servers);
|
2013-07-24 22:22:53 -04:00
|
|
|
return m_sorted_servers.getData().get(index);
|
2013-07-23 12:34:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ============================================================================
|
2013-07-24 22:22:53 -04:00
|
|
|
const Server * ServersManager::getServerByID (uint32_t id) const
|
2013-07-23 12:34:38 -04:00
|
|
|
{
|
2013-07-25 10:01:20 -04:00
|
|
|
MutexLocker(m_mapped_servers);
|
2013-07-24 22:22:53 -04:00
|
|
|
return m_mapped_servers.getData().at(id);
|
2013-07-23 12:34:38 -04:00
|
|
|
}
|
2013-07-24 22:22:53 -04:00
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
Server * ServersManager::getJoinedServer() const
|
|
|
|
{
|
|
|
|
return m_joined_server.getAtomic();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
void ServersManager::sort(bool sort_desc){
|
2013-07-25 10:01:20 -04:00
|
|
|
MutexLocker(m_sorted_servers);
|
2013-07-24 22:22:53 -04:00
|
|
|
m_sorted_servers.getData().insertionSort(0, sort_desc);
|
|
|
|
}
|
|
|
|
|
2013-07-19 17:20:32 -04:00
|
|
|
} // namespace Online
|