Skip to content

Commit

Permalink
docs: add documentation for peerRouting, contentRouting, dht
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Apr 4, 2017
1 parent af528b7 commit ac3e11e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 44 deletions.
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const WS = require('libp2p-websockets')
const spdy = require('libp2p-spdy')
const secio = require('libp2p-secio')
const MulticastDNS = require('libp2p-mdns')
const DHT = require('libp2p-dht')

class Node extends libp2p {
constructor (peerInfo, peerBook, options) {
Expand All @@ -95,7 +96,9 @@ class Node extends libp2p {
},
discovery: [
new MulticastDNS(peerInfo, 'your-identifier')
]
],
// DHT is passed as its own enabling PeerRouting, ContentRouting and DHT itself components
dht: new DHT()
}

super(modules, peerInfo, peerBook, options)
Expand Down Expand Up @@ -144,6 +147,36 @@ class Node extends libp2p {

`callback` is a function with the following `function (err) {}` signature, where `err` is an Error in case stopping the node fails.

#### `libp2p.peerRouting.findPeer(id, callback)`

> Looks up for multiaddrs of a peer in the DHT
- `id`: instance of [PeerId][]

#### `libp2p.contentRouting.findProviders(key, timeout, callback)`

- `key`:
- `timeout`: Number miliseconds

#### `libp2p.contentRouting.provide(key, timeout, callback)`

- `key`:
- `timeout`: Number miliseconds

#### `libp2p.dht.put(key, value, callback)`

- `key`:
- `value`:

#### `libp2p.dht.get(key, callback)`

- `key`:

#### `libp2p.dht.getMany(key, nVals, callback)`

- `key`:
- `nVals`: Number

#### `libp2p.handle(protocol, handlerFunc [, matchFunc])`

> Handle new protocol
Expand Down
74 changes: 31 additions & 43 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,27 @@ class Node extends EventEmitter {
this.peerBook = _peerBook || new PeerBook()
this.isOnline = false

this.swarm = new Swarm(this.peerInfo, this.peerBook)
this._swarm = new Swarm(this.peerInfo, this.peerBook)

// Attach stream multiplexers
if (this.modules.connection.muxer) {
let muxers = this.modules.connection.muxer
muxers = Array.isArray(muxers) ? muxers : [muxers]
muxers.forEach((muxer) => {
this.swarm.connection.addStreamMuxer(muxer)
this._swarm.connection.addStreamMuxer(muxer)
})

// If muxer exists, we can use Identify
this.swarm.connection.reuse()
this._swarm.connection.reuse()

// Received incommind dial and muxer upgrade happened,
// reuse this muxed connection
this.swarm.on('peer-mux-established', (peerInfo) => {
this._swarm.on('peer-mux-established', (peerInfo) => {
this.emit('peer:connect', peerInfo)
this.peerBook.put(peerInfo)
})

this.swarm.on('peer-mux-closed', (peerInfo) => {
this._swarm.on('peer-mux-closed', (peerInfo) => {
this.emit('peer:disconnect', peerInfo)
})
}
Expand All @@ -61,7 +61,7 @@ class Node extends EventEmitter {
let cryptos = this.modules.connection.crypto
cryptos = Array.isArray(cryptos) ? cryptos : [cryptos]
cryptos.forEach((crypto) => {
this.swarm.connection.crypto(crypto.tag, crypto.encrypt)
this._swarm.connection.crypto(crypto.tag, crypto.encrypt)
})
}

Expand All @@ -76,60 +76,49 @@ class Node extends EventEmitter {
}

// Mount default protocols
Ping.mount(this.swarm)
Ping.mount(this._swarm)

if (this.options.dht) {
// dht provided components (peerRouting, contentRouting, dht)
if (this.modules.dht) {
this._dht = new DHT(this)
}

this.peerRouting = {
findPeer: (id, callback) => {
if (!this._dht) {
return callback(new Error('DHT is not available'))
}
assert(this._dht, 'DHT is not available')

this._dht.findPeer(id, callback)
}
}

this.contentRouting = {
findProviders: (key, timeout, callback) => {
if (!this._dht) {
return callback(new Error('DHT is not available'))
}
assert(this._dht, 'DHT is not available')

this._dht.findProviders(key, timeout, callback)
},
provide: (key, callback) => {
if (!this._dht) {
return callback(new Error('DHT is not available'))
}
assert(this._dht, 'DHT is not available')

this._dht.provide(key, callback)
}
}

this.dht = {
put: (key, value, callback) => {
if (!this._dht) {
return callback(new Error('DHT is not available'))
}
assert(this._dht, 'DHT is not available')

this._dht.put(key, value, callback)
},
get: (key, callback) => {
if (!this._dht) {
return callback(new Error('DHT is not available'))
}
assert(this._dht, 'DHT is not available')

this._dht.get(key, callback)
},
getMany (key, nvals, callback) {
if (!this._dht) {
return callback(new Error('DHT is not available'))
}
getMany (key, nVals, callback) {
assert(this._dht, 'DHT is not available')

this._dht.getMany(key, nvals, callback)
this._dht.getMany(key, nVals, callback)
}
}
}
Expand Down Expand Up @@ -163,7 +152,7 @@ class Node extends EventEmitter {

transports.forEach((transport) => {
if (transport.filter(multiaddrs).length > 0) {
this.swarm.transport.add(
this._swarm.transport.add(
transport.tag || transport.constructor.name, transport)
} else if (transport.constructor &&
transport.constructor.name === 'WebSockets') {
Expand All @@ -174,11 +163,11 @@ class Node extends EventEmitter {
})

series([
(cb) => this.swarm.listen(cb),
(cb) => this._swarm.listen(cb),
(cb) => {
if (ws) {
// always add dialing on websockets
this.swarm.transport.add(
this._swarm.transport.add(
ws.tag || ws.constructor.name, ws
)
}
Expand Down Expand Up @@ -226,7 +215,7 @@ class Node extends EventEmitter {
}
cb()
},
(cb) => this.swarm.close(cb)
(cb) => this._swarm.close(cb)
], callback)
}

Expand All @@ -237,7 +226,7 @@ class Node extends EventEmitter {
ping (peer, callback) {
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
const peerInfo = this._getPeerInfo(peer)
callback(null, new Ping(this.swarm, peerInfo))
callback(null, new Ping(this._swarm, peerInfo))
}

dial (peer, protocol, callback) {
Expand All @@ -255,7 +244,7 @@ class Node extends EventEmitter {
return callback(err)
}

this.swarm.dial(peerInfo, protocol, (err, conn) => {
this._swarm.dial(peerInfo, protocol, (err, conn) => {
if (err) {
return callback(err)
}
Expand All @@ -268,24 +257,26 @@ class Node extends EventEmitter {
assert(this.isOn(), OFFLINE_ERROR_MESSAGE)
const peerInfo = this._getPeerInfo(peer)

this.swarm.hangUp(peerInfo, callback)
this._swarm.hangUp(peerInfo, callback)
}

handle (protocol, handlerFunc, matchFunc) {
this.swarm.handle(protocol, handlerFunc, matchFunc)
this._swarm.handle(protocol, handlerFunc, matchFunc)
}

unhandle (protocol) {
this.swarm.unhandle(protocol)
this._swarm.unhandle(protocol)
}

/*
* Helper method to check the data type of peer and convert it to PeerInfo
*/
_getPeerInfo (peer, callback) {
let p
// PeerInfo
if (PeerInfo.isPeerInfo(peer)) {
p = peer
// Multiaddr instance (not string)
} else if (multiaddr.isMultiaddr(peer)) {
const peerIdB58Str = peer.getPeerId()
try {
Expand All @@ -294,6 +285,7 @@ class Node extends EventEmitter {
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
}
p.multiaddrs.add(peer)
// PeerId
} else if (PeerId.isPeerId(peer)) {
const peerIdB58Str = peer.toB58String()
try {
Expand All @@ -302,14 +294,10 @@ class Node extends EventEmitter {
this.peerRouting.findPeer(peer, callback)
}
} else {
setImmediate(() => {
callback(new Error('peer type not recognized'))
})
setImmediate(() => callback(new Error('peer type not recognized')))
}

setImmediate(() => {
callback(null, p)
})
setImmediate(() => callback(null, p))
}
}

Expand Down

0 comments on commit ac3e11e

Please sign in to comment.