forked from rdkcentral/rdkservices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoomMaintainer.cpp
212 lines (157 loc) · 6.71 KB
/
RoomMaintainer.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2020 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Module.h"
#include "RoomMaintainer.h"
#include "RoomImpl.h"
namespace WPEFramework {
namespace Plugin {
SERVICE_REGISTRATION(RoomMaintainer, 1, 0);
/* virtual */ Exchange::IRoomAdministrator::IRoom* RoomMaintainer::Join(const string& roomId, const string& userId,
Exchange::IRoomAdministrator::IRoom::IMsgNotification* messageSink)
{
// Note: Nullptr message sink is allowed (e.g. for broadcast-only users).
RoomImpl* newRoomUser = nullptr;
_adminLock.Lock();
auto it(_roomMap.find(roomId));
if (it == _roomMap.end()) {
// Room not found, so create one, already emplacing the first user.
newRoomUser = Core::Service<RoomImpl>::Create<RoomImpl>(this, roomId, userId, messageSink);
it = _roomMap.emplace(roomId, std::list<RoomImpl*>({newRoomUser})).first;
TRACE(Trace::Information, (_T("Room Maintainer: Room '%s' created"), roomId.c_str()));
if (roomId.size() == 0) {
TRACE(Trace::Warning, (_T("Room Maintainer: Created a room with empty roomId")));
}
// Notify the observers about a new room.
for (auto& observer : _observers) {
observer->Created(roomId);
}
}
else {
// Room already created; try to add another user.
std::list<RoomImpl*>& users = (*it).second;
if (std::find_if(users.begin(), users.end(), [&userId](const RoomImpl* user) { return (user->UserId() == userId);}) == users.end()) {
newRoomUser = Core::Service<RoomImpl>::Create<RoomImpl>(this, roomId, userId, messageSink);
// Notify the room about a joining user.
// No point in sending the notification to the joining user as it cannot have its callback registered yet.
for (auto& user : users) {
user->UserJoined(userId);
}
users.push_back(newRoomUser);
}
else {
TRACE(Trace::Error, (_T("Room Maintainer: User '%s' has already joined room '%s'"),
userId.c_str(), roomId.c_str()));
}
}
if (newRoomUser) {
TRACE(Trace::Information, (_T("Room Maintainer: User '%s' has joined room '%s'"),
userId.c_str(), roomId.c_str()));
}
_adminLock.Unlock();
// May be nullptr if the user has already joined the room earlier.
return newRoomUser;
}
void RoomMaintainer::Exit(const RoomImpl* roomUser)
{
ASSERT(roomUser != nullptr);
_adminLock.Lock();
auto it(_roomMap.find(roomUser->RoomId()));
ASSERT(it != _roomMap.end());
if (it != _roomMap.end()) {
std::list<RoomImpl*>& users = (*it).second;
auto uit(std::find(users.begin(), users.end(), roomUser));
ASSERT(uit != users.end());
if (uit != users.end()) {
TRACE(Trace::Information, (_T("Room Maintainer: User '%s' is leaving room '%s'"),
roomUser->UserId().c_str(), roomUser->RoomId().c_str()));
// Notify the room members about a leaving user.
for (auto& user : users) {
user->UserLeft(roomUser->UserId());
}
users.erase(uit);
// Was it the last user?
if (users.size() == 0) {
_roomMap.erase(it);
TRACE(Trace::Information, (_T("Room Maintainer: Room '%s' has been destroyed"), roomUser->RoomId().c_str()));
// Notify the observers about the destruction of this room.
for (auto& observer : _observers) {
observer->Destroyed(roomUser->RoomId());
}
}
}
}
_adminLock.Unlock();
}
void RoomMaintainer::Notify(RoomImpl* roomUser)
{
ASSERT(roomUser != nullptr);
_adminLock.Lock();
auto it = _roomMap.find(roomUser->RoomId());
ASSERT(it != _roomMap.end());
if (it != _roomMap.end()) {
for (auto& user : (*it).second) {
roomUser->UserJoined(user->UserId());
}
}
_adminLock.Unlock();
}
void RoomMaintainer::Send(const string& message, RoomImpl* roomUser)
{
ASSERT(roomUser != nullptr);
_adminLock.Lock();
auto it(_roomMap.find(roomUser->RoomId()));
ASSERT(it != _roomMap.end());
if (it != _roomMap.end()) {
for (RoomImpl* user : (*it).second) {
user->MessageReceived(roomUser->UserId(), message);
}
}
_adminLock.Unlock();
}
/* virtual */ void RoomMaintainer::Register(INotification* sink)
{
ASSERT(sink != nullptr);
_adminLock.Lock();
// Make sure it's not registered multiple times.
ASSERT(std::find(_observers.cbegin(), _observers.cend(), sink) == _observers.cend());
_observers.push_back(sink);
sink->AddRef();
// Notify the caller about all rooms created to date.
for (auto const& room : _roomMap) {
sink->Created(room.first);
}
_adminLock.Unlock();
TRACE(Trace::Information, (_T("Room Maintainer: Registered a notification sink")));
}
/* virtual */ void RoomMaintainer::Unregister(const INotification* sink)
{
ASSERT(sink != nullptr);
_adminLock.Lock();
auto const it(std::find(_observers.cbegin(), _observers.cend(), sink));
// Make sure it was really registered.
ASSERT(it != _observers.cend());
if (it != _observers.cend()) {
(*it)->Release();
_observers.erase(it);
}
_adminLock.Unlock();
TRACE(Trace::Information, (_T("Room Maintainer: Unregistered a notification sink")));
}
} // namespace Plugin
} // namespace WPEFramework