better p2p handling

This commit is contained in:
Matias Alejo Garcia 2014-04-17 16:50:48 -03:00
commit d5fe53d23e
2 changed files with 12 additions and 8 deletions

View file

@ -101,9 +101,7 @@ Wallet.prototype._handleData = function(senderId, data, isInbound) {
this.log('badMessage FROM:', senderId); //TODO this.log('badMessage FROM:', senderId); //TODO
return; return;
} }
this.log('[Wallet.js.98]' , data.type); //TODO this.log('[Wallet.js.98]' , data.type); //TODO
switch(data.type) { switch(data.type) {
case 'publicKeyRing': case 'publicKeyRing':
this._handlePublicKeyRing(senderId, data, isInbound); this._handlePublicKeyRing(senderId, data, isInbound);
@ -127,6 +125,7 @@ Wallet.prototype._handleNetworkChange = function(newPeer) {
Wallet.prototype.netStart = function() { Wallet.prototype.netStart = function() {
var self = this; var self = this;
var net = this.network; var net = this.network;
net.removeAllListeners();
net.on('networkChange', self._handleNetworkChange.bind(self) ); net.on('networkChange', self._handleNetworkChange.bind(self) );
net.on('data', self._handleData.bind(self) ); net.on('data', self._handleData.bind(self) );
net.on('open', function() {}); // TODO net.on('open', function() {}); // TODO

View file

@ -134,6 +134,13 @@ Network.prototype._addPeer = function(peerId, isInbound) {
} }
}; };
Network.prototype._checkAnyPeer = function() {
if (!this.connectedPeers.length) {
console.log('EMIT openError: no more peers, not even you!');
this.emit('openError');
}
}
Network.prototype._setupConnectionHandlers = function(dataConn, isInbound) { Network.prototype._setupConnectionHandlers = function(dataConn, isInbound) {
var self=this; var self=this;
@ -156,17 +163,14 @@ Network.prototype._setupConnectionHandlers = function(dataConn, isInbound) {
dataConn.on('error', function(e) { dataConn.on('error', function(e) {
console.log('### DATA ERROR',e ); //TODO console.log('### DATA ERROR',e ); //TODO
self.emit('openError'); self.emit('dataError');
}); });
dataConn.on('close', function() { dataConn.on('close', function() {
if (self.closing) return; if (self.closing) return;
console.log('### CLOSE RECV FROM:', dataConn.peer); console.log('### CLOSE RECV FROM:', dataConn.peer);
self._onClose(dataConn.peer); self._onClose(dataConn.peer);
if (! isInbound) { self._checkAnyPeer();
console.log('[WebRTC.js.163] EMIT openError'); //TODO
self.emit('openError');
}
}); });
}; };
@ -191,7 +195,7 @@ Network.prototype._setupPeerHandlers = function(openCallback) {
self.peer.disconnect(); self.peer.disconnect();
self.peer.destroy(); self.peer.destroy();
self.peer = null; self.peer = null;
self.emit('openError'); self._checkAnyPeer();
}); });
p.on('connection', function(dataConn) { p.on('connection', function(dataConn) {
@ -256,6 +260,7 @@ console.log('[WebRTC.js.242] SENDING ', data.type); //TODO
var l = peerIds.length; var l = peerIds.length;
var i = 0; var i = 0;
peerIds.forEach(function(peerId) { peerIds.forEach(function(peerId) {
console.log('[WebRTC.js.258:peerId:]',peerId); //TODO
self._sendToOne(peerId, data, function () { self._sendToOne(peerId, data, function () {
if (++i === l && typeof cb === 'function') cb(); if (++i === l && typeof cb === 'function') cb();
}); });