nim-libp2p/libp2p/multistreamselect.nim

119 lines
3.2 KiB
Nim
Raw Normal View History

## 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.
2019-08-23 22:17:08 +00:00
import sequtils, strutils
2019-08-23 02:13:36 +00:00
import chronos
import connection, varint, vbuffer, protocol
2019-08-23 02:13:36 +00:00
2019-08-23 22:17:08 +00:00
const MsgSize* = 64*1024
2019-08-23 02:13:36 +00:00
const Codec* = "/multistream/1.0.0"
2019-08-25 19:08:25 +00:00
const MultiCodec* = "\x13" & Codec & "\n"
const Na = "\x03na\n"
const Ls = "\x03ls\n"
2019-08-23 02:13:36 +00:00
2019-08-26 15:37:15 +00:00
type
2019-08-23 02:13:36 +00:00
MultisteamSelectException = object of CatchableError
Matcher* = proc (proto: string): bool
HandlerHolder* = object
proto: string
protocol: LPProtocol
handler: LPProtoHandler
2019-08-23 02:13:36 +00:00
match: Matcher
MultisteamSelect* = ref object of RootObj
handlers*: seq[HandlerHolder]
codec*: string
2019-08-28 02:31:19 +00:00
na: string
ls: string
2019-08-23 02:13:36 +00:00
proc newMultistream*(): MultisteamSelect =
new result
result.codec = MultiCodec
result.ls = Ls
result.na = Na
2019-08-23 02:13:36 +00:00
2019-08-26 15:37:15 +00:00
proc select*(m: MultisteamSelect,
conn: Connection,
proto: string = ""): Future[bool] {.async.} =
2019-08-23 02:13:36 +00:00
## select a remote protocol
## TODO: select should support a list of protos to be selected
2019-08-26 15:37:15 +00:00
await conn.write(m.codec) # write handshake
if proto.len() > 0:
2019-08-24 23:47:37 +00:00
await conn.writeLp(proto) # select proto
2019-08-23 22:17:08 +00:00
var ms = cast[string](await conn.readLp())
ms.removeSuffix("\n")
if ms != Codec:
2019-08-24 23:47:37 +00:00
return false
2019-08-23 02:13:36 +00:00
if proto.len() <= 0:
return true
2019-08-23 02:13:36 +00:00
ms = cast[string](await conn.readLp())
ms.removeSuffix("\n")
result = ms == proto
2019-08-23 22:17:08 +00:00
2019-08-26 15:37:15 +00:00
proc list*(m: MultisteamSelect,
conn: Connection): Future[seq[string]] {.async.} =
2019-08-24 23:47:37 +00:00
## list remote protos requests on connection
if not (await m.select(conn)):
return
2019-08-26 15:37:15 +00:00
await conn.write(m.ls) # send ls
2019-08-25 19:08:25 +00:00
2019-08-24 23:47:37 +00:00
var list = newSeq[string]()
let ms = cast[string](await conn.readLp())
for s in ms.split("\n"):
2019-08-25 19:08:25 +00:00
if s.len() > 0:
list.add(s)
2019-08-24 23:47:37 +00:00
result = list
proc handle*(m: MultisteamSelect, conn: Connection) {.async, gcsafe.} =
## handle requests on connection
if not (await m.select(conn)):
return
2019-08-24 23:47:37 +00:00
2019-08-24 19:15:45 +00:00
while not conn.closed:
var ms = cast[string](await conn.readLp())
ms.removeSuffix("\n")
if ms.len() <= 0:
2019-08-24 23:47:37 +00:00
await conn.write(m.na)
if m.handlers.len() == 0:
await conn.write(m.na)
continue
case ms:
2019-08-25 19:08:25 +00:00
of "ls":
var protos = ""
for h in m.handlers:
2019-08-25 19:08:25 +00:00
protos &= (h.proto & "\n")
await conn.writeLp(cast[seq[byte]](toSeq(protos.items)))
2019-08-26 15:37:15 +00:00
else:
for h in m.handlers:
if (not isNil(h.match) and h.match(ms)) or ms == h.proto:
2019-08-25 19:08:25 +00:00
await conn.writeLp(h.proto & "\n")
await h.handler(h.protocol, conn, ms)
2019-08-24 19:15:45 +00:00
return
2019-08-25 19:08:25 +00:00
await conn.write(m.na)
2019-08-23 02:13:36 +00:00
proc addHandler*[T: LPProtocol](m: MultisteamSelect,
proto: string,
protocol: T,
handler: LPProtoHandler,
matcher: Matcher = nil) =
2019-08-23 02:13:36 +00:00
## register a handler for the protocol
2019-08-26 15:37:15 +00:00
m.handlers.add(HandlerHolder(proto: proto,
handler: handler,
protocol: protocol,
2019-08-23 02:13:36 +00:00
match: matcher))