Chatsync  bbffde5c0c672a526bdc83637acf66dc20a80fbf
hub.hpp
Go to the documentation of this file.
1 #pragma once
2 #include "message.hpp"
3 
4 #include <list>
5 #include <memory>
6 #include <queue>
7 #include <thread>
8 #include <mutex>
9 #include <condition_variable>
10 #include <atomic>
11 
12 namespace channeling {
13  class Channel;
14 }
15 
16 namespace Hub {
17 
18  typedef std::unique_ptr<channeling::Channel> chanPtr;
19 
24  class Hub {
25  private:
26  const std::string _name;
27  std::list<chanPtr> _inputChannels;
28  std::list<chanPtr> _outputChannels;
30  std::queue<messaging::message_ptr> _messages;
31  std::mutex _mutex;
32  std::condition_variable _cond;
34  std::unique_ptr<std::thread> _msgLoop;
35  std::atomic_bool _loopRunning;
40  void addInput(channeling::Channel * const);
41 
45  void addOutput(channeling::Channel * const);
46 
47  /*
48  * Queue operations
49  */
50 
56  const messaging::message_ptr popMessage();
57  void pushMessage(const messaging::message_ptr&& item);
58 
62  void msgLoop();
63 
67  std::shared_ptr<std::atomic<bool> > _alive;
68  public:
69  Hub(std::string const& name);
70  ~Hub() { _alive.reset(); };
71 
72  const std::string& name() const {return _name; };
73  std::shared_ptr<std::atomic<bool> > alive() const {return _alive; };
74 
78  void addChannel(channeling::Channel * const);
79 
85  void newMessage(const messaging::message_ptr&& msg);
86 
90  void activate();
91 
95  void deactivate();
96 
100  bool active() {return _loopRunning; };
101 
105  void tick();
106  };
107 }
std::list< chanPtr > _outputChannels
Definition: hub.hpp:28
std::list< chanPtr > _inputChannels
Definition: hub.hpp:27
const std::string & name() const
Definition: hub.hpp:72
Definition: hub.cpp:10
std::condition_variable _cond
Definition: hub.hpp:32
std::unique_ptr< channeling::Channel > chanPtr
Definition: hub.hpp:18
std::mutex _mutex
Definition: hub.hpp:31
const std::string _name
Definition: hub.hpp:26
~Hub()
Definition: hub.hpp:70
std::shared_ptr< const Message > message_ptr
Definition: message.hpp:36
Definition: channel.cpp:11
std::shared_ptr< std::atomic< bool > > alive() const
Definition: hub.hpp:73
std::atomic_bool _loopRunning
Definition: hub.hpp:35
Definition: channel.hpp:63
std::shared_ptr< std::atomic< bool > > _alive
Definition: hub.hpp:67
bool active()
Definition: hub.hpp:100
std::queue< messaging::message_ptr > _messages
Definition: hub.hpp:30
std::unique_ptr< std::thread > _msgLoop
Definition: hub.hpp:34