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

Commit

Permalink
feat: dial to PeerId and/or Multiaddr in addition to PeerInfo (#222)
Browse files Browse the repository at this point in the history
* chore: update deps

* feat: support dial to peerId and/or multiaddr in adition to peerInfo

* chore: update CI
  • Loading branch information
daviddias authored Jul 17, 2017
1 parent e306440 commit 80c3853
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 19 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
sudo: false
language: node_js

matrix:
include:
- node_js: 4
env: CXX=g++-4.8
- node_js: 6
env:
- SAUCE=true
- CXX=g++-4.8
- node_js: stable
env: CXX=g++-4.8

# Make sure we have new NPM.
before_install:
- npm install -g npm
- npm install -g npm@4

script:
- npm run lint
- npm test
- npm run coverage
- make test

before_script:
- export DISPLAY=:99.0
Expand All @@ -33,4 +34,4 @@ addons:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
- g++-4.8
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ libp2p-swarm is used by [libp2p](https://github.com/libp2p/js-libp2p) but it can
- [API](#api)
- [Transports](#transports)
- [Connection](#connection)
- [`swarm.dial(pi, protocol, callback)`](#swarmdialpi-protocol-callback)
- [`swarm.hangUp(pi, callback)`](#swarmhanguppi-callback)
- [`swarm.dial(peer, protocol, callback)`](#swarmdialpi-protocol-callback)
- [`swarm.hangUp(peer, callback)`](#swarmhanguppi-callback)
- [`swarm.listen(callback)`](#swarmlistencallback)
- [`swarm.handle(protocol, handler)`](#swarmhandleprotocol-handler)
- [`swarm.unhandle(protocol)`](#swarmunhandleprotocol)
Expand Down Expand Up @@ -129,19 +129,19 @@ const secio = require('libp2p-secio')
swarm.connection.crypto(secio.tag, secio.encrypt)
```

### `swarm.dial(pi, protocol, callback)`
### `swarm.dial(peer, protocol, callback)`

dial uses the best transport (whatever works first, in the future we can have some criteria), and jump starts the connection until the point where we have to negotiate the protocol. If a muxer is available, then drop the muxer onto that connection. Good to warm up connections or to check for connectivity. If we have already a muxer for that peerInfo, then do nothing.

- `pi` - peer info project
- `peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]
- `protocol`
- `callback`

### `swarm.hangUp(pi, callback)`
### `swarm.hangUp(peer, callback)`

Hang up the muxed connection we have with the peer.

- `pi` - peer info project
- `peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]
- `callback`

### `swarm.listen(callback)`
Expand Down
8 changes: 5 additions & 3 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ machine:
dependencies:
pre:
- google-chrome --version
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
- sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
- curl -L -o google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
- sudo dpkg -i google-chrome.deb || true
- sudo apt-get update
- sudo apt-get --only-upgrade install google-chrome-stable
- sudo apt-get install -f
- sudo apt-get install --only-upgrade lsb-base
- sudo dpkg -i google-chrome.deb
- google-chrome --version
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"devDependencies": {
"aegir": "^11.0.2",
"buffer-loader": "0.0.1",
"chai": "^4.0.2",
"dirty-chai": "^2.0.0",
"chai": "^4.1.0",
"dirty-chai": "^2.0.1",
"gulp": "^3.9.1",
"libp2p-multiplex": "~0.4.4",
"libp2p-secio": "~0.6.8",
Expand All @@ -68,7 +68,7 @@
"multistream-select": "~0.13.5",
"once": "^1.4.0",
"peer-id": "~0.8.7",
"peer-info": "~0.9.2",
"peer-info": "~0.9.3",
"protocol-buffers": "^3.2.1",
"pull-stream": "^3.6.0"
},
Expand Down
10 changes: 7 additions & 3 deletions src/dial.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

const multistream = require('multistream-select')
const Connection = require('interface-connection').Connection
const setImmediate = require('async/setImmediate')
const getPeerInfo = require('./get-peer-info')
const debug = require('debug')
const log = debug('libp2p:swarm:dial')
const setImmediate = require('async/setImmediate')

const protocolMuxer = require('./protocol-muxer')

module.exports = function dial (swarm) {
return (pi, protocol, callback) => {
function dial (swarm) {
return (peer, protocol, callback) => {
if (typeof protocol === 'function') {
callback = protocol
protocol = null
}

callback = callback || function noop () {}
const pi = getPeerInfo(peer, swarm._peerBook)

const proxyConn = new Connection()

Expand Down Expand Up @@ -197,3 +199,5 @@ module.exports = function dial (swarm) {
}
}
}

module.exports = dial
41 changes: 41 additions & 0 deletions src/get-peer-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')

/*
* Helper method to check the data type of peer and convert it to PeerInfo
*/
function getPeerInfo (peer, peerBook) {
let p

// PeerInfo
if (PeerInfo.isPeerInfo(peer)) {
p = peer
// Multiaddr instance (not string)
} else if (multiaddr.isMultiaddr(peer)) {
const peerIdB58Str = peer.getPeerId()
try {
p = peerBook.get(peerIdB58Str)
} catch (err) {
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
}
p.multiaddrs.add(peer)

// PeerId
} else if (PeerId.isPeerId(peer)) {
const peerIdB58Str = peer.toB58String()
try {
p = peerBook.get(peerIdB58Str)
} catch (err) {
throw new Error('Couldnt get PeerInfo')
}
} else {
throw new Error('peer type not recognized')
}

return p
}

module.exports = getPeerInfo
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const each = require('async/each')
const series = require('async/series')
const transport = require('./transport')
const connection = require('./connection')
const getPeerInfo = require('./get-peer-info')
const dial = require('./dial')
const protocolMuxer = require('./protocol-muxer')
const plaintext = require('./plaintext')
Expand Down Expand Up @@ -95,7 +96,8 @@ function Swarm (peerInfo, peerBook) {
}
}

this.hangUp = (peerInfo, callback) => {
this.hangUp = (peer, callback) => {
const peerInfo = getPeerInfo(peer, this.peerBook)
const key = peerInfo.id.toB58String()
if (this.muxedConns[key]) {
const muxer = this.muxedConns[key].muxer
Expand Down

0 comments on commit 80c3853

Please sign in to comment.