Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
feat: emit peers every 10 secs
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Jul 23, 2017
1 parent e556d8f commit 598fd94
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 28 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"aegir": "^11.0.2",
"chai": "^4.1.0",
"libp2p-swarm": "~0.31.0",
"libp2p-tcp": "~0.10.1",
"libp2p-tcp": "~0.10.2",
"pre-commit": "^1.2.2"
},
"dependencies": {
Expand All @@ -56,4 +56,4 @@
"Nuno Nogueira <nunofmn@gmail.com>",
"Richard Littauer <richard.littauer@gmail.com>"
]
}
}
38 changes: 12 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')
const EventEmitter = require('events').EventEmitter
const debug = require('debug')
const includes = require('lodash/includes')
const setImmediate = require('async/setImmediate')

const log = debug('libp2p:railing')
Expand All @@ -15,49 +14,36 @@ class Railing extends EventEmitter {
constructor (bootstrapers) {
super()
this.bootstrapers = bootstrapers
this.interval = null
}

start (callback) {
setImmediate(() => callback())
setImmediate(() => {
this.bootstrapers.forEach((candidate) => {
candidate = multiaddr(candidate)
if (this.interval) { return }

let ma
if (includes(candidate.protoNames(), 'ipfs')) {
ma = candidate.decapsulate('ipfs')
}
this.interval = setInterval(() => {
this.bootstrapers.forEach((candidate) => {
const ma = multiaddr(candidate)

// TODO: switch for multiaddr.getPeerId once merged
let peerIdB58Str
try {
peerIdB58Str = candidate.stringTuples().filter((tuple) => {
if (tuple[0] === candidate.protos().filter((proto) => {
return proto.name === 'ipfs'
})[0].code) {
return true
}
})[0][1]
} catch (e) {
throw new Error('Error extracting IPFS id from multiaddr', e)
}
const peerId = PeerId.createFromB58String(ma.getPeerId())

const peerId = PeerId.createFromB58String(peerIdB58Str)
PeerInfo.create(peerId, (err, peerInfo) => {
if (err) {
return log.error('Error creating PeerInfo from bootstrap peer', err)
}
if (err) { return log.error('Invalid bootstrap peer id', err) }

peerInfo.multiaddrs.add(ma)

this.emit('peer', peerInfo)
})
})
})
}, 10000)
}

stop (callback) {
setImmediate(callback)
if (this.interval) {
clearInterval(this.interval)
this.interval = null
}
}
}

Expand Down

0 comments on commit 598fd94

Please sign in to comment.