SuperTuxKart
protocol_manager.hpp
Go to the documentation of this file.
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2013-2015 SuperTuxKart-Team
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 
23 #ifndef PROTOCOL_MANAGER_HPP
24 #define PROTOCOL_MANAGER_HPP
25 
27 #include "network/protocol.hpp"
28 #include "utils/no_copy.hpp"
29 #include "utils/singleton.hpp"
30 #include "utils/stk_process.hpp"
31 #include "utils/synchronised.hpp"
32 #include "utils/types.hpp"
33 
34 #include <array>
35 #include <atomic>
36 #include <condition_variable>
37 #include <list>
38 #include <memory>
39 #include <mutex>
40 #include <vector>
41 #include <thread>
42 
43 class Event;
44 class STKPeer;
45 
46 // ============================================================================
96 class ProtocolManager : public NoCopy
97 {
98 private:
99 
105  {
106  private:
107  std::vector<std::shared_ptr<Protocol> > m_protocols;
108  public:
109  void removeProtocol(std::shared_ptr<Protocol> p);
110  bool notifyEvent(Event *event);
111  void update(int ticks, bool async);
112  void abort();
113  // --------------------------------------------------------------------
116  std::shared_ptr<Protocol> getFirstProtocol() { return m_protocols[0]; }
117  // --------------------------------------------------------------------
121  bool handleConnects() const
122  {
123  return !m_protocols.empty() &&
124  m_protocols[0]->handleConnects();
125  } // handleConnects
126  // --------------------------------------------------------------------
130  bool handleDisconnects() const
131  {
132  return !m_protocols.empty() &&
133  m_protocols[0]->handleDisconnects();
134  } // handleDisconnects
135  // --------------------------------------------------------------------
136  void addProtocol(std::shared_ptr<Protocol> p);
137  // --------------------------------------------------------------------
139  bool isEmpty() const { return m_protocols.empty(); }
140  // --------------------------------------------------------------------
141 
142  }; // class OneProtocolType
143 
144  // ------------------------------------------------------------------------
145 
148  std::array<OneProtocolType, PROTOCOL_MAX> m_all_protocols;
149 
151  typedef std::list<Event*> EventList;
152 
156 
160 
162  std::atomic_bool m_exit;
163 
166 
170 
171  std::condition_variable m_game_protocol_cv;
172 
173  std::mutex m_game_protocol_mutex, m_protocols_mutex;
174 
175  EventList m_controller_events_list;
176 
178  static std::weak_ptr<ProtocolManager> m_protocol_manager[PT_COUNT];
179 
180  bool sendEvent(Event* event,
181  std::array<OneProtocolType, PROTOCOL_MAX>& protocols);
182 
183  void asynchronousUpdate();
184 
185 public:
186  // ===========================================
187  // Public constructor is required for shared_ptr
188  ProtocolManager();
189  ~ProtocolManager();
190  void abort();
191  void propagateEvent(Event* event);
192  std::shared_ptr<Protocol> getProtocol(ProtocolType type);
193  void requestStart(std::shared_ptr<Protocol> protocol);
194  void requestTerminate(std::shared_ptr<Protocol> protocol);
195  void findAndTerminate(ProtocolType type);
196  void update(int ticks);
197  // ------------------------------------------------------------------------
198  bool isExiting() const { return m_exit.load(); }
199  // ------------------------------------------------------------------------
200  const std::thread& getThread() const
201  {
203  } // getThreadID
204  // ------------------------------------------------------------------------
205  static std::shared_ptr<ProtocolManager> createInstance();
206  // ------------------------------------------------------------------------
207  static bool emptyInstance()
208  {
209  return m_protocol_manager[STKProcess::getType()].expired();
210  } // emptyInstance
211  // ------------------------------------------------------------------------
212  static std::shared_ptr<ProtocolManager> lock()
213  {
214  return m_protocol_manager[STKProcess::getType()].lock();
215  } // lock
216 
217 }; // class ProtocolManager
218 
219 #endif // PROTOCOL_MANAGER_HPP
Class representing an event that need to pass trough the system. This is used to remove ENet dependen...
Definition: event.hpp:73
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:26
A simple class that stores all protocols of a certain type.
Definition: protocol_manager.hpp:105
bool handleDisconnects() const
Returns if this protocol class handles disconnect events.
Definition: protocol_manager.hpp:130
bool handleConnects() const
Returns if this protocol class handles connect events.
Definition: protocol_manager.hpp:121
void removeProtocol(std::shared_ptr< Protocol > p)
Removes a protocol from the list of protocols of a certain type.
Definition: protocol_manager.cpp:259
bool notifyEvent(Event *event)
Calls either notifyEvent(event) or notifyEventAsynchronous(evet) on all protocols.
Definition: protocol_manager.cpp:295
bool isEmpty() const
Returns if there are no protocols of this type registered.
Definition: protocol_manager.hpp:139
std::shared_ptr< Protocol > getFirstProtocol()
Returns the first protocol of a given type.
Definition: protocol_manager.hpp:116
void update(int ticks, bool async)
Calls either the synchronous update or asynchronous update function in all protocols of this type.
Definition: protocol_manager.cpp:349
Manages the protocols at runtime.
Definition: protocol_manager.hpp:97
void abort()
Stops the protocol manager.
Definition: protocol_manager.cpp:160
void findAndTerminate(ProtocolType type)
Finds a protocol with the given type and requests it to be terminated.
Definition: protocol_manager.cpp:282
static std::weak_ptr< ProtocolManager > m_protocol_manager[PT_COUNT]
Definition: protocol_manager.hpp:178
std::atomic_bool m_exit
When set to true, the main thread will exit.
Definition: protocol_manager.hpp:162
std::list< Event * > EventList
A list of network events - messages, disconnect and disconnects.
Definition: protocol_manager.hpp:151
std::shared_ptr< Protocol > getProtocol(ProtocolType type)
Get a protocol using its type.
Definition: protocol_manager.cpp:495
void asynchronousUpdate()
Updates the manager.
Definition: protocol_manager.cpp:430
bool sendEvent(Event *event, std::array< OneProtocolType, PROTOCOL_MAX > &protocols)
Sends the event to the corresponding protocol.
Definition: protocol_manager.cpp:321
void requestStart(std::shared_ptr< Protocol > protocol)
Asks the manager to start a protocol.
Definition: protocol_manager.cpp:211
void requestTerminate(std::shared_ptr< Protocol > protocol)
Notifies the manager that a protocol is terminated.
Definition: protocol_manager.cpp:225
Synchronised< EventList > m_async_events_to_process
Contains the network events to pass asynchronously to protocols (i.e.
Definition: protocol_manager.hpp:159
std::array< OneProtocolType, PROTOCOL_MAX > m_all_protocols
The list of all protocol types, each one containing a (potentially empty) list of protocols.
Definition: protocol_manager.hpp:148
void update(int ticks)
Updates the manager.
Definition: protocol_manager.cpp:370
Synchronised< EventList > m_sync_events_to_process
Contains the network events to pass synchronously to protocols (i.e.
Definition: protocol_manager.hpp:155
std::thread m_game_protocol_thread
Asynchronous game protocol thread to handle controller action as fast as possible.
Definition: protocol_manager.hpp:169
void propagateEvent(Event *event)
Function that processes incoming events.
Definition: protocol_manager.cpp:180
std::thread m_asynchronous_update_thread
Definition: protocol_manager.hpp:165
Represents a peer. This class is used to interface the ENetPeer structure.
Definition: stk_peer.hpp:76
Defines functions to easily manipulate 8-bit network destinated strings.
Generic protocols declarations.
Declares the general types that are used by the network.