many bugs fix in peer connections, and ux

This commit is contained in:
Matias Alejo Garcia 2014-04-12 13:37:41 -03:00
parent 333ecb352a
commit a28e98e85d
5 changed files with 61 additions and 44 deletions

View File

@ -74,11 +74,11 @@
<div class="large-6 columns">
<h3>Open a Existing Wallet</h3>
<select class="form-control" >
<option data-ng-repeat="walletId in listWalletIds()" value="walletId">{{walletId}}</option>
<option ng-repeat="walletId in listWalletIds()" ng-model="sel" value="walletId">{{walletId}}</option>
</select>
</div>
<div class="large-3 columns">
<button class="button primary expand round" type="button" ng-click="open(walletId)">Open</button>
<button class="button primary expand round" type="button" ng-click="open(sel || $scope.sel)">Open</button>
</div>
</div>
<hr>

View File

@ -35,8 +35,12 @@ angular.module('copay.header').controller('HeaderController',
};
$scope.signout = function() {
console.log('[header.js.37:signout:]'); //TODO
Network.disconnect(function() {
console.log('[header.js.41] disconnect CB'); //TODO
$location.path('signin');
$rootScope.$digest();
});
};

View File

@ -67,6 +67,7 @@ CopayPeer.prototype._showConnectedPeers = function() {
};
CopayPeer.prototype._onClose = function(peerId) {
console.log('[CopayPeer.js.70] _onClose'); //TODO
this.connectedPeers = CopayPeer._arrayRemove(peerId, this.connectedPeers);
this._notify();
};
@ -92,7 +93,7 @@ CopayPeer.prototype._onData = function(data, isInbound) {
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, obj.data);
switch(obj.data.type) {
case 'peerList':
@ -156,13 +157,16 @@ CopayPeer.prototype._setupConnectionHandlers = function(
});
dataConn.on('close', function() {
console.log('### CLOSE RECV FROM:', dataConn.peer); //TODO
if (self.closing) return;
console.log('### CLOSE RECV FROM:', dataConn.peer);
self._onClose(dataConn.peer);
if (typeof closeCallback === 'function') closeCallback();
});
};
CopayPeer.prototype._notify = function(newPeer) {
console.log('[CopayPeer.js.168:_notify:]'); //TODO
this._showConnectedPeers();
this.emit('networkChange', newPeer);
};
@ -182,6 +186,10 @@ CopayPeer.prototype._setupPeerHandlers = function(openCallback) {
p.on('error', function(err) {
console.log('### PEER ERROR:', err);
self.peer.disconnect();
self.peer.destroy();
self.peer = null;
this.emit('abort');
});
p.on('connection', function(dataConn) {
@ -266,9 +274,13 @@ CopayPeer.prototype.connectTo = function(peerId, openCallback, closeCallback ) {
};
CopayPeer.prototype.disconnect = function(peerId, cb) {
console.log('[CopayPeer.js.268:disconnect:]'); //TODO
var self = this;
self.closing = 1;
this.send(null, { type: 'disconnect' }, function() {
console.log('[CopayPeer.js.273] disconnect CB'); //TODO
self.connectedPeers = [];
self.peerId = null;
if (self.peer) {
@ -276,6 +288,7 @@ CopayPeer.prototype.disconnect = function(peerId, cb) {
self.peer.destroy();
self.peer = null;
}
self.closing = 0;
if (typeof cb === 'function') cb();
});
};

View File

@ -72,6 +72,7 @@ PublicKeyRing.fromObj = function (data) {
var w = new PublicKeyRing(config);
w.id = data.id;
w.requiredCopayers = data.requiredCopayers;
w.totalCopayers = data.totalCopayers;
w.addressIndex = data.addressIndex;

View File

@ -110,13 +110,13 @@ console.log('[network.js.97:priv:]',priv); //TODO
$rootScope.txProposals = new copay.TxProposals.fromObj(txp);
$rootScope.PrivateKey = new copay.PrivateKey.fromObj(priv); //TODO secure
// Add our key JIC
// JIC: Add our key
try {
$rootScope.publicKeyRing.addCopayer(
$rootScope.PrivateKey.getBIP32().extendedPublicKeyString()
);
} catch (e) {
console.log('[network.js.103]', e); //TODO
console.log('NOT NECCESARY AN ERROR:', e); //TODO
};
ret = true;
}
@ -127,7 +127,8 @@ console.log('[network.js.103]', e); //TODO
console.log('### CLOSING WALLET');
$rootScope.walletId = null;
$rootScope.publicKeyRing = null;
//TODO
$rootScope.privateKey = null;
$rootScope.txProposals = null;
};
var _checkWallet = function(walletId, allowChange) {
@ -137,9 +138,6 @@ console.log('[network.js.103]', e); //TODO
return;
if ($rootScope.walletId && $rootScope.walletId !== walletId) {
if (allowChange)
closeWallet();
else
throw new Error('message to wrong walletID');
}
@ -150,37 +148,9 @@ console.log('[network.js.103]', e); //TODO
};
var _handlePublicKeyRing = function(senderId, data, isInbound) {
var cp = $rootScope.cp;
_checkWallet(data.publicKeyRing.id, true);
var shouldSend = false;
var recipients, pkr = $rootScope.publicKeyRing;
var inPKR = copay.PublicKeyRing.fromObj(data.publicKeyRing);
if (pkr.merge(inPKR, true) && !data.isBroadcast) {
console.log('### BROADCASTING PKR');
recipients = null;
shouldSend = true;
}
else if (isInbound && !data.isBroadcast) {
// always replying to connecting peer
console.log('### REPLYING PKR TO:', senderId);
recipients = senderId;
shouldSend = true;
}
if (shouldSend) {
console.log('### SENDING PKR TO:', recipients);
cp.send( recipients, {
type: 'publicKeyRing',
publicKeyRing: $rootScope.publicKeyRing.toObj(),
});
}
};
var sendTxProposals = function(recipients) {
var cp = $rootScope.cp;
console.log('### SENDING txProposals TO:', recipients||'All');
console.log('### SENDING txProposals TO:', recipients||'All', $rootScope.txProposals);
cp.send( recipients, {
type: 'txProposals',
txProposals: $rootScope.txProposals.toObj(),
@ -197,9 +167,36 @@ console.log('[network.js.103]', e); //TODO
});
};
var _handlePublicKeyRing = function(senderId, data, isInbound) {
var cp = $rootScope.cp;
_checkWallet(data.publicKeyRing.id);
var shouldSend = false;
var recipients, pkr = $rootScope.publicKeyRing;
var inPKR = copay.PublicKeyRing.fromObj(data.publicKeyRing);
console.log('[network.js.176:inPKR:]',inPKR); //TODO
console.log('[network.js.178:pkr:]',pkr); //TODO
if (pkr.merge(inPKR, true) && !data.isBroadcast) {
console.log('### BROADCASTING PKR');
recipients = null;
shouldSend = true;
}
else if (isInbound && !data.isBroadcast) {
// always replying to connecting peer
console.log('### REPLYING PKR TO:', senderId);
recipients = senderId;
shouldSend = true;
}
console.log('[network.js.189:shouldSend:]',shouldSend); //TODO
if (shouldSend) {
sendPublicKeyRing(recipients);
}
_refreshUx();
};
var _handleTxProposals = function(senderId, data, isInbound) {
var cp = $rootScope.cp;
_checkWallet(data.txProposals.walletId, false);
_checkWallet(data.txProposals.walletId);
var shouldSend = false;
console.log('RECV TXPROPOSAL:',data); //TODO
@ -220,7 +217,7 @@ console.log('[network.js.103]', e); //TODO
}
if (shouldSend) {
this.sendTxProposals(recipients);
sendTxProposals(recipients);
}
};
@ -233,9 +230,11 @@ console.log('[network.js.103]', e); //TODO
case 'txProposals':
_handleTxProposals(senderId, data, isInbound);
break;
case 'abort':
disconnect();
_refreshUx();
break;
}
_refreshUx();
};
var _setupHandlers = function () {
var cp = $rootScope.cp;
@ -261,9 +260,9 @@ console.log('[network.js.103]', e); //TODO
if ($rootScope.cp) {
$rootScope.cp.disconnect();
}
closeWallet();
Storage.remove('peerData');
$rootScope.isLogged = false;
_refreshUx();
};
var connect = function(peerId, openCallback, failCallback) {