Skip to content

Commit

Permalink
feat: make libp2p a state machine (#257)
Browse files Browse the repository at this point in the history
* docs: add events to readme
  • Loading branch information
jacobheun committed Oct 19, 2018
1 parent 686379e commit 0b75f99
Show file tree
Hide file tree
Showing 19 changed files with 519 additions and 109 deletions.
49 changes: 36 additions & 13 deletions README.md
Expand Up @@ -49,6 +49,7 @@ We've come a long way, but this project is still in Alpha, lots of development i
- [Install](#install)
- [Usage](#usage)
- [API](#api)
- [Events](#events)
- [Development](#development)
- [Tests](#tests)
- [Packages](#packages)
Expand Down Expand Up @@ -206,40 +207,46 @@ Required keys in the `options` object:

> Start the libp2p Node.
`callback` is a function with the following `function (err) {}` signature, where `err` is an Error in case starting the node fails.
`callback` following signature `function (err) {}`, where `err` is an Error in case starting the node fails.

#### `libp2p.stop(callback)`

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

#### `libp2p.dial(peer, callback)`

> Dials to another peer in the network, establishes the connection.
- `peer`: can be an instance of [PeerInfo][], [PeerId][], [multiaddr][], or a multiaddr string
- `callback`: Function with signature `function (err, conn) {}` where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object

`callback` is a function with the following `function (err, conn) {}` signature, where `err` is an Error in of failure to dial the connection and `conn` is a [Connection][] instance in case of a protocol selected, if not it is undefined.
- `callback` following signature `function (err, conn) {}`, where `err` is an Error in of failure to dial the connection and `conn` is a [Connection][] instance in case of a protocol selected, if not it is undefined.

#### `libp2p.dialProtocol(peer, protocol, callback)`

> Dials to another peer in the network and selects a protocol to talk with that peer.
- `peer`: can be an instance of [PeerInfo][], [PeerId][], [multiaddr][], or a multiaddr string
- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
- `callback`: Function with signature `function (err, conn) {}` where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object
- `callback`: Function with signature `function (err, conn) {}`, where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object

`callback` following signature `function (err, conn) {}`, where `err` is an Error in of failure to dial the connection and `conn` is a [Connection][] instance in case of a protocol selected, if not it is undefined.

`callback` is a function with the following `function (err, conn) {}` signature, where `err` is an Error in of failure to dial the connection and `conn` is a [Connection][] instance in case of a protocol selected, if not it is undefined.
#### `libp2p.dialFSM(peer, protocol, callback)`

> Behaves like `.dial` and `.dialProtocol` but calls back with a Connection State Machine
- `peer`: can be an instance of [PeerInfo][], [PeerId][], [multiaddr][], or a multiaddr string
- `protocol`: an optional String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
- `callback`: following signature `function (err, connFSM) {}`, where `connFSM` is a [Connection State Machine](https://github.com/libp2p/js-libp2p-switch#connection-state-machine)

#### `libp2p.hangUp(peer, callback)`

> Closes an open connection with a peer, graciously.
- `peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]

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

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

Expand All @@ -265,7 +272,7 @@ Required keys in the `options` object:
> Handle new protocol
- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
- `handlerFunc`: Function with signature `function (protocol, conn) {}` where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object
- `handlerFunc`: following signature `function (protocol, conn) {}`, where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object
- `matchFunc`: Function for matching on protocol (exact matching, semver, etc). Default to exact match.

#### `libp2p.unhandle(protocol)`
Expand All @@ -274,19 +281,35 @@ Required keys in the `options` object:
- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')

#### `libp2p.on('peer:discovery', (peer) => {})`
#### Events

##### `libp2p.on('start', () => {})`

> Libp2p has started, along with all its services.
##### `libp2p.on('stop', () => {})`

> Libp2p has stopped, along with all its services.
##### `libp2p.on('error', (err) => {})`

> An error has occurred
- `err`: instance of `Error`

##### `libp2p.on('peer:discovery', (peer) => {})`

> Peer has been discovered.
- `peer`: instance of [PeerInfo][]

#### `libp2p.on('peer:connect', (peer) => {})`
##### `libp2p.on('peer:connect', (peer) => {})`

> We connected to a new peer
- `peer`: instance of [PeerInfo][]

#### `libp2p.on('peer:disconnect', (peer) => {})`
##### `libp2p.on('peer:disconnect', (peer) => {})`

> We disconnected from Peer
Expand Down Expand Up @@ -574,4 +597,4 @@ The libp2p implementation in JavaScript is a work in progress. As such, there ar

## License

[MIT](LICENSE) © David Dias
[MIT](LICENSE) © Protocol Labs
1 change: 1 addition & 0 deletions examples/discovery-mechanisms/1.js
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
'use strict'

const libp2p = require('../../')
Expand Down
1 change: 1 addition & 0 deletions examples/discovery-mechanisms/2.js
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
'use strict'

const libp2p = require('../../')
Expand Down
1 change: 1 addition & 0 deletions examples/libp2p-in-the-browser/1/src/index.js
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
'use strict'

const domReady = require('detect-dom-ready')
Expand Down
1 change: 1 addition & 0 deletions examples/peer-and-content-routing/1.js
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
'use strict'

const libp2p = require('../../')
Expand Down
1 change: 1 addition & 0 deletions examples/peer-and-content-routing/2.js
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
'use strict'

const libp2p = require('../../')
Expand Down
1 change: 1 addition & 0 deletions examples/protocol-and-stream-muxing/3.js
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
'use strict'

const libp2p = require('../../')
Expand Down
1 change: 1 addition & 0 deletions examples/pubsub/1.js
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
'use strict'

const libp2p = require('../../')
Expand Down
1 change: 1 addition & 0 deletions examples/transports/1.js
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
'use strict'

const libp2p = require('../../')
Expand Down
1 change: 1 addition & 0 deletions examples/transports/2.js
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
'use strict'

const libp2p = require('../../')
Expand Down
1 change: 1 addition & 0 deletions examples/transports/3.js
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
'use strict'

const libp2p = require('../../')
Expand Down
7 changes: 5 additions & 2 deletions package.json
Expand Up @@ -38,13 +38,15 @@
},
"dependencies": {
"async": "^2.6.1",
"debug": "^4.0.1",
"err-code": "^1.1.2",
"fsm-event": "^2.1.0",
"joi": "^13.6.0",
"joi-browser": "^13.4.0",
"libp2p-connection-manager": "~0.0.2",
"libp2p-floodsub": "~0.15.0",
"libp2p-ping": "~0.8.0",
"libp2p-switch": "~0.40.8",
"libp2p-switch": "~0.41.0",
"libp2p-websockets": "~0.12.0",
"mafmt": "^6.0.2",
"multiaddr": "^5.0.0",
Expand All @@ -57,16 +59,17 @@
"@nodeutils/defaults-deep": "^1.1.0",
"aegir": "^15.2.0",
"chai": "^4.1.2",
"chai-checkmark": "^1.0.1",
"cids": "~0.5.3",
"dirty-chai": "^2.0.1",
"electron-webrtc": "~0.3.0",
"libp2p-bootstrap": "~0.9.3",
"libp2p-circuit": "~0.2.1",
"libp2p-delegated-content-routing": "~0.2.2",
"libp2p-delegated-peer-routing": "~0.2.2",
"libp2p-kad-dht": "~0.10.5",
"libp2p-mdns": "~0.12.0",
"libp2p-mplex": "~0.8.2",
"libp2p-bootstrap": "~0.9.3",
"libp2p-secio": "~0.10.0",
"libp2p-spdy": "~0.12.1",
"libp2p-tcp": "~0.13.0",
Expand Down

0 comments on commit 0b75f99

Please sign in to comment.