Skip to content

Commit

Permalink
chore: support multiple conns
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Nov 5, 2019
1 parent 0ec963e commit d267975
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Libp2p extends EventEmitter {
onConnectionEnd: (connection) => {
const peerInfo = getPeerInfo(connection.remotePeer)

this.registrar.onDisconnect(peerInfo)
this.registrar.onDisconnect(peerInfo, connection)
this.emit('peer:disconnect', peerInfo)
}
})
Expand Down
35 changes: 26 additions & 9 deletions src/registrar.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Registrar {
/**
* Map of connections per peer
* TODO: this should be handled by connectionManager
* @type {Map<string, conn>}
* @type {Map<string, Array<conn>>}
*/
this.connections = new Map()

Expand Down Expand Up @@ -58,24 +58,39 @@ class Registrar {
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
assert(Connection.isConnection(conn), 'conn must be an instance of interface-connection')

this.connections.set(peerInfo.id.toB58String(), conn)
const id = peerInfo.id.toB58String()
const storedConn = this.connections.get(id)

if (storedConn) {
storedConn.push(conn)
} else {
this.connections.set(id, [conn])
}
}

/**
* Remove a disconnected peer from the record
* TODO: this should live in the ConnectionManager
* @param {PeerInfo} peerInfo
* @param {Connection} connection
* @param {Error} [error]
* @returns {void}
*/
onDisconnect (peerInfo, error) {
onDisconnect (peerInfo, connection, error) {
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')

for (const [, topology] of this.topologies) {
topology.disconnect(peerInfo, error)
}
const id = peerInfo.id.toB58String()
let storedConn = this.connections.get(id)

this.connections.delete(peerInfo.id.toB58String())
if (storedConn && storedConn.length > 1) {
storedConn = storedConn.filter((conn) => conn.id === connection.id)
} else if (storedConn) {
for (const [, topology] of this.topologies) {
topology.disconnect(peerInfo, error)
}

this.connections.delete(peerInfo.id.toB58String())
}
}

/**
Expand All @@ -86,7 +101,8 @@ class Registrar {
getConnection (peerInfo) {
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')

return this.connections.get(peerInfo.id.toB58String())
// TODO: what should we return
return this.connections.get(peerInfo.id.toB58String())[0]
}

/**
Expand Down Expand Up @@ -146,7 +162,8 @@ class Registrar {
const targetPeer = knownPeers.find((peerInfo) => peerInfo.id.toB58String() === id)

if (targetPeer) {
topology.tryToConnect(targetPeer, conn)
// TODO: what should we return
topology.tryToConnect(targetPeer, conn[0])
}
}
}
Expand Down

0 comments on commit d267975

Please sign in to comment.