From 03895ffaf559c9c3f8f3cc360dd9af746a3d9ed8 Mon Sep 17 00:00:00 2001 From: Dean Eigenmann <7621705+decanus@users.noreply.github.com> Date: Fri, 11 Sep 2020 13:28:27 +0200 Subject: [PATCH] feature/subscribe-to-waku (#152) * sub by default * added * fmt * fix * moved * removed * fix --- waku/node/v2/config.nim | 5 +++++ waku/node/v2/wakunode2.nim | 12 +++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/waku/node/v2/config.nim b/waku/node/v2/config.nim index cb5f4c3fa..6442d52a4 100644 --- a/waku/node/v2/config.nim +++ b/waku/node/v2/config.nim @@ -139,6 +139,11 @@ type desc: "Enable metrics logging." defaultValue: false name: "log-metrics" }: bool + + topics* {. + desc: "Default topics to subscribe to (space seperated list)." + defaultValue: "waku" + name: "topics" .}: string # TODO: # - discv5 + topic register diff --git a/waku/node/v2/wakunode2.nim b/waku/node/v2/wakunode2.nim index e48ac681c..850e021a2 100644 --- a/waku/node/v2/wakunode2.nim +++ b/waku/node/v2/wakunode2.nim @@ -55,7 +55,7 @@ template tcpEndPoint(address, port): auto = proc init*(T: type WakuNode, nodeKey: crypto.PrivateKey, bindIp: ValidIpAddress, bindPort: Port, - extIp = none[ValidIpAddress](), extPort = none[Port]()): T = + extIp = none[ValidIpAddress](), extPort = none[Port](), topics = newSeq[string]()): T = ## Creates and starts a Waku node. let hostAddress = tcpEndPoint(bindIp, bindPort) @@ -69,7 +69,13 @@ proc init*(T: type WakuNode, nodeKey: crypto.PrivateKey, var switch = newStandardSwitch(some(nodekey), hostAddress, triggerSelf = true) - return WakuNode(switch: switch, peerInfo: peerInfo) + result = WakuNode(switch: switch, peerInfo: peerInfo) + + for topic in topics: + proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} = + debug "Hit handler", topic=topic, data=data + + result.subscribe(topic, handler) proc start*(node: WakuNode) {.async.} = node.libp2pTransportLoops = await node.switch.start() @@ -226,7 +232,7 @@ when isMainModule: Port(uint16(conf.tcpPort) + conf.portsShift), Port(uint16(conf.udpPort) + conf.portsShift)) node = WakuNode.init(conf.nodeKey, conf.libp2pAddress, - Port(uint16(conf.tcpPort) + conf.portsShift), extIp, extTcpPort) + Port(uint16(conf.tcpPort) + conf.portsShift), extIp, extTcpPort, conf.topics.split(" ")) waitFor node.start()