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

Commit f29d661

Browse files
authored
Merge pull request #11 from libp2p/pull
migrate to pull-streams
2 parents 70b3d4c + 2913fd4 commit f29d661

File tree

4 files changed

+78
-50
lines changed

4 files changed

+78
-50
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ libp2p-ping JavaScript Implementation
1010
1111
## Usage
1212

13-
1413
```javascript
1514
var Ping = require('libp2p-ping')
1615

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@
3434
"chai": "^3.5.0",
3535
"debug": "^2.2.0",
3636
"gulp": "^3.9.1",
37-
"libp2p-swarm": "^0.22.2",
38-
"libp2p-tcp": "^0.7.4",
37+
"libp2p-swarm": "^0.23.0",
38+
"libp2p-tcp": "^0.8.1",
3939
"multiaddr": "^2.0.2",
4040
"peer-id": "^0.7.0",
4141
"peer-info": "^0.7.0",
4242
"pre-commit": "^1.1.2",
4343
"run-parallel": "^1.1.6"
4444
},
4545
"dependencies": {
46-
"async-buffered-reader": "^1.2.1"
46+
"pull-reader": "^1.2.8",
47+
"pull-stream": "^3.4.5"
4748
},
4849
"contributors": [
4950
"David Dias <[email protected]>",

src/index.js

Lines changed: 68 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,102 @@
11
'use strict'
22

33
const EventEmitter = require('events').EventEmitter
4-
const util = require('util')
5-
const read = require('async-buffered-reader')
4+
const pull = require('pull-stream')
5+
const Reader = require('pull-reader')
66
const debug = require('debug')
7-
const log = {}
7+
const log = debug('libp2p-ping')
88
log.error = debug('libp2p-ping:error')
99

10-
exports = module.exports = Ping
11-
exports.attach = attach
12-
exports.detach = detach
13-
1410
const PROTOCOL = '/ipfs/ping/1.0.0'
11+
const PING_LENGTH = 32
1512

16-
util.inherits(Ping, EventEmitter)
13+
class Ping extends EventEmitter {
14+
constructor (swarm, peer) {
15+
super()
16+
this.cont = true
1717

18-
function Ping (swarm, peer) {
19-
this.cont = true
18+
log('dialing %s to %s', PROTOCOL, peer.id.toB58String())
19+
swarm.dial(peer, PROTOCOL, (err, conn) => {
20+
if (err === true) {
21+
return
22+
}
2023

21-
swarm.dial(peer, PROTOCOL, (err, conn) => {
22-
if (err) {
23-
return this.emit('error', err)
24-
}
24+
if (err) {
25+
log.error(err)
26+
this.emit('error', err)
27+
return
28+
}
29+
30+
let start = new Date()
31+
let buf = new Buffer(PING_LENGTH) // buffer creation doesn't memset the buffer to 0
32+
33+
const reader = Reader()
2534

26-
let start = new Date()
27-
let buf = new Buffer(32) // buffer creation doesn't memset the buffer to 0
35+
pull(pull.values([buf]), conn, reader)
2836

29-
conn.write(buf)
37+
const gotBack = (err, bufBack) => {
38+
let end = new Date()
3039

31-
const gotBack = (bufBack) => {
32-
let end = new Date()
40+
if (err || !buf.equals(bufBack)) {
41+
pull(pull.empty(), conn)
42+
this.emit('error', err || new Error('Received wrong ping ack'))
43+
return
44+
}
3345

34-
if (buf.equals(bufBack)) {
3546
this.emit('ping', end - start)
36-
} else {
37-
conn.end()
38-
return this.emit('error', new Error('Received wrong ping ack'))
39-
}
4047

41-
if (!this.cont) {
42-
return conn.end()
43-
}
48+
if (!this.cont) {
49+
pull(pull.empty(), conn)
50+
}
4451

45-
start = new Date()
46-
buf = new Buffer(32)
47-
conn.write(buf)
48-
read(conn, 32, gotBack)
49-
}
52+
start = new Date()
53+
buf = new Buffer(PING_LENGTH)
5054

51-
read(conn, 32, gotBack)
52-
})
55+
pull(
56+
pull.values([buf]),
57+
reader,
58+
conn
59+
)
60+
61+
reader.read(PING_LENGTH, gotBack)
62+
}
5363

54-
this.stop = () => {
64+
reader.read(PING_LENGTH, gotBack)
65+
})
66+
}
67+
68+
stop () {
5569
this.cont = false
5670
}
5771
}
5872

5973
function attach (swarm) {
6074
swarm.handle(PROTOCOL, (conn) => {
61-
read(conn, 32, echo)
75+
const reader = Reader()
76+
pull(conn, reader)
6277

63-
function echo (buf) {
64-
conn.write(buf)
65-
read(conn, 32, echo)
66-
}
78+
reader.read(PING_LENGTH, echo)
6779

68-
conn.on('error', (err) => {
69-
log.error(err)
70-
})
80+
function echo (err, buf) {
81+
if (err === true) {
82+
return
83+
}
7184

72-
conn.on('end', () => {
73-
conn.end()
74-
})
85+
if (err) {
86+
log.error(err)
87+
}
88+
89+
pull(pull.values([buf]), conn)
90+
reader.read(PING_LENGTH, echo)
91+
}
7592
})
7693
}
7794

7895
function detach (swarm) {
7996
swarm.unhandle(PROTOCOL)
8097
}
98+
99+
Ping.attach = attach
100+
Ping.detach = detach
101+
102+
module.exports = Ping

test/ping.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4+
const expect = require('chai').expect
45
const Peer = require('peer-info')
56
const Swarm = require('libp2p-swarm')
67
const TCP = require('libp2p-tcp')
@@ -42,7 +43,12 @@ describe('ping', () => {
4243

4344
var p = new Ping(swarmA, peerB)
4445

46+
p.on('error', (err) => {
47+
throw err
48+
})
49+
4550
p.on('ping', (time) => {
51+
expect(time).to.be.a('Number')
4652
p.stop()
4753
Ping.detach(swarmB)
4854
done()

0 commit comments

Comments
 (0)