From 7b9bf40ee5782019932058d7086eaf4e4205ed51 Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Fri, 30 Jun 2017 17:06:09 +0100 Subject: [PATCH 1/5] emit online event once online --- src/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/index.js b/src/index.js index 35acefb9c4..867c24266e 100644 --- a/src/index.js +++ b/src/index.js @@ -187,6 +187,8 @@ class Node extends EventEmitter { return each(this.modules.discovery, (d, cb) => d.start(cb), cb) } cb() + + this.emit('online') }, (cb) => { if (this._dht) { From 29608616d0c6291146e5ef21d74f93bf10986b24 Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Thu, 6 Jul 2017 10:09:36 +0100 Subject: [PATCH 2/5] isOn -> isStarted + start and stop events --- src/index.js | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/index.js b/src/index.js index 867c24266e..8d3f8b5dc2 100644 --- a/src/index.js +++ b/src/index.js @@ -17,7 +17,7 @@ const multiaddr = require('multiaddr') exports = module.exports -const OFFLINE_ERROR_MESSAGE = 'The libp2p node is not started yet' +const NOT_STARTED_ERROR_MESSAGE = 'The libp2p node is not started yet' class Node extends EventEmitter { constructor (_modules, _peerInfo, _peerBook, _options) { @@ -28,7 +28,7 @@ class Node extends EventEmitter { this.modules = _modules this.peerInfo = _peerInfo this.peerBook = _peerBook || new PeerBook() - this.isOnline = false + this.isStarted = false this.swarm = new Swarm(this.peerInfo, this.peerBook) @@ -174,9 +174,6 @@ class Node extends EventEmitter { series([ (cb) => this.swarm.listen(cb), (cb) => { - // listeners on, libp2p is on - this.isOnline = true - if (ws) { // always add dialing on websockets this.swarm.transport.add(ws.tag || ws.constructor.name, ws) @@ -187,14 +184,16 @@ class Node extends EventEmitter { return each(this.modules.discovery, (d, cb) => d.start(cb), cb) } cb() - - this.emit('online') }, (cb) => { if (this._dht) { return this._dht.start(cb) } cb() + }, + (cb) => { + this.emit('start') + cb() } ], callback) } @@ -203,7 +202,7 @@ class Node extends EventEmitter { * Stop the libp2p node by closing its listeners and open connections */ stop (callback) { - this.isOnline = false + this.isStarted = false if (this.modules.discovery) { this.modules.discovery.forEach((discovery) => { @@ -218,16 +217,20 @@ class Node extends EventEmitter { } cb() }, - (cb) => this.swarm.close(cb) + (cb) => this.swarm.close(cb), + (cb) => { + this.emit('stop') + cb() + } ], callback) } - isOn () { - return this.isOnline + isStarted () { + return this.isStarted } ping (peer, callback) { - assert(this.isOn(), OFFLINE_ERROR_MESSAGE) + assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE) this._getPeerInfo(peer, (err, peerInfo) => { if (err) { return callback(err) @@ -238,7 +241,7 @@ class Node extends EventEmitter { } dial (peer, protocol, callback) { - assert(this.isOn(), OFFLINE_ERROR_MESSAGE) + assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE) if (typeof protocol === 'function') { callback = protocol @@ -261,7 +264,7 @@ class Node extends EventEmitter { } hangUp (peer, callback) { - assert(this.isOn(), OFFLINE_ERROR_MESSAGE) + assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE) this._getPeerInfo(peer, (err, peerInfo) => { if (err) { From 8c9973b499575df60afdd9cbe80bfbdbed1e2a23 Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Fri, 30 Jun 2017 17:06:09 +0100 Subject: [PATCH 3/5] emit online event once online --- src/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/index.js b/src/index.js index 35acefb9c4..867c24266e 100644 --- a/src/index.js +++ b/src/index.js @@ -187,6 +187,8 @@ class Node extends EventEmitter { return each(this.modules.discovery, (d, cb) => d.start(cb), cb) } cb() + + this.emit('online') }, (cb) => { if (this._dht) { From 6d3c95b8f41e6a457059d38dcea89db133adf766 Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Thu, 6 Jul 2017 10:09:36 +0100 Subject: [PATCH 4/5] isOn -> isStarted + start and stop events --- src/index.js | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/index.js b/src/index.js index 867c24266e..8d3f8b5dc2 100644 --- a/src/index.js +++ b/src/index.js @@ -17,7 +17,7 @@ const multiaddr = require('multiaddr') exports = module.exports -const OFFLINE_ERROR_MESSAGE = 'The libp2p node is not started yet' +const NOT_STARTED_ERROR_MESSAGE = 'The libp2p node is not started yet' class Node extends EventEmitter { constructor (_modules, _peerInfo, _peerBook, _options) { @@ -28,7 +28,7 @@ class Node extends EventEmitter { this.modules = _modules this.peerInfo = _peerInfo this.peerBook = _peerBook || new PeerBook() - this.isOnline = false + this.isStarted = false this.swarm = new Swarm(this.peerInfo, this.peerBook) @@ -174,9 +174,6 @@ class Node extends EventEmitter { series([ (cb) => this.swarm.listen(cb), (cb) => { - // listeners on, libp2p is on - this.isOnline = true - if (ws) { // always add dialing on websockets this.swarm.transport.add(ws.tag || ws.constructor.name, ws) @@ -187,14 +184,16 @@ class Node extends EventEmitter { return each(this.modules.discovery, (d, cb) => d.start(cb), cb) } cb() - - this.emit('online') }, (cb) => { if (this._dht) { return this._dht.start(cb) } cb() + }, + (cb) => { + this.emit('start') + cb() } ], callback) } @@ -203,7 +202,7 @@ class Node extends EventEmitter { * Stop the libp2p node by closing its listeners and open connections */ stop (callback) { - this.isOnline = false + this.isStarted = false if (this.modules.discovery) { this.modules.discovery.forEach((discovery) => { @@ -218,16 +217,20 @@ class Node extends EventEmitter { } cb() }, - (cb) => this.swarm.close(cb) + (cb) => this.swarm.close(cb), + (cb) => { + this.emit('stop') + cb() + } ], callback) } - isOn () { - return this.isOnline + isStarted () { + return this.isStarted } ping (peer, callback) { - assert(this.isOn(), OFFLINE_ERROR_MESSAGE) + assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE) this._getPeerInfo(peer, (err, peerInfo) => { if (err) { return callback(err) @@ -238,7 +241,7 @@ class Node extends EventEmitter { } dial (peer, protocol, callback) { - assert(this.isOn(), OFFLINE_ERROR_MESSAGE) + assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE) if (typeof protocol === 'function') { callback = protocol @@ -261,7 +264,7 @@ class Node extends EventEmitter { } hangUp (peer, callback) { - assert(this.isOn(), OFFLINE_ERROR_MESSAGE) + assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE) this._getPeerInfo(peer, (err, peerInfo) => { if (err) { From da9bff43ec73bbec216b601535f65995c1177f50 Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Thu, 6 Jul 2017 15:21:39 +0100 Subject: [PATCH 5/5] corrected isStarted --- src/index.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 8d3f8b5dc2..b9a09dcf19 100644 --- a/src/index.js +++ b/src/index.js @@ -28,7 +28,7 @@ class Node extends EventEmitter { this.modules = _modules this.peerInfo = _peerInfo this.peerBook = _peerBook || new PeerBook() - this.isStarted = false + this._isStarted = false this.swarm = new Swarm(this.peerInfo, this.peerBook) @@ -156,7 +156,6 @@ class Node extends EventEmitter { } }) this.peerInfo.multiaddrs.replace(maOld, maNew) - const multiaddrs = this.peerInfo.multiaddrs.toArray() transports.forEach((transport) => { @@ -186,6 +185,9 @@ class Node extends EventEmitter { cb() }, (cb) => { + // TODO: chicken-and-egg problem: + // have to set started here because DHT requires libp2p is already started + this._isStarted = true if (this._dht) { return this._dht.start(cb) } @@ -202,7 +204,7 @@ class Node extends EventEmitter { * Stop the libp2p node by closing its listeners and open connections */ stop (callback) { - this.isStarted = false + this._isStarted = false if (this.modules.discovery) { this.modules.discovery.forEach((discovery) => { @@ -226,7 +228,7 @@ class Node extends EventEmitter { } isStarted () { - return this.isStarted + return this._isStarted } ping (peer, callback) {