-
Notifications
You must be signed in to change notification settings - Fork 21
/
strophe.stream-management.js
283 lines (229 loc) · 7.68 KB
/
strophe.stream-management.js
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
/**
* StropheJS - Stream Management XEP-0198
*
* This plugin implements stream mangemament ACK capabilities of the specs XEP-0198.
* Note: Resumption is not supported in this current implementation.
*
* Reference: http://xmpp.org/extensions/xep-0198.html
*
* @class streamManagement
*/
Strophe.addConnectionPlugin('streamManagement', {
/**
* @property {Boolean} logging: Set to true to enable logging regarding out of sync stanzas.
*/
logging: true,
/**
* @property {Boolean} returnWholeStanza: Set to true to return the acknowledged stanzas, otherwise only return its ID.
*/
returnWholeStanza: false,
/**
* @property {Boolean} autoSendCountOnEveryIncomingStanza: Set to true to send an 'a' response after every stanza.
* @default false
* @public
*/
autoSendCountOnEveryIncomingStanza: false,
/**
* @property {Integer} requestResponseInterval: Set this value to send a request for counter on very interval
* number of stanzas sent. Set to 0 to disable.
* @default 5
* @public
*/
requestResponseInterval: 5,
/**
* @property {Pointer} _c: Strophe connection instance.
* @private
*/
_c: null,
/**
* @property {String} _NS XMPP Namespace.
* @private
*/
_NS: 'urn:xmpp:sm:3',
/**
* @property {Boolean} _isStreamManagementEnabled
* @private
*/
_isStreamManagementEnabled: false,
/**
* @property {Integer} _serverProcesssedStanzasCounter: Keeps count of stanzas confirmed processed by the server.
* The server is the source of truth of this value. It is the 'h' attribute on the latest 'a' element received
* from the server.
* @private
*/
_serverProcesssedStanzasCounter: null,
/**
* @property {Integer} _clientProcessedStanzasCounter: Counter of stanzas received by the client from the server.
* Client is the source of truth of this value. It is the 'h' attribute in the 'a' sent from the client to
* the server.
* @private
*/
_clientProcessedStanzasCounter: null,
/**
* @property {Integer} _clientSentStanzasCounter
* @private
*/
_clientSentStanzasCounter: null,
/**
* Stores a reference to Strophe connection xmlOutput function to wrap counting functionality.
* @method _originalXMLOutput
* @type {Handler}
* @private
*/
_originalXMLOutput: null,
/**
* @property {Handler} _requestHandler: Stores reference to handler that process count request from server.
* @private
*/
_requestHandler: null,
/**
* @property {Handler} _incomingHandler: Stores reference to handler that processes incoming stanzas count.
* @private
*/
_incomingHandler: null,
/**
* @property {Integer} _requestResponseIntervalCount: Counts sent stanzas since last response request.
*/
_requestResponseIntervalCount: 0,
/**
* @property {Queue} _unacknowledgedStanzas: Maintains a list of packet ids for stanzas which have yet to be acknowledged.
*/
_unacknowledgedStanzas: [],
/**
* @property {Array} _acknowledgedStanzaListeners: Stores callbacks for each stanza acknowledged by the server.
* Provides the packet id of the stanza as a parameter.
* @private
*/
_acknowledgedStanzaListeners: [],
addAcknowledgedStanzaListener(listener) {
this._acknowledgedStanzaListeners.push(listener);
},
enable: function() {
this._c.send($build('enable', {xmlns: this._NS, resume: false}));
this._c.flush();
this._c.pause();
},
requestAcknowledgement: function() {
this._requestResponseIntervalCount = 0;
this._c.send($build('r', { xmlns: this._NS }));
},
getOutgoingCounter: function() {
return this._clientSentStanzasCounter;
},
getIncomingCounter: function() {
return this._clientProcessedStanzasCounter;
},
init: function(conn) {
this._c = conn;
Strophe.addNamespace('SM', this._NS);
// Storing original xmlOutput function to use additional logic
this._originalXMLOutput = this._c.xmlOutput;
this._c.xmlOutput = this.xmlOutput.bind(this);
},
statusChanged: function (status) {
if (status === Strophe.Status.CONNECTED || status === Strophe.Status.DISCONNECTED) {
this._serverProcesssedStanzasCounter = 0;
this._clientProcessedStanzasCounter = 0;
this._clientSentStanzasCounter = 0;
this._isStreamManagementEnabled = false;
this._requestResponseIntervalCount = 0;
this._unacknowledgedStanzas = [];
if (this._requestHandler) {
this._c.deleteHandler(this._requestHandler);
}
if (this._incomingHandler) {
this._c.deleteHandler(this._incomingHandler);
}
this._requestHandler = this._c.addHandler(this._handleServerRequestHandler.bind(this), this._NS, 'r');
this._incomingHandler = this._c.addHandler(this._incomingStanzaHandler.bind(this));
}
},
/**
* This method overrides the send method implemented by Strophe.Connection
* to count outgoing stanzas
*
* @method Send
* @public
*/
xmlOutput: function(elem) {
var child;
for (var i = 0; i < elem.children.length; i++) {
child = elem.children[i];
if (Strophe.isTagEqual(child, 'iq') ||
Strophe.isTagEqual(child, 'presence') ||
Strophe.isTagEqual(child, 'message')) {
this._increaseSentStanzasCounter(child);
}
}
return this._originalXMLOutput.call(this._c, elem);
},
_incomingStanzaHandler: function(elem) {
if (Strophe.isTagEqual(elem, 'enabled') && elem.getAttribute('xmlns') === this._NS) {
this._isStreamManagementEnabled = true;
this._c.resume();
}
if (Strophe.isTagEqual(elem, 'iq') || Strophe.isTagEqual(elem, 'presence') || Strophe.isTagEqual(elem, 'message')) {
this._increaseReceivedStanzasCounter();
if (this.autoSendCountOnEveryIncomingStanza) {
this._answerProcessedStanzas();
}
}
if (Strophe.isTagEqual(elem, 'a')) {
var handledCount = parseInt(elem.getAttribute('h'));
this._handleAcknowledgedStanzas(handledCount, this._serverProcesssedStanzasCounter);
this._serverProcesssedStanzasCounter = handledCount;
if (this.requestResponseInterval > 0) {
this._requestResponseIntervalCount = 0;
}
}
return true;
},
_handleAcknowledgedStanzas: function(reportedHandledCount, lastKnownHandledCount) {
var delta = reportedHandledCount - lastKnownHandledCount;
if (delta < 0) {
this._throwError('New reported stanza count lower than previous. New: ' + reportedHandledCount + ' - Previous: ' + lastKnownHandledCount);
}
if (delta > this._unacknowledgedStanzas.length) {
this._throwError('Higher reported acknowledge count than unacknowledged stanzas. Reported Acknowledge Count: ' + delta + ' - Unacknowledge Stanza Count: ' + this._unacknowledgedStanzas.length + ' - New: ' + reportedHandledCount + ' - Previous: ' + lastKnownHandledCount);
}
for(var i = 0; i < delta; i++) {
var stanza = this._unacknowledgedStanzas.shift();
for (var j = 0; j < this._acknowledgedStanzaListeners.length; j++) {
this._acknowledgedStanzaListeners[j](stanza);
}
}
if (this.logging && this._unacknowledgedStanzas.length > 0) {
console.warn('Unacknowledged stanzas', this._unacknowledgedStanzas);
}
},
_handleServerRequestHandler: function() {
this._answerProcessedStanzas();
return true;
},
_answerProcessedStanzas: function() {
if (this._isStreamManagementEnabled) {
this._c.send($build('a', { xmlns: this._NS, h: this._clientProcessedStanzasCounter }));
}
},
_increaseSentStanzasCounter: function(elem) {
if (this._isStreamManagementEnabled) {
this._unacknowledgedStanzas.push(this.returnWholeStanza ? elem : elem.getAttribute('id'));
this._clientSentStanzasCounter++;
if (this.requestResponseInterval > 0) {
this._requestResponseIntervalCount++;
if (this._requestResponseIntervalCount === this.requestResponseInterval) {
this.requestAcknowledgement();
}
}
}
},
_increaseReceivedStanzasCounter: function() {
if (this._isStreamManagementEnabled) {
this._clientProcessedStanzasCounter++;
}
},
_throwError(msg) {
console.error(msg);
throw new Error(msg);
}
});