forked from libp2p/js-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make switch a state machine (libp2p#278)
* 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
- Loading branch information
Showing
21 changed files
with
1,714 additions
and
668 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
'use strict' | ||
|
||
const EventEmitter = require('events').EventEmitter | ||
const debug = require('debug') | ||
const withIs = require('class-is') | ||
|
||
class BaseConnection extends EventEmitter { | ||
constructor ({ _switch, name }) { | ||
super() | ||
|
||
this.switch = _switch | ||
this.ourPeerInfo = this.switch._peerInfo | ||
this.log = debug(`libp2p:conn:${name}`) | ||
} | ||
|
||
/** | ||
* Gets the current state of the connection | ||
* | ||
* @returns {string} The current state of the connection | ||
*/ | ||
getState () { | ||
return this._state._state | ||
} | ||
|
||
/** | ||
* Puts the state into encrypting mode | ||
* | ||
* @returns {void} | ||
*/ | ||
encrypt () { | ||
this._state('encrypt') | ||
} | ||
|
||
/** | ||
* Puts the state into privatizing mode | ||
* | ||
* @returns {void} | ||
*/ | ||
protect () { | ||
this._state('privatize') | ||
} | ||
|
||
/** | ||
* Puts the state into muxing mode | ||
* | ||
* @returns {void} | ||
*/ | ||
upgrade () { | ||
this._state('upgrade') | ||
} | ||
|
||
/** | ||
* Event handler for disconnected. | ||
* | ||
* @fires BaseConnection#close | ||
* @returns {void} | ||
*/ | ||
_onDisconnected () { | ||
this.log(`disconnected from ${this.theirB58Id}`) | ||
this.emit('close') | ||
this.removeAllListeners() | ||
} | ||
|
||
/** | ||
* Event handler for privatized | ||
* | ||
* @fires BaseConnection#private | ||
* @returns {void} | ||
*/ | ||
_onPrivatized () { | ||
this.log(`successfully privatized incoming connection`) | ||
this.emit('private', this.conn) | ||
} | ||
|
||
/** | ||
* Wraps this.conn with the Switch.protector for private connections | ||
* | ||
* @private | ||
* @fires ConnectionFSM#error | ||
* @returns {void} | ||
*/ | ||
_onPrivatizing () { | ||
if (!this.switch.protector) { | ||
return this._state('done') | ||
} | ||
|
||
this.conn = this.switch.protector.protect(this.conn, (err) => { | ||
if (err) { | ||
this.emit('error', err) | ||
return this._state('disconnect') | ||
} | ||
|
||
this.log(`successfully privatized conn to ${this.theirB58Id}`) | ||
this.conn.setPeerInfo(this.theirPeerInfo) | ||
this._state('done') | ||
}) | ||
} | ||
} | ||
|
||
module.exports = withIs(BaseConnection, { | ||
className: 'BaseConnection', | ||
symbolName: 'libp2p-switch/BaseConnection' | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict' | ||
|
||
const debug = require('debug') | ||
const IncomingConnection = require('./incoming') | ||
const observeConn = require('../observe-connection') | ||
|
||
function listener (_switch) { | ||
const log = debug(`libp2p:switch:listener`) | ||
|
||
/** | ||
* Takes a transport key and returns a connection handler function | ||
* | ||
* @param {string} transportKey The key of the transport to handle connections for | ||
* @param {function} handler A custom handler to use | ||
* @returns {function(Connection)} A connection handler function | ||
*/ | ||
return (transportKey, handler) => { | ||
/** | ||
* Takes a base connection and manages listening behavior | ||
* | ||
* @param {Connection} conn The connection to manage | ||
* @returns {void} | ||
*/ | ||
return (conn) => { | ||
// Add a transport level observer, if needed | ||
const connection = transportKey ? observeConn(transportKey, null, conn, _switch.observer) : conn | ||
|
||
log('received incoming connection') | ||
const connFSM = new IncomingConnection({ connection, _switch, transportKey }) | ||
|
||
connFSM.once('error', (err) => log(err)) | ||
connFSM.once('private', (_conn) => { | ||
// Use the custom handler, if it was provided | ||
if (handler) { | ||
return handler(_conn) | ||
} | ||
connFSM.encrypt() | ||
}) | ||
connFSM.once('encrypted', () => connFSM.upgrade()) | ||
|
||
connFSM.protect() | ||
} | ||
} | ||
} | ||
|
||
module.exports = listener |
Oops, something went wrong.