AppChat  0.5.0
TCP client-server caht application with boost::asio library.
channel.h
1 #ifndef CHANNEL_H
2 #define CHANNEL_H
3 
4 #include <unordered_map>
5 #include <mutex>
6 #include <deque>
7 #include "iroom.h"
8 #include "server/storage/database.h"
9 #include "server/log/logger.h"
10 
18 class Channel : public IRoom
19 {
20 public:
28  Channel(identifier_t room, database_ptr db) : channel_id(room)
29  {
30  if (db == nullptr) {
31  BOOST_LOG_TRIVIAL(info) << "Failed to load history. Database pointer is nullptr.";
32  } else {
33  history_room = db->get_history(channel_id);
34  BOOST_LOG_TRIVIAL(info) << "Create channel_id=" << channel_id;
35  }
36  }
37 
43  virtual void join(subscriber_ptr subscriber) override;
44 
50  virtual void leave(subscriber_ptr subscriber) override;
51 
56  virtual void notification(text_response_ptr response) override;
57 
58 
64  virtual identifier_t get_room_id() const override { return channel_id; }
65 
66  ~Channel() {
67  for(auto it:subscribers) {
68  leave(it.second);
69  }
70  }
71 private:
72  std::mutex mutex_subs;
73  std::unordered_map<identifier_t, subscriber_ptr> subscribers;
74 
75 
76  const identifier_t channel_id;
77 
78  std::deque<text_response_ptr> history_room;
79 };
80 
81 using channel_ptr = std::shared_ptr<Channel>;
82 #endif // CHANNEL_H
Channel(identifier_t room, database_ptr db)
Construct a new Channel.
Definition: channel.h:28
virtual void notification(text_response_ptr response) override
Notify all channel subscribers about new message.
Definition: channel.cpp:30
Channel class.
Definition: channel.h:18
virtual void leave(subscriber_ptr subscriber) override
Leave a user from channel.
Definition: channel.cpp:18
virtual identifier_t get_room_id() const override
Get the room id object.
Definition: channel.h:64
virtual void join(subscriber_ptr subscriber) override
Join a user to channel.
Definition: channel.cpp:4
Room interface.
Definition: iroom.h:14