-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
345 lines (299 loc) · 9.46 KB
/
index.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
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
var util = require('util');
var uuid = require('uuid');
var async = require('async');
var extend = require('extend-object');
var WildEmitter = require('wildemitter');
var ACTIONS = {
'content-accept': 'onContentAccept',
'content-add': 'onContentAdd',
'content-modify': 'onContentModify',
'content-reject': 'onContentReject',
'content-remove': 'onContentRemove',
'description-info': 'onDescriptionInfo',
'security-info': 'onSecurityInfo',
'session-accept': 'onSessionAccept',
'session-info': 'onSessionInfo',
'session-initiate': 'onSessionInitiate',
'session-terminate': 'onSessionTerminate',
'transport-accept': 'onTransportAccept',
'transport-info': 'onTransportInfo',
'transport-reject': 'onTransportReject',
'transport-replace': 'onTransportReplace',
// Unstandardized actions: might go away anytime without notice
'source-add': 'onSourceAdd',
'source-remove': 'onSourceRemove'
};
function JingleSession(opts) {
WildEmitter.call(this);
var self = this;
this.sid = opts.sid || uuid.v4();
this.peer = opts.peer;
this.peerID = opts.peerID || this.peer.full || this.peer;
this.isInitiator = opts.initiator || false;
this.parent = opts.parent;
this.state = 'starting';
this.connectionState = 'starting';
// We track the intial pending description types in case
// of the need for a tie-breaker.
this.pendingApplicationTypes = opts.applicationTypes || [];
this.pendingAction = false;
// Here is where we'll ensure that all actions are processed
// in order, even if a particular action requires async handling.
this.processingQueue = async.queue(function (task, next) {
if (self.ended) {
// Don't process anything once the session has been ended
return next();
}
var action = task.action;
var changes = task.changes;
var cb = task.cb;
self._log('debug', action);
if (!ACTIONS[action]) {
self._log('error', 'Invalid action: ' + action);
cb({condition: 'bad-request'});
return next();
}
self[ACTIONS[action]](changes, function (err, result) {
cb(err, result);
return next();
});
});
}
util.inherits(JingleSession, WildEmitter);
// We don't know how to handle any particular content types,
// so no actions are supported.
Object.keys(ACTIONS).forEach(function (action) {
var method = ACTIONS[action];
JingleSession.prototype[method] = function (changes, cb) {
this._log('error', 'Unsupported action: ' + action);
cb();
};
});
// Provide some convenience properties for checking
// the session's state.
Object.defineProperties(JingleSession.prototype, {
state: {
get: function () {
return this._sessionState;
},
set: function (value) {
if (value !== this._sessionState) {
var prev = this._sessionState;
this._log('info', 'Changing session state to: ' + value);
this._sessionState = value;
this.emit('change:sessionState', this, value);
this.emit('change:' + value, this, true);
if (prev) {
this.emit('change:' + prev, this, false);
}
}
}
},
connectionState: {
get: function () {
return this._connectionState;
},
set: function (value) {
if (value !== this._connectionState) {
var prev = this._connectionState;
this._log('info', 'Changing connection state to: ' + value);
this._connectionState = value;
this.emit('change:connectionState', this, value);
this.emit('change:' + value, this, true);
if (prev) {
this.emit('change:' + prev, this, false);
}
}
}
},
starting: {
get: function () {
return this._sessionState === 'starting';
}
},
pending: {
get: function () {
return this._sessionState === 'pending';
}
},
active: {
get: function () {
return this._sessionState === 'active';
}
},
ended: {
get: function () {
return this._sessionState === 'ended';
}
},
connected: {
get: function () {
return this._connectionState === 'connected';
}
},
connecting: {
get: function () {
return this._connectionState === 'connecting';
}
},
disconnected: {
get: function () {
return this._connectionState === 'disconnected';
}
},
interrupted: {
get: function () {
return this._connectionState === 'interrupted';
}
}
});
JingleSession.prototype = extend(JingleSession.prototype, {
_log: function (level, message) {
message = this.sid + ': ' + message;
this.emit('log:' + level, message);
},
send: function (action, data) {
data = data || {};
data.sid = this.sid;
data.action = action;
var requirePending = {
'session-inititate': true,
'session-accept': true,
'content-add': true,
'content-remove': true,
'content-reject': true,
'content-accept': true,
'content-modify': true,
'transport-replace': true,
'transport-reject': true,
'transport-accept': true,
'source-add': true,
'source-remove': true
};
if (requirePending[action]) {
this.pendingAction = action;
} else {
this.pendingAction = false;
}
this.emit('send', {
to: this.peer,
id: uuid.v4(),
type: 'set',
jingle: data
});
},
process: function (action, changes, cb) {
this.processingQueue.push({
action: action,
changes: changes,
cb: cb
});
},
start: function () {
this._log('error', 'Can not start base sessions');
this.end('unsupported-applications', true);
},
accept: function () {
this._log('error', 'Can not accept base sessions');
this.end('unsupported-applications');
},
cancel: function () {
this.end('cancel');
},
decline: function () {
this.end('decline');
},
end: function (reason, silent) {
this.state = 'ended';
this.processingQueue.kill();
if (!reason) {
reason = 'success';
}
if (typeof reason === 'string') {
reason = {
condition: reason
};
}
if (!silent) {
this.send('session-terminate', {
reason: reason
});
}
this.emit('terminated', this, reason);
},
onSessionTerminate: function (changes, cb) {
this.end(changes.reason, true);
cb();
},
// It is mandatory to reply to a session-info action with
// an unsupported-info error if the info isn't recognized.
//
// However, a session-info action with no associated payload
// is acceptable (works like a ping).
onSessionInfo: function (changes, cb) {
var okKeys = {
sid: true,
action: true,
initiator: true,
responder: true
};
var unknownPayload = false;
Object.keys(changes).forEach(function (key) {
if (!okKeys[key]) {
unknownPayload = true;
}
});
if (unknownPayload) {
cb({
type: 'modify',
condition: 'feature-not-implemented',
jingleCondition: 'unsupported-info'
});
} else {
cb();
}
},
// It is mandatory to reply to a description-info action with
// an unsupported-info error if the info isn't recognized.
onDescriptionInfo: function (changes, cb) {
cb({
type: 'modify',
condition: 'feature-not-implemented',
jingleCondition: 'unsupported-info'
});
},
// It is mandatory to reply to a transport-info action with
// an unsupported-info error if the info isn't recognized.
onTransportInfo: function (changes, cb) {
cb({
type: 'modify',
condition: 'feature-not-implemented',
jingleCondition: 'unsupported-info'
});
},
// It is mandatory to reply to a content-add action with either
// a content-accept or content-reject.
onContentAdd: function (changes, cb) {
// Allow ack for the content-add to be sent.
cb();
this.send('content-reject', {
reason: {
condition: 'failed-application',
text: 'content-add is not supported'
}
});
},
// It is mandatory to reply to a transport-add action with either
// a transport-accept or transport-reject.
onTransportReplace: function (changes, cb) {
// Allow ack for the transport-replace be sent.
cb();
this.send('transport-reject', {
reason: {
condition: 'failed-application',
text: 'transport-replace is not supported'
}
});
}
});
module.exports = JingleSession;