2022-10-03 22:00:00 +00:00
|
|
|
# Nim-LibP2P
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2022-10-03 22:00:00 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2022-10-03 22:00:00 +00:00
|
|
|
|
|
|
|
import chronos
|
|
|
|
import ./discoverymngr, ../protocols/rendezvous, ../peerid
|
|
|
|
|
|
|
|
type
|
|
|
|
RendezVousInterface* = ref object of DiscoveryInterface
|
|
|
|
rdv*: RendezVous
|
|
|
|
timeToRequest: Duration
|
|
|
|
timeToAdvertise: Duration
|
2023-09-27 13:52:22 +00:00
|
|
|
ttl: Duration
|
2022-10-03 22:00:00 +00:00
|
|
|
|
|
|
|
RdvNamespace* = distinct string
|
|
|
|
|
|
|
|
proc `==`*(a, b: RdvNamespace): bool {.borrow.}
|
|
|
|
|
|
|
|
method request*(self: RendezVousInterface, pa: PeerAttributes) {.async.} =
|
|
|
|
var namespace = ""
|
|
|
|
for attr in pa:
|
|
|
|
if attr.ofType(RdvNamespace):
|
|
|
|
namespace = string attr.to(RdvNamespace)
|
|
|
|
elif attr.ofType(DiscoveryService):
|
|
|
|
namespace = string attr.to(DiscoveryService)
|
|
|
|
elif attr.ofType(PeerId):
|
|
|
|
namespace = $attr.to(PeerId)
|
|
|
|
else:
|
|
|
|
# unhandled type
|
|
|
|
return
|
|
|
|
while true:
|
|
|
|
for pr in await self.rdv.request(namespace):
|
|
|
|
var peer: PeerAttributes
|
|
|
|
peer.add(pr.peerId)
|
|
|
|
for address in pr.addresses:
|
|
|
|
peer.add(address.address)
|
|
|
|
|
|
|
|
peer.add(DiscoveryService(namespace))
|
|
|
|
peer.add(RdvNamespace(namespace))
|
|
|
|
self.onPeerFound(peer)
|
|
|
|
|
|
|
|
await sleepAsync(self.timeToRequest)
|
|
|
|
|
|
|
|
method advertise*(self: RendezVousInterface) {.async.} =
|
|
|
|
while true:
|
|
|
|
var toAdvertise: seq[string]
|
|
|
|
for attr in self.toAdvertise:
|
|
|
|
if attr.ofType(RdvNamespace):
|
|
|
|
toAdvertise.add string attr.to(RdvNamespace)
|
|
|
|
elif attr.ofType(DiscoveryService):
|
|
|
|
toAdvertise.add string attr.to(DiscoveryService)
|
|
|
|
elif attr.ofType(PeerId):
|
|
|
|
toAdvertise.add $attr.to(PeerId)
|
|
|
|
|
|
|
|
self.advertisementUpdated.clear()
|
|
|
|
for toAdv in toAdvertise:
|
2023-09-27 13:52:22 +00:00
|
|
|
try:
|
|
|
|
await self.rdv.advertise(toAdv, self.ttl)
|
|
|
|
except CatchableError as error:
|
2024-08-14 15:19:54 +00:00
|
|
|
debug "RendezVous advertise error: ", description = error.msg
|
2022-10-03 22:00:00 +00:00
|
|
|
|
|
|
|
await sleepAsync(self.timeToAdvertise) or self.advertisementUpdated.wait()
|
|
|
|
|
|
|
|
proc new*(
|
|
|
|
T: typedesc[RendezVousInterface],
|
|
|
|
rdv: RendezVous,
|
|
|
|
ttr: Duration = 1.minutes,
|
2023-09-27 13:52:22 +00:00
|
|
|
tta: Duration = 1.minutes,
|
|
|
|
ttl: Duration = MinimumDuration,
|
|
|
|
): RendezVousInterface =
|
|
|
|
T(rdv: rdv, timeToRequest: ttr, timeToAdvertise: tta, ttl: ttl)
|