From a9c9788356ca84e7c85ba47024617c32256b7a0d Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Wed, 28 Aug 2019 13:12:15 -0600 Subject: [PATCH] we don't need the switch in the protos after all --- libp2p/protocol.nim | 19 +++++++++++-------- libp2p/switch.nim | 25 ++++++++++++++++++------- libp2p/switchtypes.nim | 26 -------------------------- 3 files changed, 29 insertions(+), 41 deletions(-) delete mode 100644 libp2p/switchtypes.nim diff --git a/libp2p/protocol.nim b/libp2p/protocol.nim index e0bd95b0f..896d524fb 100644 --- a/libp2p/protocol.nim +++ b/libp2p/protocol.nim @@ -9,18 +9,21 @@ import chronos import connection, transport, stream, - peerinfo, multiaddress, multistreamselect, - switchtypes + peerinfo, multiaddress, multistreamselect -proc newProtocol*(p: typedesc[switchtypes.Protocol], - peerInfo: PeerInfo, - switch: Switch): p = +type + ProtoHandler* = proc (conn: Connection, proto: string): Future[void] {.gcsafe.} + Protocol* = ref object of RootObj + peerInfo*: PeerInfo + codec*: string + +proc newProtocol*(p: typedesc[Protocol], + peerInfo: PeerInfo): p = new result result.peerInfo = peerInfo - result.switch = switch result.init() -method init*(p: switchtypes.Protocol) {.base.} = discard +method init*(p: Protocol) {.base.} = discard -method handle*(p: switchtypes.Protocol, peerInfo: PeerInfo, handler: ProtoHandler) +method handle*(p: Protocol, peerInfo: PeerInfo, handler: ProtoHandler) {.base, async, error: "not implemented!".} = discard diff --git a/libp2p/switch.nim b/libp2p/switch.nim index 09887478b..2a2f92692 100644 --- a/libp2p/switch.nim +++ b/libp2p/switch.nim @@ -7,25 +7,35 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. +import tables import chronos import connection, transport, stream, - peerinfo, multiaddress, multistreamselect, - switchtypes + peerinfo, multiaddress, multistreamselect, + protocol + +type + Switch* = ref object of RootObj + peerInfo*: PeerInfo + connections*: TableRef[string, Connection] + transports*: seq[Transport] + protocols*: seq[Protocol] + ms*: MultisteamSelect proc newSwitch*(peerInfo: PeerInfo, transports: seq[Transport]): Switch = new result result.peerInfo = peerInfo result.ms = newMultistream() result.transports = transports - result.protocols = newSeq[Protocol]() - result.connections = newSeq[Connection]() + result.protocols = newSeq[switchtypes.Protocol]() + result.connections = newTable[string, Connection]() proc dial*(s: Switch, peer: PeerInfo, proto: string = ""): Future[Connection] {.async.} = discard -proc mount*(s: Switch, protocol: switchtypes.Protocol) = discard +proc mount*(s: Switch, protocol: protocol.Protocol) = discard proc start*(s: Switch) {.async.} = - proc handle(conn: Connection): Future[void] = + # TODO: how bad is it that this is a closure? + proc handle(conn: Connection): Future[void] {.closure, gcsafe.} = discard for t in s.transports: # for each transport @@ -35,4 +45,5 @@ proc start*(s: Switch) {.async.} = break proc stop*(s: Switch) {.async.} = - discard + for c in s.connections.values: + await c.close() diff --git a/libp2p/switchtypes.nim b/libp2p/switchtypes.nim deleted file mode 100644 index 8456a4b3d..000000000 --- a/libp2p/switchtypes.nim +++ /dev/null @@ -1,26 +0,0 @@ -## Nim-LibP2P -## Copyright (c) 2018 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 connection, transport, stream, - peerinfo, multiaddress, multistreamselect - -type - ProtoHandler* = proc (conn: Connection, proto: string): Future[void] - Protocol* = ref object of RootObj - peerInfo*: PeerInfo - switch*: Switch - codec*: string - - Switch* = ref object of RootObj - peerInfo*: PeerInfo - connections*: seq[Connection] - transports*: seq[Transport] - protocols*: seq[Protocol] - ms*: MultisteamSelect