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

Commit 80c3853

Browse files
authored
feat: dial to PeerId and/or Multiaddr in addition to PeerInfo (#222)
* chore: update deps * feat: support dial to peerId and/or multiaddr in adition to peerInfo * chore: update CI
1 parent e306440 commit 80c3853

File tree

7 files changed

+69
-19
lines changed

7 files changed

+69
-19
lines changed

.travis.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
sudo: false
22
language: node_js
3+
34
matrix:
45
include:
56
- node_js: 4
67
env: CXX=g++-4.8
78
- node_js: 6
89
env:
9-
- SAUCE=true
1010
- CXX=g++-4.8
1111
- node_js: stable
1212
env: CXX=g++-4.8
1313

1414
# Make sure we have new NPM.
1515
before_install:
16-
- npm install -g npm
16+
- npm install -g npm@4
1717

1818
script:
1919
- npm run lint
2020
- npm test
2121
- npm run coverage
22+
- make test
2223

2324
before_script:
2425
- export DISPLAY=:99.0
@@ -33,4 +34,4 @@ addons:
3334
sources:
3435
- ubuntu-toolchain-r-test
3536
packages:
36-
- g++-4.8
37+
- g++-4.8

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ libp2p-swarm is used by [libp2p](https://github.com/libp2p/js-libp2p) but it can
2727
- [API](#api)
2828
- [Transports](#transports)
2929
- [Connection](#connection)
30-
- [`swarm.dial(pi, protocol, callback)`](#swarmdialpi-protocol-callback)
31-
- [`swarm.hangUp(pi, callback)`](#swarmhanguppi-callback)
30+
- [`swarm.dial(peer, protocol, callback)`](#swarmdialpi-protocol-callback)
31+
- [`swarm.hangUp(peer, callback)`](#swarmhanguppi-callback)
3232
- [`swarm.listen(callback)`](#swarmlistencallback)
3333
- [`swarm.handle(protocol, handler)`](#swarmhandleprotocol-handler)
3434
- [`swarm.unhandle(protocol)`](#swarmunhandleprotocol)
@@ -129,19 +129,19 @@ const secio = require('libp2p-secio')
129129
swarm.connection.crypto(secio.tag, secio.encrypt)
130130
```
131131

132-
### `swarm.dial(pi, protocol, callback)`
132+
### `swarm.dial(peer, protocol, callback)`
133133

134134
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.
135135

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

140-
### `swarm.hangUp(pi, callback)`
140+
### `swarm.hangUp(peer, callback)`
141141

142142
Hang up the muxed connection we have with the peer.
143143

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

147147
### `swarm.listen(callback)`

circle.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ machine:
55
dependencies:
66
pre:
77
- google-chrome --version
8-
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
9-
- sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
8+
- curl -L -o google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
9+
- sudo dpkg -i google-chrome.deb || true
1010
- sudo apt-get update
11-
- sudo apt-get --only-upgrade install google-chrome-stable
11+
- sudo apt-get install -f
12+
- sudo apt-get install --only-upgrade lsb-base
13+
- sudo dpkg -i google-chrome.deb
1214
- google-chrome --version

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
"devDependencies": {
4343
"aegir": "^11.0.2",
4444
"buffer-loader": "0.0.1",
45-
"chai": "^4.0.2",
46-
"dirty-chai": "^2.0.0",
45+
"chai": "^4.1.0",
46+
"dirty-chai": "^2.0.1",
4747
"gulp": "^3.9.1",
4848
"libp2p-multiplex": "~0.4.4",
4949
"libp2p-secio": "~0.6.8",
@@ -68,7 +68,7 @@
6868
"multistream-select": "~0.13.5",
6969
"once": "^1.4.0",
7070
"peer-id": "~0.8.7",
71-
"peer-info": "~0.9.2",
71+
"peer-info": "~0.9.3",
7272
"protocol-buffers": "^3.2.1",
7373
"pull-stream": "^3.6.0"
7474
},

src/dial.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22

33
const multistream = require('multistream-select')
44
const Connection = require('interface-connection').Connection
5+
const setImmediate = require('async/setImmediate')
6+
const getPeerInfo = require('./get-peer-info')
57
const debug = require('debug')
68
const log = debug('libp2p:swarm:dial')
7-
const setImmediate = require('async/setImmediate')
89

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

11-
module.exports = function dial (swarm) {
12-
return (pi, protocol, callback) => {
12+
function dial (swarm) {
13+
return (peer, protocol, callback) => {
1314
if (typeof protocol === 'function') {
1415
callback = protocol
1516
protocol = null
1617
}
1718

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

2022
const proxyConn = new Connection()
2123

@@ -197,3 +199,5 @@ module.exports = function dial (swarm) {
197199
}
198200
}
199201
}
202+
203+
module.exports = dial

src/get-peer-info.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict'
2+
3+
const PeerId = require('peer-id')
4+
const PeerInfo = require('peer-info')
5+
const multiaddr = require('multiaddr')
6+
7+
/*
8+
* Helper method to check the data type of peer and convert it to PeerInfo
9+
*/
10+
function getPeerInfo (peer, peerBook) {
11+
let p
12+
13+
// PeerInfo
14+
if (PeerInfo.isPeerInfo(peer)) {
15+
p = peer
16+
// Multiaddr instance (not string)
17+
} else if (multiaddr.isMultiaddr(peer)) {
18+
const peerIdB58Str = peer.getPeerId()
19+
try {
20+
p = peerBook.get(peerIdB58Str)
21+
} catch (err) {
22+
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
23+
}
24+
p.multiaddrs.add(peer)
25+
26+
// PeerId
27+
} else if (PeerId.isPeerId(peer)) {
28+
const peerIdB58Str = peer.toB58String()
29+
try {
30+
p = peerBook.get(peerIdB58Str)
31+
} catch (err) {
32+
throw new Error('Couldnt get PeerInfo')
33+
}
34+
} else {
35+
throw new Error('peer type not recognized')
36+
}
37+
38+
return p
39+
}
40+
41+
module.exports = getPeerInfo

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const each = require('async/each')
66
const series = require('async/series')
77
const transport = require('./transport')
88
const connection = require('./connection')
9+
const getPeerInfo = require('./get-peer-info')
910
const dial = require('./dial')
1011
const protocolMuxer = require('./protocol-muxer')
1112
const plaintext = require('./plaintext')
@@ -95,7 +96,8 @@ function Swarm (peerInfo, peerBook) {
9596
}
9697
}
9798

98-
this.hangUp = (peerInfo, callback) => {
99+
this.hangUp = (peer, callback) => {
100+
const peerInfo = getPeerInfo(peer, this.peerBook)
99101
const key = peerInfo.id.toB58String()
100102
if (this.muxedConns[key]) {
101103
const muxer = this.muxedConns[key].muxer

0 commit comments

Comments
 (0)