Skip to content

Commit

Permalink
Create Peers collection and encapsulate options
Browse files Browse the repository at this point in the history
Re-factor Peer object to use hard-coded connection options be default,
but allow these options to be overridden at instantiation or invocation.
  • Loading branch information
jugglinmike committed Mar 26, 2013
1 parent d9fa758 commit d126d69
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
18 changes: 6 additions & 12 deletions prototype/public/scripts/app.js
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
21 changes: 20 additions & 1 deletion prototype/public/scripts/modules/peer.js
Expand Up @@ -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');
Expand All @@ -19,6 +30,7 @@ define([
if (this.peerConn) {
this.destroy();
}
options = options || this.connectOptions;
try {
peerConn = this.peerConn = new rtc.RTCPeerConnection(options);
} catch (e) {
Expand Down Expand Up @@ -137,5 +149,12 @@ define([
delete this.peerConn;
};

return Peer;
var Peers = Backbone.Collection.extend({
model: Peer
});

return {
Model: Peer,
Collection: Peers
};
});
33 changes: 23 additions & 10 deletions prototype/test/client/tests/peer.js
Expand Up @@ -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);
});
});
});
});
Expand Down

0 comments on commit d126d69

Please sign in to comment.