-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgimmmmessage.cpp
163 lines (133 loc) · 4.19 KB
/
gimmmmessage.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
#include "gimmmmessage.h"
#include <sstream>
#define THROW_INVALID_ARGUMENT_EXCEPTION(errmsg) \
std::stringstream out; \
out << "Exception @ [File:" << __FILE__ \
<< ", Line:" << __LINE__ << ", errormsg:" \
<< errmsg << "]"; throw std::invalid_argument(out.str().c_str())
Message::Message(MessageType type)
:__messageType(type),
__sequenceId(0)
{
}
Message::~Message()
{
}
std::string Message::getMessageTypeString(MessageType type)
{
switch (type)
{
case MessageType::ACK:
return std::string("ACK");
case MessageType::DOWNSTREAM:
return std::string("DOWNSTREAM");
default:
{
std::stringstream err;
err << "Invalid message type [" << (int)type << "]found.";
THROW_INVALID_ARGUMENT_EXCEPTION(err.str());
}
}
}
AckMessage::AckMessage(const SequenceId_t& seqid)
:Message(MessageType::ACK)
{
setSequenceId(seqid);
}
AckMessage::~AckMessage()
{
}
void AckMessage::validateAck() const
{
if (getSequenceId() == 0)
{
std::stringstream err;
err << "Invalid ACK message. 'sequence_id' field cannot be zero.";
THROW_INVALID_ARGUMENT_EXCEPTION(err.str());
}
}
QJsonDocument AckMessage::toJson()
{
validateAck();
QJsonDocument ackmsg;
QJsonObject root;
root[msgfieldnames::SEQUENCE_ID] = (qint64)getSequenceId();
std::string msg_type = getMessageTypeString(getMessageType());
root[msgfieldnames::MESSAGE_TYPE] = msg_type.c_str();
ackmsg.setObject(root);
return ackmsg;
}
/*!
* \brief DataMessage::DataMessage
*/
DataMessage::DataMessage()
: Message(MessageType::DOWNSTREAM),
__timeToLive(10)
{
}
DataMessage::~DataMessage()
{
}
QJsonDocument DataMessage::toJson()
{
validate();
QJsonDocument msg;
QJsonObject root;
// pack gimmm header
std::string msg_type = getMessageTypeString(getMessageType());
root[msgfieldnames::MESSAGE_TYPE] = msg_type.c_str();
root[msgfieldnames::GROUP_ID] = getGroupId().c_str();
root[msgfieldnames::FCM_MESSAGE_ID] = getMessageId().c_str();
// pack fcm data
QJsonObject fcmdata;
fcmdata[fcmfieldnames::TO] = __to.c_str();
fcmdata[fcmfieldnames::MESSAGE_ID] = getMessageId().c_str();
fcmdata[fcmfieldnames::TIME_TO_LIVE] = __timeToLive;
if (!__collapseKey.empty())
fcmdata[fcmfieldnames::COLLAPSE_KEY]= __collapseKey.c_str();
if (!__priority.empty())
fcmdata[fcmfieldnames::PRIORITY] = __priority.c_str();
if (!__contentAvailable.isNull())
fcmdata[fcmfieldnames::CONTENT_AVAILABLE] = __contentAvailable.toBool();
if (!__mutableContent.isNull())
fcmdata[fcmfieldnames::MUTABLE_CONTENT] = __mutableContent.toBool();
if (!__deliveryRecieptRequested.isNull())
fcmdata[fcmfieldnames::DELIVER_RECIEPT_REQUESTED] = __deliveryRecieptRequested.toBool();
if (!__dryRun.isNull())
fcmdata[fcmfieldnames::DRY_RUN] = __dryRun.toBool();
if (!__data.empty())
fcmdata[fcmfieldnames::DATA] = __data;
root[msgfieldnames::FCM_DATA] = fcmdata;
msg.setObject(root);
return msg;
}
void DataMessage::validate()const
{
//std::cout << "Validate downstream mesage" << std::endl;
// validate gimmm header
if (getMessageType() == MessageType::UNKNOWN)
{
std::stringstream err;
err << "Invalid DOWNSTREAM message. Message type cannot be unknown.";
THROW_INVALID_ARGUMENT_EXCEPTION(err.str());
}
if (getMessageId().empty() == true)
{
std::stringstream err;
err << "Invalid DOWNSTREAM message. 'message_id' field cannot be empty.";
THROW_INVALID_ARGUMENT_EXCEPTION(err.str());
}
if (__to.empty() && __condition.empty())
{
std::stringstream err;
err << "Invalid DOWNSTREAM message. Both 'to' && 'condition' field cannot be empty.";
THROW_INVALID_ARGUMENT_EXCEPTION(err.str());
}
// max is 4 week as per FCM documentation.
if (__timeToLive < MIN_TTL || __timeToLive > MAX_TTL )
{
std::stringstream err;
err << "Invalid DOWNSTREAM message. Invalid TTL <" << __timeToLive << "> found.";
THROW_INVALID_ARGUMENT_EXCEPTION(err.str());
}
}