SuperTuxKart
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 #include <assert.h>
27 #include <memory>
28 #include <string>
29 
30 namespace Online
31 {
56  class Request : public std::enable_shared_from_this<Request>,
57  public NoCopy
58  {
59  private:
61  const int m_type;
62 
65  const int m_priority;
66 
82  enum State
83  {
84  S_PREPARING,
85  S_BUSY,
86  S_EXECUTED,
87  S_DONE
88  };
89 
90  protected:
91 
94 
99 
103 
104  // --------------------------------------------------------------------
107  virtual void operation() {}
108 
109  // --------------------------------------------------------------------
111  virtual void prepareOperation() {}
112 
113  // --------------------------------------------------------------------
115  virtual void afterOperation() {}
116 
117  public:
118  enum RequestType
119  {
120  RT_QUIT = 1
121  };
122 
123  Request(int priority, int type);
124  virtual ~Request() {}
125  void execute();
126  void executeNow();
127  void queue();
128 
129  // --------------------------------------------------------------------
131  virtual void callback() {}
132 
133  // --------------------------------------------------------------------
135  int getType() const { return m_type; }
136 
137  // --------------------------------------------------------------------
139  int getPriority() const { return m_priority; }
140 
141  // --------------------------------------------------------------------
143  void cancel() { m_cancel.setAtomic(true); }
144 
145  // --------------------------------------------------------------------
147  bool isCancelled() const { return m_cancel.getAtomic(); }
148 
149  // --------------------------------------------------------------------
151  bool isAbortable() const { return m_is_abortable.getAtomic(); }
152 
153  // --------------------------------------------------------------------
155  void setAbortable(bool b) { m_is_abortable.setAtomic(b); }
156 
157  // --------------------------------------------------------------------
159  void setBusy()
160  {
161  assert(m_state.getAtomic() == S_PREPARING);
162  m_state.setAtomic(S_BUSY);
163  } // setBusy
164 
165  // --------------------------------------------------------------------
167  void setExecuted()
168  {
169  assert(m_state.getAtomic() == S_BUSY);
170  m_state.setAtomic(S_EXECUTED);
171  } // setExecuted
172 
173  // --------------------------------------------------------------------
175  void setDone()
176  {
177  assert(m_state.getAtomic() == S_EXECUTED);
178  m_state.setAtomic(S_DONE);
179  } // setDone
180 
181  // --------------------------------------------------------------------
183  bool isDone() const { return m_state.getAtomic() == S_DONE; }
184 
185  // --------------------------------------------------------------------
187  bool isPreparing() const { return m_state.getAtomic() == S_PREPARING; }
188 
189  // --------------------------------------------------------------------
191  bool isBusy() const { return m_state.getAtomic() == S_BUSY; }
192 
193  // --------------------------------------------------------------------
197  bool hasBeenExecuted() const
198  {
199  State s = m_state.getAtomic();
200  return s == S_EXECUTED || s == S_DONE;
201  } // hasBeenExecuted
202 
203  // --------------------------------------------------------------------
206  virtual bool isAllowedToAdd() const { return isPreparing(); }
207 
208  // ====================================================================
212  class Compare
213  {
214  public:
217  bool operator() (const std::shared_ptr<Request>& a,
218  const std::shared_ptr<Request>& b) const
219  {
220  return a->getPriority() < b->getPriority();
221  }
222  }; // class Compare
223  }; // class Request
224 } //namespace Online
225 #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:213
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:217
Stores a request for the HTTP Manager.
Definition: request.hpp:58
bool isAbortable() const
Returns if this request can be aborted.
Definition: request.hpp:151
void setAbortable(bool b)
Sets if this request is abortable or not.
Definition: request.hpp:155
const int m_type
Type of the request.
Definition: request.hpp:61
int getType() const
Returns the type of the request.
Definition: request.hpp:135
virtual void callback()
Executed when a request has finished.
Definition: request.hpp:131
const int m_priority
The priority of this request.
Definition: request.hpp:65
virtual void operation()
The actual operation to be executed.
Definition: request.hpp:107
void execute()
Executes the request.
Definition: request.cpp:54
void setDone()
Should only be called by the manager.
Definition: request.hpp:175
bool isDone() const
Returns if this request is done.
Definition: request.hpp:183
Synchronised< State > m_state
Set to though if the reply of the request is in and callbacks are executed.
Definition: request.hpp:102
virtual void afterOperation()
Virtual function to be called after an operation.
Definition: request.hpp:115
virtual bool isAllowedToAdd() const
Virtual method to check if a request has initialized all needed members to a valid value.
Definition: request.hpp:206
bool isPreparing() const
Returns if this request is being prepared.
Definition: request.hpp:187
State
The different state of the requst:
Definition: request.hpp:83
void setBusy()
Sets the request state to busy.
Definition: request.hpp:159
virtual void prepareOperation()
Virtual function to be called before an operation.
Definition: request.hpp:111
Synchronised< bool > m_is_abortable
If this request can be aborted (at the end of STK).
Definition: request.hpp:98
Request(int priority, int type)
Creates a request that can be handled by the RequestManager.
Definition: request.cpp:34
void setExecuted()
Sets the request to be completed.
Definition: request.hpp:167
bool hasBeenExecuted() const
Checks if the request has completed or done (i.e.
Definition: request.hpp:197
bool isBusy() const
Returns if this request is busy.
Definition: request.hpp:191
Synchronised< bool > m_cancel
Cancel this request if it is active.
Definition: request.hpp:93
void queue()
Inserts this request into the RequestManager's queue for executing.
Definition: request.cpp:45
void executeNow()
Executes the request now, i.e.
Definition: request.cpp:77
bool isCancelled() const
Returns if this request is to be canceled.
Definition: request.hpp:147
int getPriority() const
Returns the priority of this request.
Definition: request.hpp:139
void cancel()
Signals that this request should be canceled.
Definition: request.hpp:143
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