import { NetworkPeer } from 'shared/model/Peer/networkPeer' ; import { PeerAddress } from 'shared/model/types' ; import IPeerRepository from 'core/repository/peer/index' ; import { logger } from 'shared/util/logger' ; import { peerAddressToString } from 'core/util/peer' ; class PeerNetworkRepository implements IPeerRepository < PeerAddress , NetworkPeer > { private peers : Map < string , NetworkPeer > ; private banList : Set < string > ; constructor ( ) { this . peers = new Map ( ) ; this . banList = new Set ( ) ; } ban ( peerAddress ) { logger . debug ( `[Repository][Peer][ban] peer ${ peerAddressToString ( peerAddress ) } has been banned` ) ; this . banList . add ( peerAddressToString ( peerAddress ) ) ; if ( this . has ( peerAddress ) ) { const peer = this . get ( peerAddress ) ; peer . ban ( ) ; } } unban ( peerAddress ) { logger . debug ( `[Repository][Peer][unban] peer ${ peerAddressToString ( peerAddress ) }` ) ; this . banList . delete ( peerAddressToString ( peerAddress ) ) ; if ( this . has ( peerAddress ) ) { const peer = this . get ( peerAddress ) ; peer . unban ( ) ; } } isBanned ( peerAddress ) { return this . banList . has ( peerAddressToString ( peerAddress ) ) ; } clearBanList ( ) { this . banList . clear ( ) ; } add ( peerAddress , socket ) { this . peers . set ( peerAddressToString ( peerAddress ) , new NetworkPeer ( { peerAddress , socket , isBanned : this . isBanned ( peerAddress ) } ) ) ; } remove ( peerAddress ) { if ( ! this . has ( peerAddress ) ) { return ; } const peer = this . get ( peerAddress ) ; peer . disconnect ( ) ; this . peers . delete ( peerAddressToString ( peerAddress ) ) ; } removeAll ( ) { [ ... this . peers . keys ( ) ] . forEach ( key => { this . peers . get ( key ) . disconnect ( ) ; this . peers . delete ( key ) ; } ) ; } get ( peerAddress ) { return this . peers . get ( peerAddressToString ( peerAddress ) ) ; } getManyByAddress ( peerAddresses ) { return peerAddresses . filter ( ( peerAddress ) => this . has ( peerAddress ) ) . map ( ( peerAddress ) => this . get ( peerAddress ) ) ; } getAll ( ) { return [ ... this . peers . values ( ) ] ; } has ( peerAddress ) { return this . peers . has ( peerAddressToString ( peerAddress ) ) ; } get count ( ) : number { return this . peers . size ; } get unbanCount ( ) : number { return this . getAll ( ) . filter ( ( networkPeer ) => ! this . isBanned ( networkPeer . peerAddress ) ) . length ; } } export default new PeerNetworkRepository ( ) ;