SuperTuxKart
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 #include <atomic>
32 #include <condition_variable>
33 #include <memory>
34 #include <queue>
35 #include <thread>
36 
37 namespace Online
38 {
74  {
75  public:
83  {
84  IPERM_NOT_ASKED = 0,
85  IPERM_ALLOWED = 1,
86  IPERM_NOT_ALLOWED = 2
87  };
88  static bool m_disable_polling;
89  private:
92 
94  std::shared_ptr<Online::Request> m_current_request;
95 
97  std::condition_variable m_condition_variable;
98 
101 
103  std::atomic_bool m_paused;
104 
107 
110 
112  std::thread m_thread;
113 
116  Synchronised< std::priority_queue <
117  std::shared_ptr<Online::Request>,
118  std::vector<std::shared_ptr<Online::Request> >,
120  >
122 
127 
128  void addResult(std::shared_ptr<Online::Request> request);
129  void handleResultQueue();
130 
131  static void mainLoop(void *obj);
132 
133  RequestManager(); //const std::string &url
134  ~RequestManager();
135 
136  static RequestManager * m_request_manager;
137 
138  public:
139  static const int HTTP_MAX_PRIORITY = 9999;
140 
141  // ----------------------------------------------------------------
144  static RequestManager* get()
145  {
146  if (m_request_manager == NULL)
147  {
148  m_request_manager = new RequestManager();
149  }
150  return m_request_manager;
151  } // get
152  // ----------------------------------------------------------------
153 
154  static void deallocate();
155  static bool isRunning();
156 
157  void addRequest(std::shared_ptr<Online::Request> request);
158  void startNetworkThread();
159  void stopNetworkThread();
160 
161  bool getAbort() { return m_abort.getAtomic(); }
162  bool getPaused() { return m_paused.load(); }
163  void setPaused(bool val) { m_paused.store(val); }
164  void update(float dt);
165 
166  // ----------------------------------------------------------------
170  void setMenuPollingInterval(float polling_interval)
171  {
172  m_menu_polling_interval = polling_interval;
173  } // setPollingInterval
174  // ----------------------------------------------------------------
178  void setGamePollingInterval(float polling_interval)
179  {
180  m_game_polling_interval = polling_interval;
181  } // setPollingInterval
182 
183  }; //class RequestManager
184 } // namespace Online
185 #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:74
float m_menu_polling_interval
The polling interval while the menu is shown.
Definition: request_manager.hpp:109
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:121
float m_game_polling_interval
The polling interval while a game is running.
Definition: request_manager.hpp:106
Synchronised< bool > m_abort
Signal an abort in case that a download is still happening.
Definition: request_manager.hpp:100
std::atomic_bool m_paused
Signal an pause before STK goes into background in iOS.
Definition: request_manager.hpp:103
void addRequest(std::shared_ptr< Online::Request > request)
Inserts a request into the queue of all requests.
Definition: request_manager.cpp:149
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:266
void setGamePollingInterval(float polling_interval)
Sets the interval with which poll requests are send to the server.
Definition: request_manager.hpp:178
void handleResultQueue()
Takes a request out of the result queue, if any is present.
Definition: request_manager.cpp:245
void stopNetworkThread()
This function inserts a high priority request to quit into the request queue of the network thead,...
Definition: request_manager.cpp:119
std::thread m_thread
Thread id of the thread running in this object.
Definition: request_manager.hpp:112
void addResult(std::shared_ptr< Online::Request > request)
Inserts a request into the queue of results.
Definition: request_manager.cpp:232
void setMenuPollingInterval(float polling_interval)
Sets the interval with which poll requests are send to the server.
Definition: request_manager.hpp:170
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:126
float m_time_since_poll
Time passed since the last poll request.
Definition: request_manager.hpp:91
void startNetworkThread()
Start the actual network thread.
Definition: request_manager.cpp:99
RequestManager()
Constructor.
Definition: request_manager.cpp:72
static void deallocate()
Deletes the http manager.
Definition: request_manager.cpp:52
std::shared_ptr< Online::Request > m_current_request
The current requested being worked on.
Definition: request_manager.hpp:94
std::condition_variable m_condition_variable
A conditional variable to wake up the main loop.
Definition: request_manager.hpp:97
static void mainLoop(void *obj)
The actual main loop, which is started as a separate thread from the constructor.
Definition: request_manager.cpp:174
static bool isRunning()
Checks if the http manager is running.
Definition: request_manager.cpp:64
InternetPermission
If stk has permission to access the internet (for news server etc).
Definition: request_manager.hpp:83
static RequestManager * get()
Singleton access function.
Definition: request_manager.hpp:144
This class is used by the priority queue to sort requests by priority.
Definition: request.hpp:213
TYPE getAtomic() const
Returns a copy of this variable.
Definition: synchronised.hpp:68