Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat: adding relay #59

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -54,4 +55,4 @@
"greenkeeperio-bot <support@greenkeeper.io>",
"mayerwin <mayerwin@users.noreply.github.com>"
]
}
}
26 changes: 22 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()

Expand All @@ -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) => {
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?


callback()
})
}
Expand All @@ -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)
}

//
Expand Down