Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make libp2p a state machine #257

Merged
merged 13 commits into from
Oct 19, 2018
49 changes: 36 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
'use strict'

const libp2p = require('../../')
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
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
Loading