2019-08-20 16:18:15 +00:00
|
|
|
## 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
|
2019-08-22 21:35:47 +00:00
|
|
|
import peerinfo, connection, multiaddress, multicodec, readerwriter
|
2019-08-20 16:18:15 +00:00
|
|
|
|
|
|
|
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-22 00:23:13 +00:00
|
|
|
method init*(t: Transport) {.base, error: "not implemented".} =
|
2019-08-20 16:18:15 +00:00
|
|
|
## perform protocol initialization
|
|
|
|
discard
|
|
|
|
|
2019-08-21 23:13:20 +00:00
|
|
|
proc newTransport*(t: typedesc[Transport]): t =
|
2019-08-20 16:18:15 +00:00
|
|
|
new result
|
|
|
|
result.init()
|
|
|
|
|
|
|
|
method close*(t: Transport) {.base, async.} =
|
2019-08-21 22:53:16 +00:00
|
|
|
## stop and cleanup the transport
|
|
|
|
## including all outstanding connections
|
|
|
|
for c in t.connections:
|
2019-08-22 21:35:47 +00:00
|
|
|
await c.connection.close()
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2019-08-21 23:13:20 +00:00
|
|
|
method listen*(t: Transport, ma: MultiAddress, handler: ConnHandler) {.base, async.} =
|
2019-08-21 22:53:16 +00:00
|
|
|
## listen for incoming connections
|
2019-08-21 23:13:20 +00:00
|
|
|
t.ma = ma
|
|
|
|
t.handler = handler
|
2019-08-20 16:18:15 +00:00
|
|
|
|
2019-08-22 00:23:13 +00:00
|
|
|
method dial*(t: Transport, address: MultiAddress): Future[Connection] {.base, async, error: "not implemented".} =
|
2019-08-20 16:18:15 +00:00
|
|
|
## dial a peer
|
|
|
|
discard
|
|
|
|
|
2019-08-22 00:23:13 +00:00
|
|
|
method supports(t: Transport, address: MultiAddress): bool {.base, error: "not implemented".} =
|
2019-08-20 16:18:15 +00:00
|
|
|
## 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
|
2019-08-22 00:23:13 +00:00
|
|
|
discard
|