logos-delivery/waku/protocol/v2/waku_relay.nim
Oskar Thorén e5f0f36f01
Add basic rpc scripts to publish and subscribe (#140)
* Add basic rpc scripts to publish and subscribe

* Fix publish topic and payload script

Also change parameter name in waku relay due to weird shadowing of log
topic:

DBG 2020-09-09 12:07:54+08:00 waku_publish                               tid=8795 file=wakurpc.nim:30 topic=waku payload=@[]
DBG 2020-09-09 12:07:54+08:00 publish                                    tid=8795 file=waku_relay.nim:65 topic=WakuRelay

Above should show topic=waku but it gets topic=WakuRelay from log scope
for some reason.

* Scripts take arguments

* Add basic nangang tutorial

* Update docs/tutorial/nangang.md

Co-authored-by: Kim De Mey <kim.demey@gmail.com>

* Update docs/tutorial/nangang.md

Co-authored-by: Kim De Mey <kim.demey@gmail.com>

* meh

* ENsure subscribe call succeeds

Co-authored-by: Kim De Mey <kim.demey@gmail.com>
2020-09-10 12:18:35 +08:00

94 lines
2.5 KiB
Nim

## 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
std/strutils,
chronos, chronicles, metrics,
libp2p/protocols/pubsub/[pubsub, floodsub, gossipsub],
libp2p/protocols/pubsub/rpc/messages,
libp2p/stream/connection
declarePublicGauge total_messages, "number of messages received"
logScope:
topic = "WakuRelay"
const WakuRelayCodec* = "/vac/waku/relay/2.0.0-alpha2"
type
WakuRelay* = ref object of GossipSub
gossipEnabled*: bool
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"
# Not using GossipSub
w.gossipEnabled = false
if w.gossipEnabled:
procCall GossipSub(w).initPubSub()
else:
procCall FloodSub(w).initPubSub()
w.init()
method subscribe*(w: WakuRelay,
pubSubTopic: string,
handler: TopicHandler) {.async.} =
debug "subscribe", pubSubTopic=pubSubTopic
if w.gossipEnabled:
await procCall GossipSub(w).subscribe(pubSubTopic, handler)
else:
await procCall FloodSub(w).subscribe(pubSubTopic, handler)
method publish*(w: WakuRelay,
pubSubTopic: string,
message: seq[byte]
): Future[int] {.async.} =
debug "publish", pubSubTopic=pubSubTopic, message=message
if w.gossipEnabled:
return await procCall GossipSub(w).publish(pubSubTopic, message)
else:
return await procCall FloodSub(w).publish(pubSubTopic, message)
method unsubscribe*(w: WakuRelay,
topics: seq[TopicPair]) {.async.} =
debug "unsubscribe"
if w.gossipEnabled:
await procCall GossipSub(w).unsubscribe(topics)
else:
await procCall FloodSub(w).unsubscribe(topics)
# GossipSub specific methods --------------------------------------------------
method start*(w: WakuRelay) {.async.} =
debug "start"
if w.gossipEnabled:
await procCall GossipSub(w).start()
else:
await procCall FloodSub(w).start()
method stop*(w: WakuRelay) {.async.} =
debug "stop"
if w.gossipEnabled:
await procCall GossipSub(w).stop()
else:
await procCall FloodSub(w).stop()