-
Notifications
You must be signed in to change notification settings - Fork 1
/
fcmconnection.cpp
651 lines (571 loc) · 18.7 KB
/
fcmconnection.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
#include "fcmconnection.h"
#include "macros.h"
#include "message.h"
#include <thread>
#include <chrono>
#include <sstream>
#include <iostream>
#include <QTimer>
#include <QJsonDocument>
#include <QJsonObject>
/*!
* \brief FcmConnection::FcmConnection
*/
FcmConnection::FcmConnection(int id)
:__state(FcmSessionState::UNKNOWN),
__expBoff(),
__connectionDrainingInProgress(false),
__id(id)
{
}
/*!
* \brief FcmConnection::~FcmConnection
*/
FcmConnection::~FcmConnection()
{
emit connectionShutdownStarted(__id);
if (__fcmSocket.state() == QAbstractSocket::ConnectedState)
{
//Socket still open. Send STREAM END before closing connection to FCM.
QByteArray arr("</stream:stream>");
__fcmSocket.write(arr);
std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME_BEFORE_DISCONNECT));
blockSignals(true);
__fcmSocket.close();
blockSignals(false);
}
emit connectionShutdownCompleted(__id);
}
/*!
* \brief FcmConnection::connectToFcm
* \param server_id
* \param server_key
* \param host
* \param port_no
*/
void FcmConnection::connectToFcm(
QString server_id,
QString server_key,
QString host,
quint16 port_no)
{
__fcmServerId = server_id;
__fcmServerKey = server_key;
__fcmHostAddress = host;
__fcmPortNo = port_no;
//fcm handle
__fcmSocket.setProtocol(QSsl::TlsV1_0);
connect(&__fcmSocket, SIGNAL(encrypted()), this, SLOT(socketEncrypted()), Qt::DirectConnection);
connect(&__fcmSocket, SIGNAL(disconnected()), this, SLOT(handleDisconnected()), Qt::DirectConnection);
//connect and print errors for debug. 'handleDisconnected' will be called eventually.
connect(&__fcmSocket, static_cast<void(QSslSocket::*)(const QList<QSslError> &)>(&QSslSocket::sslErrors),
[=](const QList<QSslError> &errors)
{
for ( auto&& i: errors)
{
emit connectionError(__id, i.errorString());
}
});
__fcmWriter.setDevice(&__fcmSocket);
connectToFirebase();
}
/*!
* \brief FcmConnection::connectToFirebase
*/
void FcmConnection::connectToFirebase()
{
//std::cout << "Connecting to FCM server..." << std::endl;
emit connectionStarted(__id);
__fcmSocket.connectToHostEncrypted(__fcmHostAddress, __fcmPortNo);
}
/*!
* \brief FcmConnection::socketEncrypted
*/
void FcmConnection::socketEncrypted()
{
emit connectionEstablished(__id);
//std::cout << "Connected to FCM server.Starting XMPP handshake. Opening stream..." << std::endl;
//socket is secured now, lets prepare to read messages.
connect(&__fcmSocket, SIGNAL(readyRead()), this, SLOT(handleReadyRead()), Qt::DirectConnection);
emit xmppHandshakeStarted(__id);
__fcmWriter.writeStartDocument();
__fcmWriter.writeStartElement("stream:stream");
__fcmWriter.writeAttribute("to", "gcm.googleapis.com");
__fcmWriter.writeAttribute("version", "1.0");
__fcmWriter.writeAttribute("xmlns", "jabber:client");
__fcmWriter.writeAttribute("xmlns:stream", "http://etherx.jabber.org/streams");
QByteArray arr(">");// Do we need to escape this??
__fcmSocket.write(arr);
}
/*!
* \brief FcmConnection::handleDisconnected
* If the disconnect arrives because of a connection draining in progress
* then emit 'connectionDrainingCompleted' otherwise emit 'connectionLost'
* and reconnect.
*/
void FcmConnection::handleDisconnected()
{
__state = FcmSessionState::UNKNOWN;
if ( __connectionDrainingInProgress == false)
{
emit connectionLost(__id);
int msec = __expBoff.next();
QTimer::singleShot(msec, this, &FcmConnection::connectToFirebase);
}else
{
emit connectionDrainingCompleted(__id);
}
}
/*!
* \brief FcmConnection::handleReadyRead
*/
void FcmConnection::handleReadyRead()
{
QSslSocket* socket = (QSslSocket*)sender();
QByteArray bytes;
bytes = socket->readAll();
if (bytes.length() == 1 && isspace(bytes.at(0)))
{
//std::cout << "RX[1]: Recieved keepalive from FCM." << std::endl;
emit heartbeatRecieved(__id);
return;
}
// Recieved regular message. We can start parsing.
// TODO implement an option to print this to a file.
//std::cout << std::endl << "RX["<< bytes.length() <<"]:" << bytes.toStdString() << std::endl;
__fcmReader.addData(bytes);
try
{
parseXml();
}
catch(std::exception& err)
{
//std::cout << "Exception caught. Error[" << err.what() << std::endl;
emit connectionError(__id, err.what());
}
catch (...)
{
std::stringstream err;
err << "unknown exception caught.";
emit connectionError(__id, err.str().c_str());
}
}
/*!
* \brief FcmConnection::parseXml
*/
void FcmConnection::parseXml()
{
//std::cout << "Parsing xml..." << std::endl;
while (__fcmReader.readNextStartElement())
{
handleStartElement();
}
// check if we got an end of stream stanza from FCM.
handleOtherElement();
// Log if there was an unexpected error.
if (__fcmReader.error())
{
if (__fcmReader.error() != QXmlStreamReader::PrematureEndOfDocumentError)
{
//std::cout << __fcmReader.errorString().toStdString() << std::endl;
emit connectionError(__id, __fcmReader.errorString());
}
}
}
/*!
* \brief FcmConnection::handleOtherElement
*/
void FcmConnection::handleOtherElement()
{
while (!__fcmReader.atEnd())
{
__fcmReader.readNext();
if (__fcmReader.isEndDocument())
{
handleEndOfStream();
}
}
}
/*!
* \brief FcmConnection::handleEndOfStream
* Section 4.4 RFC 6120
*/
void FcmConnection::handleEndOfStream()
{
//std::cout << "Recieved 'END STREAM' from FCM. Closing stream..." << std::endl;
__fcmWriter.writeEndElement();
__fcmWriter.writeEndDocument();
emit streamClosed(__id);
}
/*!
* \brief FcmConnection::handleStartElement
*/
void FcmConnection::handleStartElement()
{
if (__fcmReader.name() == "stream" && __fcmReader.namespaceUri() == STREAM_NSPACE_URI )
{
handleStartStream();
}
else if (__fcmReader.name() == "features" && __fcmReader.namespaceUri() == STREAM_NSPACE_URI)
{
handleFeatures();
}
else if (__fcmReader.name() == "iq" && __fcmReader.attributes().value("type") == "result" &&
__fcmReader.namespaceUri() == DEFAULT_NSPACE_URI)
{
handleIq();
}
else if (__fcmReader.name() == "success" && __fcmReader.namespaceUri() == SASL_NSPACE_URI)
{
handleSaslSuccess();
}
else if (__fcmReader.name() == "failure" && __fcmReader.namespaceUri() == SASL_NSPACE_URI)
{
handleSaslFailure();
}
else if (__fcmReader.name() == "message" && __fcmReader.namespaceUri() == DEFAULT_NSPACE_URI)
{
handleMessage();
}
else
{
std::stringstream err;
err << "ERROR:Unknown stanza recieved from FCM, name:["
<< __fcmReader.name().toString().toStdString() << "]";
THROW_INVALID_ARGUMENT_EXCEPTION(err.str());
}
}
/*!
* \brief FcmConnection::handleStartStream
*/
void FcmConnection::handleStartStream()
{
/*
std::cout << "Recieved new stream header from FCM, id:"
<< __fcmReader.attributes().value("id").toString().toStdString()
<< std::endl;
*/
}
/*!
* \brief FcmConnection::handleFeatures
*/
void FcmConnection::handleFeatures()
{
//std::cout << "Recieved 'features' stanza from FCM server." << std::endl;
while (__fcmReader.readNextStartElement())
{
if (__fcmReader.name() == "mechanisms" && __fcmReader.namespaceUri() == SASL_NSPACE_URI)
readMechanisms();
else if (__fcmReader.name() == "bind" && __fcmReader.namespaceUri() == BIND_NSPACE_URI)
readBind();
else if (__fcmReader.name() == "session" && __fcmReader.namespaceUri() == SESSION_NSPACE_URI)
readSession();
else
__fcmReader.skipCurrentElement();
}
}
/*!
* \brief FcmConnection::readMechanisms
*/
void FcmConnection::readMechanisms()
{
//std::cout << " Recieved supported SASL auth mechanism list from FCM server." << std::endl;
while (__fcmReader.readNextStartElement()) {
if (__fcmReader.name() == "mechanism")
readMechanism();
else
__fcmReader.skipCurrentElement();
}
QTimer::singleShot(10, this, &FcmConnection::sendAuthenticationInfo);
}
/*!
* \brief FcmConnection::readMechanism
*/
void FcmConnection::readMechanism()
{
std::string method = __fcmReader.readElementText().toStdString();
// std::cout << " Recieved sasl auth method:" << method << std::endl;
__authMethodVect.push_back(method);
}
/*!
* \brief FcmConnection::readBind
*/
void FcmConnection::readBind()
{
__fcmReader.readElementText();
//std::cout << " Recieved xmpp 'bind' feature from FCM server." << std::endl;
QTimer::singleShot(10, this, &FcmConnection::sendIQBind);
}
/*!
* \brief FcmConnection::readSession
*/
void FcmConnection::readSession()
{
__fcmReader.readElementText();
//std::cout << " Recieved xmpp 'session' feature from FCM server." << std::endl;
}
/*!
* \brief FcmConnection::handleSaslSuccess
* 4.3.3 On successful negotiation of a feature that necessitates a stream
* restart, both parties MUST consider the previous stream to be
* replaced but MUST NOT send a closing </stream> tag and MUST NOT
* terminate the underlying TCP connection; instead, the parties MUST
* reuse the existing connection, which might be in a new state (e.g.,
* encrypted as a result of TLS negotiation). The initiating entity
* then MUST send a new initial stream header, which SHOULD be preceded
* by an XML declaration as described under Section 11.5. When the
* receiving entity receives the new initial stream header, it MUST
* generate a new stream ID (instead of reusing the old stream ID)
* before sending a new response stream header (which SHOULD be preceded
* by an XML declaration as described under Section 11.5).
*/
void FcmConnection::handleSaslSuccess()
{
__fcmReader.readElementText();
//std::cout << "Recieved xmpp-sasl SUCCESS from FCM server." << std::endl;
emit saslSucess(__id);
QTimer::singleShot(10, this, &FcmConnection::startNewStream);
}
/*!
* \brief FcmConnection::handleSaslFailure
*/
void FcmConnection::handleSaslFailure()
{
__fcmReader.readNextStartElement();
QString failureReason = __fcmReader.name().toString();
std::stringstream err;
err << "Recieved xmpp-sasl FAILURE from FCM server. Reason:"
<< failureReason.toStdString() << std::endl;
emit connectionError(__id, err.str().c_str());
// TODO try another authentication method ??
}
/*!
* \brief FcmConnection::handleIq
*/
void FcmConnection::handleIq()
{
//std::cout << "Recieved IQ stanza from FCM server." << std::endl;
//std::cout << "Bind attr:" << __fcmReader.attributes().value("type").toString().toStdString() << std::endl;
while (__fcmReader.readNextStartElement())
{
if (__fcmReader.name() == "bind" &&
__fcmReader.namespaceUri() == BIND_NSPACE_URI)
{
readIQBindResult();
}
else
__fcmReader.skipCurrentElement();
}
}
/*!
* \brief FcmConnection::readIQBindResult
*/
void FcmConnection::readIQBindResult()
{
//std::cout << "Recieved IQ Bind result from FCM server." << std::endl;
while (__fcmReader.readNextStartElement()) {
if (__fcmReader.name() == "jid")
readJid();
else
__fcmReader.skipCurrentElement();
}
}
/*!
* \brief FcmConnection::readJid
*/
void FcmConnection::readJid()
{
//std::string jid = __fcmReader.readElementText().toStdString();
//std::cout << "Recieved JID from FCM server:"<< jid << std::endl;
__state = FcmSessionState::AUTHENTICATED;
__expBoff.reset();
emit sessionEstablished(__id);
}
/*!
* \brief FcmConnection::handleMessage
*/
void FcmConnection::handleMessage()
{
//std::cout << "Parsing new XMPP stanza..." << std::endl;
while (__fcmReader.readNextStartElement())
{
if (__fcmReader.name() == "gcm" && __fcmReader.namespaceUri() == GCM_NSPACE_URI)
{
QString json = __fcmReader.readElementText();
handleFcmMessage(json);
}
else
__fcmReader.skipCurrentElement();
}
}
/*!
* \brief FcmConnection::handleFcmMessage
* \param json_str
*/
void FcmConnection::handleFcmMessage(const QString& json_str)
{
//std::cout << "-------------------START NEW FCM MESSAGE ----------------------" << std::endl;
QByteArray bytes(json_str.toStdString().c_str());
QJsonParseError parseErr;
QJsonDocument jdoc = QJsonDocument::fromJson(bytes, &parseErr);
if (jdoc.isNull())
{
std::stringstream err;
err << "ERROR: Failed to parse recieved msg, error["
<< parseErr.errorString().toStdString()<< "]";
emit connectionError(__id, err.str().c_str());
return;
}
//std::cout << "Message:" << std::endl;
//PRINT_JSON_DOC(std::cout, jdoc);
// All good so far.
if ( jdoc.object().contains("message_type"))
{
// this is either an 'ack'/'nack'/'control'
std::string msg_type = jdoc.object().value(fcmfieldnames::MESSAGE_TYPE).toString().toStdString();
if ( msg_type == "ack")
{
emit newAckMessage(__id, jdoc);
}
else if ( msg_type == "nack")
{
emit newNackMessage(__id, jdoc);
}
else if ( msg_type == "control")
{
handleControlMessage(jdoc);
}else if (msg_type == "receipt")
{
handleReceiptMessage(jdoc);
}else
{
std::stringstream err;
err << "Unknown message type<" << msg_type << "> found.";
THROW_INVALID_ARGUMENT_EXCEPTION(err.str());
}
}else
{
// this is a new message from a client.
emit newMessage(__id, jdoc);
}
//std::cout << "-------------------END NEW FCM MESSAGE ----------------------" << std::endl;
}
/*!
* \brief Application::handleControlMessage
* Control messages @ https://firebase.google.com/docs/cloud-messaging/server
*
* Periodically, CCS needs to close down a connection to perform load balancing.
* Before it closes the connection, CCS sends a CONNECTION_DRAINING message to
* indicate that the connection is being drained and will be closed soon.
* "Draining" refers to shutting off the flow of messages coming into a connection,
* but allowing whatever is already in the pipeline to continue. When you receive
* a CONNECTION_DRAINING message, you should immediately begin sending messages to
* another CCS connection, opening a new connection if necessary. You should,
* however, keep the original connection open and continue receiving messages that
* may come over the connection (and ACKing them)—CCS handles initiating a connection
* close when it is ready.
* The CONNECTION_DRAINING message looks like this:
*
* <message>
* <data:gcm xmlns:data="google:mobile:data">
* {
* "message_type":"control"
* "control_type":"CONNECTION_DRAINING"
* }
* </data:gcm>
* </message>
* CONNECTION_DRAINING is currently the only control_type supported.
*
* \param control_msg
*/
void FcmConnection::handleControlMessage(const QJsonDocument& control_msg)
{
//std::cout << "Recieved 'control' message from FCM..." << std::endl;
QString control_type = control_msg.object().value(fcmfieldnames::CONTROL_TYPE).toString();
if ( control_type == "CONNECTION_DRAINING" )
{
__connectionDrainingInProgress = true;
emit connectionDrainingStarted(__id);
}
else
{
std::stringstream err;
err << "Unknown control message recieved from FCM.";
QString qerr(err.str().c_str());
emit connectionError(__id, qerr);
}
}
/*!
* \brief FcmConnection::handleReceiptMessage
* \param receipt_msg
*/
void FcmConnection::handleReceiptMessage(const QJsonDocument& receipt_msg)
{
emit newReceiptMessage(__id, receipt_msg);
}
/*!
* \brief FcmConnection::sendAuthenticationInfo
*/
void FcmConnection::sendAuthenticationInfo()
{
//std::cout << std::endl << "TX:Sending authen info to FCM..." << std::endl;
__fcmWriter.writeStartElement("auth");
__fcmWriter.writeAttribute("mechanism", "PLAIN");
__fcmWriter.writeAttribute("xmlns","urn:ietf:params:xml:ns:xmpp-sasl");
QByteArray authenString(__fcmServerId.toStdString().c_str());
authenString.append("@gcm.googleapis.com");
authenString.insert(0, '\0');
int size = authenString.size();
authenString.insert(size, '\0');
QByteArray passwd(__fcmServerKey.toStdString().c_str());
size = authenString.size();
authenString.insert(size, passwd);
__fcmWriter.writeCharacters(authenString.toBase64().data());
__fcmWriter.writeEndElement();
}
/*!
* \brief FcmConnection::startNewStream
*/
void FcmConnection::startNewStream()
{
//std::cout << std::endl << "TX:Starting new stream...." << std::endl;
__fcmWriter.writeStartElement("stream:stream");
__fcmWriter.writeAttribute("to", "gcm.googleapis.com");
__fcmWriter.writeAttribute("version", "1.0");
__fcmWriter.writeAttribute("xmlns", "jabber:client");
__fcmWriter.writeAttribute("xmlns:stream", "http://etherx.jabber.org/streams");
// QT stream reader doesn't let me write '>'. So let's write it directly to
// the socket.
QByteArray arr(">");
__fcmSocket.write(arr);
emit streamOpened(__id);
}
/*!
* \brief FcmConnection::sendIQBind
*/
void FcmConnection::sendIQBind()
{
//std::cout << std::endl << "TX:Sending IQ bind to FCM..." << std::endl;
__fcmWriter.writeStartElement("iq");
__fcmWriter.writeAttribute("type","set");
__fcmWriter.writeStartElement("bind");
__fcmWriter.writeAttribute("xmlns", "urn:ietf:params:xml:ns:xmpp-bind");
__fcmWriter.writeEndElement();
__fcmWriter.writeEndElement();
}
/*!
* \brief Application::sendMessage
* \param data - This is a fully formed FCM message.
*/
void FcmConnection::handleSendMessage(const QJsonDocument &data)
{
//std::cout << "Sending Message to FCM:" << std::endl;
//PRINT_JSON_DOC(std::cout, data);
__fcmWriter.writeStartElement("message");
__fcmWriter.writeAttribute("id", "");
__fcmWriter.writeStartElement("gcm");
__fcmWriter.writeAttribute("xmlns", GCM_NSPACE_URI);
__fcmWriter.writeCharacters(data.toJson());
__fcmWriter.writeEndElement();
__fcmWriter.writeEndElement();
}