From d126d69baa349d65936d9e420c42bf2bdfae6fcd Mon Sep 17 00:00:00 2001 From: Mike Pennisi Date: Tue, 26 Mar 2013 10:47:12 -0400 Subject: [PATCH] Create Peers collection and encapsulate options Re-factor Peer object to use hard-coded connection options be default, but allow these options to be overridden at instantiation or invocation. --- prototype/public/scripts/app.js | 18 +++++-------- prototype/public/scripts/modules/peer.js | 21 ++++++++++++++- prototype/test/client/tests/peer.js | 33 +++++++++++++++++------- 3 files changed, 49 insertions(+), 23 deletions(-) diff --git a/prototype/public/scripts/app.js b/prototype/public/scripts/app.js index a607afe..2334545 100644 --- a/prototype/public/scripts/app.js +++ b/prototype/public/scripts/app.js @@ -4,23 +4,19 @@ require([ 'use strict'; var config = { - socketServer: 'ws://' + window.location.host, - pcConfig: { - iceServers: [ - { url: 'stun:stun.l.google.com:19302' }, - { url: 'stun:23.21.150.121' } - ] - } + socketServer: 'ws://' + window.location.host }; + var user = new Peer.Model(); + var pc = new Peer.Model(); // TODO: Fetch contacts from remote identitiy provider - var contacts = [ + var contacts = new Peer.Collection([ { name: 'creationix' }, { name: 'robin' }, { name: 'erik' }, { name: 'lawrence' }, { name: 'cassie' }, { name: 'jugglinmike' } - ]; + ]); var mediaConstraints = { mandatory: { OfferToReceiveAudio: true, @@ -36,12 +32,10 @@ require([ console.error('Create Answer failed'); } - var user = new Peer(); - var pc = new Peer(); var layout = new Layout({ el: '#app', user: user, - contacts: new Backbone.Collection(contacts) + contacts: contacts }); layout.render(); layout.on('connectRequest', function(stream) { diff --git a/prototype/public/scripts/modules/peer.js b/prototype/public/scripts/modules/peer.js index 870c318..75907d4 100644 --- a/prototype/public/scripts/modules/peer.js +++ b/prototype/public/scripts/modules/peer.js @@ -5,6 +5,17 @@ define([ var Peer = Backbone.Model.extend({ nameRegex: /^[0-9a-z\.-]+$/i, + connectOptions: { + iceServers: [ + { url: 'stun:stun.l.google.com:19302' }, + { url: 'stun:23.21.150.121' } + ] + }, + initialize: function(options) { + if (options && options.connectOptions) { + this.connectOptions = options.connectOptions; + } + }, validate: function(attrs) { if (!attrs || !attrs.name) { return new Error('No username specified'); @@ -19,6 +30,7 @@ define([ if (this.peerConn) { this.destroy(); } + options = options || this.connectOptions; try { peerConn = this.peerConn = new rtc.RTCPeerConnection(options); } catch (e) { @@ -137,5 +149,12 @@ define([ delete this.peerConn; }; - return Peer; + var Peers = Backbone.Collection.extend({ + model: Peer + }); + + return { + Model: Peer, + Collection: Peers + }; }); diff --git a/prototype/test/client/tests/peer.js b/prototype/test/client/tests/peer.js index 476262e..734b00e 100644 --- a/prototype/test/client/tests/peer.js +++ b/prototype/test/client/tests/peer.js @@ -2,17 +2,30 @@ define(['modules/peer'], function(Peer) { 'use strict'; suite('Peer', function() { - suite('#validate', function() { - var peer; - suiteSetup(function() { - peer = new Peer(); - }); - test('Rejects unspecified string names', function() { - assert.instanceOf(peer.validate(), Error); - assert.instanceOf(peer.validate({ name: '' }), Error); + + suite('Model', function() { + suite('#initialize', function() { + test('Defines default connection options', function() { + assert.ok((new Peer.Model()).connectOptions); + }); + test('Accepts connection options as overrides', function() { + var newOpts = {}; + var peer = new Peer.Model({ connectOptions: newOpts }); + assert.equal(peer.connectOptions, newOpts); + }); }); - test('Rejects invalid names', function() { - assert.instanceOf(peer.validate({ name: '!@#@#$' }), Error); + suite('#validate', function() { + var peer; + suiteSetup(function() { + peer = new Peer.Model(); + }); + test('Rejects unspecified string names', function() { + assert.instanceOf(peer.validate(), Error); + assert.instanceOf(peer.validate({ name: '' }), Error); + }); + test('Rejects invalid names', function() { + assert.instanceOf(peer.validate({ name: '!@#@#$' }), Error); + }); }); }); });