Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

Commit

Permalink
refactor: clean up some logic and make inc muxed conns FSMs
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobheun committed Sep 27, 2018
1 parent 770643c commit 7a5d1b0
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 44 deletions.
12 changes: 11 additions & 1 deletion src/connection/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ class BaseConnection extends EventEmitter {
this._state('upgrade')
}

/**
* Event handler for disconneced.
*
* @returns {void}
*/
_onDisconnected () {
this.log(`disconnected from ${this.theirB58Id}`)
this.removeAllListeners()
}

/**
* Wraps this.conn with the Switch.protector for private connections
*
Expand Down Expand Up @@ -67,5 +77,5 @@ class BaseConnection extends EventEmitter {

module.exports = withIs(BaseConnection, {
className: 'BaseConnection',
symbolName: 'libp2p-switch/BaseConnection',
symbolName: 'libp2p-switch/BaseConnection'
})
4 changes: 0 additions & 4 deletions src/connection/handler.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
'use strict'

const FSM = require('fsm-event')
const debug = require('debug')
const multistream = require('multistream-select')
const withIs = require('class-is')

const IncomingConnection = require('./incoming')
const observeConn = require('../observe-connection')

Expand Down
7 changes: 3 additions & 4 deletions src/connection/incoming.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict'

const FSM = require('fsm-event')
const debug = require('debug')
const multistream = require('multistream-select')
const withIs = require('class-is')

const observeConn = require('../observe-connection')
const BaseConnection = require('./base')

class IncomingConnectionFSM extends BaseConnection {
Expand Down Expand Up @@ -69,6 +67,7 @@ class IncomingConnectionFSM extends BaseConnection {
if (this.theirPeerInfo) {
this.theirPeerInfo.disconnect()
}
this._state('done')
})
}

Expand Down Expand Up @@ -120,5 +119,5 @@ class IncomingConnectionFSM extends BaseConnection {

module.exports = withIs(IncomingConnectionFSM, {
className: 'IncomingConnectionFSM',
symbolName: 'libp2p-switch/IncomingConnectionFSM',
})
symbolName: 'libp2p-switch/IncomingConnectionFSM'
})
19 changes: 6 additions & 13 deletions src/connection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ class ConnectionFSM extends BaseConnection {

// TODO: If given a muxer, we need to set the state
// at connected.
// * A muxed connection should be fully connected.
// * A protocol handshake should generate a new connection
let startState = 'DISCONNECTED'
if (this.muxer) {
startState = 'MUXED'
}

this._state = FSM('DISCONNECTED', {
this._state = FSM(startState, {
DISCONNECTED: { // No active connections exist for the peer
dial: 'DIALING'
},
Expand Down Expand Up @@ -254,15 +256,6 @@ class ConnectionFSM extends BaseConnection {
this.emit('connected', this.conn)
}

/**
* Event handler for disconneced.
*
* @returns {void}
*/
_onDisconnected () {
this.log(`disconnected from ${this.theirB58Id}`)
}

/**
* Event handler for disconnecting. Handles any needed cleanup
*
Expand Down Expand Up @@ -459,5 +452,5 @@ class ConnectionFSM extends BaseConnection {

module.exports = withIs(ConnectionFSM, {
className: 'ConnectionFSM',
symbolName: 'libp2p-switch/ConnectionFSM',
symbolName: 'libp2p-switch/ConnectionFSM'
})
7 changes: 6 additions & 1 deletion src/connection/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const debug = require('debug')
const log = debug('libp2p:switch:conn-manager')
const once = require('once')
const setImmediate = require('async/setImmediate')
const ConnectionFSM = require('../connection')

const Circuit = require('libp2p-circuit')

Expand Down Expand Up @@ -89,7 +90,11 @@ class ConnectionManager {
}
const b58Str = peerInfo.id.toB58String()

this.switch.muxedConns[b58Str] = { muxer: muxedConn }
this.switch.muxedConns[b58Str] = new ConnectionFSM({
_switch: this.switch,
peerInfo,
muxer: muxedConn
})

if (peerInfo.multiaddrs.size > 0) {
// with incomming conn and through identify, going to pick one
Expand Down
41 changes: 20 additions & 21 deletions src/dialer.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,31 @@ function dial (_switch) {
peerInfo,
muxer: _switch.muxedConns[b58Id] || null
})
connection.once('error', (err) => callback(err))
connection.on('connected', () => connection.protect())
connection.on('private', () => connection.encrypt())
connection.on('encrypted', () => connection.upgrade())
connection.on('muxed', () => {
maybePerformHandshake({
protocol,
proxyConnection,
connection,
callback
})
})
connection.on('unmuxed', () => {
maybePerformHandshake({
protocol,
proxyConnection,
connection,
callback
})
})
}

const proxyConnection = new Connection()
proxyConnection.setPeerInfo(peerInfo)

connection.once('error', (err) => callback(err))
connection.once('connected', () => connection.protect())
connection.once('private', () => connection.encrypt())
connection.once('encrypted', () => connection.upgrade())
connection.once('muxed', () => {
maybePerformHandshake({
protocol,
proxyConnection,
connection,
callback
})
})
connection.once('unmuxed', () => {
maybePerformHandshake({
protocol,
proxyConnection,
connection,
callback
})
})

// If we have a connection, maybe perform the protocol handshake
// TODO: The basic connection probably shouldnt be reused
const state = connection.getState()
Expand Down

0 comments on commit 7a5d1b0

Please sign in to comment.