@@ -18,7 +18,7 @@ const Circuit = require('libp2p-circuit')
18
18
19
19
exports = module . exports
20
20
21
- const OFFLINE_ERROR_MESSAGE = 'The libp2p node is not started yet'
21
+ const NOT_STARTED_ERROR_MESSAGE = 'The libp2p node is not started yet'
22
22
23
23
class Node extends EventEmitter {
24
24
constructor ( _modules , _peerInfo , _peerBook , _options ) {
@@ -29,7 +29,7 @@ class Node extends EventEmitter {
29
29
this . modules = _modules
30
30
this . peerInfo = _peerInfo
31
31
this . peerBook = _peerBook || new PeerBook ( )
32
- this . isOnline = false
32
+ this . _isStarted = false
33
33
this . relayCircuit = null
34
34
35
35
this . swarm = new Swarm ( this . peerInfo , this . peerBook )
@@ -176,7 +176,6 @@ class Node extends EventEmitter {
176
176
}
177
177
} )
178
178
this . peerInfo . multiaddrs . replace ( maOld , maNew )
179
-
180
179
const multiaddrs = this . peerInfo . multiaddrs . toArray ( )
181
180
182
181
transports . forEach ( ( transport ) => {
@@ -194,9 +193,6 @@ class Node extends EventEmitter {
194
193
series ( [
195
194
( cb ) => this . swarm . listen ( cb ) ,
196
195
( cb ) => {
197
- // listeners on, libp2p is on
198
- this . isOnline = true
199
-
200
196
if ( ws ) {
201
197
// always add dialing on websockets
202
198
this . swarm . transport . add ( ws . tag || ws . constructor . name , ws )
@@ -209,10 +205,17 @@ class Node extends EventEmitter {
209
205
cb ( )
210
206
} ,
211
207
( cb ) => {
208
+ // TODO: chicken-and-egg problem:
209
+ // have to set started here because DHT requires libp2p is already started
210
+ this . _isStarted = true
212
211
if ( this . _dht ) {
213
212
return this . _dht . start ( cb )
214
213
}
215
214
cb ( )
215
+ } ,
216
+ ( cb ) => {
217
+ this . emit ( 'start' )
218
+ cb ( )
216
219
}
217
220
] , callback )
218
221
}
@@ -221,7 +224,7 @@ class Node extends EventEmitter {
221
224
* Stop the libp2p node by closing its listeners and open connections
222
225
*/
223
226
stop ( callback ) {
224
- this . isOnline = false
227
+ this . _isStarted = false
225
228
226
229
if ( this . modules . discovery ) {
227
230
this . modules . discovery . forEach ( ( discovery ) => {
@@ -236,16 +239,20 @@ class Node extends EventEmitter {
236
239
}
237
240
cb ( )
238
241
} ,
239
- ( cb ) => this . swarm . close ( cb )
242
+ ( cb ) => this . swarm . close ( cb ) ,
243
+ ( cb ) => {
244
+ this . emit ( 'stop' )
245
+ cb ( )
246
+ }
240
247
] , callback )
241
248
}
242
249
243
- isOn ( ) {
244
- return this . isOnline
250
+ isStarted ( ) {
251
+ return this . _isStarted
245
252
}
246
253
247
254
ping ( peer , callback ) {
248
- assert ( this . isOn ( ) , OFFLINE_ERROR_MESSAGE )
255
+ assert ( this . isStarted ( ) , NOT_STARTED_ERROR_MESSAGE )
249
256
this . _getPeerInfo ( peer , ( err , peerInfo ) => {
250
257
if ( err ) {
251
258
return callback ( err )
@@ -256,7 +263,7 @@ class Node extends EventEmitter {
256
263
}
257
264
258
265
dial ( peer , protocol , callback ) {
259
- assert ( this . isOn ( ) , OFFLINE_ERROR_MESSAGE )
266
+ assert ( this . isStarted ( ) , NOT_STARTED_ERROR_MESSAGE )
260
267
261
268
if ( typeof protocol === 'function' ) {
262
269
callback = protocol
@@ -279,7 +286,7 @@ class Node extends EventEmitter {
279
286
}
280
287
281
288
hangUp ( peer , callback ) {
282
- assert ( this . isOn ( ) , OFFLINE_ERROR_MESSAGE )
289
+ assert ( this . isStarted ( ) , NOT_STARTED_ERROR_MESSAGE )
283
290
284
291
this . _getPeerInfo ( peer , ( err , peerInfo ) => {
285
292
if ( err ) {
0 commit comments