nimpretty

This commit is contained in:
Dmitriy Ryajov 2019-08-26 09:37:15 -06:00
parent ebab744106
commit d23398f498
7 changed files with 145 additions and 100 deletions

View File

@ -25,16 +25,25 @@ proc newConnection*(stream: ReadWrite): Connection =
method read*(s: Connection, n = -1): Future[seq[byte]] {.async.} =
result = await s.stream.read(n)
method readExactly*(s: Connection, pbytes: pointer, nbytes: int): Future[void] {.async.} =
method readExactly*(s: Connection,
pbytes: pointer,
nbytes: int): Future[void] {.async.} =
await s.stream.readExactly(pbytes, nbytes)
method readLine*(s: Connection, limit = 0, sep = "\r\n"): Future[string] {.async.} =
method readLine*(s: Connection,
limit = 0,
sep = "\r\n"): Future[string] {.async.} =
result = await s.stream.readLine(limit, sep)
method readOnce*(s: Connection, pbytes: pointer, nbytes: int): Future[int] {.async.} =
method readOnce*(s: Connection,
pbytes: pointer,
nbytes: int): Future[int] {.async.} =
result = await s.stream.readOnce(pbytes, nbytes)
method readUntil*(s: Connection, pbytes: pointer, nbytes: int, sep: seq[byte]): Future[int] {.async.} =
method readUntil*(s: Connection,
pbytes: pointer,
nbytes: int,
sep: seq[byte]): Future[int] {.async.} =
result = await s.stream.readUntil(pbytes, nbytes, sep)
method write*(s: Connection, pbytes: pointer, nbytes: int) {.async.} =
@ -84,6 +93,7 @@ method getPeerInfo* (c: Connection): Future[PeerInfo] {.base, async.} =
## TODO: implement PeerInfo refresh over identify
discard
method getObservedAddrs(c: Connection): Future[seq[MultiAddress]] {.base, async.} =
method getObservedAddrs(c: Connection): Future[seq[MultiAddress]] {.base,
async.} =
## get resolved multiaddresses for the connection
discard

View File

@ -40,7 +40,9 @@ proc newMultistream*(): MultisteamSelect =
result.ls = Ls
result.na = Na
proc select*(m: MultisteamSelect, conn: Connection, proto: string = ""): Future[bool] {.async.} =
proc select*(m: MultisteamSelect,
conn: Connection,
proto: string = ""): Future[bool] {.async.} =
## select a remote protocol
## TODO: select should support a list of protos to be selected
@ -60,7 +62,8 @@ proc select*(m: MultisteamSelect, conn: Connection, proto: string = ""): Future[
ms.removeSuffix("\n")
result = ms == proto
proc list*(m: MultisteamSelect, conn: Connection): Future[seq[string]] {.async.} =
proc list*(m: MultisteamSelect,
conn: Connection): Future[seq[string]] {.async.} =
## list remote protos requests on connection
if not (await m.select(conn)):
return

View File

@ -28,7 +28,9 @@ method readOnce*(s: ReadWrite, pbytes: pointer, nbytes: int): Future[int]
{.base, async.} =
discard
method readUntil*(s: ReadWrite, pbytes: pointer, nbytes: int, sep: seq[byte]): Future[int]
method readUntil*(s: ReadWrite,
pbytes: pointer, nbytes: int,
sep: seq[byte]): Future[int]
{.base, async.} =
discard

View File

@ -8,14 +8,17 @@
## those terms.
import chronos
import transport, wire, connection, multiaddress, connection, multicodec, chronosstream
import transport, wire, connection,
multiaddress, connection,
multicodec, chronosstream
type TcpTransport* = ref object of Transport
server*: StreamServer
method connHandler*(t: Transport,
server: StreamServer,
client: StreamTransport): Future[Connection] {.base, gcsafe, async.} =
client: StreamTransport): Future[Connection]
{.base, gcsafe, async.} =
let conn: Connection = newConnection(newChronosStream(server, client))
let handlerFut = if t.handler == nil: nil else: t.handler(conn)
let connHolder: ConnHolder = ConnHolder(connection: conn,
@ -38,7 +41,9 @@ method close*(t: TcpTransport): Future[void] {.async.} =
t.server.stop()
await t.server.closeWait()
method listen*(t: TcpTransport, ma: MultiAddress, handler: ConnHandler): Future[void] {.async.} =
method listen*(t: TcpTransport,
ma: MultiAddress,
handler: ConnHandler): Future[void] {.async.} =
await procCall Transport(t).listen(ma, handler) # call base
## listen on the transport
@ -50,7 +55,8 @@ method listen*(t: TcpTransport, ma: MultiAddress, handler: ConnHandler): Future[
server.start()
listenFuture.complete()
method dial*(t: TcpTransport, address: MultiAddress): Future[Connection] {.async.} =
method dial*(t: TcpTransport,
address: MultiAddress): Future[Connection] {.async.} =
## dial a peer
let client: StreamTransport = await connect(address)
result = await t.connHandler(t.server, client)

View File

@ -37,12 +37,15 @@ method close*(t: Transport) {.base, async.} =
for c in t.connections:
await c.connection.close()
method listen*(t: Transport, ma: MultiAddress, handler: ConnHandler) {.base, async.} =
method listen*(t: Transport,
ma: MultiAddress,
handler: ConnHandler) {.base, async.} =
## listen for incoming connections
t.ma = ma
t.handler = handler
method dial*(t: Transport, address: MultiAddress): Future[Connection] {.base, async.} =
method dial*(t: Transport,
address: MultiAddress): Future[Connection] {.base, async.} =
## dial a peer
discard

View File

@ -9,7 +9,9 @@ type
TestSelectStream = ref object of ReadWrite
step*: int
method readExactly*(s: TestSelectStream, pbytes: pointer, nbytes: int): Future[void] {.async.} =
method readExactly*(s: TestSelectStream,
pbytes: pointer,
nbytes: int): Future[void] {.async.} =
case s.step:
of 1:
var buf = newSeq[byte](1)
@ -29,7 +31,9 @@ method readExactly*(s: TestSelectStream, pbytes: pointer, nbytes: int): Future[v
var buf = "/test/proto/1.0.0\n"
copyMem(cast[pointer](cast[uint](pbytes)), addr buf[0], buf.len())
else:
copyMem(cast[pointer](cast[uint](pbytes)), cstring("\0x3na\n"), "\0x3na\n".len())
copyMem(cast[pointer](cast[uint](pbytes)),
cstring("\0x3na\n"),
"\0x3na\n".len())
proc newTestSelectStream(): TestSelectStream =
new result
@ -40,7 +44,9 @@ type
TestHandlesStream = ref object of ReadWrite
step*: int
method readExactly*(s: TestHandlesStream, pbytes: pointer, nbytes: int): Future[void] {.async.} =
method readExactly*(s: TestHandlesStream,
pbytes: pointer,
nbytes: int): Future[void] {.async.} =
case s.step:
of 1:
var buf = newSeq[byte](1)
@ -60,7 +66,9 @@ method readExactly*(s: TestHandlesStream, pbytes: pointer, nbytes: int): Future[
var buf = "/test/proto/1.0.0\n"
copyMem(cast[pointer](cast[uint](pbytes)), addr buf[0], buf.len())
else:
copyMem(cast[pointer](cast[uint](pbytes)), cstring("\0x3na\n"), "\0x3na\n".len())
copyMem(cast[pointer](cast[uint](pbytes)),
cstring("\0x3na\n"),
"\0x3na\n".len())
proc newTestHandlesStream(): TestHandlesStream =
new result
@ -74,7 +82,9 @@ type
step*: int
ls*: LsHandler
method readExactly*(s: TestLsStream, pbytes: pointer, nbytes: int): Future[void] {.async.} =
method readExactly*(s: TestLsStream,
pbytes: pointer,
nbytes: int): Future[void] {.async.} =
case s.step:
of 1:
var buf = newSeq[byte](1)
@ -94,7 +104,9 @@ method readExactly*(s: TestLsStream, pbytes: pointer, nbytes: int): Future[void]
var buf = "ls\n"
copyMem(cast[pointer](cast[uint](pbytes)), addr buf[0], buf.len())
else:
copyMem(cast[pointer](cast[uint](pbytes)), cstring("\0x3na\n"), "\0x3na\n".len())
copyMem(cast[pointer](cast[uint](pbytes)),
cstring("\0x3na\n"),
"\0x3na\n".len())
method write*(s: TestLsStream, msg: seq[byte], msglen = -1) {.async.} =
if s.step == 4:
@ -113,7 +125,9 @@ type
step*: int
na*: NaHandler
method readExactly*(s: TestNaStream, pbytes: pointer, nbytes: int): Future[void] {.async.} =
method readExactly*(s: TestNaStream,
pbytes: pointer,
nbytes: int): Future[void] {.async.} =
case s.step:
of 1:
var buf = newSeq[byte](1)
@ -133,7 +147,9 @@ method readExactly*(s: TestNaStream, pbytes: pointer, nbytes: int): Future[void]
var buf = "/test/proto/1.0.0\n"
copyMem(cast[pointer](cast[uint](pbytes)), addr buf[0], buf.len())
else:
copyMem(cast[pointer](cast[uint](pbytes)), cstring("\0x3na\n"), "\0x3na\n".len())
copyMem(cast[pointer](cast[uint](pbytes)),
cstring("\0x3na\n"),
"\0x3na\n".len())
method write*(s: TestNaStream, msg: string, msglen = -1) {.async.} =
if s.step == 4:
@ -159,7 +175,8 @@ suite "Multistream select":
let ms = newMultistream()
let conn = newConnection(newTestHandlesStream())
proc testHandler(conn: Connection, proto: string): Future[void] {.async.} =
proc testHandler(conn: Connection,
proto: string): Future[void] {.async.} =
check proto == "/test/proto/1.0.0"
ms.addHandler("/test/proto/1.0.0", testHandler)
@ -181,7 +198,8 @@ suite "Multistream select":
check strProto == "\x26/test/proto1/1.0.0\n/test/proto2/1.0.0\n"
await conn.close()
proc testHandler(conn: Connection, proto: string): Future[void] {.async.} = discard
proc testHandler(conn: Connection,
proto: string): Future[void] {.async.} = discard
ms.addHandler("/test/proto1/1.0.0", testHandler)
ms.addHandler("/test/proto2/1.0.0", testHandler)
await ms.handle(conn)
@ -201,7 +219,8 @@ suite "Multistream select":
check cast[string](msg) == "\x3na\n"
await conn.close()
proc testHandler(conn: Connection, proto: string): Future[void] {.async.} = discard
proc testHandler(conn: Connection,
proto: string): Future[void] {.async.} = discard
ms.addHandler("/unabvailable/proto/1.0.0", testHandler)
await ms.handle(conn)
@ -210,10 +229,11 @@ suite "Multistream select":
check:
waitFor(testNa()) == true
test "end to end - handle":
test "e2e - handle":
proc endToEnd(): Future[bool] {.async.} =
let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/53340")
proc testHandler(conn: Connection, proto: string): Future[void] {.async.} =
proc testHandler(conn: Connection,
proto: string): Future[void] {.async.} =
check proto == "/test/proto/1.0.0"
await conn.writeLp("Hello!")
await conn.close()
@ -241,12 +261,13 @@ suite "Multistream select":
check:
waitFor(endToEnd()) == true
test "end to end - ls":
test "e2e - ls":
proc endToEnd(): Future[bool] {.async.} =
let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/53341")
let msListen = newMultistream()
proc testHandler(conn: Connection, proto: string): Future[void] {.async.} = discard
proc testHandler(conn: Connection,
proto: string): Future[void] {.async.} = discard
msListen.addHandler("/test/proto1/1.0.0", testHandler)
msListen.addHandler("/test/proto2/1.0.0", testHandler)

View File

@ -91,7 +91,7 @@ suite "TCP transport suite":
await server.join()
check waitFor(testDialer(initTAddress("127.0.0.1:53337"))) == true
test "test listener - dialer: handle write":
test "e2e: handle write":
proc testListenerDialer(): Future[bool] {.async.} =
let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/53339")
proc connHandler(conn: Connection): Future[void] {.async, gcsafe.} =
@ -110,7 +110,7 @@ suite "TCP transport suite":
check:
waitFor(testListenerDialer()) == true
test "test listener - dialer: handle read":
test "e2e: handle read":
proc testListenerDialer(): Future[bool] {.async.} =
let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/53340")
proc connHandler(conn: Connection): Future[void] {.async, gcsafe.} =