nim-libp2p/tests/stubs/autonatclientstub.nim

56 lines
1.4 KiB
Nim
Raw Normal View History

2023-01-06 10:14:38 +00:00
# Nim-LibP2P
2023-01-20 14:47:40 +00:00
# Copyright (c) 2023 Status Research & Development GmbH
2023-01-06 10:14:38 +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.
{.used.}
when (NimMajor, NimMinor) < (1, 4):
{.push raises: [Defect].}
else:
{.push raises: [].}
import chronos
2023-01-06 10:14:38 +00:00
import ../../libp2p/[protocols/connectivity/autonat/client,
peerid,
multiaddress,
switch]
type
2023-01-06 10:14:38 +00:00
AutonatClientStub* = ref object of AutonatClient
answer*: Answer
dials: int
expectedDials: int
finished*: Future[void]
Answer* = enum
Reachable,
NotReachable,
Unknown
2023-01-06 10:14:38 +00:00
proc new*(T: typedesc[AutonatClientStub], expectedDials: int): T =
return T(dials: 0, expectedDials: expectedDials, finished: newFuture[void]())
method dialMe*(
2023-01-06 10:14:38 +00:00
self: AutonatClientStub,
switch: Switch,
pid: PeerId,
addrs: seq[MultiAddress] = newSeq[MultiAddress]()):
Future[MultiAddress] {.async.} =
self.dials += 1
if self.dials == self.expectedDials:
self.finished.complete()
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, "")