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.} = method read*(s: Connection, n = -1): Future[seq[byte]] {.async.} =
result = await s.stream.read(n) 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) 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) 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) 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) result = await s.stream.readUntil(pbytes, nbytes, sep)
method write*(s: Connection, pbytes: pointer, nbytes: int) {.async.} = 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 ## TODO: implement PeerInfo refresh over identify
discard 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 ## get resolved multiaddresses for the connection
discard discard

View File

@ -40,7 +40,9 @@ proc newMultistream*(): MultisteamSelect =
result.ls = Ls result.ls = Ls
result.na = Na 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 ## select a remote protocol
## TODO: select should support a list of protos to be selected ## 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") ms.removeSuffix("\n")
result = ms == proto 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 ## list remote protos requests on connection
if not (await m.select(conn)): if not (await m.select(conn)):
return return

View File

@ -28,7 +28,9 @@ method readOnce*(s: ReadWrite, pbytes: pointer, nbytes: int): Future[int]
{.base, async.} = {.base, async.} =
discard 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.} = {.base, async.} =
discard discard

View File

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

View File

@ -37,12 +37,15 @@ method close*(t: Transport) {.base, async.} =
for c in t.connections: for c in t.connections:
await c.connection.close() 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 ## listen for incoming connections
t.ma = ma t.ma = ma
t.handler = handler 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 ## dial a peer
discard discard

View File

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

View File

@ -7,7 +7,7 @@ suite "TCP transport suite":
test "test listener: handle write": test "test listener: handle write":
proc testListener(): Future[bool] {.async.} = proc testListener(): Future[bool] {.async.} =
let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/53335") let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/53335")
proc connHandler(conn: Connection): Future[void] {.async ,gcsafe.} = proc connHandler(conn: Connection): Future[void] {.async, gcsafe.} =
result = conn.write(cstring("Hello!"), 6) result = conn.write(cstring("Hello!"), 6)
let transport: TcpTransport = newTransport(TcpTransport) let transport: TcpTransport = newTransport(TcpTransport)
@ -25,7 +25,7 @@ suite "TCP transport suite":
test "test listener: handle read": test "test listener: handle read":
proc testListener(): Future[bool] {.async.} = proc testListener(): Future[bool] {.async.} =
let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/53336") let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/53336")
proc connHandler(conn: Connection): Future[void] {.async ,gcsafe.} = proc connHandler(conn: Connection): Future[void] {.async, gcsafe.} =
let msg = await conn.read(6) let msg = await conn.read(6)
check cast[string](msg) == "Hello!" check cast[string](msg) == "Hello!"
@ -91,10 +91,10 @@ suite "TCP transport suite":
await server.join() await server.join()
check waitFor(testDialer(initTAddress("127.0.0.1:53337"))) == true 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.} = proc testListenerDialer(): Future[bool] {.async.} =
let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/53339") let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/53339")
proc connHandler(conn: Connection): Future[void] {.async ,gcsafe.} = proc connHandler(conn: Connection): Future[void] {.async, gcsafe.} =
result = conn.write(cstring("Hello!"), 6) result = conn.write(cstring("Hello!"), 6)
let transport1: TcpTransport = newTransport(TcpTransport) let transport1: TcpTransport = newTransport(TcpTransport)
@ -110,10 +110,10 @@ suite "TCP transport suite":
check: check:
waitFor(testListenerDialer()) == true waitFor(testListenerDialer()) == true
test "test listener - dialer: handle read": test "e2e: handle read":
proc testListenerDialer(): Future[bool] {.async.} = proc testListenerDialer(): Future[bool] {.async.} =
let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/53340") let ma: MultiAddress = Multiaddress.init("/ip4/127.0.0.1/tcp/53340")
proc connHandler(conn: Connection): Future[void] {.async ,gcsafe.} = proc connHandler(conn: Connection): Future[void] {.async, gcsafe.} =
let msg = await conn.read(6) let msg = await conn.read(6)
check cast[string](msg) == "Hello!" check cast[string](msg) == "Hello!"