Skip to content

Commit

Permalink
move things around
Browse files Browse the repository at this point in the history
  • Loading branch information
ericz committed Jan 5, 2013
0 parents commit 1c5b22c
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
@@ -0,0 +1,15 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

node_modules
npm-debug.log
92 changes: 92 additions & 0 deletions lib/server.js
@@ -0,0 +1,92 @@
var WebSocketServer = require('ws').Server;
var util = require('./util');
var EventEmitter = require('events').EventEmitter;


function PeerServer(options) {
if (!(this instanceof PeerServer)) return new PeerServer(options);

EventEmitter.call(this);


options = util.extend({
port: 80
}, options);

var wss = new WebSocketServer({ port: options.port });


this.clients = {};
var self = this;

// For connecting clients:
// Src will connect upon creating a link.
// Receivers will connect after clicking a button and entering an optional key.
wss.on('connection', function(socket) {
var clientId = util.randomId();
while (!!self.clients[clientId]) {
clientId = util.randomId();
}
self.clients[clientId] = socket;

socket.on('message', function(data) {
var message = JSON.parse(data);
if (options.debug) {
console.log('PeerServer: ', message);
}

switch (message.type) {
// Source connected -- send back its ID.
case 'SOURCE':
socket.send(JSON.stringify({ type: 'SOURCE-ID', id: clientId }));
break;
// Sink connected -- send back its ID and notify src.
case 'SINK':
if (!!message.source && !!self.clients[message.source]) {
self.clients[message.source].send(JSON.stringify({
type: 'SINK-CONNECTED', sink: clientId }));

socket.send(JSON.stringify({ type: 'SINK-ID', id: clientId }));
} else {
util.prettyError('source invalid');
}
break;
case 'LEAVE':
if (!!self.clients[message.dst]) {
try {
self.clients[message.dst].send(data);
} catch (e) {
if (options.debug) {
console.log('Error', e);
}
}
delete self.clients[message.src];
}
break;
// Offer or answer from src to sink.
case 'OFFER':
case 'ANSWER':
case 'CANDIDATE':
case 'PORT':
if (!!self.clients[message.dst]) {
try {
self.clients[message.dst].send(data);
} catch (e) {
if (options.debug) {
console.log('Error', e);
}
}
}
break;
default:
util.prettyError('message unrecognized');
}
});
});

};


util.inherits(PeerServer, EventEmitter);

exports.PeerServer = PeerServer;
32 changes: 32 additions & 0 deletions lib/util.js
@@ -0,0 +1,32 @@

var util = {
inherits: function(ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
},
extend: function(dest, source) {
for(var key in source) {
if(source.hasOwnProperty(key)) {
dest[key] = source[key];
}
}
return dest;
},
randomId: function () {
return Math.random().toString(36).substr(2);
},
prettyError: function (msg) {
console.log('PeerServer: ', msg);
}
};

// if node
module.exports = util;
// end node
16 changes: 16 additions & 0 deletions package.json
@@ -0,0 +1,16 @@
{
"name": "peer",
"version": "0.0.1",
"description": "Simple p2p file transfer",
"main": "lib/server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"repository": "",
"author": "Michelle Bu",
"license": "BSD",
"dependencies": {
"ws": "~0.4.25"
}
}

0 comments on commit 1c5b22c

Please sign in to comment.