refactored disconnect as one more message

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

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;