3
3
const lp = require ( 'it-length-prefixed' )
4
4
const pipe = require ( 'it-pipe' )
5
5
6
+ const MulticodecTopology = require ( 'libp2p-interfaces/src/topology/multicodec-topology' )
7
+
6
8
const Message = require ( './types/message' )
7
9
const CONSTANTS = require ( './constants' )
8
10
const logger = require ( './utils' ) . logger
@@ -33,12 +35,19 @@ class Network {
33
35
this . _onConnection = this . _onConnection . bind ( this )
34
36
}
35
37
36
- start ( ) {
38
+ async start ( ) {
37
39
this . _running = true
38
40
this . libp2p . handle ( this . protocols , this . _onConnection )
39
41
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 = await this . libp2p . registrar . register ( topology )
42
51
43
52
// All existing connections are like new ones for us
44
53
for ( const peer of this . libp2p . peerStore . peers . values ( ) ) {
@@ -48,14 +57,14 @@ class Network {
48
57
}
49
58
}
50
59
51
- stop ( ) {
60
+ async stop ( ) {
52
61
this . _running = false
53
62
54
63
// Unhandle both, libp2p doesn't care if it's not already handled
55
64
this . libp2p . unhandle ( this . protocols )
56
65
57
- this . libp2p . connectionManager . removeListener ( 'peer:connect' , this . _onPeerConnect )
58
- this . libp2p . connectionManager . removeListener ( 'peer:disconnect' , this . _onPeerDisconnect )
66
+ // unregister protocol and handlers
67
+ await this . libp2p . registrar . unregister ( this . _registrarId )
59
68
}
60
69
61
70
/**
@@ -92,12 +101,12 @@ class Network {
92
101
}
93
102
}
94
103
95
- _onPeerConnect ( connection ) {
96
- this . bitswap . _onPeerConnected ( connection . remotePeer )
104
+ _onPeerConnect ( peerId ) {
105
+ this . bitswap . _onPeerConnected ( peerId )
97
106
}
98
107
99
- _onPeerDisconnect ( connection ) {
100
- this . bitswap . _onPeerDisconnected ( connection . remotePeer )
108
+ _onPeerDisconnect ( peerId ) {
109
+ this . bitswap . _onPeerDisconnected ( peerId )
101
110
}
102
111
103
112
/**
0 commit comments