Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

Commit

Permalink
feat: update Ping to latests libp2p versions, fix tests, add more tes…
Browse files Browse the repository at this point in the history
…ts, update API
  • Loading branch information
daviddias committed Dec 1, 2016
1 parent f1434bb commit fc1be3a
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 99 deletions.
41 changes: 20 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
{
"name": "libp2p-ping",
"version": "0.2.0",
"description": "Node.js IPFS Ping protocol implementation",
"main": "lib/index.js",
"jsnext:main": "src/index.js",
"description": "libp2p Ping protocol implementation",
"main": "src/index.js",
"pre-commit": [
"lint",
"test"
],
"scripts": {
"test": "aegir-test --env node",
"test": "aegir-test",
"lint": "aegir-lint",
"build": "aegir-build node",
"release": "aegir-release --env node",
"release-minor": "aegir-release --env node --type minor",
"release-major": "aegir-release --env node --type major",
"release": "aegir-release",
"release-minor": "aegir-release --type minor",
"release-major": "aegir-release --type major",
"coverage": "aegir-coverage",
"coverage-publish": "aegir-coverage publish"
},
"repository": {
"type": "git",
"url": "https://github.com/diasdavid/js-ipfs-ping.git"
"url": "https://github.com/libp2p/js-ipfs-ping.git"
},
"keywords": [
"IPFS"
],
"author": "David Dias <daviddias@ipfs.io>",
"license": "MIT",
"bugs": {
"url": "https://github.com/diasdavid/js-ipfs-ping/issues"
"url": "https://github.com/libp2p/js-ipfs-ping/issues"
},
"homepage": "https://github.com/diasdavid/js-ipfs-ping",
"homepage": "https://github.com/libp2p/js-ipfs-ping",
"devDependencies": {
"aegir": "^9.1.1",
"aegir": "^9.1.2",
"async": "^2.1.4",
"chai": "^3.5.0",
"debug": "^2.2.0",
"debug": "^2.3.3",
"gulp": "^3.9.1",
"libp2p-swarm": "^0.26.0",
"libp2p-tcp": "^0.8.1",
"multiaddr": "^2.0.2",
"peer-id": "^0.7.0",
"peer-info": "^0.7.0",
"pre-commit": "^1.1.2",
"libp2p-swarm": "^0.26.3",
"libp2p-tcp": "^0.9.1",
"multiaddr": "^2.1.1",
"peer-id": "^0.8.0",
"peer-info": "^0.8.1",
"pre-commit": "^1.1.3",
"run-parallel": "^1.1.6"
},
"dependencies": {
"pull-reader": "^1.2.8",
"pull-stream": "^3.4.5"
"pull-stream": "^3.5.0"
},
"contributors": [
"David Dias <daviddias.p@gmail.com>",
Expand All @@ -55,4 +54,4 @@
"Richard Littauer <richard.littauer@gmail.com>",
"greenkeeperio-bot <support@greenkeeper.io>"
]
}
}
53 changes: 32 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,44 @@ class Ping extends EventEmitter {
this.cont = true

log('dialing %s to %s', PROTOCOL, peer.id.toB58String())
swarm.dial(peer, PROTOCOL, (err, conn) => {
if (err === true) {
return
}

swarm.dial(peer, PROTOCOL, (err, conn) => {
if (err) {
log.error(err)
this.emit('error', err)
return
}

let start = new Date()
let buf = new Buffer(PING_LENGTH) // buffer creation doesn't memset the buffer to 0

const reader = Reader()
// buffer creation doesn't memset the buffer to 0
let buf = new Buffer(PING_LENGTH)
let reader = Reader()

pull(pull.values([buf]), conn, reader)
pull(
pull.values([buf]),
conn,
reader)

const gotBack = (err, bufBack) => {
let end = new Date()

if (err || !buf.equals(bufBack)) {
pull(pull.empty(), conn)
pull(
pull.empty(),
conn
)
this.emit('error', err || new Error('Received wrong ping ack'))
return
}

this.emit('ping', end - start)

if (!this.cont) {
pull(pull.empty(), conn)
pull(
pull.empty(),
conn
)
}

start = new Date()
Expand All @@ -70,33 +77,37 @@ class Ping extends EventEmitter {
}
}

function attach (swarm) {
swarm.handle(PROTOCOL, (conn) => {
function mount (swarm) {
swarm.handle(PROTOCOL, (protocol, conn) => {
const reader = Reader()
pull(conn, reader)

pull(
conn,
reader
)

reader.read(PING_LENGTH, echo)

function echo (err, buf) {
if (err === true) {
return
}

if (err) {
log.error(err)
return log.error(err)
}

pull(pull.values([buf]), conn)
pull(
pull.values([buf]),
conn
)

reader.read(PING_LENGTH, echo)
}
})
}

function detach (swarm) {
function unmount (swarm) {
swarm.unhandle(PROTOCOL)
}

Ping.attach = attach
Ping.detach = detach
Ping.mount = mount
Ping.unmount = unmount

module.exports = Ping
1 change: 1 addition & 0 deletions test/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'use strict'
3 changes: 3 additions & 0 deletions test/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict'

require('./test-ping.js')
57 changes: 0 additions & 57 deletions test/ping.spec.js

This file was deleted.

82 changes: 82 additions & 0 deletions test/test-ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const PeerInfo = require('peer-info')
const Swarm = require('libp2p-swarm')
const TCP = require('libp2p-tcp')
const multiaddr = require('multiaddr')
const series = require('async/series')
const parallel = require('async/parallel')

const Ping = require('./../src')

describe('libp2p ping', () => {
let swarmA
let swarmB
let peerA
let peerB

before((done) => {
series([
(cb) => {
PeerInfo.create((err, peerInfo) => {
expect(err).to.not.exist
peerA = peerInfo
peerA.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/0'))
cb()
})
},
(cb) => {
PeerInfo.create((err, peerInfo) => {
expect(err).to.not.exist
peerB = peerInfo
peerB.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/0'))
cb()
})
},
(cb) => {
swarmA = new Swarm(peerA)
swarmB = new Swarm(peerB)
swarmA.transport.add('tcp', new TCP())
swarmB.transport.add('tcp', new TCP())
cb()
},
(cb) => swarmA.listen(cb),
(cb) => swarmB.listen(cb)
], done)
})

after((done) => {
parallel([
(cb) => swarmA.close(cb),
(cb) => swarmB.close(cb)
], done)
})

it('mount ping protocol', () => {
Ping.mount(swarmA)
Ping.mount(swarmB)
})

it('ping once from peerA to peerB', (done) => {
const p = new Ping(swarmA, peerB)

p.on('error', (err) => {
expect(err).to.not.exist
})

p.on('ping', (time) => {
expect(time).to.be.a('Number')
p.stop()
done()
})
})

it.skip('ping 3 times from peerA to peerB', (done) => {})

it('unmount PING protocol', () => {
Ping.unmount(swarmA)
Ping.unmount(swarmB)
})
})

0 comments on commit fc1be3a

Please sign in to comment.