SuperTuxKart
Loading...
Searching...
No Matches
request.hpp
1// SuperTuxKart - a fun racing game with go-kart
2// Copyright (C) 2011-2015 Joerg Henrichs
3// 2013 Glenn De Jonghe
4//
5// This program is free software; you can redistribute it and/or
6// modify it under the terms of the GNU General Public License
7// as published by the Free Software Foundation; either version 3
8// of the License, or (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19#ifndef HEADER_ONLINE_REQUEST_HPP
20#define HEADER_ONLINE_REQUEST_HPP
21
22#include "utils/cpp2011.hpp"
23#include "utils/no_copy.hpp"
24#include "utils/synchronised.hpp"
25
26#ifdef WIN32
27# include <winsock2.h>
28#endif
29#include <curl/curl.h>
30#include <assert.h>
31#include <memory>
32#include <string>
33
34namespace Online
35{
60 class Request : public std::enable_shared_from_this<Request>,
61 public NoCopy
62 {
63 private:
65 const int m_type;
66
69 const int m_priority;
70
86 enum State
87 {
88 S_PREPARING,
89 S_BUSY,
90 S_EXECUTED,
91 S_DONE
92 };
93
94 protected:
95
98
103
107
108 // --------------------------------------------------------------------
111 virtual void operation() {}
112
113 // --------------------------------------------------------------------
115 virtual void prepareOperation() {}
116
117 // --------------------------------------------------------------------
119 virtual void afterOperation() {}
120
121 public:
122 enum RequestType
123 {
124 RT_QUIT = 1
125 };
126
127 Request(int priority, int type);
128 virtual ~Request() {}
129 void execute();
130 void executeNow();
131 void queue();
132
133 // --------------------------------------------------------------------
135 virtual void callback() {}
136
137 // --------------------------------------------------------------------
139 int getType() const { return m_type; }
140
141 // --------------------------------------------------------------------
143 int getPriority() const { return m_priority; }
144
145 // --------------------------------------------------------------------
147 void cancel() { m_cancel.setAtomic(true); }
148
149 // --------------------------------------------------------------------
151 bool isCancelled() const { return m_cancel.getAtomic(); }
152
153 // --------------------------------------------------------------------
155 bool isAbortable() const { return m_is_abortable.getAtomic(); }
156
157 // --------------------------------------------------------------------
160
161 // --------------------------------------------------------------------
163 void setBusy()
164 {
165 assert(m_state.getAtomic() == S_PREPARING);
166 m_state.setAtomic(S_BUSY);
167 } // setBusy
168
169 // --------------------------------------------------------------------
172 {
173 assert(m_state.getAtomic() == S_BUSY);
174 m_state.setAtomic(S_EXECUTED);
175 } // setExecuted
176
177 // --------------------------------------------------------------------
179 void setDone()
180 {
181 assert(m_state.getAtomic() == S_EXECUTED);
182 m_state.setAtomic(S_DONE);
183 } // setDone
184
185 // --------------------------------------------------------------------
187 bool isDone() const { return m_state.getAtomic() == S_DONE; }
188
189 // --------------------------------------------------------------------
191 bool isPreparing() const { return m_state.getAtomic() == S_PREPARING; }
192
193 // --------------------------------------------------------------------
195 bool isBusy() const { return m_state.getAtomic() == S_BUSY; }
196
197 // --------------------------------------------------------------------
201 bool hasBeenExecuted() const
202 {
203 State s = m_state.getAtomic();
204 return s == S_EXECUTED || s == S_DONE;
205 } // hasBeenExecuted
206
207 // --------------------------------------------------------------------
210 virtual bool isAllowedToAdd() const { return isPreparing(); }
211
212 // ====================================================================
217 {
218 public:
221 bool operator() (const std::shared_ptr<Request>& a,
222 const std::shared_ptr<Request>& b) const
223 {
224 return a->getPriority() < b->getPriority();
225 }
226 }; // class Compare
227 }; // class Request
228} //namespace Online
229#endif // HEADER_ONLINE_REQUEST_HPP
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:26
This class is used by the priority queue to sort requests by priority.
Definition: request.hpp:217
bool operator()(const std::shared_ptr< Request > &a, const std::shared_ptr< Request > &b) const
Compares two requests, returns if the first request has a lower priority than the second one.
Definition: request.hpp:221
Stores a request for the HTTP Manager.
Definition: request.hpp:62
bool isAbortable() const
Returns if this request can be aborted.
Definition: request.hpp:155
void setAbortable(bool b)
Sets if this request is abortable or not.
Definition: request.hpp:159
const int m_type
Type of the request.
Definition: request.hpp:65
int getType() const
Returns the type of the request.
Definition: request.hpp:139
virtual void callback()
Executed when a request has finished.
Definition: request.hpp:135
const int m_priority
The priority of this request.
Definition: request.hpp:69
virtual void operation()
The actual operation to be executed.
Definition: request.hpp:111
void execute()
Executes the request.
Definition: request.cpp:58
void setDone()
Should only be called by the manager.
Definition: request.hpp:179
bool isDone() const
Returns if this request is done.
Definition: request.hpp:187
Synchronised< State > m_state
Set to though if the reply of the request is in and callbacks are executed.
Definition: request.hpp:106
virtual void afterOperation()
Virtual function to be called after an operation.
Definition: request.hpp:119
virtual bool isAllowedToAdd() const
Virtual method to check if a request has initialized all needed members to a valid value.
Definition: request.hpp:210
bool isPreparing() const
Returns if this request is being prepared.
Definition: request.hpp:191
State
The different state of the requst:
Definition: request.hpp:87
void setBusy()
Sets the request state to busy.
Definition: request.hpp:163
virtual void prepareOperation()
Virtual function to be called before an operation.
Definition: request.hpp:115
Synchronised< bool > m_is_abortable
If this request can be aborted (at the end of STK).
Definition: request.hpp:102
void setExecuted()
Sets the request to be completed.
Definition: request.hpp:171
bool hasBeenExecuted() const
Checks if the request has completed or done (i.e.
Definition: request.hpp:201
bool isBusy() const
Returns if this request is busy.
Definition: request.hpp:195
Synchronised< bool > m_cancel
Cancel this request if it is active.
Definition: request.hpp:97
void queue()
Inserts this request into the RequestManager's queue for executing.
Definition: request.cpp:49
void executeNow()
Executes the request now, i.e.
Definition: request.cpp:81
bool isCancelled() const
Returns if this request is to be canceled.
Definition: request.hpp:151
int getPriority() const
Returns the priority of this request.
Definition: request.hpp:143
void cancel()
Signals that this request should be canceled.
Definition: request.hpp:147
A variable that is automatically synchronised using pthreads mutex.
Definition: synchronised.hpp:28
void setAtomic(const TYPE &v)
Sets the value of this variable using a mutex.
Definition: synchronised.hpp:59
TYPE getAtomic() const
Returns a copy of this variable.
Definition: synchronised.hpp:68