2022-12-16 11:32:00 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import chronos
|
|
|
|
import ../../libp2p/protocols/connectivity/autonat
|
|
|
|
import ../../libp2p/peerid
|
|
|
|
import ../../libp2p/multiaddress
|
|
|
|
|
|
|
|
type
|
|
|
|
AutonatStub* = ref object of Autonat
|
2022-12-23 15:49:25 +00:00
|
|
|
answer*: Answer
|
2022-12-16 11:32:00 +00:00
|
|
|
dials: int
|
|
|
|
expectedDials: int
|
|
|
|
finished*: Future[void]
|
|
|
|
|
2022-12-23 15:49:25 +00:00
|
|
|
Answer* = enum
|
|
|
|
Reachable,
|
|
|
|
NotReachable,
|
|
|
|
Unknown
|
|
|
|
|
2022-12-16 11:32:00 +00:00
|
|
|
proc new*(T: typedesc[AutonatStub], expectedDials: int): T =
|
|
|
|
return T(dials: 0, expectedDials: expectedDials, finished: newFuture[void]())
|
|
|
|
|
|
|
|
method dialMe*(
|
|
|
|
self: AutonatStub,
|
|
|
|
pid: PeerId,
|
|
|
|
addrs: seq[MultiAddress] = newSeq[MultiAddress]()):
|
|
|
|
Future[MultiAddress] {.async.} =
|
|
|
|
|
|
|
|
self.dials += 1
|
|
|
|
|
|
|
|
if self.dials == self.expectedDials:
|
|
|
|
self.finished.complete()
|
2022-12-23 15:49:25 +00:00
|
|
|
case self.answer:
|
|
|
|
of Reachable:
|
|
|
|
return MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()
|
|
|
|
of NotReachable:
|
|
|
|
raise newException(AutonatUnreachableError, "")
|
|
|
|
of Unknown:
|
|
|
|
raise newException(AutonatError, "")
|