From d3a86910438abbce9881db8e2828decfb10ce8b0 Mon Sep 17 00:00:00 2001 From: Francisco Baio Dias Date: Thu, 30 Jun 2016 10:02:35 +0200 Subject: [PATCH] Integrate libp2p-ping --- package.json | 3 ++- src/index.js | 7 +++++++ test/libp2p.spec.js | 9 +++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 0d95bca..9aca183 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "run-parallel": "^1.1.6" }, "dependencies": { + "libp2p-ping": "^0.2.0", "libp2p-secio": "^0.5.0", "libp2p-spdy": "^0.9.0", "libp2p-swarm": "^0.23.0", @@ -67,4 +68,4 @@ "Stephen Whitmore ", "dignifiedquire " ] -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index b6ebc98..958b4eb 100644 --- a/src/index.js +++ b/src/index.js @@ -9,6 +9,7 @@ const secio = require('libp2p-secio') const PeerId = require('peer-id') const PeerInfo = require('peer-info') const PeerBook = require('peer-book') +const Ping = require('libp2p-ping') const multiaddr = require('multiaddr') const mafmt = require('mafmt') const EE = require('events').EventEmitter @@ -197,6 +198,12 @@ exports.Node = function Node (pInfo, pBook) { return this.swarm.unhandle(protocol) } + Ping.attach(this.swarm) // Enable this peer to echo Ping requests + + this.ping = (peerDst) => { + return new Ping(this.swarm, peerDst) // Ping peerDst, peerDst must be a peer-info object + } + this.discovery = new EE() this.routing = null this.records = null diff --git a/test/libp2p.spec.js b/test/libp2p.spec.js index 2df7032..3e79103 100644 --- a/test/libp2p.spec.js +++ b/test/libp2p.spec.js @@ -415,6 +415,15 @@ describe('libp2p-ipfs', () => { } }) + it('nodeE ping to nodeF', (done) => { + const p = nodeE.ping(nodeF.peerInfo) + + p.once('ping', (time) => { + p.stop() + done() + }) + }) + it('stop', (done) => { parallel([ nodeA.stop,