From 75f573308bd5142ef5e7a531a84d2277745765e1 Mon Sep 17 00:00:00 2001 From: dmitriy ryajov Date: Sat, 11 Mar 2017 01:28:59 -0800 Subject: [PATCH 1/2] feat: adding relay --- package.json | 3 ++- src/index.js | 26 ++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index f67decccc5..af740ec93d 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "dependencies": { "libp2p-ping": "~0.3.1", "libp2p-swarm": "~0.26.17", + "libp2p-circuit": "~0.0.1", "mafmt": "^2.1.6", "multiaddr": "^2.2.1", "peer-book": "~0.3.1", @@ -54,4 +55,4 @@ "greenkeeperio-bot ", "mayerwin " ] -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index 47984055ce..b48c5bade6 100644 --- a/src/index.js +++ b/src/index.js @@ -10,6 +10,8 @@ const EE = require('events').EventEmitter const assert = require('assert') const Ping = require('libp2p-ping') const setImmediate = require('async/setImmediate') +const series = require('async/series') +const Relay = require('libp2p-circuit') exports = module.exports @@ -25,6 +27,7 @@ class Node { this.peerInfo = _peerInfo this.peerBook = _peerBook || new PeerBook() this.isOnline = false + this.relay = _options.relay ? new Relay(this) : null this.discovery = new EE() @@ -41,6 +44,11 @@ class Node { // If muxer exists, we can use Identify this.swarm.connection.reuse() + if (!this.relay) { + // If muxer exists, we can use Relay + this.swarm.connection.relay() + } + // Received incommind dial and muxer upgrade happened, reuse this // muxed connection this.swarm.on('peer-mux-established', (peerInfo) => { @@ -100,7 +108,7 @@ class Node { this.swarm.transport.add( transport.tag || transport.constructor.name, transport) } else if (transport.constructor && - transport.constructor.name === 'WebSockets') { + transport.constructor.name === 'WebSockets') { // TODO find a cleaner way to signal that a transport is always // used for dialing, even if no listener ws = transport @@ -119,10 +127,16 @@ class Node { if (this.modules.discovery) { this.modules.discovery.forEach((discovery) => { - setImmediate(() => discovery.start(() => {})) + setImmediate(() => discovery.start(() => { + })) }) } + // are we a relay + if (this.relay) { + return this.relay.start(callback) + } + callback() }) } @@ -135,11 +149,15 @@ class Node { if (this.modules.discovery) { this.modules.discovery.forEach((discovery) => { - setImmediate(() => discovery.stop(() => {})) + setImmediate(() => discovery.stop(() => { + })) }) } - this.swarm.close(callback) + series([ + (cb) => (this.relay ? this.relay.stop(cb) : cb()), + (cb) => this.swarm.close(cb) + ], callback) } // From 437b0a24207491b3561b6ffa29380c0917d25326 Mon Sep 17 00:00:00 2001 From: dmitriy ryajov Date: Mon, 27 Mar 2017 11:30:52 -0700 Subject: [PATCH 2/2] feat: adding relay --- src/index.js | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/index.js b/src/index.js index b48c5bade6..a32d755ef9 100644 --- a/src/index.js +++ b/src/index.js @@ -11,7 +11,6 @@ const assert = require('assert') const Ping = require('libp2p-ping') const setImmediate = require('async/setImmediate') const series = require('async/series') -const Relay = require('libp2p-circuit') exports = module.exports @@ -27,7 +26,6 @@ class Node { this.peerInfo = _peerInfo this.peerBook = _peerBook || new PeerBook() this.isOnline = false - this.relay = _options.relay ? new Relay(this) : null this.discovery = new EE() @@ -44,10 +42,8 @@ class Node { // If muxer exists, we can use Identify this.swarm.connection.reuse() - if (!this.relay) { - // If muxer exists, we can use Relay - this.swarm.connection.relay() - } + // If muxer exists, we can use Relay + this.swarm.connection.relay() // Received incommind dial and muxer upgrade happened, reuse this // muxed connection @@ -132,11 +128,6 @@ class Node { }) } - // are we a relay - if (this.relay) { - return this.relay.start(callback) - } - callback() }) } @@ -155,7 +146,6 @@ class Node { } series([ - (cb) => (this.relay ? this.relay.stop(cb) : cb()), (cb) => this.swarm.close(cb) ], callback) }