forked from dedis/Dissent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGetMessagesService.cpp
84 lines (67 loc) · 2.25 KB
/
GetMessagesService.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
#include <QtCore>
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#include <QUrlQuery>
#endif
#include "Utils/Serialization.hpp"
#include "GetMessagesService.hpp"
namespace Dissent {
namespace Web {
const QString GetMessagesService::OFFSET_FIELD = "offset";
const QString GetMessagesService::COUNT_FIELD = "count";
const QString GetMessagesService::WAIT_FIELD = "wait";
void GetMessagesService::HandleRequest(QHttpRequest *request,
QHttpResponse *response)
{
QUrl url = request->url();
int total = m_message_list.count();
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
int urlItemOffset = url.queryItemValue(OFFSET_FIELD).toInt();
bool wait_flag = QVariant(url.queryItemValue(WAIT_FIELD)).toBool();
#else
int urlItemOffset = QUrlQuery(url).queryItemValue(OFFSET_FIELD).toInt();
bool wait_flag = QVariant(QUrlQuery(url).queryItemValue(WAIT_FIELD)).toBool();
#endif
if((urlItemOffset == total) && wait_flag) {
m_pending.append(ReqRep(request, response));
return;
}
int offset = qMax(qMin(urlItemOffset, total), 0);
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
int count = url.queryItemValue(COUNT_FIELD).toInt();
#else
int count = QUrlQuery(url).queryItemValue(COUNT_FIELD).toInt();
#endif
count = count < 0 || (total < offset + count) ? total : count + offset;
QList<QVariant> messages;
for(int idx = offset; idx < count; idx++) {
messages.append(m_message_list[idx]);
}
QVariantHash data;
data["total"] = total;
data["offset"] = offset;
data["messages"] = messages;
SendJsonResponse(response, data);
}
void GetMessagesService::HandleMessage(const QByteArray &data)
{
int offset = 0;
while(offset + 8 < data.size()) {
int length = Utils::Serialization::ReadInt(data, offset);
if(length < 0 || data.size() < offset + 8 + length) {
return;
}
int zeroes = Utils::Serialization::ReadInt(data, offset + 4);
if(zeroes == 0) {
QByteArray message = data.mid(offset + 8, length);
m_message_list.append(message);
}
offset += 8 + length;
}
QList<ReqRep> curr_pending(m_pending);
m_pending.clear();
foreach(ReqRep reqrep, curr_pending) {
HandleRequest(reqrep.first, reqrep.second);
}
}
}
}