refactored disconnect as one more message

This commit is contained in:
Manuel Araoz 2014-08-19 11:38:53 -04:00
parent 789388af3c
commit f219da9740
3 changed files with 18 additions and 29 deletions

View File

@ -326,12 +326,12 @@ Wallet.prototype._onData = function(senderId, data, isInbound) {
preconditions.checkArgument(data);
preconditions.checkArgument(data.type);
this.updateTimestamp();
if (data.type !== 'walletId' && this.id !== data.walletId) {
this.emit('badMessage', senderId);
this.log('badMessage FROM:', senderId);
this.emit('corrupt', senderId);
return;
}
this.updateTimestamp();
switch (data.type) {
// This handler is repeaded on WalletFactory (#join). TODO
@ -361,6 +361,9 @@ Wallet.prototype._onData = function(senderId, data, isInbound) {
case 'addressbook':
this._onAddressBook(senderId, data, isInbound);
break;
case 'disconnect':
this._onDisconnect(senderId, data, isInbound);
break;
}
};
@ -437,7 +440,6 @@ Wallet.prototype.netStart = function(callback) {
net.removeAllListeners();
net.on('connect', self._onConnect.bind(self));
net.on('disconnect', self._onDisconnect.bind(self));
net.on('data', self._onData.bind(self));
var myId = self.getMyCopayerId();
@ -1747,7 +1749,13 @@ Wallet.prototype.indexDiscovery = function(start, change, copayerIndex, gap, cb)
Wallet.prototype.disconnect = function() {
this.log('## DISCONNECTING');
this.lock.release();
this.network.disconnect();
var self = this;
self.send(null, {
type: 'disconnect',
walletId: this.id,
}, function() {
self.network.cleanUp();
});
};
Wallet.prototype.getNetwork = function() {

View File

@ -46,7 +46,6 @@ Network.prototype.cleanUp = function() {
this.copayerForPeer = {};
this.connections = {};
this.criticalErr = '';
this.closing = 0;
this.removeAllListeners();
};
@ -215,9 +214,6 @@ Network.prototype._onMessage = function(enc) {
var self = this;
switch (payload.type) {
case 'disconnect':
this._onClose(sender);
break;
case 'hello':
if (this.allowedCopayerIds && !this.allowedCopayerIds[payload.copayerId]) {
this._deletePeer(sender);
@ -225,15 +221,11 @@ Network.prototype._onMessage = function(enc) {
}
this._addConnectedCopayer(payload.copayerId);
break;
default:
default:
this.emit('data', sender, payload);
}
};
Network.prototype._onClose = function(copayerId) {
// TODO
};
Network.prototype._setupConnectionHandlers = function(cb) {
preconditions.checkState(this.socket);
var self = this;
@ -244,7 +236,7 @@ Network.prototype._setupConnectionHandlers = function(cb) {
});
if (typeof cb === 'function') cb();
});
self.socket.on('message', function (m) {
self.socket.on('message', function(m) {
// delay execution, to improve error handling
setTimeout(function() {
self._onMessage(m);
@ -280,7 +272,7 @@ Network.prototype._setInboundPeerAuth = function(peerId) {
Network.prototype.setCopayerId = function(copayerId) {
preconditions.checkState(!this.started, 'network already started: can not change peerId');
this.copayerId = copayerId;
this.copayerIdBuf = new Buffer(copayerId, 'hex');
this.peerId = this.peerFromCopayer(this.copayerId);
@ -394,15 +386,4 @@ Network.prototype.lockIncommingConnections = function(allowedCopayerIdsArray) {
}
};
Network.prototype.disconnect = function(cb, forced) {
var self = this;
self.closing = 1;
self.send(null, {
type: 'disconnect'
}, function() {
self.cleanUp();
if (typeof cb === 'function') cb();
});
};
module.exports = Network;

View File

@ -120,8 +120,8 @@ angular.module('copayApp.services')
notification.enableHtml5Mode(); // for chrome: if support, enable it
w.on('badMessage', function(peerId) {
notification.error('Error', 'Received wrong message from peer ' + peerId);
w.on('corrupt', function(peerId) {
notification.error('Error', 'Received corrupt message from ' + peerId);
});
w.on('ready', function(myPeerID) {
$rootScope.wallet = w;