mirror of
https://github.com/waku-org/nwaku.git
synced 2025-01-14 08:57:14 +00:00
Fix timing issue with subscribe in chat2 (#226)
- Support static node - Ensure nodes are connected before subscribing - Sleep to allow settling down before subscribing - Default chat2 log to DEBUG
This commit is contained in:
parent
f31ab81546
commit
d6fdecafd8
@ -66,6 +66,11 @@ proc dialPeer(c: Chat, address: string) {.async.} =
|
|||||||
discard await c.node.switch.dial(remotePeer, WakuRelayCodec)
|
discard await c.node.switch.dial(remotePeer, WakuRelayCodec)
|
||||||
c.connected = true
|
c.connected = true
|
||||||
|
|
||||||
|
proc connectToNodes(c: Chat, nodes: openArray[string]) =
|
||||||
|
echo "Connecting to nodes"
|
||||||
|
for nodeId in nodes:
|
||||||
|
discard dialPeer(c, nodeId)
|
||||||
|
|
||||||
proc publish(c: Chat, line: string) =
|
proc publish(c: Chat, line: string) =
|
||||||
let payload = cast[seq[byte]](line)
|
let payload = cast[seq[byte]](line)
|
||||||
let message = WakuMessage(payload: payload, contentTopic: DefaultContentTopic)
|
let message = WakuMessage(payload: payload, contentTopic: DefaultContentTopic)
|
||||||
@ -158,6 +163,11 @@ proc processInput(rfd: AsyncFD, rng: ref BrHmacDrbgContext) {.async.} =
|
|||||||
# waitFor vs await
|
# waitFor vs await
|
||||||
await node.start()
|
await node.start()
|
||||||
|
|
||||||
|
var chat = Chat(node: node, transp: transp, subscribed: true, connected: false, started: true)
|
||||||
|
|
||||||
|
if conf.staticnodes.len > 0:
|
||||||
|
connectToNodes(chat, conf.staticnodes)
|
||||||
|
|
||||||
let peerInfo = node.peerInfo
|
let peerInfo = node.peerInfo
|
||||||
let listenStr = $peerInfo.addrs[0] & "/p2p/" & $peerInfo.peerId
|
let listenStr = $peerInfo.addrs[0] & "/p2p/" & $peerInfo.peerId
|
||||||
echo &"Listening on\n {listenStr}"
|
echo &"Listening on\n {listenStr}"
|
||||||
@ -171,9 +181,10 @@ proc processInput(rfd: AsyncFD, rng: ref BrHmacDrbgContext) {.async.} =
|
|||||||
let payload = cast[string](message.payload)
|
let payload = cast[string](message.payload)
|
||||||
echo &"{payload}"
|
echo &"{payload}"
|
||||||
info "Hit subscribe handler", topic=topic, payload=payload, contentTopic=message.contentTopic
|
info "Hit subscribe handler", topic=topic, payload=payload, contentTopic=message.contentTopic
|
||||||
await node.subscribe(topic, handler)
|
|
||||||
|
|
||||||
var chat = Chat(node: node, transp: transp, subscribed: true, connected: false, started: true)
|
# XXX Timing issue with subscribe, need to wait a bit to ensure GRAFT message is sent
|
||||||
|
await sleepAsync(5.seconds)
|
||||||
|
await node.subscribe(topic, handler)
|
||||||
|
|
||||||
await chat.readWriteLoop()
|
await chat.readWriteLoop()
|
||||||
runForever()
|
runForever()
|
||||||
|
@ -80,4 +80,5 @@ task chat2, "Build example Waku v2 chat usage":
|
|||||||
let name = "chat2"
|
let name = "chat2"
|
||||||
# NOTE For debugging, set debug level. For chat usage we want minimal log
|
# NOTE For debugging, set debug level. For chat usage we want minimal log
|
||||||
# output to STDOUT. Can be fixed by redirecting logs to file (e.g.)
|
# output to STDOUT. Can be fixed by redirecting logs to file (e.g.)
|
||||||
buildBinary name, "examples/v2/", "-d:chronicles_log_level=WARN"
|
#buildBinary name, "examples/v2/", "-d:chronicles_log_level=WARN"
|
||||||
|
buildBinary name, "examples/v2/", "-d:chronicles_log_level=DEBUG"
|
||||||
|
@ -97,6 +97,8 @@ proc init*(T: type WakuNode, nodeKey: crypto.PrivateKey,
|
|||||||
filters: initTable[string, Filter]()
|
filters: initTable[string, Filter]()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# TODO This is _not_ safe. Subscribe should happen in start and after things settled down.
|
||||||
|
# Otherwise GRAFT message isn't sent to a node.
|
||||||
for topic in topics:
|
for topic in topics:
|
||||||
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} =
|
||||||
debug "Hit handler", topic=topic, data=data
|
debug "Hit handler", topic=topic, data=data
|
||||||
|
Loading…
x
Reference in New Issue
Block a user