-
Notifications
You must be signed in to change notification settings - Fork 0
/
room.cpp
39 lines (34 loc) · 1012 Bytes
/
room.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "guard.h"
#include "message.h"
#include "message_queue.h"
#include "user.h"
#include "room.h"
#include <iterator>
Room::Room(const std::string &room_name)
: room_name(room_name) {
// TODO: initialize the mutex
pthread_mutex_init(&lock, NULL);
}
Room::~Room() {
// TODO: destroy the mutex
pthread_mutex_destroy(&lock);
}
void Room::add_member(User *user) {
// TODO: add User to the room
Guard g(lock);
members.insert(user);
}
void Room::remove_member(User *user) {
// TODO: remove User from the room
Guard g(lock);
members.erase(user);
}
void Room::broadcast_message(const std::string &sender_username, const std::string &message_text) {
// TODO: send a message to every (receiver) User in the room
Guard g(lock);
for (UserSet::iterator it = members.begin(); it != members.end(); ++it) {
std::string content = room_name + ":" + sender_username + ":" + message_text;
Message* tbs_msg = new Message(TAG_DELIVERY, content);
(*it)->mqueue.enqueue(tbs_msg);
}
}