@@ -4,7 +4,6 @@ const assert = require('assert')
4
4
const debug = require ( 'debug' )
5
5
const log = debug ( 'libp2p:peer-store' )
6
6
log . error = debug ( 'libp2p:peer-store:error' )
7
- const errCode = require ( 'err-code' )
8
7
9
8
const { Connection } = require ( 'libp2p-interfaces/src/connection' )
10
9
const PeerInfo = require ( 'peer-info' )
@@ -25,7 +24,7 @@ class Registrar {
25
24
/**
26
25
* Map of connections per peer
27
26
* TODO: this should be handled by connectionManager
28
- * @type {Map<string, conn> }
27
+ * @type {Map<string, Array< conn> > }
29
28
*/
30
29
this . connections = new Map ( )
31
30
@@ -58,24 +57,39 @@ class Registrar {
58
57
assert ( PeerInfo . isPeerInfo ( peerInfo ) , 'peerInfo must be an instance of peer-info' )
59
58
assert ( Connection . isConnection ( conn ) , 'conn must be an instance of interface-connection' )
60
59
61
- this . connections . set ( peerInfo . id . toB58String ( ) , conn )
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
+ }
62
68
}
63
69
64
70
/**
65
71
* Remove a disconnected peer from the record
66
72
* TODO: this should live in the ConnectionManager
67
73
* @param {PeerInfo } peerInfo
74
+ * @param {Connection } connection
68
75
* @param {Error } [error]
69
76
* @returns {void }
70
77
*/
71
- onDisconnect ( peerInfo , error ) {
78
+ onDisconnect ( peerInfo , connection , error ) {
72
79
assert ( PeerInfo . isPeerInfo ( peerInfo ) , 'peerInfo must be an instance of peer-info' )
73
80
74
- for ( const [ , topology ] of this . topologies ) {
75
- topology . disconnect ( peerInfo , error )
76
- }
81
+ const id = peerInfo . id . toB58String ( )
82
+ let storedConn = this . connections . get ( id )
77
83
78
- this . connections . delete ( peerInfo . id . toB58String ( ) )
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
+ }
79
93
}
80
94
81
95
/**
@@ -86,71 +100,32 @@ class Registrar {
86
100
getConnection ( peerInfo ) {
87
101
assert ( PeerInfo . isPeerInfo ( peerInfo ) , 'peerInfo must be an instance of peer-info' )
88
102
89
- return this . connections . get ( peerInfo . id . toB58String ( ) )
103
+ // TODO: what should we return
104
+ return this . connections . get ( peerInfo . id . toB58String ( ) ) [ 0 ]
90
105
}
91
106
92
107
/**
93
108
* Register handlers for a set of multicodecs given
94
- * @param {Array<string>|string } multicodecs
95
- * @param {object } handlers
96
- * @param {function } handlers.onConnect
97
- * @param {function } handlers.onDisconnect
98
- * @param {object } topologyProps properties for topology
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
99
114
* @return {string } registrar identifier
100
115
*/
101
- register ( multicodecs , handlers , topologyProps = { } ) {
102
- if ( ! multicodecs ) {
103
- throw errCode ( new Error ( 'one or more multicodec should be provided' ) , 'ERR_NO_MULTICODECS' )
104
- } else if ( ! Array . isArray ( multicodecs ) ) {
105
- multicodecs = [ multicodecs ]
106
- }
107
-
108
- if ( ! handlers ) {
109
- throw errCode ( new Error ( 'the handlers should be provided' ) , 'ERR_NO_HANDLERS' )
110
- } else if ( ! handlers . onConnect || typeof handlers . onConnect !== 'function' ) {
111
- throw errCode ( new Error ( 'the \'onConnect\' handler must be provided' ) , 'ERR_NO_ONCONNECT_HANDLER' )
112
- } else if ( ! handlers . onDisconnect || typeof handlers . onDisconnect !== 'function' ) {
113
- throw errCode ( new Error ( 'the \'onDisconnect\' handler must be provided' ) , 'ERR_NO_ONDISCONNECT_HANDLER' )
114
- }
115
-
116
+ register ( topologyProps ) {
116
117
// Create multicodec topology
117
- const topology = new Toplogy ( {
118
- onConnect : handlers . onConnect ,
119
- onDisconnect : handlers . onDisconnect ,
120
- registrar : this ,
121
- multicodecs,
122
- peerStore : this . peerStore ,
123
- ...topologyProps
124
- } )
125
-
126
118
const id = ( parseInt ( Math . random ( ) * 1e9 ) ) . toString ( 36 ) + Date . now ( )
127
- this . topologies . set ( id , topology )
119
+ const topology = new Toplogy ( topologyProps )
128
120
129
- this . _addConnectedPeers ( multicodecs , topology )
121
+ this . topologies . set ( id , topology )
130
122
131
- // TODO: try to connect to peers-store peers according to current topology
123
+ // Set registrar
124
+ topology . registrar = this
132
125
133
126
return id
134
127
}
135
128
136
- _addConnectedPeers ( multicodecs , topology ) {
137
- const knownPeers = [ ]
138
-
139
- for ( const [ , peer ] of this . peerStore . peers ) {
140
- if ( multicodecs . filter ( multicodec => peer . protocols . has ( multicodec ) ) ) {
141
- knownPeers . push ( peer )
142
- }
143
- }
144
-
145
- for ( const [ id , conn ] of this . connections . entries ( ) ) {
146
- const targetPeer = knownPeers . find ( ( peerInfo ) => peerInfo . id . toB58String ( ) === id )
147
-
148
- if ( targetPeer ) {
149
- topology . tryToConnect ( targetPeer , conn )
150
- }
151
- }
152
- }
153
-
154
129
/**
155
130
* Unregister topology.
156
131
* @param {string } id registrar identifier
0 commit comments