2023-04-18 10:50:21 +00:00
|
|
|
# Nim-LibP2P
|
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
|
|
|
# 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.}
|
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2023-04-18 10:50:21 +00:00
|
|
|
|
|
|
|
import chronos
|
|
|
|
import ../../libp2p/[peerid, multiaddress, switch]
|
|
|
|
|
|
|
|
type
|
|
|
|
SwitchStub* = ref object of Switch
|
|
|
|
switch*: Switch
|
2023-06-13 15:58:41 +00:00
|
|
|
connectStub*: connectStubType
|
|
|
|
|
|
|
|
connectStubType* = proc(
|
|
|
|
self: SwitchStub,
|
|
|
|
peerId: PeerId,
|
|
|
|
addrs: seq[MultiAddress],
|
|
|
|
forceDial = false,
|
|
|
|
reuseConnection = true,
|
2023-12-05 07:05:32 +00:00
|
|
|
dir = Direction.Out,
|
|
|
|
): Future[void] {.async.}
|
2023-04-18 10:50:21 +00:00
|
|
|
|
|
|
|
method connect*(
|
|
|
|
self: SwitchStub,
|
|
|
|
peerId: PeerId,
|
|
|
|
addrs: seq[MultiAddress],
|
|
|
|
forceDial = false,
|
|
|
|
reuseConnection = true,
|
2023-11-29 16:38:47 +00:00
|
|
|
dir = Direction.Out,
|
|
|
|
) {.async.} =
|
2023-04-18 10:50:21 +00:00
|
|
|
if (self.connectStub != nil):
|
2023-11-29 16:38:47 +00:00
|
|
|
await self.connectStub(self, peerId, addrs, forceDial, reuseConnection, dir)
|
2023-04-18 10:50:21 +00:00
|
|
|
else:
|
2023-11-29 16:38:47 +00:00
|
|
|
await self.switch.connect(peerId, addrs, forceDial, reuseConnection, dir)
|
2023-04-18 10:50:21 +00:00
|
|
|
|
2023-06-13 15:58:41 +00:00
|
|
|
proc new*(
|
|
|
|
T: typedesc[SwitchStub], switch: Switch, connectStub: connectStubType = nil
|
|
|
|
): T =
|
2023-04-18 10:50:21 +00:00
|
|
|
return SwitchStub(
|
|
|
|
switch: switch,
|
|
|
|
peerInfo: switch.peerInfo,
|
|
|
|
ms: switch.ms,
|
|
|
|
transports: switch.transports,
|
|
|
|
connManager: switch.connManager,
|
|
|
|
peerStore: switch.peerStore,
|
|
|
|
dialer: switch.dialer,
|
|
|
|
nameResolver: switch.nameResolver,
|
|
|
|
services: switch.services,
|
|
|
|
connectStub: connectStub,
|
|
|
|
)
|