refactor: ping (#505)

* refactor: ping

* chore: ping is now a function

* chore: address review
This commit is contained in:
Vasco Santos
2019-12-10 19:26:54 +01:00
committed by Jacob Heun
parent 7fc1900343
commit 64cbf90e02
10 changed files with 142 additions and 174 deletions

View File

@ -21,6 +21,7 @@ const TransportManager = require('./transport-manager')
const Upgrader = require('./upgrader')
const PeerStore = require('./peer-store')
const Registrar = require('./registrar')
const ping = require('./ping')
const {
IdentifyService,
multicodecs: IDENTIFY_PROTOCOLS
@ -148,6 +149,9 @@ class Libp2p extends EventEmitter {
this.peerRouting = peerRouting(this)
this.contentRouting = contentRouting(this)
// Mount default protocols
ping.mount(this)
this._onDiscoveryPeer = this._onDiscoveryPeer.bind(this)
}
@ -203,6 +207,8 @@ class Libp2p extends EventEmitter {
await this.transportManager.close()
await this.registrar.close()
ping.unmount(this)
} catch (err) {
if (err) {
log.error(err)
@ -280,17 +286,16 @@ class Libp2p extends EventEmitter {
)
}
// TODO: Update ping
// /**
// * Pings the provided peer
// *
// * @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to ping
// * @returns {Promise<Ping>}
// */
// ping (peer) {
// const peerInfo = await getPeerInfoRemote(peer, this)
// return new Ping(this._switch, peerInfo)
// }
/**
* Pings the given peer
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to ping
* @returns {Promise<number>}
*/
async ping (peer) {
const peerInfo = await getPeerInfo(peer, this.peerStore)
return ping(this, peerInfo)
}
/**
* Registers the `handler` for each protocol