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

Commit

Permalink
feat: supports multiple pings
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Dec 1, 2016
1 parent bca4a21 commit be4c9b0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 64 deletions.
36 changes: 18 additions & 18 deletions src/handler.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict'

const pull = require('pull-stream')
const Reader = require('pull-reader')
// const pullHandshake = require('pull-handshake')
const handshake = require('pull-handshake')
const config = require('./config')
const PROTOCOL = config.PROTOCOL
const PING_LENGTH = config.PING_LENGTH
Expand All @@ -13,27 +12,28 @@ log.error = debug('libp2p-ping:error')

function mount (swarm) {
swarm.handle(PROTOCOL, (protocol, conn) => {
const reader = Reader()
const stream = handshake({ timeout: 0 })
const shake = stream.handshake

// receive and echo back
function next () {
shake.read(PING_LENGTH, (err, buf) => {
if (err) {
return log.error(err)
}

shake.write(buf)
next()
})
}

pull(
conn,
reader
stream,
conn
)

reader.read(PING_LENGTH, echo)

function echo (err, buf) {
if (err) {
return log.error(err)
}

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

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

Expand Down
89 changes: 43 additions & 46 deletions src/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

const EventEmitter = require('events').EventEmitter
const pull = require('pull-stream')
const Reader = require('pull-reader')
// const pullHandshake = require('pull-handshake')
const handshake = require('pull-handshake')
const config = require('./config')
const util = require('./util')
const rnd = util.rnd
Expand All @@ -17,7 +16,10 @@ const PING_LENGTH = config.PING_LENGTH
class Ping extends EventEmitter {
constructor (swarm, peer) {
super()
this.continue = false

let stop = false
let shake
let self = this

log('dialing %s to %s', PROTOCOL, peer.id.toB58String())

Expand All @@ -26,56 +28,51 @@ class Ping extends EventEmitter {
return this.emit('error', err)
}

let start = new Date()

// buffer creation doesn't memset the buffer to 0
let buf = rnd(PING_LENGTH)
let reader = Reader()
const stream = handshake({ timeout: 0 })
shake = stream.handshake

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

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

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

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

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

start = new Date()
buf = rnd(PING_LENGTH)

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

reader.read(PING_LENGTH, gotBack)
stream
)

// write and wait to see ping back
function next () {
let start = new Date()
let buf = rnd(PING_LENGTH)
shake.write(buf)
shake.read(PING_LENGTH, (err, bufBack) => {
let end = new Date()
if (err || !buf.equals(bufBack)) {
const err = new Error('Received wrong ping ack')
return self.emit('error', err)
}

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

if (stop) {
return
}
next()
})
}

reader.read(PING_LENGTH, gotBack)
next()
})
}

stop () {
this.continue = false
this.stop = () => {
if (stop || !shake) {
return
}

stop = true

pull(
pull.empty(),
shake.rest()
)
}
}
}

Expand Down

0 comments on commit be4c9b0

Please sign in to comment.