You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 23, 2019. It is now read-only.
* feat: add basic state machine functionality to switch
* feat: make connections state machines
* refactor: clean up logs
* feat: add dialFSM to the switch
* feat: add better support for closing connections
* test: add tests for some uncovered lines
* feat: add warning emitter for muxer upgrade failed
* docs: update readme
- peerInfo is a [PeerInfo](https://github.com/libp2p/js-peer-info) object that has the peer information.
87
88
- peerBook is a [PeerBook](https://github.com/libp2p/js-peer-book) object that stores all the known peers.
88
89
90
+
### `switch.connection`
91
+
92
+
##### `switch.connection.addUpgrade()`
93
+
94
+
A connection upgrade must be able to receive and return something that implements the [interface-connection](https://github.com/libp2p/interface-connection) specification.
95
+
96
+
> **WIP**
97
+
98
+
##### `switch.connection.addStreamMuxer(muxer)`
99
+
100
+
Upgrading a connection to use a stream muxer is still considered an upgrade, but a special case since once this connection is applied, the returned obj will implement the [interface-stream-muxer](https://github.com/libp2p/interface-stream-muxer) spec.
101
+
102
+
-`muxer`
103
+
104
+
##### `switch.connection.reuse()`
105
+
106
+
Enable the identify protocol.
107
+
108
+
##### `switch.connection.crypto([tag, encrypt])`
109
+
110
+
Enable a specified crypto protocol. By default no encryption is used, aka `plaintext`. If called with no arguments it resets to use `plaintext`.
111
+
112
+
You can use for example [libp2p-secio](https://github.com/libp2p/js-libp2p-secio) like this
- enabled - activates relay dialing and listening functionality
125
+
- hop - an object with two properties
126
+
- enabled - enables circuit relaying
127
+
- active - is it an active or passive relay (default false)
128
+
- `callback`
129
+
89
130
### `switch.dial(peer, protocol, callback)`
90
131
91
132
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.
@@ -94,13 +135,24 @@ dial uses the best transport (whatever works first, in the future we can have so
94
135
- `protocol`
95
136
- `callback`
96
137
97
-
### `switch.hangUp(peer, callback)`
138
+
### `switch.dialFSM(peer, protocol, callback)`
98
139
99
-
Hang up the muxed connection we have with the peer.
140
+
works like dial, but calls back with a [Connection State Machine](#connection-state-machine)
100
141
101
142
- `peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]
102
-
-`callback`
143
+
- `protocol`: String that defines the protocol (e.g'/ipfs/bitswap/1.1.0') to be used
144
+
- `callback`: Function with signature `function (err, connFSM) {}` where `connFSM` is a [Connection State Machine](#connection-state-machine)
103
145
146
+
#### Connection State Machine
147
+
Connection state machines emit a number of events that can be used to determine the current state of the connection
148
+
and to received the underlying connection that can be used to transfer data.
149
+
150
+
##### Events
151
+
- `error`: emitted whenever a fatal error occurs with the connection; the error will be emitted.
152
+
- `error:upgrade_failed`: emitted whenever the connection fails to upgrade with a muxer, this is not fatal.
153
+
- `error:connection_attempt_failed`: emitted whenever a dial attempt fails for a given transport. An array of errors is emitted.
154
+
- `connection`: emitted whenever a useable connection has been established; the underlying [Connection](https://github.com/libp2p/interface-connection) will be emitted.
155
+
- `close`: emitted when the connection has closed.
-`peer`: is instance of [PeerInfo][] that has info of the peer we have just established a muxed connection with.
122
-
123
-
### `switch.on('peer-mux-closed', (peer) => {})`
124
-
125
-
-`peer`: is instance of [PeerInfo][] that has info of the peer we have just closed a muxed connection.
126
-
127
-
### `switch.start(callback)`
128
-
129
-
Start listening on all added transports that are available on the current `peerInfo`.
130
-
131
-
### `switch.stop(callback)`
165
+
### `switch.hangUp(peer, callback)`
132
166
133
-
Close all the listeners and muxers.
167
+
Hang up the muxed connection we have with the peer.
134
168
169
+
- `peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]
135
170
- `callback`
136
171
137
-
### `switch.connection`
172
+
### `switch.on('error', (err) => {})`
138
173
139
-
##### `switch.connection.addUpgrade()`
174
+
Emitted when the switch encounters an error.
140
175
141
-
A connection upgrade must be able to receive and return something that implements the [interface-connection](https://github.com/libp2p/interface-connection) specification.
- `peer`: is instance of [PeerInfo][] that has info of the peer we have just established a muxed connection with.
146
181
147
-
Upgrading a connection to use a stream muxer is still considered an upgrade, but a special case since once this connection is applied, the returned obj will implement the [interface-stream-muxer](https://github.com/libp2p/interface-stream-muxer) spec.
182
+
### `switch.on('peer-mux-closed', (peer) => {})`
148
183
149
-
-`muxer`
184
+
- `peer`: is instance of [PeerInfo][] that has info of the peer we have just closed a muxed connection.
150
185
151
-
#####`switch.connection.reuse()`
186
+
### `switch.on('start', () => {})`
152
187
153
-
Enable the identify protocol.
188
+
Emitted when the switch has successfully started.
154
189
155
-
#####`switch.connection.crypto([tag, encrypt])`
190
+
### `switch.on('stop', () => {})`
156
191
157
-
Enable a specified crypto protocol. By default no encryption is used, aka `plaintext`. If called with no arguments it resets to use `plaintext`.
192
+
Emitted when the switch has successfully stopped.
158
193
159
-
You can use for example [libp2p-secio](https://github.com/libp2p/js-libp2p-secio) like this
- enabled - activates relay dialing and listening functionality
172
-
- hop - an object with two properties
173
-
- enabled - enables circuit relaying
174
-
- active - is it an active or passive relay (default false)
175
202
- `callback`
176
203
177
204
### Stats API
@@ -278,6 +305,11 @@ Each one of these values is [an exponential moving-average instance](https://git
278
305
279
306
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.
0 commit comments