SuperTuxKart
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
Online::RequestManager Class Reference

A class to execute requests in a separate thread. More...

#include <request_manager.hpp>

Inheritance diagram for Online::RequestManager:
Inheritance graph
[legend]

Public Types

enum  InternetPermission { IPERM_NOT_ASKED = 0 , IPERM_ALLOWED = 1 , IPERM_NOT_ALLOWED = 2 }
 If stk has permission to access the internet (for news server etc). More...
 

Public Member Functions

void addRequest (std::shared_ptr< Online::Request > request)
 Inserts a request into the queue of all requests.
 
void startNetworkThread ()
 Start the actual network thread.
 
void stopNetworkThread ()
 This function inserts a high priority request to quit into the request queue of the network thead, and also aborts any ongoing download.
 
bool getAbort ()
 
bool getPaused ()
 
void setPaused (bool val)
 
void update (float dt)
 Should be called every frame and takes care of processing the result queue and polling the database server if a user is signed in.
 
void setMenuPollingInterval (float polling_interval)
 Sets the interval with which poll requests are send to the server.
 
void setGamePollingInterval (float polling_interval)
 Sets the interval with which poll requests are send to the server.
 
- Public Member Functions inherited from CanBeDeleted
 CanBeDeleted ()
 Set this instance to be not ready to be deleted.
 
void setCanBeDeleted ()
 Sets this instance to be ready to be deleted.
 
void resetCanBeDeleted ()
 
bool canBeDeletedNow ()
 
bool waitForReadyToDeleted (float waiting_time)
 Waits at most t seconds for this class to be ready to be deleted.
 

Static Public Member Functions

static RequestManagerget ()
 Singleton access function.
 
static void deallocate ()
 Deletes the http manager.
 
static bool isRunning ()
 Checks if the http manager is running.
 

Static Public Attributes

static bool m_disable_polling = false
 
static const int HTTP_MAX_PRIORITY = 9999
 

Private Member Functions

void addResult (std::shared_ptr< Online::Request > request)
 Inserts a request into the queue of results.
 
void handleResultQueue ()
 Takes a request out of the result queue, if any is present.
 
 RequestManager ()
 Constructor.
 

Static Private Member Functions

static void mainLoop (void *obj)
 The actual main loop, which is started as a separate thread from the constructor.
 

Private Attributes

float m_time_since_poll
 Time passed since the last poll request.
 
std::shared_ptr< Online::Requestm_current_request
 The current requested being worked on.
 
std::condition_variable m_condition_variable
 A conditional variable to wake up the main loop.
 
Synchronised< bool > m_abort
 Signal an abort in case that a download is still happening.
 
std::atomic_bool m_paused
 Signal an pause before STK goes into background in iOS.
 
float m_game_polling_interval
 The polling interval while a game is running.
 
float m_menu_polling_interval
 The polling interval while the menu is shown.
 
std::thread m_thread
 Thread id of the thread running in this object.
 
Synchronised< std::priority_queue< std::shared_ptr< Online::Request >, std::vector< std::shared_ptr< Online::Request > >, Online::Request::Compare > > m_request_queue
 The list of pointers to all requests that still need to be handled.
 
Synchronised< std::queue< std::shared_ptr< Online::Request > > > m_result_queue
 The list of pointers to all requests that are already executed by the networking thread, but still need to be processed by the main thread.
 

Static Private Attributes

static RequestManagerm_request_manager = NULL
 

Detailed Description

A class to execute requests in a separate thread.

Typically the requests involve a http(s) requests to be sent to the stk server, and receive an answer (e.g. to sign in; or to download an addon). The requests are sorted by priority (e.g. sign in and out have higher priority than downloading addon icons). A request is created and initialised from the main thread. When it is moved into the request queue, it must not be handled by the main thread anymore, only the RequestManager thread can handle it. Once the request is finished, it is put in a separate ready queue. The main thread regularly checks the ready queue for any ready request, and executes a callback. So there is no need to protect any functions or data members in requests, since they will either be handled by the main thread, or RequestManager thread, never by both. On exit, if necessary a high priority sign-out or client-quit request is put into the queue, and a flag is set which causes libcurl to abort any ongoing download. Then an additional 'quit' event with same priority as the sign-out is added to the queue (since it will be added later, the sign-out will be executed first, making sure that a logged in user is logged out (or its session saved). Once this is done, most of stk is deleted (except a few objects like the file manager which might be accessed if a download just finished before the abort). On executing the quit request, the request manager will set a flag that it is ready to be deleted (using the CanBeDeleted class). The main thread will wait for a certain amount of time for the RequestManager to be ready to be deleted (i.e. the sign-out and quit request have been processes), before deleting the RequestManager. Typically the RequestManager will finish while the rest of stk is shutting down, so the user will not experience any waiting time. Only on first start of stk (which will trigger downloading of all addon icons) is it possible that actually a download request is running, which might take a bit before it can be deleted.

Member Enumeration Documentation

◆ InternetPermission

If stk has permission to access the internet (for news server etc).

IPERM_NOT_ASKED: The user needs to be asked if he wants to grant permission IPERM_ALLOWED: STK is allowed to access server. IPERM_NOT_ALLOWED: STK must not access external servers.

Constructor & Destructor Documentation

◆ RequestManager()

Online::RequestManager::RequestManager ( )
private

Constructor.

It only initialised values, it does not start the actual thread.

Member Function Documentation

◆ addRequest()

void Online::RequestManager::addRequest ( std::shared_ptr< Online::Request request)

Inserts a request into the queue of all requests.

The request will be sorted by priority.

Parameters
requestThe pointer to the new request to insert.

◆ addResult()

void Online::RequestManager::addResult ( std::shared_ptr< Online::Request request)
private

Inserts a request into the queue of results.

Parameters
requestThe pointer to the request to insert.

◆ get()

static RequestManager * Online::RequestManager::get ( )
inlinestatic

Singleton access function.

Creates the RequestManager if necessary.

◆ handleResultQueue()

void Online::RequestManager::handleResultQueue ( )
private

Takes a request out of the result queue, if any is present.

Calls the callback method of the request and takes care of memory management if necessary.

◆ mainLoop()

void Online::RequestManager::mainLoop ( void *  obj)
staticprivate

The actual main loop, which is started as a separate thread from the constructor.

After testing for a new server, fetching news, the list of packages to download, it will wait for commands to be issued.

Parameters
objA pointer to this object, passed on by pthread_create

◆ setGamePollingInterval()

void Online::RequestManager::setGamePollingInterval ( float  polling_interval)
inline

Sets the interval with which poll requests are send to the server.

This can happen from the news manager (i.e. info contained in the news.xml file), or a poll request.

◆ setMenuPollingInterval()

void Online::RequestManager::setMenuPollingInterval ( float  polling_interval)
inline

Sets the interval with which poll requests are send to the server.

This can happen from the news manager (i.e. info contained in the news.xml file), or a poll request.

◆ startNetworkThread()

void Online::RequestManager::startNetworkThread ( )

Start the actual network thread.

This can not be done as part of the constructor, since the assignment to the global network_http variable has not been assigned at that stage, and the thread might use network_http - a very subtle race condition. So the thread can only be started after the assignment (in main) has been done.

Precondition
PlayerManager was created and has read the main data for each player so that all data for automatic login is availale.

◆ stopNetworkThread()

void Online::RequestManager::stopNetworkThread ( )

This function inserts a high priority request to quit into the request queue of the network thead, and also aborts any ongoing download.

Separating this allows more time for the thread to finish cleanly, before it gets cancelled in the destructor.


The documentation for this class was generated from the following files: