From 0780879205f4227401762053053b26f0a3fbc7ba Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 9 Apr 2014 11:05:25 -0300 Subject: [PATCH] add maxPeer check --- index.html | 12 ++++++++++- js/config.js | 3 ++- js/controllers/header.js | 5 ++++- js/controllers/signin.js | 13 +++++++++--- js/models/CopayPeer.js | 45 +++++++++++++++++++++++++++++----------- js/services/network.js | 29 +++++++++++++------------- 6 files changed, 75 insertions(+), 32 deletions(-) diff --git a/index.html b/index.html index 872b83cbc..b5942f087 100644 --- a/index.html +++ b/index.html @@ -10,7 +10,8 @@ -
+
+
@@ -38,8 +39,17 @@ +
+
+ {{$root.flashMessage.type}}: + {{$root.flashMessage.message}} + Dismiss +
+ +
+
diff --git a/js/config.js b/js/config.js index 4372631ec..abead97d4 100644 --- a/js/config.js +++ b/js/config.js @@ -3,5 +3,6 @@ var config = { networkName: 'testnet', p2pApiKey: 'lwjd5qra8257b9', - p2pDebug: 3 + p2pDebug: 3, + maxPeers: 5, }; diff --git a/js/controllers/header.js b/js/controllers/header.js index 5e510854a..6a2787e54 100644 --- a/js/controllers/header.js +++ b/js/controllers/header.js @@ -32,9 +32,12 @@ angular.module('copay.header').controller('HeaderController', }; $scope.signout = function() { - $rootScope.isLogged = false; Network.disconnect(function() { $location.path('signin'); }); }; + + $scope.clearFlashMessage = function() { + $rootScope.flashMessage = {}; + }; }); diff --git a/js/controllers/signin.js b/js/controllers/signin.js index 4fcc7ce26..fee00a14a 100644 --- a/js/controllers/signin.js +++ b/js/controllers/signin.js @@ -38,9 +38,16 @@ angular.module('copay.signin').controller('SigninController', if (cid) { Network.init(function() { - Network.connect(cid, function() { - $location.path('peer'); - $rootScope.$digest(); + Network.connect(cid, + function() { + $location.path('peer'); + $rootScope.$digest(); + }, function() { + +console.log('[signin.js.46] SETTING MESSAGE'); //TODO + $rootScope.flashMessage = { message: 'Connection refussed', type: 'error'}; + $location.path('home'); + $rootScope.$digest(); }); }); } diff --git a/js/models/CopayPeer.js b/js/models/CopayPeer.js index c4c7309f6..5a25206de 100644 --- a/js/models/CopayPeer.js +++ b/js/models/CopayPeer.js @@ -16,10 +16,11 @@ var EventEmitter= imports.EventEmitter || require('events').EventEmitter; */ function CopayPeer(opts) { - opts = opts || {}; - this.peerId = opts.peerId; - this.apiKey = opts.apiKey || 'lwjd5qra8257b9'; - this.debug = opts.debug || 3; + opts = opts || {}; + this.peerId = opts.peerId; + this.apiKey = opts.apiKey || 'lwjd5qra8257b9'; + this.debug = opts.debug || 3; + this.maxPeers = opts.maxPeers || 5; this.connectedPeers = []; } @@ -84,7 +85,13 @@ CopayPeer.prototype._connectToPeers = function(peerIds) { }; CopayPeer.prototype._onData = function(data, isInbound) { - var obj = JSON.parse(data); + var obj; + try { + obj = JSON.parse(data); + } catch (e) { + console.log('### ERROR ON DATA: "%s" ', data, isInbound, e); + return; + }; console.log('### RECEIVED TYPE: %s FROM %s', obj.data.type, obj.sender); switch(obj.data.type) { @@ -101,7 +108,7 @@ CopayPeer.prototype._onData = function(data, isInbound) { }; CopayPeer.prototype._sendPeers = function(peerIds) { - console.log('#### SENDING PEER LIST: ', this.connectedPeers, ' TO ', peerIds); + console.log('#### SENDING PEER LIST: ', this.connectedPeers, ' TO ', peerIds?peerIds: 'ALL'); this.send(peerIds, { type: 'peerList', peers: this.connectedPeers, @@ -123,7 +130,9 @@ CopayPeer.prototype._addPeer = function(peerId, isInbound) { } }; -CopayPeer.prototype._setupConnectionHandlers = function(dataConn, isInbound, openCallback) { +CopayPeer.prototype._setupConnectionHandlers = function( + dataConn, isInbound, openCallback, closeCallback) { + var self=this; dataConn.on('open', function() { @@ -143,11 +152,13 @@ CopayPeer.prototype._setupConnectionHandlers = function(dataConn, isInbound, ope }); dataConn.on('error', function(e) { - console.log('### ## INBOUND DATA ERROR',e ); //TODO + console.log('### DATA ERROR',e ); //TODO }); dataConn.on('close', function() { + console.log('### CLOSE RECV FROM:', dataConn.peer); //TODO self._onClose(dataConn.peer); + if (typeof closeCallback === 'function') closeCallback(); }); }; @@ -174,8 +185,18 @@ CopayPeer.prototype._setupPeerHandlers = function(openCallback) { }); p.on('connection', function(dataConn) { - console.log('### NEW INBOUND CONNECTION'); //TODO - self._setupConnectionHandlers(dataConn, true); + + console.log('### NEW INBOUND CONNECTION %d/%d', self.connectedPeers.length, self.maxPeers); + if (self.connectedPeers.length >= self.maxPeers) { + console.log('### PEER REJECTED. PEER MAX LIMIT REACHED'); + dataConn.on('open', function() { + console.log('### CLOSING CONN FROM:' + dataConn.peer); + dataConn.close(); + }); + } + else { + self._setupConnectionHandlers(dataConn, true); + } }); }; @@ -230,7 +251,7 @@ console.log('[CopayPeer.js.216:SENDD:]',data); //TODO self._sendToOne(peerIds, data, cb); }; -CopayPeer.prototype.connectTo = function(peerId, cb) { +CopayPeer.prototype.connectTo = function(peerId, openCallback, closeCallback ) { var self = this; console.log('### STARTING TO CONNECT TO:' + peerId ); @@ -242,7 +263,7 @@ CopayPeer.prototype.connectTo = function(peerId, cb) { metadata: { message: 'hi copayer!' } }); - self._setupConnectionHandlers(dataConn, false, cb); + self._setupConnectionHandlers(dataConn, false, openCallback, closeCallback); }; CopayPeer.prototype.disconnect = function(peerId, cb) { diff --git a/js/services/network.js b/js/services/network.js index 05f064b47..d1cf09fcc 100644 --- a/js/services/network.js +++ b/js/services/network.js @@ -97,14 +97,10 @@ angular.module('copay.network') switch(data.type) { case 'publicKeyRing': - - console.log('[network.js.91:data:]',data); //TODO - _checkWallet(data.publicKeyRing.id); var shouldSend = false; var recipients, pkr = $rootScope.publicKeyRing; - console.log('### RECEIVED PKR FROM:', senderId); if (pkr.merge(data.publicKeyRing, true) && !data.isBroadcast) { console.log('### BROADCASTING PKR'); recipients = null; @@ -125,7 +121,6 @@ angular.module('copay.network') }); } -console.log('[network.js.126] END'); //TODO _refreshUx(); break; } @@ -142,6 +137,7 @@ console.log('[network.js.126] END'); //TODO var cp = $rootScope.cp = new copay.CopayPeer({ apiKey: config.p2pApiKey, debug: config.p2pDebug, + maxPeers: config.maxPeers, // TODO: This should be on wallet configuration }); _setupHandlers(); @@ -151,19 +147,24 @@ console.log('[network.js.126] END'); //TODO }); }; - var connect = function(peerId, cb) { - if ($rootScope.cp) { - $rootScope.cp.connectTo(peerId, cb); - } - else - return cb(); - }; - - var disconnect = function(cb) { + var disconnect = function() { if ($rootScope.cp) { $rootScope.cp.disconnect(); } Storage.remove('peerData'); + $rootScope.isLogged = false; + _refreshUx(); + }; + + var connect = function(peerId, openCallback, failCallBack) { + if ($rootScope.cp) { + $rootScope.cp.connectTo(peerId, openCallback, function () { + disconnect(); + failCallBack(); + }); + } + else + return failCallBack(); }; return {