forked from CPP-Final-Project/Chat_Server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoomStorage_Service.cpp
More file actions
377 lines (294 loc) · 11.7 KB
/
Copy pathRoomStorage_Service.cpp
File metadata and controls
377 lines (294 loc) · 11.7 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "RoomStorage_Service.h"
#include "DTOMessage.h"
#include "DTORoom.h"
void RoomStorage_Service::init()
{
if (!getInstance()->is_started()) {
getInstance()->make_started();
shp_instance->downloadRoomsFromDB();
}
else
{
PLOGW << "Service already started";
}
}
bool RoomStorage_Service::is_started()
{
return started;
}
void RoomStorage_Service::make_started()
{
started = true;
PLOGI << "RoomStorage_Service started.";
}
void RoomStorage_Service::addLikeToMessage(const quint32& room_id_, const QUuid& message_id_, const QDateTime& message_datetime_, const QString& user_login_, const bool like_dislike_)
{
QtConcurrent::run([](const quint32& room_id_, const QUuid& message_id_, const QDateTime& message_datetime_, const QString& user_login_, const bool like_dislike_) {
LocalStorage_Service::getInstance()->addLikeToMessage(room_id_, message_id_, message_datetime_, user_login_, like_dislike_);
RatingCounter_Service::getInstance()->addRating(user_login_, like_dislike_);
}, room_id_, message_id_, message_datetime_, user_login_, like_dislike_);
}
void RoomStorage_Service::downloadRoomsFromDB()
{
auto future = QtConcurrent::run([this]() {
auto dbrooms = DBService::RoomRepository::getAllActiveRooms();
for (auto& room : dbrooms) {
rooms_storage.insert(room->getId(), QSharedPointer<SrvRoom>(new SrvRoom(
room->getId(),
room->getName(),
room->getDescription(),
room->getTopicId(),
room->getTopicName(),
room->isPrivate(),
room->getPassword(),
room->isDeleted()
), &QObject::deleteLater));
}
PLOGD << "Uploaded rooms: " + QString::number(rooms_storage.size());
});
}
RoomStorage_Service* RoomStorage_Service::getInstance()
{
try
{
if (shp_instance == nullptr) {
shp_instance = QSharedPointer<RoomStorage_Service>(new RoomStorage_Service, &QObject::deleteLater);
PLOGD << "RoomStorage_Service instance created";
}
return shp_instance.get();
}
catch (const QException& ex)
{
PLOGE << "Cannot get instance" << ex.what();
return nullptr;
}
}
QList<QSharedPointer<SrvRoom>> RoomStorage_Service::getRooms()
{
return rooms_storage.values();
}
QSharedPointer<SrvRoom> RoomStorage_Service::getRoom(const qint32& room_id_)
{
if (rooms_storage.contains(room_id_)) {
return rooms_storage.value(room_id_);
}
return nullptr;
}
void RoomStorage_Service::uploadRoomToDB(const QSharedPointer<SrvRoom>& shp_new_room_) const
{
if (shp_new_room_ != nullptr) {
DBService::RoomRepository::createRoom( DBEntity::DBRoom(shp_new_room_));
}
}
QFuture<QSharedPointer<SrvRoom>> RoomStorage_Service::createRoom(QSharedPointer<SrvRoom>& shp_new_room_)
{
return QtConcurrent::run([&, shp_new_room_]() -> QSharedPointer<SrvRoom> {
try
{
if (!rooms_storage.contains(shp_new_room_->getId())) {
auto topic_id = DBService::RoomRepository::getTopicIdByTopicName(shp_new_room_->getTopicName());
shp_new_room_->setTopicId(1);
uploadRoomToDB(shp_new_room_);
addRoom(shp_new_room_);
return shp_new_room_;
}
else {
PLOGE << "Room already exist! " + shp_new_room_->getName();
return nullptr;
}
}
catch (const QException& ex)
{
PLOGE << "Cannot create room in roomStorage" << ex.what();
}
});
}
void RoomStorage_Service::addMessageToRoom(const qint32& room_id_, const QSharedPointer<User_Message>& message_)
{
QtConcurrent::run([&](const qint32& room_id_, const QSharedPointer<User_Message>& message_) {
try
{
Q_ASSERT(rooms_storage.contains(room_id_));
if (rooms_storage.contains(room_id_)) {
rooms_storage.value(room_id_)->addMessage(message_);
LocalStorage_Service::getInstance()->addMessage(message_, room_id_);
}
else PLOGE << "Room doesn't exist. Id: " + room_id_;
}
catch (const QException& ex)
{
PLOGE << "Cannot add message to roomStorage" << ex.what();
}
}, room_id_, message_);
}
void RoomStorage_Service::addRoom(const QSharedPointer<SrvRoom>& shp_room_)
{
rooms_storage.insert(shp_room_->getId(), shp_room_);
}
RoomStorage_Service::RoomStorage_Service(QObject* parent_) : QObject(parent_) {};
bool RoomStorage_Service::addConnecntedUserToRoom(const qint32& room_id_, const QSharedPointer<SrvUser>& shp_user_)
{
if (rooms_storage.contains(room_id_)) {
return rooms_storage.value(room_id_)->connectUser(shp_user_);
}
else {
PLOGE << "Room doesn't exist. Id: " + room_id_;
return false;
}
}
bool RoomStorage_Service::deleteConnecntedUserFromRoom(const qint32& room_id_, const QSharedPointer<SrvUser>& shp_user_)
{
if (rooms_storage.contains(room_id_)) {
return rooms_storage.value(room_id_)->disconnectUser(shp_user_);
}
else {
PLOGE << "Room doesn't exist. Id: " + room_id_;
return false;
}
}
void RoomStorage_Service::addMessagesToRoom(const qint32& room_id_, const QSet<QSharedPointer<User_Message>>& messages_)
{
if (rooms_storage.contains(room_id_)) {
rooms_storage.value(room_id_)->addMessages(messages_);
}
else PLOGE << "Room doesn't exist. Id: " + room_id_;
}
void RoomStorage_Service::getMessagesFromDB(const quint32& room_id_, const QDateTime& from_, const QDateTime& to_)
{
try
{
QSet<QSharedPointer<DBEntity::DBMessage>> messages;
auto files = LocalStorage_Service::getInstance()->searchForFiles(from_, to_, room_id_);
foreach(const auto & file, files)
{
messages.unite(LocalStorage_Service::readMessagesFromDB(file));
}
foreach(const auto & message, messages) {
rooms_storage[room_id_]->addMessage(DTOModel::DTOMessage::createSrvFromDB(message));
}
}
catch (const QException& ex)
{
PLOGE << "Cannot get messages from DB" << ex.what();
}
}
QSet<QSharedPointer<User_Message>> RoomStorage_Service::getMessagesFromDB(const quint32& room_id_, const QDateTime& date_, const bool& from_to_, const quint32 pool_size_)
{
try
{
QList<QSharedPointer<User_Message>> temp;
auto date = date_;
while (true)
{
auto file = LocalStorage_Service::getInstance()->searchForFiles(date, room_id_, from_to_);
if(file == "")
{
break;
}
QSet<QSharedPointer<DBEntity::DBMessage>> messages;
messages.unite(LocalStorage_Service::readMessagesFromDB(file));
auto it = messages.begin();
quint32 k = 0;
while (it != messages.end() && k < pool_size_) {
rooms_storage[room_id_]->addMessage(DTOModel::DTOMessage::createSrvFromDB(*it));
temp.append(DTOModel::DTOMessage::createSrvFromDB(*it));
k++;
}
if(k >= pool_size_)
{
break;
}
if(from_to_)
{
date = temp.last()->getDateTime().addSecs(60);
}
date = temp.first()->getDateTime().addSecs(-60);
}
return QSet(temp.begin(), temp.end());
}
catch (const QException& ex)
{
PLOGE << "Cannot get messages from DB" << ex.what();
throw;
}
}
void RoomStorage_Service::getMessagesFromLocalStorage(const quint32& room_id_, const QDateTime& from_, const QDateTime& to_)
{
QSet<QSharedPointer<User_Message>> messages;
messages.unite(LocalStorage_Service::getInstance()->getMessages(from_, to_, room_id_));
rooms_storage[room_id_]->addMessages(messages);
}
QSet<QSharedPointer<User_Message>> RoomStorage_Service::getMessagesFromLocalStorage(const quint32& room_id_, const QDateTime& time_, bool from_to_, const quint32& pool_size_)
{
QSet<QSharedPointer<User_Message>> messages;
messages.unite(LocalStorage_Service::getInstance()->getMessages(room_id_, time_, from_to_, pool_size_));
rooms_storage[room_id_]->addMessages(messages);
return messages;
}
QSet<QSharedPointer<User_Message>> RoomStorage_Service::getMessages(const quint32& room_id_, const QDateTime& from_, const QDateTime& to_)
{
QSet<QSharedPointer<User_Message>> messages;
if (!rooms_storage.contains(room_id_))
{
auto future = DBService::RoomRepository::getRoomById(room_id_);
future.waitForFinished();
auto room = future.result();
if (room != nullptr) {
addRoom(DTOModel::DTORoom::createSrvRoomFromDB(*room));
}
else {
PLOGE << "Room id: " + QString::number(room_id_) + " doesn't exist";
return messages;
}
}
QList<std::function<void(const quint32&, const QDateTime&, const QDateTime&)>> delegates;
delegates.append([this](const quint32& room_id_, const QDateTime& from_, const QDateTime& to_) {
getMessagesFromLocalStorage(room_id_, from_, to_);
});
delegates.append([this](const quint32& room_id_, const QDateTime& from_, const QDateTime& to_) {
getMessagesFromDB(room_id_, from_, to_);
});
QtConcurrent::blockingMap(delegates, [&](std::function<void(const quint32&, const QDateTime&, const QDateTime&)> delegate) {
delegate(room_id_, from_, to_);
PLOGF << "DRATUTY!!!";
});
messages.unite(rooms_storage[room_id_]->getMessages(from_, to_));
return messages;
//emit messageRetrieved(QList(messages.begin(), messages.end()));
}
QFuture<QSet<QSharedPointer<User_Message>>> RoomStorage_Service::getMessages(const quint32& room_id_, const QDateTime& time_, const quint32& pool_size_, const bool from_to_)
{
return QtConcurrent::run([&](const quint32& room_id_, const QDateTime& time_, const bool from_to_, const quint32& pool_size_) {
auto result = getRoom(room_id_)->getMessages(time_, from_to_, pool_size_);
if (result.size() == pool_size_) {
return result;
}
QDateTime new_time;
if (from_to_) new_time = result.values().first()->getDateTime();
else new_time = result.values().last()->getDateTime();
auto new_pool_size = pool_size_ - result.size();
result.unite(getMessagesFromLocalStorage(room_id_, new_time, from_to_, new_pool_size));
auto temp_list = result.values();
std::sort(temp_list.begin(), temp_list.end(), [](const QSharedPointer<User_Message>& a, const QSharedPointer<User_Message>& b) {
return a->getDateTime() > b->getDateTime();
});
result = QSet<QSharedPointer<User_Message>>(temp_list.begin(), temp_list.end());
if (result.size() == pool_size_) {
return result;
}
if (from_to_) new_time = temp_list.first()->getDateTime();
else new_time = temp_list.last()->getDateTime();
new_pool_size = pool_size_ - result.size();
result.unite(getMessagesFromDB(room_id_, new_time, from_to_, new_pool_size));
temp_list = result.values();
std::sort(temp_list.begin(), temp_list.end(), [](const QSharedPointer<User_Message>& a, const QSharedPointer<User_Message>& b) {
return a->getDateTime() > b->getDateTime();
});
QSet<QSharedPointer<User_Message>> result_2 = QSet<QSharedPointer<User_Message>>(temp_list.begin(), temp_list.end());
for (auto& var : temp_list)
{
result_2.insert(var);
}
return result_2;
},room_id_, time_, from_to_, pool_size_);
}