mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-21 04:51:33 +00:00
chore: refactor connection manager and registrar
This commit is contained in:
@ -46,16 +46,28 @@ class IdentifyService {
|
||||
/**
|
||||
* @constructor
|
||||
* @param {object} options
|
||||
* @param {Registrar} options.registrar
|
||||
* @param {PeerStore} options.peerStore
|
||||
* @param {ConnectionManager} options.connectionManager
|
||||
* @param {Map<string, handler>} options.protocols A reference to the protocols we support
|
||||
* @param {PeerId} options.peerId The peer running the identify service
|
||||
* @param {{ listen: Array<Multiaddr>}} options.addresses The peer addresses
|
||||
*/
|
||||
constructor (options) {
|
||||
/**
|
||||
* @property {Registrar}
|
||||
* @property {PeerStore}
|
||||
*/
|
||||
this.registrar = options.registrar
|
||||
this.peerStore = options.peerStore
|
||||
|
||||
/**
|
||||
* @property {ConnectionManager}
|
||||
*/
|
||||
this.connectionManager = options.connectionManager
|
||||
this.connectionManager.on('peer:connect', (connection) => {
|
||||
const peerId = connection.remotePeer
|
||||
|
||||
this.identify(connection, peerId).catch(log.error)
|
||||
})
|
||||
|
||||
/**
|
||||
* @property {PeerId}
|
||||
*/
|
||||
@ -104,7 +116,7 @@ class IdentifyService {
|
||||
const connections = []
|
||||
let connection
|
||||
for (const peer of peerStore.peers.values()) {
|
||||
if (peer.protocols.includes(MULTICODEC_IDENTIFY_PUSH) && (connection = this.registrar.getConnection(peer.id))) {
|
||||
if (peer.protocols.includes(MULTICODEC_IDENTIFY_PUSH) && (connection = this.connectionManager.get(peer.id))) {
|
||||
connections.push(connection)
|
||||
}
|
||||
}
|
||||
@ -160,8 +172,8 @@ class IdentifyService {
|
||||
observedAddr = IdentifyService.getCleanMultiaddr(observedAddr)
|
||||
|
||||
// Update peers data in PeerStore
|
||||
this.registrar.peerStore.addressBook.set(id, listenAddrs.map((addr) => multiaddr(addr)))
|
||||
this.registrar.peerStore.protoBook.set(id, protocols)
|
||||
this.peerStore.addressBook.set(id, listenAddrs.map((addr) => multiaddr(addr)))
|
||||
this.peerStore.protoBook.set(id, protocols)
|
||||
|
||||
// TODO: Track our observed address so that we can score it
|
||||
log('received observed address of %s', observedAddr)
|
||||
@ -245,13 +257,13 @@ class IdentifyService {
|
||||
// Update peers data in PeerStore
|
||||
const id = connection.remotePeer
|
||||
try {
|
||||
this.registrar.peerStore.addressBook.set(id, message.listenAddrs.map((addr) => multiaddr(addr)))
|
||||
this.peerStore.addressBook.set(id, message.listenAddrs.map((addr) => multiaddr(addr)))
|
||||
} catch (err) {
|
||||
return log.error('received invalid listen addrs', err)
|
||||
}
|
||||
|
||||
// Update the protocols
|
||||
this.registrar.peerStore.protoBook.set(id, message.protocols)
|
||||
this.peerStore.protoBook.set(id, message.protocols)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user