From f219da9740061b22d62995497ffd21ab7c01f9d4 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Tue, 19 Aug 2014 11:38:53 -0400 Subject: [PATCH] refactored disconnect as one more message --- js/models/core/Wallet.js | 18 +++++++++++++----- js/models/network/Async.js | 25 +++---------------------- js/services/controllerUtils.js | 4 ++-- 3 files changed, 18 insertions(+), 29 deletions(-) diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index a86e6a177..a9c9584af 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -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() { diff --git a/js/models/network/Async.js b/js/models/network/Async.js index ab7f843f9..994761bfa 100644 --- a/js/models/network/Async.js +++ b/js/models/network/Async.js @@ -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; diff --git a/js/services/controllerUtils.js b/js/services/controllerUtils.js index 463ee0a16..48ab8049f 100644 --- a/js/services/controllerUtils.js +++ b/js/services/controllerUtils.js @@ -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;