This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement
ipfs ping
flags #928
- Loading branch information
Melvin Philips
committed
Feb 1, 2018
1 parent
3bca165
commit 293bcf9
Showing
7 changed files
with
112 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict' | ||
|
||
const print = require('../utils').print | ||
|
||
module.exports = { | ||
command: 'ping <peerId>', | ||
|
||
describe: 'Measure the latency of a connection', | ||
|
||
builder: { | ||
count: { | ||
alias: 'n', | ||
type: 'integer', | ||
default: 10 | ||
} | ||
}, | ||
|
||
handler (argv) { | ||
const peerId = argv.peerId | ||
const count = argv.count || 10 | ||
|
||
print('PING ' + peerId) | ||
|
||
let noOfTimes = 0 | ||
let totalTime = 0 | ||
|
||
const pingCb = (err, p) => { | ||
if (err) { | ||
throw err | ||
} | ||
let time = p.Time | ||
totalTime = totalTime + time | ||
noOfTimes = noOfTimes + 1 | ||
print('Pong received: time=' + time + ' ms') | ||
if (noOfTimes === count) { | ||
print('Average latency: ' + totalTime / count + 'ms') | ||
} | ||
} | ||
|
||
for (let i = 0; i < count; i++) { | ||
argv.ipfs.ping(peerId, pingCb) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,35 @@ | ||
'use strict' | ||
|
||
const promisify = require('promisify-es6') | ||
const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR | ||
const PeerId = require('peer-id') | ||
const PeerInfo = require('peer-info') | ||
var Readable = require('stream').Readable | ||
|
||
module.exports = function ping (self) { | ||
return promisify((callback) => { | ||
callback(new Error('Not implemented')) | ||
return promisify((peerId, cb) => { | ||
if (!self.isOnline()) { | ||
return cb(new Error(OFFLINE_ERROR)) | ||
} | ||
|
||
var outputStream = new Readable() | ||
outputStream._read = function (size) { | ||
} | ||
|
||
let peer | ||
try { | ||
peer = self._libp2pNode.peerBook.get(peerId) | ||
} catch (err) { | ||
peer = new PeerInfo(PeerId.createFromB58String(peerId)) | ||
} | ||
|
||
self._libp2pNode.ping(peer, (err, p) => { | ||
p.once('ping', (time) => { | ||
outputStream.push(JSON.stringify([{}, { Success: true, Time: time }, { Text: 'Average latency: ' + time + ' ms' }])) | ||
outputStream.push(null) | ||
p.stop() | ||
cb(err, outputStream) | ||
}) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict' | ||
|
||
const boom = require('boom') | ||
|
||
exports = module.exports | ||
|
||
exports.get = (request, reply) => { | ||
const ipfs = request.server.app.ipfs | ||
const peerId = request.query.arg | ||
|
||
ipfs.ping(peerId, (err, outputStream) => { | ||
if (err) { | ||
return reply(boom.badRequest(err)) | ||
} | ||
|
||
return reply(outputStream).type('application/json').header('x-chunked-output', '1') | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict' | ||
|
||
const resources = require('./../resources') | ||
|
||
module.exports = (server) => { | ||
const api = server.select('API') | ||
|
||
api.route({ | ||
method: '*', | ||
path: '/api/v0/ping', | ||
config: { | ||
payload: { | ||
parse: false, | ||
output: 'stream' | ||
}, | ||
handler: resources.ping.get | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters