nwaku/waku/v2/protocol/waku_relay.nim

77 lines
2.1 KiB
Nim
Raw Normal View History

2020-04-29 05:02:42 +00:00
## Waku on libp2p
##
## This file should eventually correspond to waku_protocol as RLPx subprotocol.
## Instead, it should likely be on top of GossipSub with a similar interface.
import
chronos, chronicles, metrics,
libp2p/protocols/pubsub/[pubsub, gossipsub],
libp2p/protocols/pubsub/rpc/messages,
libp2p/stream/connection,
../waku_types
2020-05-27 04:00:50 +00:00
declarePublicGauge total_messages, "number of messages received"
2020-05-01 05:00:00 +00:00
logScope:
topics = "wakurelay"
2020-05-01 05:00:00 +00:00
const WakuRelayCodec* = "/vac/waku/relay/2.0.0-beta1"
2020-05-01 05:00:00 +00:00
method init*(w: WakuRelay) =
debug "init"
proc handler(conn: Connection, proto: string) {.async.} =
## main protocol handler that gets triggered on every
## connection for a protocol string
## e.g. ``/wakusub/0.0.1``, etc...
##
debug "Incoming WakuRelay connection"
await w.handleConn(conn, proto)
# XXX: Handler hijack GossipSub here?
w.handler = handler
w.codec = WakuRelayCodec
method initPubSub*(w: WakuRelay) =
debug "initWakuRelay"
2020-06-02 11:27:53 +00:00
procCall GossipSub(w).initPubSub()
2020-06-02 11:27:53 +00:00
w.init()
method subscribe*(w: WakuRelay,
pubSubTopic: string,
handler: TopicHandler) {.async.} =
debug "subscribe", pubSubTopic=pubSubTopic
await procCall GossipSub(w).subscribe(pubSubTopic, handler)
method publish*(w: WakuRelay,
pubSubTopic: string,
message: seq[byte]
): Future[int] {.async.} =
debug "publish", pubSubTopic=pubSubTopic, message=message
2020-06-02 11:27:53 +00:00
return await procCall GossipSub(w).publish(pubSubTopic, message)
method unsubscribe*(w: WakuRelay,
topics: seq[TopicPair]) {.async.} =
debug "unsubscribe"
await procCall GossipSub(w).unsubscribe(topics)
method unsubscribeAll*(w: WakuRelay,
pubSubTopic: string) {.async.} =
debug "unsubscribeAll"
await procCall GossipSub(w).unsubscribeAll(pubSubTopic)
# GossipSub specific methods --------------------------------------------------
method start*(w: WakuRelay) {.async.} =
debug "start"
await procCall GossipSub(w).start()
method stop*(w: WakuRelay) {.async.} =
debug "stop"
await procCall GossipSub(w).stop()