Skip to content

Commit

Permalink
peers command shouldn't list yourself
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew committed Apr 23, 2021
1 parent 3478e5c commit 22852b3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/commands/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ const forage = require('../forage');

(async () => {
var db = forage.connectDB()
await forage.connectIPFS(db)
var ipfsID = await forage.connectIPFS(db)

var peerIds = await forage.core.activePeers()

forage.core.listPeers(db).then(async function(peers) {
forage.core.listPeers(db, ipfsID.id).then(async function(peers) {
peers.forEach(peer => {
if(peerIds.includes(peer)){
console.log(`${peer} (online)`)
Expand Down
8 changes: 4 additions & 4 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,22 +248,22 @@ async function exportPackages(db) {
return await ipfs.files.stat('/forage/export')
}

function listPeers(db) {
function listPeers(db, self) {
return new Promise((resolve, reject) => {
var names = []
db.createKeyStream({gte: 'repub:', lt: 'repub:~'})
.on('data', function (data) {
var parts = data.split(':')
names.push(parts[4])
if(parts[4] !== self){ names.push(parts[4]) }
})
.on('end', function () {
resolve( [...new Set(names)])
})
})
}

async function dialPeers(db, topic) {
listPeers(db).then(async function(peers) {
async function dialPeers(db, self, topic) {
listPeers(db, self).then(async function(peers) {
peers.forEach(async function(peer) {
try {
debug('dialing', peer)
Expand Down
6 changes: 3 additions & 3 deletions lib/forage.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ async function connectIPFS(db){
ipfsID = await ipfs.id()
console.log('Connected to IPFS')
subscribePackageAnnoucements()
await core.dialPeers(db, packageAnnoucementsTopic)
await core.dialPeers(db, ipfsID, packageAnnoucementsTopic)
return ipfsID;
} catch {
try{
var ipfsd = await core.startIPFS()
subscribePackageAnnoucements()
ipfsID = await ipfsd.api.id()
await core.dialPeers(db, packageAnnoucementsTopic)
await core.dialPeers(db, ipfsID, packageAnnoucementsTopic)
return ipfsID;
} catch(e){
console.log('ERROR', e)
Expand Down Expand Up @@ -150,7 +150,7 @@ async function defaultAnnounceCb(msg) {
async function subscribePackageAnnoucements(receiveMsg = defaultAnnounceCb) {
try {
await ipfs.pubsub.subscribe(packageAnnoucementsTopic, receiveMsg)
console.log(`Subscribed to '${packageAnnoucementsTopic}' pubsub topic`)
debug(`Subscribed to '${packageAnnoucementsTopic}' pubsub topic`)
await core.savePeers(db, packageAnnoucementsTopic)
} catch(e) {
console.error(`Failed to subscribe to '${packageAnnoucementsTopic}' pubsub topic`)
Expand Down

0 comments on commit 22852b3

Please sign in to comment.