Fixed coding style, removed unnecessary #include in hpp file.

This commit is contained in:
hiker 2015-11-06 16:50:58 +11:00
parent 47a7bd1caf
commit 60f550e9d1
4 changed files with 187 additions and 185 deletions

View File

@ -29,171 +29,171 @@
namespace Online namespace Online
{ {
static ServersManager* manager_singleton(NULL); static ServersManager* manager_singleton(NULL);
ServersManager* ServersManager::get() ServersManager* ServersManager::get()
{
if (manager_singleton == NULL)
manager_singleton = new ServersManager();
return manager_singleton;
} // get
// ------------------------------------------------------------------------
void ServersManager::deallocate()
{
delete manager_singleton;
manager_singleton = NULL;
} // deallocate
// ===========================================================-=============
ServersManager::ServersManager()
{
m_last_load_time.setAtomic(0.0f);
m_joined_server.setAtomic(NULL);
} // ServersManager
// ------------------------------------------------------------------------
ServersManager::~ServersManager()
{
cleanUpServers();
MutexLocker(m_joined_server);
delete m_joined_server.getData();
} // ~ServersManager
// ------------------------------------------------------------------------
void ServersManager::cleanUpServers()
{
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();
} // cleanUpServers
// ------------------------------------------------------------------------
XMLRequest* ServersManager::refreshRequest(bool request_now) const
{
if (StkTime::getRealTime() - m_last_load_time.getAtomic()
< SERVER_REFRESH_INTERVAL)
{ {
if (manager_singleton == NULL) // Avoid too frequent refreshing
manager_singleton = new ServersManager();
return manager_singleton;
} // get
// ------------------------------------------------------------------------
void ServersManager::deallocate()
{
delete manager_singleton;
manager_singleton = NULL;
} // deallocate
// ========================================================================
ServersManager::ServersManager()
{
m_last_load_time.setAtomic(0.0f);
m_joined_server.setAtomic(NULL);
} // ServersManager
// ------------------------------------------------------------------------
ServersManager::~ServersManager()
{
cleanUpServers();
MutexLocker(m_joined_server);
delete m_joined_server.getData();
} // ~ServersManager
// ------------------------------------------------------------------------
void ServersManager::cleanUpServers()
{
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();
} // cleanUpServers
// ------------------------------------------------------------------------
XMLRequest* ServersManager::refreshRequest(bool request_now) const
{
if (StkTime::getRealTime() - m_last_load_time.getAtomic()
< SERVER_REFRESH_INTERVAL)
{
// Avoid too frequent refreshing
return NULL;
}
// ====================================================================
class RefreshRequest : public XMLRequest
{
virtual void callback()
{
ServersManager::get()->refresh(isSuccess(), getXMLData());
} // callback
}; // RefreshRequest
// ====================================================================
RefreshRequest* request = new RefreshRequest();
request->setApiURL(API::SERVER_PATH, "get-all");
if (request_now)
RequestManager::get()->addRequest(request);
return request;
} // refreshRequest
// ------------------------------------------------------------------------
/** Callback from the refresh request.
* \param success If the refresh was successful.
* \param input The XML data describing the server.
*/
void ServersManager::refresh(bool success, const XMLNode *input)
{
if (!success)
{
Log::error("Server Manager", "Could not refresh server list");
return;
}
const XMLNode *servers_xml = input->getNode("servers");
cleanUpServers();
for (unsigned int i = 0; i < servers_xml->getNumNodes(); i++)
{
addServer(new Server(*servers_xml->getNode(i), /*is_lan*/false));
}
m_last_load_time.setAtomic((float)StkTime::getRealTime());
} // refresh
// ------------------------------------------------------------------------
const Server* ServersManager::getQuickPlay() const
{
if(m_sorted_servers.getData().size() > 0)
return getServerBySort(0);
return NULL; return NULL;
} // getQuickPlay }
// ------------------------------------------------------------------------ // ====================================================================
void ServersManager::setJoinedServer(uint32_t id) class RefreshRequest : public XMLRequest
{ {
MutexLocker(m_joined_server); virtual void callback()
delete m_joined_server.getData(); {
ServersManager::get()->refresh(isSuccess(), getXMLData());
} // callback
}; // RefreshRequest
// It's a copy! // ====================================================================
m_joined_server.getData() = new Server(*getServerByID(id)); RefreshRequest* request = new RefreshRequest();
} // setJoinedServer request->setApiURL(API::SERVER_PATH, "get-all");
// ------------------------------------------------------------------------ if (request_now)
void ServersManager::unsetJoinedServer() RequestManager::get()->addRequest(request);
return request;
} // refreshRequest
// ------------------------------------------------------------------------
/** Callback from the refresh request.
* \param success If the refresh was successful.
* \param input The XML data describing the server.
*/
void ServersManager::refresh(bool success, const XMLNode *input)
{
if (!success)
{ {
MutexLocker(m_joined_server); Log::error("Server Manager", "Could not refresh server list");
delete m_joined_server.getData(); return;
m_joined_server.getData() = NULL; }
} // unsetJoinedServer
// ------------------------------------------------------------------------ const XMLNode *servers_xml = input->getNode("servers");
void ServersManager::addServer(Server *server) cleanUpServers();
for (unsigned int i = 0; i < servers_xml->getNumNodes(); i++)
{ {
m_sorted_servers.lock(); addServer(new Server(*servers_xml->getNode(i), /*is_lan*/false));
m_sorted_servers.getData().push_back(server); }
m_sorted_servers.unlock(); m_last_load_time.setAtomic((float)StkTime::getRealTime());
} // refresh
m_mapped_servers.lock(); // ------------------------------------------------------------------------
m_mapped_servers.getData()[server->getServerId()] = server; const Server* ServersManager::getQuickPlay() const
m_mapped_servers.unlock(); {
} // addServer if (m_sorted_servers.getData().size() > 0)
return getServerBySort(0);
// ------------------------------------------------------------------------ return NULL;
int ServersManager::getNumServers () const } // getQuickPlay
{
MutexLocker(m_sorted_servers);
return m_sorted_servers.getData().size();
} // getNumServers
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
const Server* ServersManager::getServerBySort (int index) const void ServersManager::setJoinedServer(uint32_t id)
{ {
MutexLocker(m_sorted_servers); MutexLocker(m_joined_server);
return m_sorted_servers.getData().get(index); delete m_joined_server.getData();
} // getServerBySort
// ------------------------------------------------------------------------ // It's a copy!
const Server* ServersManager::getServerByID (uint32_t id) const m_joined_server.getData() = new Server(*getServerByID(id));
{ } // setJoinedServer
MutexLocker(m_mapped_servers);
return m_mapped_servers.getData().at(id);
} // getServerByID
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
Server* ServersManager::getJoinedServer() const void ServersManager::unsetJoinedServer()
{ {
return m_joined_server.getAtomic(); MutexLocker(m_joined_server);
} // getJoinedServer delete m_joined_server.getData();
m_joined_server.getData() = NULL;
} // unsetJoinedServer
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void ServersManager::sort(bool sort_desc) void ServersManager::addServer(Server *server)
{ {
MutexLocker(m_sorted_servers); m_sorted_servers.lock();
m_sorted_servers.getData().insertionSort(0, sort_desc); m_sorted_servers.getData().push_back(server);
} // sort m_sorted_servers.unlock();
m_mapped_servers.lock();
m_mapped_servers.getData()[server->getServerId()] = server;
m_mapped_servers.unlock();
} // addServer
// ------------------------------------------------------------------------
int ServersManager::getNumServers() const
{
MutexLocker(m_sorted_servers);
return m_sorted_servers.getData().size();
} // getNumServers
// ------------------------------------------------------------------------
const Server* ServersManager::getServerBySort(int index) const
{
MutexLocker(m_sorted_servers);
return m_sorted_servers.getData().get(index);
} // getServerBySort
// ------------------------------------------------------------------------
const Server* ServersManager::getServerByID(uint32_t id) const
{
MutexLocker(m_mapped_servers);
return m_mapped_servers.getData().at(id);
} // getServerByID
// ------------------------------------------------------------------------
Server* ServersManager::getJoinedServer() const
{
return m_joined_server.getAtomic();
} // getJoinedServer
// ------------------------------------------------------------------------
void ServersManager::sort(bool sort_desc)
{
MutexLocker(m_sorted_servers);
m_sorted_servers.getData().insertionSort(0, sort_desc);
} // sort
} // namespace Online } // namespace Online

View File

@ -28,47 +28,47 @@
namespace Online namespace Online
{ {
/** /**
* \brief * \brief
* \ingroup online * \ingroup online
*/ */
class ServersManager class ServersManager
{ {
private: private:
ServersManager(); ServersManager();
~ServersManager(); ~ServersManager();
/** Sorted vector of servers */ /** Sorted vector of servers */
Synchronised<PtrVector<Server> > m_sorted_servers; Synchronised<PtrVector<Server> > m_sorted_servers;
/** Maps server id's to the same servers*/ /** Maps server id's to the same servers*/
Synchronised<std::map<uint32_t, Server*> > m_mapped_servers; Synchronised<std::map<uint32_t, Server*> > m_mapped_servers;
/** This is a pointer to a copy of the server, the moment it got joined */ /** This is a pointer to a copy of the server, the moment it got joined */
Synchronised<Server *> m_joined_server; Synchronised<Server *> m_joined_server;
Synchronised<float> m_last_load_time; Synchronised<float> m_last_load_time;
void refresh(bool success, const XMLNode * input); void refresh(bool success, const XMLNode * input);
void cleanUpServers(); void cleanUpServers();
public: public:
// Singleton // Singleton
static ServersManager* get(); static ServersManager* get();
static void deallocate(); static void deallocate();
XMLRequest * refreshRequest(bool request_now = true) const; XMLRequest * refreshRequest(bool request_now = true) const;
void setJoinedServer(uint32_t server_id); void setJoinedServer(uint32_t server_id);
void unsetJoinedServer(); void unsetJoinedServer();
void addServer(Server * server); void addServer(Server * server);
int getNumServers () const; int getNumServers() const;
const Server * getServerByID (uint32_t server_id) const; const Server * getServerByID(uint32_t server_id) const;
const Server * getServerBySort (int index) const; const Server * getServerBySort(int index) const;
void sort(bool sort_desc); void sort(bool sort_desc);
Server * getJoinedServer() const; Server * getJoinedServer() const;
// Returns the best server to join // Returns the best server to join
const Server * getQuickPlay() const; const Server * getQuickPlay() const;
}; // class ServersManager }; // class ServersManager
} // namespace Online } // namespace Online
#endif // HEADER_SERVERS_MANAGER_HPP #endif // HEADER_SERVERS_MANAGER_HPP

View File

@ -19,6 +19,7 @@
#include "audio/sfx_manager.hpp" #include "audio/sfx_manager.hpp"
#include "guiengine/modaldialog.hpp" #include "guiengine/modaldialog.hpp"
#include "online/servers_manager.hpp"
#include "states_screens/dialogs/message_dialog.hpp" #include "states_screens/dialogs/message_dialog.hpp"
#include "states_screens/dialogs/server_info_dialog.hpp" #include "states_screens/dialogs/server_info_dialog.hpp"
#include "states_screens/state_manager.hpp" #include "states_screens/state_manager.hpp"

View File

@ -20,7 +20,8 @@
#include "guiengine/screen.hpp" #include "guiengine/screen.hpp"
#include "guiengine/widgets.hpp" #include "guiengine/widgets.hpp"
#include "online/servers_manager.hpp"
namespace Online { class XMLRequest; }
namespace GUIEngine { class Widget; } namespace GUIEngine { class Widget; }