From 013c2756faeb9914f37fd8a6394fd0364d5548b5 Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Wed, 10 Apr 2019 12:54:05 +0200 Subject: [PATCH 1/5] fix: create separate events for connection tracking --- README.md | 12 +++++++++++- src/connection/manager.js | 6 +++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4d2014d..06190fc 100644 --- a/README.md +++ b/README.md @@ -198,7 +198,17 @@ Emitted when the switch encounters an error. ### `switch.on('peer-mux-closed', (peer) => {})` -- `peer`: is instance of [PeerInfo][] that has info of the peer we have just closed a muxed connection. +- `peer`: is instance of [PeerInfo][] that has info of the peer we have just closed a muxed connection with. + +### `switch.on('connection:start', (peer) => {})` +This will be triggered anytime a new connection, regardless of state, is added to the switches internal connection tracking. + +- `peer`: is instance of [PeerInfo][] that has info of the peer we have just started a connection with. + +### `switch.on('connection:end', (peer) => {})` +This will be triggered anytime an existing connection, regardless of state, is removed from the switches internal connection tracking. + +- `peer`: is instance of [PeerInfo][] that has info of the peer we have just closed a connection with. ### `switch.on('start', () => {})` diff --git a/src/connection/manager.js b/src/connection/manager.js index 1f17873..4316eb1 100644 --- a/src/connection/manager.js +++ b/src/connection/manager.js @@ -33,7 +33,8 @@ class ConnectionManager { // Only add it if it's not there if (!this.get(connection)) { this.connections[connection.theirB58Id].push(connection) - this.switch.emit('peer-mux-established', connection.theirPeerInfo) + this.switch.emit('connection:start', connection.theirPeerInfo) + connection.once('muxed', () => this.switch.emit('peer-mux-established', connection.theirPeerInfo)) } } @@ -99,6 +100,9 @@ class ConnectionManager { connection.theirPeerInfo.disconnect() this.switch.emit('peer-mux-closed', connection.theirPeerInfo) } + + // A tracked connection was closed, let the world know + this.switch.emit('connection:end', connection.theirPeerInfo) } /** From 7dc4fd19978fb59cb193d5573c8c110297028a11 Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Wed, 10 Apr 2019 14:17:06 +0200 Subject: [PATCH 2/5] fix: remove the connection on disconnect instead of disconnecting --- src/connection/base.js | 1 + src/connection/index.js | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/connection/base.js b/src/connection/base.js index cb3d5a5..36f7842 100644 --- a/src/connection/base.js +++ b/src/connection/base.js @@ -80,6 +80,7 @@ class BaseConnection extends EventEmitter { * @returns {void} */ _onDisconnected () { + this.switch.connection.remove(this) this.log('disconnected from %s', this.theirB58Id) this.emit('close') this.removeAllListeners() diff --git a/src/connection/index.js b/src/connection/index.js index 1bfe139..a2df251 100644 --- a/src/connection/index.js +++ b/src/connection/index.js @@ -269,8 +269,6 @@ class ConnectionFSM extends BaseConnection { _onDisconnecting () { this.log('disconnecting from %s', this.theirB58Id, Boolean(this.muxer)) - this.switch.connection.remove(this) - delete this.switch.conns[this.theirB58Id] let tasks = [] From fedb927e5e6c7da77b2c870137923102815334d2 Mon Sep 17 00:00:00 2001 From: dirkmc Date: Wed, 10 Apr 2019 14:18:23 +0200 Subject: [PATCH 3/5] chore: update README.md Co-Authored-By: jacobheun --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 06190fc..b5e5d47 100644 --- a/README.md +++ b/README.md @@ -206,7 +206,7 @@ This will be triggered anytime a new connection, regardless of state, is added t - `peer`: is instance of [PeerInfo][] that has info of the peer we have just started a connection with. ### `switch.on('connection:end', (peer) => {})` -This will be triggered anytime an existing connection, regardless of state, is removed from the switches internal connection tracking. +This will be triggered anytime an existing connection, regardless of state, is removed from the switch's internal connection tracking. - `peer`: is instance of [PeerInfo][] that has info of the peer we have just closed a connection with. From 8215e8e6ea71bd02c6aa0f29a667c2d4b1590a69 Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Wed, 10 Apr 2019 15:08:44 +0200 Subject: [PATCH 4/5] chore: update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b5e5d47..1a2eeee 100644 --- a/README.md +++ b/README.md @@ -201,7 +201,7 @@ Emitted when the switch encounters an error. - `peer`: is instance of [PeerInfo][] that has info of the peer we have just closed a muxed connection with. ### `switch.on('connection:start', (peer) => {})` -This will be triggered anytime a new connection, regardless of state, is added to the switches internal connection tracking. +This will be triggered anytime a new connection is created. - `peer`: is instance of [PeerInfo][] that has info of the peer we have just started a connection with. From a518a50cf9ce3848285a18060ab7d58175ff1b65 Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Wed, 10 Apr 2019 15:29:35 +0200 Subject: [PATCH 5/5] fix: resolve issues with tests --- src/connection/manager.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/connection/manager.js b/src/connection/manager.js index 4316eb1..b6abd36 100644 --- a/src/connection/manager.js +++ b/src/connection/manager.js @@ -34,7 +34,11 @@ class ConnectionManager { if (!this.get(connection)) { this.connections[connection.theirB58Id].push(connection) this.switch.emit('connection:start', connection.theirPeerInfo) - connection.once('muxed', () => this.switch.emit('peer-mux-established', connection.theirPeerInfo)) + if (connection.getState() === 'MUXED') { + this.switch.emit('peer-mux-established', connection.theirPeerInfo) + } else { + connection.once('muxed', () => this.switch.emit('peer-mux-established', connection.theirPeerInfo)) + } } } @@ -82,8 +86,10 @@ class ConnectionManager { remove (connection) { // No record of the peer, disconnect it if (!this.connections[connection.theirB58Id]) { - connection.theirPeerInfo.disconnect() - this.switch.emit('peer-mux-closed', connection.theirPeerInfo) + if (connection.theirPeerInfo) { + connection.theirPeerInfo.disconnect() + this.switch.emit('peer-mux-closed', connection.theirPeerInfo) + } return }