Compare commits

...

18 Commits

Author SHA1 Message Date
db3f6dbb06 chore: release version v0.20.0 2018-04-06 17:03:47 +01:00
3808c365b1 chore: update contributors 2018-04-06 17:03:47 +01:00
19528ef15e chore: update deps 2018-04-06 17:02:51 +01:00
bb0c9905ed feat: use class-is for type checks 2018-04-05 19:47:02 +01:00
141920cd14 chore: update deps 2018-04-05 19:47:02 +01:00
2dc94cd907 chore: release version v0.19.2 2018-03-28 15:34:00 -07:00
7fc1cd0f7c chore: update contributors 2018-03-28 15:33:59 -07:00
26f3f9a319 chore: release version v0.19.1 2018-03-28 15:32:20 -07:00
e9ce4ac795 chore: update deps 2018-03-28 15:32:20 -07:00
bca86873cc docs: enable missing syntax highlighting (#178)
Enable missing syntax highlighting for example code in `discovery-mechanisms/README.md`.
2018-03-20 21:43:56 -07:00
bcca813171 chore: release version v0.19.0 2018-03-15 08:16:12 -07:00
cfc5958a4b chore: update contributors 2018-03-15 08:16:12 -07:00
4527d5fff1 stats: tests: bind to localhost only 2018-03-15 08:09:56 -07:00
2c04a71007 stats: exposed and documented 2018-03-15 08:09:56 -07:00
642b8ad751 chore: update deps 2018-03-15 07:57:30 -07:00
ada73221b0 chore: update deps 2018-03-14 06:27:53 -07:00
2e40e9dda1 docs: update echo and chat examples to use libp2p-switch (#174) 2018-03-06 13:25:55 +01:00
e531b1bf3d docs: fix pubsub example, solves #167 2018-02-22 07:06:21 +00:00
11 changed files with 213 additions and 38 deletions

View File

@ -1,3 +1,28 @@
<a name="0.20.0"></a>
# [0.20.0](https://github.com/libp2p/js-libp2p/compare/v0.19.2...v0.20.0) (2018-04-06)
### Features
* use class-is for type checks ([bb0c990](https://github.com/libp2p/js-libp2p/commit/bb0c990))
<a name="0.19.2"></a>
## [0.19.2](https://github.com/libp2p/js-libp2p/compare/v0.19.0...v0.19.2) (2018-03-28)
<a name="0.19.1"></a>
## [0.19.1](https://github.com/libp2p/js-libp2p/compare/v0.19.0...v0.19.1) (2018-03-28)
<a name="0.19.0"></a>
# [0.19.0](https://github.com/libp2p/js-libp2p/compare/v0.18.0...v0.19.0) (2018-03-15)
<a name="0.18.0"></a> <a name="0.18.0"></a>
# [0.18.0](https://github.com/libp2p/js-libp2p/compare/v0.17.0...v0.18.0) (2018-02-19) # [0.18.0](https://github.com/libp2p/js-libp2p/compare/v0.17.0...v0.18.0) (2018-02-19)

107
README.md
View File

@ -277,6 +277,113 @@ class Node extends libp2p {
[multiaddr]: https://github.com/multiformats/js-multiaddr [multiaddr]: https://github.com/multiformats/js-multiaddr
[Connection]: https://github.com/libp2p/interface-connection [Connection]: https://github.com/libp2p/interface-connection
-------
### Switch Stats API
##### `libp2p.stats.emit('update')`
Every time any stat value changes, this object emits an `update` event.
#### Global stats
##### `libp2p.stats.global.snapshot`
Should return a stats snapshot, which is an object containing the following keys and respective values:
- dataSent: amount of bytes sent, [Big](https://github.com/MikeMcl/big.js#readme) number
- dataReceived: amount of bytes received, [Big](https://github.com/MikeMcl/big.js#readme) number
##### `libp2p.stats.global.movingAverages`
Returns an object containing the following keys:
- dataSent
- dataReceived
Each one of them contains an object that has a key for each interval (`60000`, `300000` and `900000` miliseconds).
Each one of these values is [an exponential moving-average instance](https://github.com/pgte/moving-average#readme).
#### Per-transport stats
##### `libp2p.stats.transports()`
Returns an array containing the tags (string) for each observed transport.
##### `libp2p.stats.forTransport(transportTag).snapshot`
Should return a stats snapshot, which is an object containing the following keys and respective values:
- dataSent: amount of bytes sent, [Big](https://github.com/MikeMcl/big.js#readme) number
- dataReceived: amount of bytes received, [Big](https://github.com/MikeMcl/big.js#readme) number
##### `libp2p.stats.forTransport(transportTag).movingAverages`
Returns an object containing the following keys:
dataSent
dataReceived
Each one of them contains an object that has a key for each interval (`60000`, `300000` and `900000` miliseconds).
Each one of these values is [an exponential moving-average instance](https://github.com/pgte/moving-average#readme).
#### Per-protocol stats
##### `libp2p.stats.protocols()`
Returns an array containing the tags (string) for each observed protocol.
##### `libp2p.stats.forProtocol(protocolTag).snapshot`
Should return a stats snapshot, which is an object containing the following keys and respective values:
- dataSent: amount of bytes sent, [Big](https://github.com/MikeMcl/big.js#readme) number
- dataReceived: amount of bytes received, [Big](https://github.com/MikeMcl/big.js#readme) number
##### `libp2p.stats.forProtocol(protocolTag).movingAverages`
Returns an object containing the following keys:
- dataSent
- dataReceived
Each one of them contains an object that has a key for each interval (`60000`, `300000` and `900000` miliseconds).
Each one of these values is [an exponential moving-average instance](https://github.com/pgte/moving-average#readme).
#### Per-peer stats
##### `libp2p.stats.peers()`
Returns an array containing the peerIDs (B58-encoded string) for each observed peer.
##### `libp2p.stats.forPeer(peerId:String).snapshot`
Should return a stats snapshot, which is an object containing the following keys and respective values:
- dataSent: amount of bytes sent, [Big](https://github.com/MikeMcl/big.js#readme) number
- dataReceived: amount of bytes received, [Big](https://github.com/MikeMcl/big.js#readme) number
##### `libp2p.stats.forPeer(peerId:String).movingAverages`
Returns an object containing the following keys:
- dataSent
- dataReceived
Each one of them contains an object that has a key for each interval (`60000`, `300000` and `900000` miliseconds).
Each one of these values is [an exponential moving-average instance](https://github.com/pgte/moving-average#readme).
#### Stats update interval
Stats are not updated in real-time. Instead, measurements are buffered and stats are updated at an interval. The maximum interval can be defined through the `Switch` constructor option `stats.computeThrottleTimeout`, defined in miliseconds.
## Development ## Development
**Clone and install dependencies:** **Clone and install dependencies:**

View File

@ -21,7 +21,7 @@ PeerId.createFromJSON(require('./peer-id-listener'), (err, idListener) => {
throw err throw err
} }
nodeListener.swarm.on('peer-mux-established', (peerInfo) => { nodeListener.switch.on('peer-mux-established', (peerInfo) => {
console.log(peerInfo.id.toB58String()) console.log(peerInfo.id.toB58String())
}) })

View File

@ -46,7 +46,7 @@ const bootstrapers = [
Now, once we create and start the node, we can listen for events such as `peer:discovery` and `peer:connect`, these events tell us when we found a peer, independently of the discovery mechanism used and when we actually dialed to that peer. Now, once we create and start the node, we can listen for events such as `peer:discovery` and `peer:connect`, these events tell us when we found a peer, independently of the discovery mechanism used and when we actually dialed to that peer.
``` ```JavaScript
let node let node
waterfall([ waterfall([

View File

@ -27,7 +27,7 @@ series([
listenerPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/10333') listenerPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/10333')
listenerNode = new Node(listenerPeerInfo) listenerNode = new Node(listenerPeerInfo)
listenerNode.swarm.on('peer-mux-established', (peerInfo) => { listenerNode.switch.on('peer-mux-established', (peerInfo) => {
console.log('received dial to me from:', peerInfo.id.toB58String()) console.log('received dial to me from:', peerInfo.id.toB58String())
}) })

View File

@ -54,11 +54,17 @@ parallel([
], (err) => { ], (err) => {
if (err) { throw err } if (err) { throw err }
// Subscribe to the topic 'news'
node1.pubsub.subscribe('news', node1.pubsub.subscribe('news',
(msg) => console.log(msg.from, msg.data.toString()), (msg) => console.log(msg.from, msg.data.toString()),
() => { () => {
setInterval(() => { setInterval(() => {
node2.pubsub.publish('news', Buffer.from('Bird bird bird, bird is the word!')) // Publish the message on topic 'news'
node2.pubsub.publish(
'news',
Buffer.from('Bird bird bird, bird is the word!'),
() => {}
)
}, 1000) }, 1000)
} }
) )

View File

@ -21,12 +21,20 @@ series([
], (err) => { ], (err) => {
if (err) { throw err } if (err) { throw err }
node1.pubsub.on('news', (msg) => console.log(msg.from, msg.data.toString())) // Subscribe to the topic 'news'
node1.pubsub.subscribe('news') node1.pubsub.subscribe('news',
(msg) => console.log(msg.from, msg.data.toString()),
setInterval(() => { () => {
node2.pubsub.publish('news', Buffer.from('Bird bird bird, bird is the word!')) setInterval(() => {
}, 1000) // Publish the message on topic 'news'
node2.pubsub.publish(
'news',
Buffer.from('Bird bird bird, bird is the word!'),
() => {}
)
}, 1000)
}
)
}) })
``` ```

View File

@ -1,6 +1,6 @@
{ {
"name": "libp2p", "name": "libp2p",
"version": "0.18.0", "version": "0.20.0",
"description": "JavaScript base class for libp2p bundles", "description": "JavaScript base class for libp2p bundles",
"main": "src/index.js", "main": "src/index.js",
"scripts": { "scripts": {
@ -38,47 +38,48 @@
"homepage": "https://github.com/libp2p/js-libp2p", "homepage": "https://github.com/libp2p/js-libp2p",
"dependencies": { "dependencies": {
"async": "^2.6.0", "async": "^2.6.0",
"libp2p-floodsub": "^0.14.1", "libp2p-floodsub": "^0.15.0",
"libp2p-ping": "~0.6.1", "libp2p-ping": "~0.7.0",
"libp2p-switch": "~0.36.1", "libp2p-switch": "~0.39.0",
"mafmt": "^4.0.0", "mafmt": "^6.0.0",
"multiaddr": "^3.0.2", "multiaddr": "^4.0.0",
"peer-book": "~0.5.4", "peer-book": "~0.7.0",
"peer-id": "~0.10.6", "peer-id": "~0.10.7",
"peer-info": "~0.11.6" "peer-info": "~0.14.0"
}, },
"devDependencies": { "devDependencies": {
"aegir": "^13.0.1", "aegir": "^13.0.6",
"chai": "^4.1.2", "chai": "^4.1.2",
"cids": "~0.5.2", "cids": "~0.5.3",
"dirty-chai": "^2.0.1", "dirty-chai": "^2.0.1",
"electron-webrtc": "~0.3.0", "electron-webrtc": "~0.3.0",
"libp2p-circuit": "~0.1.4", "libp2p-circuit": "~0.2.0",
"libp2p-kad-dht": "~0.8.0", "libp2p-kad-dht": "~0.10.0",
"libp2p-mdns": "~0.9.2", "libp2p-mdns": "~0.11.0",
"libp2p-mplex": "~0.6.0", "libp2p-mplex": "~0.7.0",
"libp2p-railing": "~0.7.1", "libp2p-railing": "~0.8.0",
"libp2p-secio": "~0.9.2", "libp2p-secio": "~0.10.0",
"libp2p-spdy": "~0.11.0", "libp2p-spdy": "~0.12.1",
"libp2p-tcp": "~0.11.5", "libp2p-tcp": "~0.12.0",
"libp2p-webrtc-star": "~0.13.3", "libp2p-webrtc-star": "~0.14.0",
"libp2p-websockets": "~0.10.4", "libp2p-websockets": "~0.11.0",
"libp2p-websocket-star": "~0.7.6", "libp2p-websocket-star": "~0.8.0",
"libp2p-websocket-star-rendezvous": "~0.2.3", "libp2p-websocket-star-rendezvous": "~0.2.3",
"lodash.times": "^4.3.2", "lodash.times": "^4.3.2",
"pre-commit": "^1.2.2", "pre-commit": "^1.2.2",
"pull-goodbye": "0.0.2", "pull-goodbye": "0.0.2",
"pull-serializer": "~0.3.2", "pull-serializer": "~0.3.2",
"pull-stream": "^3.6.1", "pull-stream": "^3.6.7",
"safe-buffer": "^5.1.1", "safe-buffer": "^5.1.1",
"sinon": "^4.3.0", "sinon": "^4.5.0",
"wrtc": "0.0.66" "wrtc": "0.1.1"
}, },
"contributors": [ "contributors": [
"Chris Bratlien <chrisbratlien@gmail.com>", "Chris Bratlien <chrisbratlien@gmail.com>",
"Chris Dostert <chrisdostert@users.noreply.github.com>", "Chris Dostert <chrisdostert@users.noreply.github.com>",
"Daijiro Wachi <daijiro.wachi@gmail.com>", "Daijiro Wachi <daijiro.wachi@gmail.com>",
"David Dias <daviddias.p@gmail.com>", "David Dias <daviddias.p@gmail.com>",
"Diogo Silva <fsdiogo@gmail.com>",
"Dmitriy Ryajov <dryajov@gmail.com>", "Dmitriy Ryajov <dryajov@gmail.com>",
"Elven <mon.samuel@qq.com>", "Elven <mon.samuel@qq.com>",
"Friedel Ziegelmayer <dignifiedquire@gmail.com>", "Friedel Ziegelmayer <dignifiedquire@gmail.com>",
@ -94,7 +95,9 @@
"RasmusErik Voel Jensen <github@solsort.com>", "RasmusErik Voel Jensen <github@solsort.com>",
"Richard Littauer <richard.littauer@gmail.com>", "Richard Littauer <richard.littauer@gmail.com>",
"Ryan Bell <ryan@piing.net>", "Ryan Bell <ryan@piing.net>",
"Sönke Hahn <soenkehahn@gmail.com>",
"Tiago Alves <alvesjtiago@gmail.com>", "Tiago Alves <alvesjtiago@gmail.com>",
"Zane Starr <zcstarr@gmail.com>",
"greenkeeperio-bot <support@greenkeeper.io>", "greenkeeperio-bot <support@greenkeeper.io>",
"mayerwin <mayerwin@users.noreply.github.com>", "mayerwin <mayerwin@users.noreply.github.com>",
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>" "ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>"

View File

@ -10,6 +10,7 @@ const series = require('async/series')
const PeerBook = require('peer-book') const PeerBook = require('peer-book')
const Switch = require('libp2p-switch') const Switch = require('libp2p-switch')
const Ping = require('libp2p-ping') const Ping = require('libp2p-ping')
const WebSockets = require('libp2p-websockets')
const peerRouting = require('./peer-routing') const peerRouting = require('./peer-routing')
const contentRouting = require('./content-routing') const contentRouting = require('./content-routing')
@ -34,7 +35,8 @@ class Node extends EventEmitter {
this._isStarted = false this._isStarted = false
this.switch = new Switch(this.peerInfo, this.peerBook) this.switch = new Switch(this.peerInfo, this.peerBook, _options.switch)
this.stats = this.switch.stats
// Attach stream multiplexers // Attach stream multiplexers
if (this.modules.connection && this.modules.connection.muxer) { if (this.modules.connection && this.modules.connection.muxer) {
@ -128,8 +130,7 @@ class Node extends EventEmitter {
if (transport.filter(multiaddrs).length > 0) { if (transport.filter(multiaddrs).length > 0) {
this.switch.transport.add( this.switch.transport.add(
transport.tag || transport.constructor.name, transport) transport.tag || transport.constructor.name, transport)
} else if (transport.constructor && } else if (WebSockets.isWebSockets(transport)) {
transport.constructor.name === 'WebSockets') {
// TODO find a cleaner way to signal that a transport is always // TODO find a cleaner way to signal that a transport is always
// used for dialing, even if no listener // used for dialing, even if no listener
ws = transport ws = transport

View File

@ -9,3 +9,4 @@ require('./peer-routing.node')
require('./content-routing.node') require('./content-routing.node')
require('./circuit-relay.node') require('./circuit-relay.node')
require('./multiaddr-trim') require('./multiaddr-trim')
require('./stats')

24
test/stats.js Normal file
View File

@ -0,0 +1,24 @@
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const createNode = require('./utils/node').createNode
describe('libp2p', (done) => {
it('has stats', () => {
createNode('/ip4/127.0.0.1/tcp/0', {
mdns: false,
dht: true
}, (err, node) => {
expect(err).to.not.exist()
node.start((err) => {
expect(err).to.not.exist()
expect(node.stats).to.exist()
node.stop(done)
})
})
})
})