This repository was archived by the owner on Sep 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectionmanager.js.html
More file actions
350 lines (313 loc) · 13.2 KB
/
connectionmanager.js.html
File metadata and controls
350 lines (313 loc) · 13.2 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: connectionmanager.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: connectionmanager.js</h1>
<section>
<article>
<pre class="prettyprint source"><code>/** @fileOverview Mid-level connection broking and signaling functionality. */
var Peer = require('./peer.js');
/**
* @constructor
* @class Handles the connection establishment to other nodes as
* well as joining a network (bootstrapping).
*/
var ConnectionManager = function() {
if (!(this instanceof ConnectionManager)) {
return new ConnectionManager();
}
this._bootstrap = null;
this._pending = {};
this._connections = {};
this._pcoptions = {
iceServers: [{
"url": "stun:stun.l.google.com:19302"
}]
};
//possible states: 'uninitialized', 'bootstrapping', 'ready'
this._state = 'uninitialized';
return this;
};
ConnectionManager.prototype = {
utils: {
/**
* Returns a list of field values of the given field in the given SDP.
*/
findInSDP: function(sdp, field) {
var result = [];
sdp.split('\r\n').forEach(function(line) {
if (line.match(new RegExp("^" + field + "="))) {
result.push(line.split("=", 2)[1]);
}
});
return result;
},
/**
* Returns the session ID contained in the given SDP. This ID is used
* for glare handling.
*/
findSessionId: function(sdp) {
return parseInt(this.findInSDP(sdp, "o")[0].split(" ")[1], 10);
},
},
/**
* Connects this instance to the P2P network by establishing a DataChannel
* connection to an arbitrary peer.
*
* @param router {Router} used for delivering the initial offer.
* @param successCallback {Function} called when a connection has been established
* and the peer is ready to send/receive data.
* @param errorCallback {Function} called when the connection could not be
* established.
*/
bootstrap: function(router, successCallback, errorCallback) {
if (this._state !== 'uninitialized') {
errorCallback('Invalid state');
return;
}
this._state = 'bootstrapping';
this._router = router;
var pc = new RTCPeerConnection(this._pcoptions);
var to = '*';
this._bootstrap = {
pc: pc,
dc: pc.createDataChannel(null, {}),
onsuccess: successCallback,
onerror: errorCallback,
};
router.registerDeliveryCallback('signaling-protocol', this._onMessage.bind(this));
pc.createOffer(this._onCreateOfferSuccess.bind(this, pc, to, this._bootstrap, errorCallback),
this._onCreateOfferError.bind(this, errorCallback));
},
/**
* Creates a DataChannel connection to the given peer.
*
* @param to ID of the remote peer
* @param successCallback {Function} called when the connection has been
* established
* @param errorCallback {Function} called when the connection establishment
* failed
*/
connect: function(to, successCallback, errorCallback) {
if (this._state !== 'ready') {
errorCallback('Invalid state');
return;
}
if (this._pending[to] !== undefined) {
errorCallback('Already connecting');
}
var pc = new RTCPeerConnection(this._pcoptions);
var dc = pc.createDataChannel(null, {});
this._pending[to] = {
pc: pc,
dc: dc,
onsuccess: successCallback,
onerror: errorCallback,
};
pc.createOffer(this._onCreateOfferSuccess.bind(this, pc, to, this._pending[to],
errorCallback), this._onCreateOfferError.bind(this, errorCallback));
},
_onCreateOfferSuccess: function(pc, to, pendingOffer, errorCallback, sessionDesc) {
if (pendingOffer.drop) {
return;
}
pc.onicecandidate = function(iceEvent) {
if (pc.iceGatheringState === 'complete' || iceEvent.candidate === null) {
// spec specifies that a null candidate means that the ice gathering is complete
pc.onicecandidate = function() {};
pc.createOffer(function(offer) {
this._router.route(to, 'signaling-protocol', offer);
}.bind(this), this._onCreateOfferError.bind(this, errorCallback));
}
}.bind(this);
pendingOffer.offerId = this.utils.findSessionId(sessionDesc.sdp);
pc.setLocalDescription(sessionDesc);
},
_onCreateOfferError: function(errorCallback, error) {
// TODO(max): clean up state (delete PC object etc.)
errorCallback(error);
},
_onMessage: function(msg, from) {
switch (msg.type) {
case 'offer':
this._onReceiveOffer(msg, from);
break;
case 'answer':
this._onReceiveAnswer(msg, from);
break;
case 'denied':
this._onOfferDenied(msg, from);
break;
default:
console.log('ConnectionManager: Discarding JSEP message because the type is unknown: ' + JSON.stringify(msg));
}
},
_onReceiveAnswer: function(desc, from) {
if (this._state === 'bootstrapping') {
// TODO(max): check if we actually have a pending PC and ``drop'' is not set (glare).
this._bootstrap.pc.setRemoteDescription(new RTCSessionDescription(desc));
this._bootstrap.dc.onopen = function(ev) {
// nodejs wrtc-library does not include a channel reference in `ev.target`
this._router.addPeer(new Peer(from, this._bootstrap.pc, this._bootstrap.dc));
this._state = 'ready';
this._bootstrap.onsuccess();
this._bootstrap = null;
}.bind(this);
} else {
var pending = this._pending[from];
if (pending === undefined) {
return; // we haven't offered to this node, silently discard
}
pending.pc.setRemoteDescription(new RTCSessionDescription(desc));
pending.dc.onopen = function(ev) {
// nodejs wrtc-library does not include a channel reference in `ev.target`
var peer = new Peer(from, pending.pc, pending.dc);
this._router.addPeer(peer);
if (typeof(pending.onsuccess) === 'function') {
// TODO(max): would it make sense to pass the remote peer's
// ID to the handler?
pending.onsuccess();
}
delete this._pending[from];
this._connections[from] = peer;
}.bind(this);
}
},
_onReceiveOffer: function(desc, from) {
// if we're already connected or are already processing an offer from
// this peer, deny this offer
if (this._connections[from] !== undefined || this._pending[from] !== undefined) {
this._router.route(from, 'signaling-protocol', {
type: 'denied'
});
}
var offerId = this.utils.findSessionId(desc.sdp);
if (this._state === 'bootstrapping') {
if (this._bootstrap.pc.remoteDescription !== null) {
// we already have a bootstrap peer
return;
}
if (offerId > this._bootstrap.offerId) {
// discard our offer and accept this one
var newBootstrap = {
pc: new RTCPeerConnection(this._pcoptions),
onsuccess: this._bootstrap.onsuccess,
onerror: this._bootstrap.onerror,
};
// cancel all actions on the old object
// FIXME(max): setting ``drop'' here makes no sense since it's overwritten in the next line.
this._bootstrap.drop = true;
this._bootstrap = newBootstrap;
} else {
// silently discard this offer
return;
}
this._bootstrap.pc.setRemoteDescription(new RTCSessionDescription(desc));
this._bootstrap.pc.ondatachannel = function(ev) {
ev.channel.onopen = function(ev2) {
// nodejs wrtc-library does not include a channel reference in `ev2.target`
var peer = new Peer(from, this._bootstrap.pc, ev.channel);
this._router.addPeer(peer);
this._state = 'ready';
this._bootstrap.onsuccess();
this._connections[from] = peer;
this._bootstrap = null;
}.bind(this);
}.bind(this);
this._bootstrap.pc.createAnswer(this._onCreateAnswerSuccess.bind(this, from, this._bootstrap.pc), this._onCreateAnswerError.bind(this));
} else {
var pendingOffer = this._pending[from];
if (pendingOffer !== undefined) {
// handle glare
if (offerId > pendingOffer.offerId) {
// discard our offer and accept this one
newPendingOffer = {
onsuccess: pendingOffer.onsuccess,
onerror: pendingOffer.onerror,
};
pendingOffer = newPendingOffer;
delete this._pending[from];
} else {
// silently discard this offer
return;
}
}
var pc = new RTCPeerConnection(this._pcoptions);
pc.setRemoteDescription(new RTCSessionDescription(desc));
this._pending[from] = pendingOffer || {};
this._pending[from].pc = pc;
pc.ondatachannel = function(ev) {
ev.channel.onopen = function(ev2) {
// nodejs wrtc-library does not include a channel reference in `ev2.target`
var peer = new Peer(from, pc, ev.channel);
this._router.addPeer(peer);
if (typeof(this._pending[from].onsuccess) === 'function') {
this._pending[from].onsuccess();
}
delete this._pending[from];
this._connections[from] = peer;
}.bind(this);
}.bind(this);
pc.createAnswer(this._onCreateAnswerSuccess.bind(this, from, pc), this._onCreateAnswerError.bind(this));
}
},
_onCreateAnswerSuccess: function(to, pc, sessionDesc) {
pc.onicecandidate = function(iceEvent) {
if (pc.iceGatheringState === 'complete' || iceEvent.candidate === null) {
// spec specifies that a null candidate means that the ice gathering is complete
pc.onicecandidate = function() {};
pc.createAnswer(function(answer) {
this._router.route(to, 'signaling-protocol', answer);
}.bind(this), this._onCreateAnswerError.bind(this));
}
}.bind(this);
pc.setLocalDescription(new RTCSessionDescription(sessionDesc));
},
_onCreateAnswerError: function(error) {
console.log(error);
},
/**
* The server denies offers when only one peer is connected since there is
* no other peer that could answer the offer. In that case the first peer
* just has to sit and wait for an offer. Eventually the successCallback is
* called.
*/
_onOfferDenied: function(desc) {
if (this._state === 'bootstrapping') {
this._bootstrap.pc = new RTCPeerConnection(this._pcoptions);
this._bootstrap.dc = this._bootstrap.pc.createDataChannel(null, {});
this._bootstrap.offerId = null;
this._bootstrap.onsuccess();
this._bootstrap.onsuccess = function() {};
}
},
};
if (typeof(module) !== 'undefined') {
module.exports = ConnectionManager;
}
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Index</a></h2><h3>Classes</h3><ul><li><a href="BOPlishClient.html">BOPlishClient</a></li><li><a href="BopURI.html">BopURI</a></li><li><a href="Chord.html">Chord</a></li><li><a href="ConnectionManager.html">ConnectionManager</a></li><li><a href="Peer.html">Peer</a></li><li><a href="Router.html">Router</a></li><li><a href="sha1.html">sha1</a></li></ul><h3>Global</h3><ul><li><a href="global.html#Helper">Helper</a></li></ul>
</nav>
<br clear="both">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a> on Fri Aug 15 2014 17:48:09 GMT+0200 (CEST)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>