mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-16 09:01:58 +00:00
* refactor: add js-libp2p-connection-manager to repo Co-authored-by: David Dias <daviddias.p@gmail.com> Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Pedro Teixeira <i@pgte.me> Co-authored-by: Vasco Santos <vasco.santos@ua.pt> * test(conn-mgr): only run in node * refactor: add js-libp2p-identify to repo Co-authored-by: David Dias <daviddias.p@gmail.com> Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com> Co-authored-by: Hugo Dias <hugomrdias@gmail.com> Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Maciej Krüger <mkg20001@gmail.com> Co-authored-by: Richard Littauer <richard.littauer@gmail.com> Co-authored-by: Vasco Santos <vasco.santos@moxy.studio> Co-authored-by: Yusef Napora <yusef@protocol.ai> Co-authored-by: ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com> * refactor: add libp2p-pnet to repo Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Vasco Santos <vasco.santos@moxy.studio> * refactor: add libp2p-ping to repo Co-authored-by: David Dias <daviddias.p@gmail.com> Co-authored-by: Francisco Baio Dias <xicombd@gmail.com> Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com> Co-authored-by: Hugo Dias <mail@hugodias.me> Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: João Antunes <j.goncalo.antunes@gmail.com> Co-authored-by: Richard Littauer <richard.littauer@gmail.com> Co-authored-by: Vasco Santos <vasco.santos@moxy.studio> Co-authored-by: Vasco Santos <vasco.santos@ua.pt> Co-authored-by: ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com> * refactor: add libp2p-circuit to repo Co-authored-by: David Dias <daviddias.p@gmail.com> Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com> Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com> Co-authored-by: Hugo Dias <mail@hugodias.me> Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Maciej Krüger <mkg20001@gmail.com> Co-authored-by: Oli Evans <oli@tableflip.io> Co-authored-by: Pedro Teixeira <i@pgte.me> Co-authored-by: Vasco Santos <vasco.santos@ua.pt> Co-authored-by: Victor Bjelkholm <victorbjelkholm@gmail.com> Co-authored-by: Yusef Napora <yusef@napora.org> Co-authored-by: dirkmc <dirk@mccormick.cx> * test(switch): avoid using instanceof * chore(switch): update bignumber dep * refactor(circuit): clean up tests * refactor(switch): consolidate get peer utils * test(identify): do deep checks of addresses * test(identify): bump timeout for identify test * test(switch): tidy up limit dialer test * refactor(switch): remove redundant circuit tests * chore: add coverage script * refactor(circuit): consolidate get peer info * docs: reference original repositories in each sub readme * docs: fix comment * refactor: clean up sub package.json files and readmes
84 lines
1.7 KiB
JavaScript
84 lines
1.7 KiB
JavaScript
'use strict'
|
|
|
|
const EventEmitter = require('events').EventEmitter
|
|
const pull = require('pull-stream/pull')
|
|
const empty = require('pull-stream/sources/empty')
|
|
const handshake = require('pull-handshake')
|
|
const constants = require('./constants')
|
|
const util = require('./util')
|
|
const rnd = util.rnd
|
|
const debug = require('debug')
|
|
const log = debug('libp2p-ping')
|
|
log.error = debug('libp2p-ping:error')
|
|
|
|
const PROTOCOL = constants.PROTOCOL
|
|
const PING_LENGTH = constants.PING_LENGTH
|
|
|
|
class Ping extends EventEmitter {
|
|
constructor (swarm, peer) {
|
|
super()
|
|
|
|
this._stopped = false
|
|
this.peer = peer
|
|
this.swarm = swarm
|
|
}
|
|
|
|
start () {
|
|
log('dialing %s to %s', PROTOCOL, this.peer.id.toB58String())
|
|
|
|
this.swarm.dial(this.peer, PROTOCOL, (err, conn) => {
|
|
if (err) {
|
|
return this.emit('error', err)
|
|
}
|
|
|
|
const stream = handshake({ timeout: 0 })
|
|
this.shake = stream.handshake
|
|
|
|
pull(
|
|
stream,
|
|
conn,
|
|
stream
|
|
)
|
|
|
|
// write and wait to see ping back
|
|
const self = this
|
|
function next () {
|
|
const start = new Date()
|
|
const buf = rnd(PING_LENGTH)
|
|
self.shake.write(buf)
|
|
self.shake.read(PING_LENGTH, (err, bufBack) => {
|
|
const 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 (self._stopped) {
|
|
return
|
|
}
|
|
next()
|
|
})
|
|
}
|
|
|
|
next()
|
|
})
|
|
}
|
|
|
|
stop () {
|
|
if (this._stopped || !this.shake) {
|
|
return
|
|
}
|
|
|
|
this._stopped = true
|
|
|
|
pull(
|
|
empty(),
|
|
this.shake.rest()
|
|
)
|
|
}
|
|
}
|
|
|
|
module.exports = Ping
|