mirror of
https://github.com/status-im/nim-libp2p.git
synced 2025-01-11 05:26:02 +00:00
feat(peerEvents): add a peerEvent Identified (#843)
Co-authored-by: diegomrsantos <diego@status.im>
This commit is contained in:
parent
d1d53ff369
commit
100f3188ed
@ -48,10 +48,11 @@ type
|
|||||||
PeerEventKind* {.pure.} = enum
|
PeerEventKind* {.pure.} = enum
|
||||||
Left
|
Left
|
||||||
Joined
|
Joined
|
||||||
|
Identified
|
||||||
|
|
||||||
PeerEvent* = object
|
PeerEvent* = object
|
||||||
case kind*: PeerEventKind
|
case kind*: PeerEventKind
|
||||||
of PeerEventKind.Joined:
|
of PeerEventKind.Joined, PeerEventKind.Identified:
|
||||||
initiator*: bool
|
initiator*: bool
|
||||||
else:
|
else:
|
||||||
discard
|
discard
|
||||||
|
@ -195,6 +195,10 @@ proc internalConnect(
|
|||||||
try:
|
try:
|
||||||
self.connManager.storeMuxer(muxed)
|
self.connManager.storeMuxer(muxed)
|
||||||
await self.peerStore.identify(muxed)
|
await self.peerStore.identify(muxed)
|
||||||
|
await self.connManager.triggerPeerEvents(
|
||||||
|
muxed.connection.peerId,
|
||||||
|
PeerEvent(kind: PeerEventKind.Identified, initiator: true),
|
||||||
|
)
|
||||||
except CatchableError as exc:
|
except CatchableError as exc:
|
||||||
trace "Failed to finish outgoung upgrade", err = exc.msg
|
trace "Failed to finish outgoung upgrade", err = exc.msg
|
||||||
await muxed.close()
|
await muxed.close()
|
||||||
|
@ -66,8 +66,8 @@ method setup*(self: AutoRelayService, switch: Switch): Future[bool] {.async.} =
|
|||||||
|
|
||||||
let hasBeenSetUp = await procCall Service(self).setup(switch)
|
let hasBeenSetUp = await procCall Service(self).setup(switch)
|
||||||
if hasBeenSetUp:
|
if hasBeenSetUp:
|
||||||
proc handlePeerJoined(peerId: PeerId, event: PeerEvent) {.async.} =
|
proc handlePeerIdentified(peerId: PeerId, event: PeerEvent) {.async.} =
|
||||||
trace "Peer Joined", peerId
|
trace "Peer Identified", peerId
|
||||||
if self.relayPeers.len < self.maxNumRelays:
|
if self.relayPeers.len < self.maxNumRelays:
|
||||||
self.peerAvailable.fire()
|
self.peerAvailable.fire()
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ method setup*(self: AutoRelayService, switch: Switch): Future[bool] {.async.} =
|
|||||||
self.relayPeers.withValue(peerId, future):
|
self.relayPeers.withValue(peerId, future):
|
||||||
future[].cancel()
|
future[].cancel()
|
||||||
|
|
||||||
switch.addPeerEventHandler(handlePeerJoined, Joined)
|
switch.addPeerEventHandler(handlePeerIdentified, Identified)
|
||||||
switch.addPeerEventHandler(handlePeerLeft, Left)
|
switch.addPeerEventHandler(handlePeerLeft, Left)
|
||||||
switch.peerInfo.addressMappers.add(self.addressMapper)
|
switch.peerInfo.addressMappers.add(self.addressMapper)
|
||||||
await self.run(switch)
|
await self.run(switch)
|
||||||
@ -119,7 +119,6 @@ proc innerRun(self: AutoRelayService, switch: Switch) {.async.} =
|
|||||||
await one(toSeq(self.relayPeers.values())) or self.peerAvailable.wait()
|
await one(toSeq(self.relayPeers.values())) or self.peerAvailable.wait()
|
||||||
else:
|
else:
|
||||||
await self.peerAvailable.wait()
|
await self.peerAvailable.wait()
|
||||||
await sleepAsync(200.millis)
|
|
||||||
|
|
||||||
method run*(self: AutoRelayService, switch: Switch) {.async.} =
|
method run*(self: AutoRelayService, switch: Switch) {.async.} =
|
||||||
if self.running:
|
if self.running:
|
||||||
|
@ -204,6 +204,9 @@ proc upgrader(switch: Switch, trans: Transport, conn: Connection) {.async.} =
|
|||||||
let muxed = await trans.upgrade(conn, Opt.none(PeerId))
|
let muxed = await trans.upgrade(conn, Opt.none(PeerId))
|
||||||
switch.connManager.storeMuxer(muxed)
|
switch.connManager.storeMuxer(muxed)
|
||||||
await switch.peerStore.identify(muxed)
|
await switch.peerStore.identify(muxed)
|
||||||
|
await switch.connManager.triggerPeerEvents(
|
||||||
|
muxed.connection.peerId, PeerEvent(kind: PeerEventKind.Identified, initiator: false)
|
||||||
|
)
|
||||||
trace "Connection upgrade succeeded"
|
trace "Connection upgrade succeeded"
|
||||||
|
|
||||||
proc upgradeMonitor(
|
proc upgradeMonitor(
|
||||||
@ -295,9 +298,6 @@ proc stop*(s: Switch) {.async, public.} =
|
|||||||
except CatchableError as exc:
|
except CatchableError as exc:
|
||||||
warn "error cleaning up transports", msg = exc.msg
|
warn "error cleaning up transports", msg = exc.msg
|
||||||
|
|
||||||
for service in s.services:
|
|
||||||
discard await service.stop(s)
|
|
||||||
|
|
||||||
await s.ms.stop()
|
await s.ms.stop()
|
||||||
|
|
||||||
trace "Switch stopped"
|
trace "Switch stopped"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user