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

View File

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

View File

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