fixing several issues found while testing

This commit is contained in:
Dmitriy Ryajov 2019-09-06 00:49:01 -06:00
parent dee46c4d37
commit b7f999d316
2 changed files with 37 additions and 61 deletions

View File

@ -7,7 +7,7 @@
## 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 sequtils, strutils import sequtils, strutils, strformat
import chronos import chronos
import connection, import connection,
varint, varint,
@ -73,17 +73,18 @@ proc select*(m: MultisteamSelect,
proc select*(m: MultisteamSelect, proc select*(m: MultisteamSelect,
conn: Connection, conn: Connection,
proto: string): Future[string] = proto: string): Future[bool] {.async.} =
result = if proto.len > 0: m.select(conn, @[proto]) else: m.select(conn, @[]) result = if proto.len > 0:
(await m.select(conn, @[proto])) == proto
else:
(await m.select(conn, @[])) == Codec
proc select*(m: MultisteamSelect, proc select*(m: MultisteamSelect, conn: Connection): Future[bool] = m.select(conn, "")
conn: Connection): Future[string] =
result = m.select(conn, @[])
proc list*(m: MultisteamSelect, proc list*(m: MultisteamSelect,
conn: Connection): Future[seq[string]] {.async.} = conn: Connection): Future[seq[string]] {.async.} =
## list remote protos requests on connection ## list remote protos requests on connection
if (await m.select(conn)).len == 0: if not await m.select(conn):
return return
await conn.write(m.ls) # send ls await conn.write(m.ls) # send ls
@ -97,34 +98,38 @@ proc list*(m: MultisteamSelect,
result = list result = list
proc handle*(m: MultisteamSelect, conn: Connection) {.async, gcsafe.} = proc handle*(m: MultisteamSelect, conn: Connection) {.async, gcsafe.} =
## handle requests on connection
if (await m.select(conn)).len == 0:
return
while not conn.closed: while not conn.closed:
var ms = cast[string](await conn.readLp()) block main:
ms.removeSuffix("\n") var ms = cast[string](await conn.readLp())
if ms.len() <= 0: echo ms
await conn.write(m.na) ms.removeSuffix("\n")
if ms.len() <= 0:
if m.handlers.len() == 0:
await conn.write(m.na)
continue
case ms:
of "ls":
var protos = ""
for h in m.handlers:
protos &= (h.proto & "\n")
await conn.writeLp(cast[seq[byte]](toSeq(protos.items)))
else:
for h in m.handlers:
if (not isNil(h.match) and h.match(ms)) or ms == h.proto:
await conn.writeLp((h.proto & "\n"))
await h.protocol.handler(conn, ms)
return
await conn.write(m.na) await conn.write(m.na)
if m.handlers.len() == 0:
await conn.write(m.na)
continue
case ms:
of "ls":
var protos = ""
for h in m.handlers:
protos &= (h.proto & "\n")
await conn.writeLp(protos)
of Codec:
await conn.write(m.codec)
else:
for h in m.handlers:
if (not isNil(h.match) and h.match(ms)) or ms == h.proto:
echo h.proto
await conn.writeLp((h.proto & "\n"))
try:
await h.protocol.handler(conn, ms)
break main
except Exception as exc:
echo exc.msg
await conn.write(m.na)
proc addHandler*[T: LPProtocol](m: MultisteamSelect, proc addHandler*[T: LPProtocol](m: MultisteamSelect,
codec: string, codec: string,
protocol: T, protocol: T,

View File

@ -1,29 +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 protocol
import ../connection
const PlainTextCodec* = "/plaintext/1.0.0"
type
PlainText* = ref object of LPProtocol
method init(p: PlainText) {.gcsafe.} =
proc handle(conn: Connection, proto: string) {.async, gcsafe.} =
let msg = await conn.readLp()
await conn.writeLp(msg)
p.codec = PlainTextCodec
p.handler = handle
proc newPlainText*(): PlainText =
new result
result.init()