From 60337320e3fd1a6b5a54762c0d20fb0c081fdfb3 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 9 May 2023 17:00:40 -0600 Subject: [PATCH] split switch and services decls --- libp2p.nimble | 2 +- .../connectivity/autonat/service.nim | 1 + libp2p/services/autorelayservice.nim | 2 + libp2p/services/hpservice.nim | 1 + libp2p/services/service.nim | 29 ++++++++++++ libp2p/switch.nim | 40 ++--------------- libp2p/types.nim | 45 +++++++++++++++++++ 7 files changed, 83 insertions(+), 37 deletions(-) create mode 100644 libp2p/services/service.nim create mode 100644 libp2p/types.nim diff --git a/libp2p.nimble b/libp2p.nimble index f67aad5..7ec5472 100644 --- a/libp2p.nimble +++ b/libp2p.nimble @@ -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: diff --git a/libp2p/protocols/connectivity/autonat/service.nim b/libp2p/protocols/connectivity/autonat/service.nim index 50edae1..fabaf2a 100644 --- a/libp2p/protocols/connectivity/autonat/service.nim +++ b/libp2p/protocols/connectivity/autonat/service.nim @@ -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 diff --git a/libp2p/services/autorelayservice.nim b/libp2p/services/autorelayservice.nim index defd42f..fcb54e0 100644 --- a/libp2p/services/autorelayservice.nim +++ b/libp2p/services/autorelayservice.nim @@ -16,6 +16,8 @@ import chronos, chronicles, times, tables, sequtils, options import ../switch, ../protocols/connectivity/relay/[client, utils] +import ./service + logScope: topics = "libp2p autorelay" diff --git a/libp2p/services/hpservice.nim b/libp2p/services/hpservice.nim index a2ee497..a1c2ec6 100644 --- a/libp2p/services/hpservice.nim +++ b/libp2p/services/hpservice.nim @@ -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" diff --git a/libp2p/services/service.nim b/libp2p/services/service.nim new file mode 100644 index 0000000..bf2617a --- /dev/null +++ b/libp2p/services/service.nim @@ -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 diff --git a/libp2p/switch.nim b/libp2p/switch.nim index bc2975d..e682757 100644 --- a/libp2p/switch.nim +++ b/libp2p/switch.nim @@ -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.} = diff --git a/libp2p/types.nim b/libp2p/types.nim new file mode 100644 index 0000000..96574cf --- /dev/null +++ b/libp2p/types.nim @@ -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]