diff --git a/libp2p/multistream.nim b/libp2p/multistream.nim index 9eaab4ff6..b14b23171 100644 --- a/libp2p/multistream.nim +++ b/libp2p/multistream.nim @@ -7,7 +7,7 @@ ## This file may not be copied, modified, or distributed except according to ## those terms. -import sequtils, strutils +import sequtils, strutils, strformat import chronos import connection, varint, @@ -73,17 +73,18 @@ proc select*(m: MultisteamSelect, proc select*(m: MultisteamSelect, conn: Connection, - proto: string): Future[string] = - result = if proto.len > 0: m.select(conn, @[proto]) else: m.select(conn, @[]) + proto: string): Future[bool] {.async.} = + result = if proto.len > 0: + (await m.select(conn, @[proto])) == proto + else: + (await m.select(conn, @[])) == Codec -proc select*(m: MultisteamSelect, - conn: Connection): Future[string] = - result = m.select(conn, @[]) +proc select*(m: MultisteamSelect, conn: Connection): Future[bool] = m.select(conn, "") proc list*(m: MultisteamSelect, conn: Connection): Future[seq[string]] {.async.} = ## list remote protos requests on connection - if (await m.select(conn)).len == 0: + if not await m.select(conn): return await conn.write(m.ls) # send ls @@ -97,34 +98,38 @@ proc list*(m: MultisteamSelect, result = list proc handle*(m: MultisteamSelect, conn: Connection) {.async, gcsafe.} = - ## handle requests on connection - if (await m.select(conn)).len == 0: - return - while not conn.closed: - var ms = cast[string](await conn.readLp()) - ms.removeSuffix("\n") - if ms.len() <= 0: - 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(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 + block main: + var ms = cast[string](await conn.readLp()) + echo ms + ms.removeSuffix("\n") + if ms.len() <= 0: 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, codec: string, protocol: T, diff --git a/libp2p/protocols/plaintext.nim b/libp2p/protocols/plaintext.nim deleted file mode 100644 index 1704ccfeb..000000000 --- a/libp2p/protocols/plaintext.nim +++ /dev/null @@ -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()