split switch and services decls

This commit is contained in:
Dmitriy Ryajov 2023-05-09 17:00:40 -06:00
parent a1eb53b181
commit 60337320e3
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4
7 changed files with 83 additions and 37 deletions

View File

@ -34,7 +34,7 @@ proc runTest(filename: string, verify: bool = true, sign: bool = true,
rmFile "tests/" & filename.toExe
proc buildSample(filename: string, run = false, extraFlags = "") =
var excstr = "nim c --opt:speed --threads:on -d:debug --verbosity:0 --hints:off -p:. " & extraFlags
var excstr = "nim c --skipParentCfg --opt:speed --threads:on -d:debug --verbosity:0 --hints:off -p:. " & extraFlags
excstr.add(" examples/" & filename)
exec excstr
if run:

View File

@ -15,6 +15,7 @@ else:
import std/[options, deques, sequtils]
import chronos, metrics
import ../../../switch
import ../../../services/service
import ../../../wire
import client
from core import NetworkReachability, AutonatUnreachableError

View File

@ -16,6 +16,8 @@ import chronos, chronicles, times, tables, sequtils, options
import ../switch,
../protocols/connectivity/relay/[client, utils]
import ./service
logScope:
topics = "libp2p autorelay"

View File

@ -24,6 +24,7 @@ import ../protocols/connectivity/relay/relay
import ../protocols/connectivity/autonat/service
import ../protocols/connectivity/dcutr/[client, server]
import ./service
logScope:
topics = "libp2p hpservice"

View File

@ -0,0 +1,29 @@
# 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.
import chronos, chronicles
import ../types
method setup*(self: Service, switch: Switch): Future[bool] {.base, async, gcsafe.} =
if self.inUse:
warn "service setup has already been called"
return false
self.inUse = true
return true
method run*(self: Service, switch: Switch) {.base, async, gcsafe.} =
doAssert(false, "Not implemented!")
method stop*(self: Service, switch: Switch): Future[bool] {.base, async, gcsafe.} =
if not self.inUse:
warn "service is already stopped"
return false
self.inUse = false
return true

View File

@ -45,9 +45,11 @@ import stream/connection,
peerstore,
errors,
utility,
dialer
dialer,
services/service,
types
export connmanager, upgrade, dialer, peerstore
export connmanager, upgrade, dialer, peerstore, types
logScope:
topics = "libp2p switch"
@ -61,40 +63,6 @@ logScope:
const
ConcurrentUpgrades* = 4
type
Switch* {.public.} = ref object of Dial
peerInfo*: PeerInfo
connManager*: ConnManager
transports*: seq[Transport]
ms*: MultistreamSelect
acceptFuts: seq[Future[void]]
dialer*: Dial
peerStore*: PeerStore
nameResolver*: NameResolver
started: bool
services*: seq[Service]
Service* = ref object of RootObj
inUse: bool
method setup*(self: Service, switch: Switch): Future[bool] {.base, async, gcsafe.} =
if self.inUse:
warn "service setup has already been called"
return false
self.inUse = true
return true
method run*(self: Service, switch: Switch) {.base, async, gcsafe.} =
doAssert(false, "Not implemented!")
method stop*(self: Service, switch: Switch): Future[bool] {.base, async, gcsafe.} =
if not self.inUse:
warn "service is already stopped"
return false
self.inUse = false
return true
proc addConnEventHandler*(s: Switch,
handler: ConnEventHandler,
kind: ConnEventKind) {.public.} =

45
libp2p/types.nim Normal file
View File

@ -0,0 +1,45 @@
# 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.
import chronos
import stream/connection,
transports/transport,
upgrademngrs/[upgrade, muxedupgrade],
multistream,
multiaddress,
protocols/protocol,
protocols/secure/secure,
peerinfo,
protocols/identify,
muxers/muxer,
utils/semaphore,
connmanager,
nameresolving/nameresolver,
peerid,
peerstore,
errors,
utility,
dialer
type
Service* = ref object of RootObj
inUse*: bool
Switch* {.public.} = ref object of Dial
peerInfo*: PeerInfo
connManager*: ConnManager
transports*: seq[Transport]
ms*: MultistreamSelect
acceptFuts*: seq[Future[void]]
dialer*: Dial
peerStore*: PeerStore
nameResolver*: NameResolver
started*: bool
services*: seq[Service]