add maxPeer check

This commit is contained in:
Matias Alejo Garcia 2014-04-09 11:05:25 -03:00
parent 9eadda0ed3
commit 0780879205
6 changed files with 75 additions and 32 deletions

View File

@ -10,7 +10,8 @@
<link rel="stylesheet" href="css/main.css">
</head>
<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">
<figure class="left">
<img src="./img/logo-negative.svg" alt="" width="130">
@ -38,8 +39,17 @@
</ul>
</section>
</nav>
</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="large-12 columns" ng-view></div>
</div>

View File

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

View File

@ -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 = {};
};
});

View File

@ -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();
});
});
}

View File

@ -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) {

View File

@ -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 {