|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const assert = require('assert') |
| 4 | + |
| 5 | +class Topology { |
| 6 | + /** |
| 7 | + * @param {Object} props |
| 8 | + * @param {number} props.min minimum needed connections (default: 0) |
| 9 | + * @param {number} props.max maximum needed connections (default: Infinity) |
| 10 | + * @param {Array<string>} props.multicodecs protocol multicodecs |
| 11 | + * @param {Object} props.handlers |
| 12 | + * @param {function} props.handlers.onConnect protocol "onConnect" handler |
| 13 | + * @param {function} props.handlers.onDisconnect protocol "onDisconnect" handler |
| 14 | + * @constructor |
| 15 | + */ |
| 16 | + constructor ({ |
| 17 | + min = 0, |
| 18 | + max = Infinity, |
| 19 | + multicodecs, |
| 20 | + handlers |
| 21 | + }) { |
| 22 | + assert(multicodecs, 'one or more multicodec should be provided') |
| 23 | + assert(handlers, 'the handlers should be provided') |
| 24 | + assert(handlers.onConnect && typeof handlers.onConnect === 'function', |
| 25 | + 'the \'onConnect\' handler must be provided') |
| 26 | + assert(handlers.onDisconnect && typeof handlers.onDisconnect === 'function', |
| 27 | + 'the \'onDisconnect\' handler must be provided') |
| 28 | + |
| 29 | + this.multicodecs = Array.isArray(multicodecs) ? multicodecs : [multicodecs] |
| 30 | + this.min = min |
| 31 | + this.max = max |
| 32 | + |
| 33 | + // Handlers |
| 34 | + this._onConnect = handlers.onConnect |
| 35 | + this._onDisconnect = handlers.onDisconnect |
| 36 | + |
| 37 | + this.peers = new Map() |
| 38 | + this._registrar = undefined |
| 39 | + |
| 40 | + this._onProtocolChange = this._onProtocolChange.bind(this) |
| 41 | + } |
| 42 | + |
| 43 | + set registrar (registrar) { |
| 44 | + this._registrar = registrar |
| 45 | + this._registrar.peerStore.on('change:protocols', this._onProtocolChange) |
| 46 | + |
| 47 | + // Update topology peers |
| 48 | + this._updatePeers(this._registrar.peerStore.peers.values()) |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Update topology. |
| 53 | + * @param {Array<PeerInfo>} peerInfoIterable |
| 54 | + * @returns {void} |
| 55 | + */ |
| 56 | + _updatePeers (peerInfoIterable) { |
| 57 | + for (const peerInfo of peerInfoIterable) { |
| 58 | + if (this.multicodecs.filter(multicodec => peerInfo.protocols.has(multicodec))) { |
| 59 | + // Add the peer regardless of whether or not there is currently a connection |
| 60 | + this.peers.set(peerInfo.id.toB58String(), peerInfo) |
| 61 | + // If there is a connection, call _onConnect |
| 62 | + const connection = this._registrar.getConnection(peerInfo) |
| 63 | + connection && this._onConnect(peerInfo, connection) |
| 64 | + } else { |
| 65 | + // Remove any peers we might be tracking that are no longer of value to us |
| 66 | + this.peers.delete(peerInfo.id.toB58String()) |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Notify protocol of peer disconnected. |
| 73 | + * @param {PeerInfo} peerInfo |
| 74 | + * @param {Error} [error] |
| 75 | + * @returns {void} |
| 76 | + */ |
| 77 | + disconnect (peerInfo, error) { |
| 78 | + this._onDisconnect(peerInfo, error) |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Check if a new peer support the multicodecs for this topology. |
| 83 | + * @param {Object} props |
| 84 | + * @param {PeerInfo} props.peerInfo |
| 85 | + * @param {Array<string>} props.protocols |
| 86 | + */ |
| 87 | + _onProtocolChange ({ peerInfo, protocols }) { |
| 88 | + const existingPeer = this.peers.get(peerInfo.id.toB58String()) |
| 89 | + const hasProtocol = protocols.filter(protocol => this.multicodecs.includes(protocol)) |
| 90 | + |
| 91 | + // Not supporting the protocol anymore? |
| 92 | + if (existingPeer && hasProtocol.length === 0) { |
| 93 | + this._onDisconnect({ |
| 94 | + peerInfo |
| 95 | + }) |
| 96 | + } |
| 97 | + |
| 98 | + // New to protocol support |
| 99 | + for (const protocol of protocols) { |
| 100 | + if (this.multicodecs.includes(protocol)) { |
| 101 | + this._updatePeers([peerInfo]) |
| 102 | + return |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +module.exports = Topology |
0 commit comments