nim-libp2p-experimental/libp2p/transports/transport.nim

72 lines
2.1 KiB
Nim
Raw Normal View History

## Nim-LibP2P
2019-09-24 17:48:23 +00:00
## Copyright (c) 2019 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.
2019-08-31 17:58:08 +00:00
import sequtils
2019-09-28 19:56:13 +00:00
import chronos, chronicles
2019-09-23 22:35:23 +00:00
import ../peerinfo,
../connection,
../multiaddress,
../multicodec
type
ConnHandler* = proc (conn: Connection): Future[void] {.gcsafe.}
ConnHolder* = object
connection*: Connection
connFuture*: Future[void]
Transport* = ref object of RootObj
ma*: Multiaddress
connections*: seq[ConnHolder]
handler*: ConnHandler
multicodec*: MultiCodec
2019-08-30 15:28:07 +00:00
method init*(t: Transport) {.base, gcsafe.} =
## perform protocol initialization
discard
2019-08-30 15:28:07 +00:00
proc newTransport*(t: typedesc[Transport]): t {.gcsafe.} =
new result
result.init()
2019-08-30 15:28:07 +00:00
method close*(t: Transport) {.base, async, gcsafe.} =
2019-08-21 22:53:16 +00:00
## stop and cleanup the transport
## including all outstanding connections
2019-08-31 17:58:08 +00:00
await allFutures(t.connections.mapIt(it.connection.close()))
2019-08-26 15:37:15 +00:00
method listen*(t: Transport,
ma: MultiAddress,
2019-09-23 22:35:23 +00:00
handler: ConnHandler):
2019-09-12 00:15:04 +00:00
Future[Future[void]] {.base, async, gcsafe.} =
2019-08-21 22:53:16 +00:00
## listen for incoming connections
t.ma = ma
t.handler = handler
2019-09-28 19:56:13 +00:00
debug "starting node", address = $ma
2019-08-26 15:37:15 +00:00
method dial*(t: Transport,
2019-09-23 22:35:23 +00:00
address: MultiAddress):
2019-08-30 15:28:07 +00:00
Future[Connection] {.base, async, gcsafe.} =
## dial a peer
discard
2019-09-23 22:35:23 +00:00
method upgrade*(t: Transport) {.base, async, gcsafe.} =
2019-08-30 22:16:37 +00:00
## base upgrade method that the transport uses to perform
## transport specific upgrades
discard
2019-08-30 15:28:07 +00:00
method handles*(t: Transport, address: MultiAddress): bool {.base, gcsafe.} =
## check if transport supportes the multiaddress
# TODO: this should implement generic logic that would use the multicodec
# declared in the multicodec field and set by each individual transport
discard
2019-09-12 17:07:34 +00:00
method localAddress*(t: Transport): MultiAddress {.base, gcsafe.} =
## get the local address of the transport in case started with 0.0.0.0:0
discard