SuperTuxKart
Loading...
Searching...
No Matches
request_manager.hpp
1//
2// SuperTuxKart - a fun racing game with go-kart
3// Copyright (C) 2010-2015 Lucas Baudin
4// (C) 2011-2015 Joerg Henrichs
5// (C) 2013-2015 Glenn De Jonghe
6//
7// This program is free software; you can redistribute it and/or
8// modify it under the terms of the GNU General Public License
9// as published by the Free Software Foundation; either version 3
10// of the License, or (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program; if not, write to the Free Software
19// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
21#ifndef HEADER_REQUEST_MANAGER_HPP
22#define HEADER_REQUEST_MANAGER_HPP
23
24#include "online/request.hpp"
25#include "utils/can_be_deleted.hpp"
26#include "utils/synchronised.hpp"
27
28#include <irrString.h>
29
30#include <string>
31#ifdef WIN32
32# define WIN32_LEAN_AND_MEAN
33# include <winsock2.h>
34#endif
35
36#include <atomic>
37#include <condition_variable>
38#include <curl/curl.h>
39#include <memory>
40#include <queue>
41#include <thread>
42
43namespace Online
44{
80 {
81 public:
89 {
90 IPERM_NOT_ASKED = 0,
91 IPERM_ALLOWED = 1,
92 IPERM_NOT_ALLOWED = 2
93 };
94 static bool m_disable_polling;
95 private:
98
100 std::shared_ptr<Online::Request> m_current_request;
101
103 std::condition_variable m_condition_variable;
104
107
109 std::atomic_bool m_paused;
110
113
116
118 std::thread m_thread;
119
122 Synchronised< std::priority_queue <
123 std::shared_ptr<Online::Request>,
124 std::vector<std::shared_ptr<Online::Request> >,
126 >
128
133
134 void addResult(std::shared_ptr<Online::Request> request);
135 void handleResultQueue();
136
137 static void mainLoop(void *obj);
138
139 RequestManager(); //const std::string &url
141
142 static RequestManager * m_request_manager;
143
144 public:
145 static const int HTTP_MAX_PRIORITY = 9999;
146
147 // ----------------------------------------------------------------
151 {
152 if (m_request_manager == NULL)
153 {
154 m_request_manager = new RequestManager();
155 }
156 return m_request_manager;
157 } // get
158 // ----------------------------------------------------------------
159
160 static void deallocate();
161 static bool isRunning();
162
163 void addRequest(std::shared_ptr<Online::Request> request);
164 void startNetworkThread();
165 void stopNetworkThread();
166
167 bool getAbort() { return m_abort.getAtomic(); }
168 bool getPaused() { return m_paused.load(); }
169 void setPaused(bool val) { m_paused.store(val); }
170 void update(float dt);
171
172 // ----------------------------------------------------------------
176 void setMenuPollingInterval(float polling_interval)
177 {
178 m_menu_polling_interval = polling_interval;
179 } // setPollingInterval
180 // ----------------------------------------------------------------
184 void setGamePollingInterval(float polling_interval)
185 {
186 m_game_polling_interval = polling_interval;
187 } // setPollingInterval
188
189 }; //class RequestManager
190} // namespace Online
191#endif // request_manager_HPP
A simple class that a adds a function to wait with a timeout for a class to be ready to be deleted.
Definition: can_be_deleted.hpp:38
A class to execute requests in a separate thread.
Definition: request_manager.hpp:80
float m_menu_polling_interval
The polling interval while the menu is shown.
Definition: request_manager.hpp:115
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.
Definition: request_manager.hpp:127
float m_game_polling_interval
The polling interval while a game is running.
Definition: request_manager.hpp:112
Synchronised< bool > m_abort
Signal an abort in case that a download is still happening.
Definition: request_manager.hpp:106
std::atomic_bool m_paused
Signal an pause before STK goes into background in iOS.
Definition: request_manager.hpp:109
void addRequest(std::shared_ptr< Online::Request > request)
Inserts a request into the queue of all requests.
Definition: request_manager.cpp:148
void update(float dt)
Should be called every frame and takes care of processing the result queue and polling the database s...
Definition: request_manager.cpp:265
void setGamePollingInterval(float polling_interval)
Sets the interval with which poll requests are send to the server.
Definition: request_manager.hpp:184
void handleResultQueue()
Takes a request out of the result queue, if any is present.
Definition: request_manager.cpp:244
void stopNetworkThread()
This function inserts a high priority request to quit into the request queue of the network thead,...
Definition: request_manager.cpp:118
std::thread m_thread
Thread id of the thread running in this object.
Definition: request_manager.hpp:118
void addResult(std::shared_ptr< Online::Request > request)
Inserts a request into the queue of results.
Definition: request_manager.cpp:231
void setMenuPollingInterval(float polling_interval)
Sets the interval with which poll requests are send to the server.
Definition: request_manager.hpp:176
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,...
Definition: request_manager.hpp:132
float m_time_since_poll
Time passed since the last poll request.
Definition: request_manager.hpp:97
void startNetworkThread()
Start the actual network thread.
Definition: request_manager.cpp:98
RequestManager()
Constructor.
Definition: request_manager.cpp:71
static void deallocate()
Deletes the http manager.
Definition: request_manager.cpp:51
std::shared_ptr< Online::Request > m_current_request
The current requested being worked on.
Definition: request_manager.hpp:100
std::condition_variable m_condition_variable
A conditional variable to wake up the main loop.
Definition: request_manager.hpp:103
static void mainLoop(void *obj)
The actual main loop, which is started as a separate thread from the constructor.
Definition: request_manager.cpp:173
static bool isRunning()
Checks if the http manager is running.
Definition: request_manager.cpp:63
InternetPermission
If stk has permission to access the internet (for news server etc).
Definition: request_manager.hpp:89
static RequestManager * get()
Singleton access function.
Definition: request_manager.hpp:150
This class is used by the priority queue to sort requests by priority.
Definition: request.hpp:217
A variable that is automatically synchronised using pthreads mutex.
Definition: synchronised.hpp:28
TYPE getAtomic() const
Returns a copy of this variable.
Definition: synchronised.hpp:68