@@ -5,19 +5,18 @@ const PeerId = require('peer-id')
5
5
const PeerInfo = require ( 'peer-info' )
6
6
const PeerBook = require ( 'peer-book' )
7
7
const multiaddr = require ( 'multiaddr' )
8
- const mafmt = require ( 'mafmt' )
9
- const EE = require ( 'events' ) . EventEmitter
8
+ const EventEmitter = require ( 'events' ) . EventEmitter
10
9
const assert = require ( 'assert' )
11
10
const Ping = require ( 'libp2p-ping' )
12
11
const setImmediate = require ( 'async/setImmediate' )
13
12
14
13
exports = module . exports
15
14
16
15
const OFFLINE_ERROR_MESSAGE = 'The libp2p node is not started yet'
17
- const IPFS_CODE = 421
18
16
19
- class Node {
17
+ class Node extends EventEmitter {
20
18
constructor ( _modules , _peerInfo , _peerBook , _options ) {
19
+ super ( )
21
20
assert ( _modules , 'requires modules to equip libp2p with features' )
22
21
assert ( _peerInfo , 'requires a PeerInfo instance' )
23
22
@@ -26,8 +25,6 @@ class Node {
26
25
this . peerBook = _peerBook || new PeerBook ( )
27
26
this . isOnline = false
28
27
29
- this . discovery = new EE ( )
30
-
31
28
this . swarm = new Swarm ( this . peerInfo )
32
29
33
30
// Attach stream multiplexers
@@ -66,9 +63,7 @@ class Node {
66
63
let discoveries = this . modules . discovery
67
64
discoveries = Array . isArray ( discoveries ) ? discoveries : [ discoveries ]
68
65
discoveries . forEach ( ( discovery ) => {
69
- discovery . on ( 'peer' , ( peerInfo ) => {
70
- this . discovery . emit ( 'peer' , peerInfo )
71
- } )
66
+ discovery . on ( 'peer' , ( peerInfo ) => this . emit ( 'peer' , peerInfo ) )
72
67
} )
73
68
}
74
69
@@ -142,88 +137,26 @@ class Node {
142
137
this . swarm . close ( callback )
143
138
}
144
139
145
- //
146
- // Ping
147
- //
148
-
149
- // TODO
150
- pingById ( id , callback ) {
151
- assert ( this . isOnline , OFFLINE_ERROR_MESSAGE )
152
- callback ( new Error ( 'not implemented yet' ) )
153
- }
154
-
155
- // TODO
156
- pingByMultiaddr ( maddr , callback ) {
157
- assert ( this . isOnline , OFFLINE_ERROR_MESSAGE )
158
- callback ( new Error ( 'not implemented yet' ) )
140
+ isOn ( ) {
141
+ return this . isOnline
159
142
}
160
143
161
- pingByPeerInfo ( peerInfo , callback ) {
162
- assert ( this . isOnline , OFFLINE_ERROR_MESSAGE )
144
+ ping ( peer , callback ) {
145
+ assert ( this . isOn , OFFLINE_ERROR_MESSAGE )
146
+ const peerInfo = this . _getPeerInfo ( peer )
163
147
callback ( null , new Ping ( this . swarm , peerInfo ) )
164
148
}
165
149
166
- //
167
- // Dialing methods
168
- //
169
-
170
- // TODO
171
- dialById ( id , protocol , callback ) {
172
- // NOTE: dialById only works if a previous dial was made. This will
173
- // change once we have PeerRouting
174
-
175
- assert ( this . isOnline , OFFLINE_ERROR_MESSAGE )
176
-
177
- if ( typeof protocol === 'function' ) {
178
- callback = protocol
179
- protocol = undefined
180
- }
181
-
182
- callback ( new Error ( 'not implemented yet' ) )
183
- }
184
-
185
- dialByMultiaddr ( maddr , protocol , callback ) {
186
- assert ( this . isOnline , OFFLINE_ERROR_MESSAGE )
187
-
188
- if ( typeof protocol === 'function' ) {
189
- callback = protocol
190
- protocol = undefined
191
- }
192
-
193
- if ( typeof maddr === 'string' ) {
194
- maddr = multiaddr ( maddr )
195
- }
196
-
197
- if ( ! mafmt . IPFS . matches ( maddr . toString ( ) ) ) {
198
- return callback ( new Error ( 'multiaddr not valid' ) )
199
- }
200
-
201
- const ipfsIdB58String = maddr . stringTuples ( ) . filter ( ( tuple ) => {
202
- if ( tuple [ 0 ] === IPFS_CODE ) {
203
- return true
204
- }
205
- } ) [ 0 ] [ 1 ]
206
-
207
- let peer
208
- try {
209
- peer = this . peerBook . getByB58String ( ipfsIdB58String )
210
- } catch ( err ) {
211
- peer = new PeerInfo ( PeerId . createFromB58String ( ipfsIdB58String ) )
212
- }
213
-
214
- peer . multiaddr . add ( maddr )
215
- this . dialByPeerInfo ( peer , protocol , callback )
216
- }
217
-
218
- dialByPeerInfo ( peer , protocol , callback ) {
150
+ dial ( peer , protocol , callback ) {
219
151
assert ( this . isOnline , OFFLINE_ERROR_MESSAGE )
152
+ const peerInfo = this . _getPeerInfo ( peer )
220
153
221
154
if ( typeof protocol === 'function' ) {
222
155
callback = protocol
223
156
protocol = undefined
224
157
}
225
158
226
- this . swarm . dial ( peer , protocol , ( err , conn ) => {
159
+ this . swarm . dial ( peerInfo , protocol , ( err , conn ) => {
227
160
if ( err ) {
228
161
return callback ( err )
229
162
}
@@ -232,59 +165,52 @@ class Node {
232
165
} )
233
166
}
234
167
235
- //
236
- // Disconnecting (hangUp) methods
237
- //
238
-
239
- hangUpById ( id , callback ) {
240
- // TODO
241
- callback ( new Error ( 'not implemented yet' ) )
242
- }
243
-
244
- hangUpByMultiaddr ( maddr , callback ) {
168
+ hangUp ( peer , callback ) {
245
169
assert ( this . isOnline , OFFLINE_ERROR_MESSAGE )
170
+ const peerInfo = this . _getPeerInfo ( peer )
246
171
247
- if ( typeof maddr === 'string' ) {
248
- maddr = multiaddr ( maddr )
249
- }
250
-
251
- if ( ! mafmt . IPFS . matches ( maddr . toString ( ) ) ) {
252
- return callback ( new Error ( 'multiaddr not valid' ) )
253
- }
254
-
255
- const ipfsIdB58String = maddr . stringTuples ( ) . filter ( ( tuple ) => {
256
- if ( tuple [ 0 ] === IPFS_CODE ) {
257
- return true
258
- }
259
- } ) [ 0 ] [ 1 ]
260
-
261
- try {
262
- const pi = this . peerBook . getByB58String ( ipfsIdB58String )
263
- this . hangUpByPeerInfo ( pi , callback )
264
- } catch ( err ) {
265
- // already disconnected
266
- callback ( )
267
- }
268
- }
269
-
270
- hangUpByPeerInfo ( peer , callback ) {
271
- assert ( this . isOnline , OFFLINE_ERROR_MESSAGE )
272
-
273
- this . peerBook . removeByB58String ( peer . id . toB58String ( ) )
172
+ this . peerBook . removeByB58String ( peerInfo . id . toB58String ( ) )
274
173
this . swarm . hangUp ( peer , callback )
275
174
}
276
175
277
- //
278
- // Protocol multiplexing handling
279
- //
280
-
281
176
handle ( protocol , handlerFunc , matchFunc ) {
282
177
this . swarm . handle ( protocol , handlerFunc , matchFunc )
283
178
}
284
179
285
180
unhandle ( protocol ) {
286
181
this . swarm . unhandle ( protocol )
287
182
}
183
+
184
+ /*
185
+ * Helper method to check the data type of peer and convert it to PeerInfo
186
+ */
187
+ _getPeerInfo ( peer ) {
188
+ let p
189
+ switch ( peer ) {
190
+ case PeerInfo . isPeerInfo ( peer ) : p = peer ; break
191
+ case multiaddr . isMultiaddr ( peer ) : {
192
+ const peerIdB58Str = multiaddr . getPeerId ( peer )
193
+ try {
194
+ p = this . peerBook . getByB58String ( peerIdB58Str )
195
+ } catch ( err ) {
196
+ p = new PeerInfo ( PeerId . createFromB58String ( peerIdB58Str ) )
197
+ }
198
+ p . multiaddr . add ( peer )
199
+ } break
200
+ case PeerId . isPeerId ( peer ) : {
201
+ const peerIdB58Str = peer . toB58String ( )
202
+ try {
203
+ p = this . peerBook . getByB58String ( peerIdB58Str )
204
+ } catch ( err ) {
205
+ // TODO this is where PeerRouting comes into place
206
+ throw new Error ( 'No knowledge about: ' + peerIdB58Str )
207
+ }
208
+ } break
209
+ default : throw new Error ( 'Peer type not recognized' )
210
+ }
211
+
212
+ return p
213
+ }
288
214
}
289
215
290
216
module . exports = Node
0 commit comments