js-libp2p/src/identify.js

44 lines
1.2 KiB
JavaScript
Raw Normal View History

2015-07-08 16:22:59 -07:00
/*
* Identify is one of the protocols swarms speaks in order to broadcast and learn about the ip:port
* pairs a specific peer is available through
*/
2015-07-08 16:33:57 -07:00
var swarm = require('./')
2015-07-08 16:22:59 -07:00
var Interactive = require('multistream-select').Interactive
exports = module.exports
// peer acting as server, asking whom is talking
exports.inquiry = function (spdyConnection, cb) {
spdyConnection.request({method: 'GET', path: '/', headers: {}}, function (stream) {
var msi = new Interactive()
msi.handle(stream)
msi.select('/ipfs/identify/1.0.0', function (ds) {
var peerId = ''
ds.setEncoding('utf8')
ds.on('data', function (chunk) {
peerId += chunk
})
ds.on('end', function () {
cb(null, spdyConnection, peerId)
})
})
})
2015-07-08 16:23:03 -07:00
// 0. open a stream
// 1. negotiate /ipfs/identify/1.0.0
// 2. check other peerId
// 3. reply back with cb(null, connection, peerId)
2015-07-08 16:22:59 -07:00
}
// peer asking which pairs ip:port does the other peer see
2015-07-08 16:23:03 -07:00
exports.whoAmI = function () {}
2015-07-08 16:22:59 -07:00
exports.start = function (peerSelf) {
swarm.registerHandle('/ipfs/identify/1.0.0', function (ds) {
ds.setDefaultEncoding('utf8')
ds.write(peerSelf.toB58String())
ds.end()
})
}