Skip to content

Commit

Permalink
feat: emit connect and disconnect events
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobheun committed Oct 18, 2019
1 parent b653951 commit 4b7e5bc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
19 changes: 13 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const peerRouting = require('./peer-routing')
const contentRouting = require('./content-routing')
const dht = require('./dht')
const pubsub = require('./pubsub')
const { getPeerInfoRemote } = require('./get-peer-info')
const validateConfig = require('./config').validate
const { getPeerInfo, getPeerInfoRemote } = require('./get-peer-info')
const { validate: validateConfig } = require('./config')
const { codes } = require('./errors')

const Dialer = require('./dialer')
Expand Down Expand Up @@ -66,15 +66,22 @@ class Libp2p extends EventEmitter {

// Setup the Upgrader
this.upgrader = new Upgrader({
localPeer: this.peerInfo.id
localPeer: this.peerInfo.id,
// TODO: Route incoming connections to a multiplex protocol router
onConnection: (connection) => {
const peerInfo = getPeerInfo(connection.remotePeer)
this.emit('peer:connect', peerInfo)
},
onConnectionEnd: (connection) => {
const peerInfo = getPeerInfo(connection.remotePeer)
this.emit('peer:disconnect', peerInfo)
}
})

// Setup the transport manager
this.transportManager = new TransportManager({
libp2p: this,
upgrader: this.upgrader,
// TODO: Route incoming connections to a multiplex protocol router
onConnection: (connection) => { }
upgrader: this.upgrader
})
this._modules.transport.forEach((Transport) => {
this.transportManager.add(Transport.prototype[Symbol.toStringTag], Transport)
Expand Down
54 changes: 48 additions & 6 deletions test/upgrading/upgrader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ describe('Upgrader', () => {
expect(remoteUpgrader.onConnection.callCount).to.equal(1)
expect(remoteUpgrader.onConnection.getCall(0).args).to.eql([connections[1]])


// Verify onConnectionEnd is called with the connection
sinon.spy(localUpgrader, 'onConnectionEnd')
sinon.spy(remoteUpgrader, 'onConnectionEnd')
Expand Down Expand Up @@ -271,12 +270,15 @@ describe('Upgrader', () => {
})

describe('libp2p.upgrader', () => {
let peerInfo
let peers
let libp2p

before(async () => {
const peerId = await PeerId.createFromJSON(Peers[0])
peerInfo = new PeerInfo(peerId)
const ids = await Promise.all([
PeerId.createFromJSON(Peers[0]),
PeerId.createFromJSON(Peers[1])
])
peers = ids.map(peerId => new PeerInfo(peerId))
})

afterEach(async () => {
Expand All @@ -287,7 +289,7 @@ describe('libp2p.upgrader', () => {

it('should create an Upgrader', () => {
libp2p = new Libp2p({
peerInfo,
peerInfo: peers[0],
modules: {
transport: [Transport],
streamMuxer: [Muxer],
Expand All @@ -304,7 +306,7 @@ describe('libp2p.upgrader', () => {

it('should be able to register and unregister a handler', () => {
libp2p = new Libp2p({
peerInfo,
peerInfo: peers[0],
modules: {
transport: [Transport],
streamMuxer: [Muxer],
Expand All @@ -325,4 +327,44 @@ describe('libp2p.upgrader', () => {
expect(libp2p.upgrader.protocols.get('/echo/1.0.0')).to.equal(undefined)
expect(libp2p.upgrader.protocols.get('/echo/1.0.1')).to.equal(echoHandler)
})

it('should emit connect and disconnect events', async () => {
const remotePeer = peers[1]
libp2p = new Libp2p({
peerInfo: peers[0],
modules: {
transport: [Transport],
streamMuxer: [Muxer],
connEncryption: [mockCrypto]
}
})

const remoteUpgrader = new Upgrader({
localPeer: remotePeer.id,
muxers: new Map([[Muxer.multicodec, Muxer]]),
cryptos: new Map([[mockCrypto.tag, mockCrypto]])
})

const { inbound, outbound } = mockMultiaddrConn({ addrs, remotePeer: remotePeer.id })

// Spy on emit for easy verification
sinon.spy(libp2p, 'emit')

// Upgrade and check the connect event
const connections = await Promise.all([
libp2p.upgrader.upgradeOutbound(outbound),
remoteUpgrader.upgradeInbound(inbound)
])
expect(libp2p.emit.callCount).to.equal(1)
let [event, peerInfo] = libp2p.emit.getCall(0).args
expect(event).to.equal('peer:connect')
expect(peerInfo.id.isEqual(remotePeer.id)).to.equal(true)

// Close and check the disconnect event
await Promise.all(connections.map(conn => conn.close()))
expect(libp2p.emit.callCount).to.equal(2)
;([event, peerInfo] = libp2p.emit.getCall(1).args)
expect(event).to.equal('peer:disconnect')
expect(peerInfo.id.isEqual(remotePeer.id)).to.equal(true)
})
})

0 comments on commit 4b7e5bc

Please sign in to comment.