add maxPeer check

This commit is contained in:
Matias Alejo Garcia 2014-04-09 11:05:25 -03:00
commit 0780879205
6 changed files with 73 additions and 30 deletions

View file

@ -10,7 +10,8 @@
<link rel="stylesheet" href="css/main.css"> <link rel="stylesheet" href="css/main.css">
</head> </head>
<body> <body>
<div class="header" data-ng-init="init()" data-ng-controller="HeaderController"> <div data-ng-init="init()" data-ng-controller="HeaderController">
<div class="header">
<div class="header-content"> <div class="header-content">
<figure class="left"> <figure class="left">
<img src="./img/logo-negative.svg" alt="" width="130"> <img src="./img/logo-negative.svg" alt="" width="130">
@ -38,8 +39,17 @@
</ul> </ul>
</section> </section>
</nav> </nav>
</div> </div>
<div ng-if='$root.flashMessage.message' class="panel callout radius" style="color:red" >
{{$root.flashMessage.type}}:
{{$root.flashMessage.message}}
<a href="#" ng-click="clearFlashMessage()" class="button tiny">Dismiss</a>
</div>
</div>
<div class="row"> <div class="row">
<div class="large-12 columns" ng-view></div> <div class="large-12 columns" ng-view></div>
</div> </div>

View file

@ -3,5 +3,6 @@
var config = { var config = {
networkName: 'testnet', networkName: 'testnet',
p2pApiKey: 'lwjd5qra8257b9', p2pApiKey: 'lwjd5qra8257b9',
p2pDebug: 3 p2pDebug: 3,
maxPeers: 5,
}; };

View file

@ -32,9 +32,12 @@ angular.module('copay.header').controller('HeaderController',
}; };
$scope.signout = function() { $scope.signout = function() {
$rootScope.isLogged = false;
Network.disconnect(function() { Network.disconnect(function() {
$location.path('signin'); $location.path('signin');
}); });
}; };
$scope.clearFlashMessage = function() {
$rootScope.flashMessage = {};
};
}); });

View file

@ -38,9 +38,16 @@ angular.module('copay.signin').controller('SigninController',
if (cid) { if (cid) {
Network.init(function() { Network.init(function() {
Network.connect(cid, function() { Network.connect(cid,
$location.path('peer'); function() {
$rootScope.$digest(); $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();
}); });
}); });
} }

View file

@ -16,10 +16,11 @@ var EventEmitter= imports.EventEmitter || require('events').EventEmitter;
*/ */
function CopayPeer(opts) { function CopayPeer(opts) {
opts = opts || {}; opts = opts || {};
this.peerId = opts.peerId; this.peerId = opts.peerId;
this.apiKey = opts.apiKey || 'lwjd5qra8257b9'; this.apiKey = opts.apiKey || 'lwjd5qra8257b9';
this.debug = opts.debug || 3; this.debug = opts.debug || 3;
this.maxPeers = opts.maxPeers || 5;
this.connectedPeers = []; this.connectedPeers = [];
} }
@ -84,7 +85,13 @@ CopayPeer.prototype._connectToPeers = function(peerIds) {
}; };
CopayPeer.prototype._onData = function(data, isInbound) { 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); console.log('### RECEIVED TYPE: %s FROM %s', obj.data.type, obj.sender);
switch(obj.data.type) { switch(obj.data.type) {
@ -101,7 +108,7 @@ CopayPeer.prototype._onData = function(data, isInbound) {
}; };
CopayPeer.prototype._sendPeers = function(peerIds) { 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, { this.send(peerIds, {
type: 'peerList', type: 'peerList',
peers: this.connectedPeers, 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; var self=this;
dataConn.on('open', function() { dataConn.on('open', function() {
@ -143,11 +152,13 @@ CopayPeer.prototype._setupConnectionHandlers = function(dataConn, isInbound, ope
}); });
dataConn.on('error', function(e) { dataConn.on('error', function(e) {
console.log('### ## INBOUND DATA ERROR',e ); //TODO console.log('### DATA ERROR',e ); //TODO
}); });
dataConn.on('close', function() { dataConn.on('close', function() {
console.log('### CLOSE RECV FROM:', dataConn.peer); //TODO
self._onClose(dataConn.peer); self._onClose(dataConn.peer);
if (typeof closeCallback === 'function') closeCallback();
}); });
}; };
@ -174,8 +185,18 @@ CopayPeer.prototype._setupPeerHandlers = function(openCallback) {
}); });
p.on('connection', function(dataConn) { 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); self._sendToOne(peerIds, data, cb);
}; };
CopayPeer.prototype.connectTo = function(peerId, cb) { CopayPeer.prototype.connectTo = function(peerId, openCallback, closeCallback ) {
var self = this; var self = this;
console.log('### STARTING TO CONNECT TO:' + peerId ); console.log('### STARTING TO CONNECT TO:' + peerId );
@ -242,7 +263,7 @@ CopayPeer.prototype.connectTo = function(peerId, cb) {
metadata: { message: 'hi copayer!' } metadata: { message: 'hi copayer!' }
}); });
self._setupConnectionHandlers(dataConn, false, cb); self._setupConnectionHandlers(dataConn, false, openCallback, closeCallback);
}; };
CopayPeer.prototype.disconnect = function(peerId, cb) { CopayPeer.prototype.disconnect = function(peerId, cb) {

View file

@ -97,14 +97,10 @@ angular.module('copay.network')
switch(data.type) { switch(data.type) {
case 'publicKeyRing': case 'publicKeyRing':
console.log('[network.js.91:data:]',data); //TODO
_checkWallet(data.publicKeyRing.id); _checkWallet(data.publicKeyRing.id);
var shouldSend = false; var shouldSend = false;
var recipients, pkr = $rootScope.publicKeyRing; var recipients, pkr = $rootScope.publicKeyRing;
console.log('### RECEIVED PKR FROM:', senderId);
if (pkr.merge(data.publicKeyRing, true) && !data.isBroadcast) { if (pkr.merge(data.publicKeyRing, true) && !data.isBroadcast) {
console.log('### BROADCASTING PKR'); console.log('### BROADCASTING PKR');
recipients = null; recipients = null;
@ -125,7 +121,6 @@ angular.module('copay.network')
}); });
} }
console.log('[network.js.126] END'); //TODO
_refreshUx(); _refreshUx();
break; break;
} }
@ -142,6 +137,7 @@ console.log('[network.js.126] END'); //TODO
var cp = $rootScope.cp = new copay.CopayPeer({ var cp = $rootScope.cp = new copay.CopayPeer({
apiKey: config.p2pApiKey, apiKey: config.p2pApiKey,
debug: config.p2pDebug, debug: config.p2pDebug,
maxPeers: config.maxPeers, // TODO: This should be on wallet configuration
}); });
_setupHandlers(); _setupHandlers();
@ -151,19 +147,24 @@ console.log('[network.js.126] END'); //TODO
}); });
}; };
var connect = function(peerId, cb) { var disconnect = function() {
if ($rootScope.cp) {
$rootScope.cp.connectTo(peerId, cb);
}
else
return cb();
};
var disconnect = function(cb) {
if ($rootScope.cp) { if ($rootScope.cp) {
$rootScope.cp.disconnect(); $rootScope.cp.disconnect();
} }
Storage.remove('peerData'); 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 { return {