we don't need the switch in the protos after all

This commit is contained in:
Dmitriy Ryajov 2019-08-28 13:12:15 -06:00
parent 8d4de6b587
commit a9c9788356
3 changed files with 29 additions and 41 deletions

View File

@ -9,18 +9,21 @@
import chronos import chronos
import connection, transport, stream, import connection, transport, stream,
peerinfo, multiaddress, multistreamselect, peerinfo, multiaddress, multistreamselect
switchtypes
proc newProtocol*(p: typedesc[switchtypes.Protocol], type
peerInfo: PeerInfo, ProtoHandler* = proc (conn: Connection, proto: string): Future[void] {.gcsafe.}
switch: Switch): p = Protocol* = ref object of RootObj
peerInfo*: PeerInfo
codec*: string
proc newProtocol*(p: typedesc[Protocol],
peerInfo: PeerInfo): p =
new result new result
result.peerInfo = peerInfo result.peerInfo = peerInfo
result.switch = switch
result.init() 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 {.base, async, error: "not implemented!".} = discard

View File

@ -7,25 +7,35 @@
## This file may not be copied, modified, or distributed except according to ## This file may not be copied, modified, or distributed except according to
## those terms. ## those terms.
import tables
import chronos import chronos
import connection, transport, stream, import connection, transport, stream,
peerinfo, multiaddress, multistreamselect, peerinfo, multiaddress, multistreamselect,
switchtypes 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 = proc newSwitch*(peerInfo: PeerInfo, transports: seq[Transport]): Switch =
new result new result
result.peerInfo = peerInfo result.peerInfo = peerInfo
result.ms = newMultistream() result.ms = newMultistream()
result.transports = transports result.transports = transports
result.protocols = newSeq[Protocol]() result.protocols = newSeq[switchtypes.Protocol]()
result.connections = newSeq[Connection]() result.connections = newTable[string, Connection]()
proc dial*(s: Switch, peer: PeerInfo, proto: string = ""): Future[Connection] {.async.} = discard 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 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 discard
for t in s.transports: # for each transport for t in s.transports: # for each transport
@ -35,4 +45,5 @@ proc start*(s: Switch) {.async.} =
break break
proc stop*(s: Switch) {.async.} = proc stop*(s: Switch) {.async.} =
discard for c in s.connections.values:
await c.close()

View File

@ -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