|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const assert = require('assert') |
| 4 | +const debug = require('debug') |
| 5 | +const log = debug('libp2p:peer-store') |
| 6 | +log.error = debug('libp2p:peer-store:error') |
| 7 | + |
| 8 | +const { Connection } = require('libp2p-interfaces/src/connection') |
| 9 | +const PeerInfo = require('peer-info') |
| 10 | +const Toplogy = require('./connection-manager/topology') |
| 11 | + |
| 12 | +/** |
| 13 | + * Responsible for notifying registered protocols of events in the network. |
| 14 | + */ |
| 15 | +class Registrar { |
| 16 | + /** |
| 17 | + * @param {Object} props |
| 18 | + * @param {PeerStore} props.peerStore |
| 19 | + * @constructor |
| 20 | + */ |
| 21 | + constructor ({ peerStore }) { |
| 22 | + this.peerStore = peerStore |
| 23 | + |
| 24 | + /** |
| 25 | + * Map of connections per peer |
| 26 | + * TODO: this should be handled by connectionManager |
| 27 | + * @type {Map<string, Array<conn>>} |
| 28 | + */ |
| 29 | + this.connections = new Map() |
| 30 | + |
| 31 | + /** |
| 32 | + * Map of topologies |
| 33 | + * |
| 34 | + * @type {Map<string, object>} |
| 35 | + */ |
| 36 | + this.topologies = new Map() |
| 37 | + |
| 38 | + this._handle = undefined |
| 39 | + } |
| 40 | + |
| 41 | + get handle () { |
| 42 | + return this._handle |
| 43 | + } |
| 44 | + |
| 45 | + set handle (handle) { |
| 46 | + this._handle = handle |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Add a new connected peer to the record |
| 51 | + * TODO: this should live in the ConnectionManager |
| 52 | + * @param {PeerInfo} peerInfo |
| 53 | + * @param {Connection} conn |
| 54 | + * @returns {void} |
| 55 | + */ |
| 56 | + onConnect (peerInfo, conn) { |
| 57 | + assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info') |
| 58 | + assert(Connection.isConnection(conn), 'conn must be an instance of interface-connection') |
| 59 | + |
| 60 | + const id = peerInfo.id.toB58String() |
| 61 | + const storedConn = this.connections.get(id) |
| 62 | + |
| 63 | + if (storedConn) { |
| 64 | + storedConn.push(conn) |
| 65 | + } else { |
| 66 | + this.connections.set(id, [conn]) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Remove a disconnected peer from the record |
| 72 | + * TODO: this should live in the ConnectionManager |
| 73 | + * @param {PeerInfo} peerInfo |
| 74 | + * @param {Connection} connection |
| 75 | + * @param {Error} [error] |
| 76 | + * @returns {void} |
| 77 | + */ |
| 78 | + onDisconnect (peerInfo, connection, error) { |
| 79 | + assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info') |
| 80 | + |
| 81 | + const id = peerInfo.id.toB58String() |
| 82 | + let storedConn = this.connections.get(id) |
| 83 | + |
| 84 | + if (storedConn && storedConn.length > 1) { |
| 85 | + storedConn = storedConn.filter((conn) => conn.id === connection.id) |
| 86 | + } else if (storedConn) { |
| 87 | + for (const [, topology] of this.topologies) { |
| 88 | + topology.disconnect(peerInfo, error) |
| 89 | + } |
| 90 | + |
| 91 | + this.connections.delete(peerInfo.id.toB58String()) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Get a connection with a peer. |
| 97 | + * @param {PeerInfo} peerInfo |
| 98 | + * @returns {Connection} |
| 99 | + */ |
| 100 | + getConnection (peerInfo) { |
| 101 | + assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info') |
| 102 | + |
| 103 | + // TODO: what should we return |
| 104 | + return this.connections.get(peerInfo.id.toB58String())[0] |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Register handlers for a set of multicodecs given |
| 109 | + * @param {Object} topologyProps properties for topology |
| 110 | + * @param {Array<string>|string} topologyProps.multicodecs |
| 111 | + * @param {Object} topologyProps.handlers |
| 112 | + * @param {function} topologyProps.handlers.onConnect |
| 113 | + * @param {function} topologyProps.handlers.onDisconnect |
| 114 | + * @return {string} registrar identifier |
| 115 | + */ |
| 116 | + register (topologyProps) { |
| 117 | + // Create multicodec topology |
| 118 | + const id = (parseInt(Math.random() * 1e9)).toString(36) + Date.now() |
| 119 | + const topology = new Toplogy(topologyProps) |
| 120 | + |
| 121 | + this.topologies.set(id, topology) |
| 122 | + |
| 123 | + // Set registrar |
| 124 | + topology.registrar = this |
| 125 | + |
| 126 | + return id |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Unregister topology. |
| 131 | + * @param {string} id registrar identifier |
| 132 | + * @return {boolean} unregistered successfully |
| 133 | + */ |
| 134 | + unregister (id) { |
| 135 | + return this.topologies.delete(id) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +module.exports = Registrar |
0 commit comments