feat: registrar (#471)

* feat: peer-store v0

* feat: registrar

* chore: apply suggestions from code review

Co-Authored-By: Jacob Heun <jacobheun@gmail.com>

* chore: address review

* chore: support multiple conns

* chore: address review

* fix: no remote peer from topology on disconnect
This commit is contained in:
Vasco Santos
2019-11-06 15:47:11 +01:00
committed by Jacob Heun
parent f3e276eb79
commit 797d8f0cf1
8 changed files with 594 additions and 8 deletions

View File

@ -29,6 +29,7 @@ const Dialer = require('./dialer')
const TransportManager = require('./transport-manager')
const Upgrader = require('./upgrader')
const PeerStore = require('./peer-store')
const Registrar = require('./registrar')
const notStarted = (action, state) => {
return errCode(
@ -71,10 +72,13 @@ class Libp2p extends EventEmitter {
const peerInfo = getPeerInfo(connection.remotePeer)
this.peerStore.put(peerInfo)
this.registrar.onConnect(peerInfo, connection)
this.emit('peer:connect', peerInfo)
},
onConnectionEnd: (connection) => {
const peerInfo = getPeerInfo(connection.remotePeer)
this.registrar.onDisconnect(peerInfo, connection)
this.emit('peer:disconnect', peerInfo)
}
})
@ -108,6 +112,10 @@ class Libp2p extends EventEmitter {
transportManager: this.transportManager
})
this.registrar = new Registrar({ peerStore: this.peerStore })
this.handle = this.handle.bind(this)
this.registrar.handle = this.handle
// Attach private network protector
if (this._modules.connProtector) {
this.upgrader.protector = this._modules.connProtector