SEDNL  .
Simple Event Driven Network Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
EventConsumer.hpp
1 // SEDNL - Copyright (c) 2013 Jeremy S. Cochoy
2 //
3 // This software is provided 'as-is', without any express or implied warranty.
4 // In no event will the authors be held liable for any damages arising from
5 // the use of this software.
6 //
7 // Permission is granted to anyone to use this software for any purpose,
8 // including commercial applications, and to alter it and redistribute it
9 // freely, subject to the following restrictions:
10 //
11 // 1. The origin of this software must not be misrepresented; you must not
12 // claim that you wrote the original software. If you use this software
13 // in a product, an acknowledgment in the product documentation would
14 // be appreciated but is not required.
15 //
16 // 2. Altered source versions must be plainly marked as such, and must not
17 // be misrepresented as being the original software.
18 //
19 // 3. This notice may not be removed or altered from any source
20 // distribution.
21 
22 #ifndef EVENT_CONSUMER_HPP_
23 #define EVENT_CONSUMER_HPP_
24 
25 #include "SEDNL/sednlfwd.hpp"
26 #include "SEDNL/Export.hpp"
27 #include "SEDNL/Exception.hpp"
28 #include "SEDNL/Types.hpp"
29 #include "SEDNL/Slot.hpp"
30 #include "SEDNL/ThreadHelp.hpp"
31 
32 #include <unordered_map>
33 #include <condition_variable>
34 #include <thread>
35 #include <mutex>
36 
37 namespace SedNL
38 {
39 
44 {
45 public:
47  :wake_up(false)
48  {};
49  std::mutex mutex;
50  bool wake_up;
51  std::condition_variable cv;
52 };
53 
54 template<typename... Args>
55 using SlotMap = std::unordered_map<std::string, Slot<Args...>>;
56 
61 class SEDNL_API EventConsumer
62 {
63 public:
65  EventConsumer();
66 
74  EventConsumer(EventListener &producer);
75 
77  ~EventConsumer();
78 
92  void set_producer(EventListener& producer) throw(EventException);
93 
97  void remove_producer() noexcept;
98 
107  void run() throw(EventException);
108 
110  void join();
111 
134  inline Slot<Connection&>& on_disconnect();
135 
147  inline Slot<TCPServer&>& on_server_disconnect();
148 
160  inline Slot<Connection&, const Event&>& on_event();
161 
173  inline Slot<Connection&, const Event&>& bind(std::string event_name);
174 
175 private:
176  ConsumerDescriptor m_descriptor;
177 
178  // Member data
179  EventListener* m_producer;
180  Slot<Connection&> m_on_disconnect_slot;
181  Slot<TCPServer&> m_on_server_disconnect_slot;
182  Slot<Connection&, const Event&> m_on_event_slot;
183  SlotMap<Connection&, const Event&> m_slots;
184 
185  std::thread m_thread;
186 
187  SafeType<bool> m_running;
188 
189  //
190  // Consumer implementation
191  //
192 
193  void run_init();
194  void run_imp();
195 
197  void clean_slots();
198 
200  void consume_events() noexcept;
201 
202  friend class EventListener;
203 };
204 
205 } // namespace SedNL
206 
207 #include "SEDNL/EventConsumer.ipp"
208 
209 #endif /* !EVENT_CONSUMER_HPP_ */
210 
287