33const lp = require ( 'it-length-prefixed' )
44const pipe = require ( 'it-pipe' )
55
6+ const MulticodecTopology = require ( 'libp2p-interfaces/src/topology/multicodec-topology' )
7+
68const Message = require ( './types/message' )
79const CONSTANTS = require ( './constants' )
810const logger = require ( './utils' ) . logger
@@ -37,8 +39,15 @@ class Network {
3739 this . _running = true
3840 this . libp2p . handle ( this . protocols , this . _onConnection )
3941
40- this . libp2p . connectionManager . on ( 'peer:connect' , this . _onPeerConnect )
41- this . libp2p . connectionManager . on ( 'peer:disconnect' , this . _onPeerDisconnect )
42+ // register protocol with topology
43+ const topology = new MulticodecTopology ( {
44+ multicodecs : this . protocols ,
45+ handlers : {
46+ onConnect : this . _onPeerConnect ,
47+ onDisconnect : this . _onPeerDisconnect
48+ }
49+ } )
50+ this . _registrarId = this . libp2p . registrar . register ( topology )
4251
4352 // All existing connections are like new ones for us
4453 for ( const peer of this . libp2p . peerStore . peers . values ( ) ) {
@@ -54,8 +63,8 @@ class Network {
5463 // Unhandle both, libp2p doesn't care if it's not already handled
5564 this . libp2p . unhandle ( this . protocols )
5665
57- this . libp2p . connectionManager . removeListener ( 'peer:connect' , this . _onPeerConnect )
58- this . libp2p . connectionManager . removeListener ( 'peer:disconnect' , this . _onPeerDisconnect )
66+ // unregister protocol and handlers
67+ this . libp2p . registrar . unregister ( this . _registrarId )
5968 }
6069
6170 /**
@@ -92,12 +101,12 @@ class Network {
92101 }
93102 }
94103
95- _onPeerConnect ( connection ) {
96- this . bitswap . _onPeerConnected ( connection . remotePeer )
104+ _onPeerConnect ( peerId ) {
105+ this . bitswap . _onPeerConnected ( peerId )
97106 }
98107
99- _onPeerDisconnect ( connection ) {
100- this . bitswap . _onPeerDisconnected ( connection . remotePeer )
108+ _onPeerDisconnect ( peerId ) {
109+ this . bitswap . _onPeerDisconnected ( peerId )
101110 }
102111
103112 /**
0 commit comments