feature/subscribe-to-waku (#152)

* sub by default

* added

* fmt

* fix

* moved

* removed

* fix
This commit is contained in:
Dean Eigenmann 2020-09-11 13:28:27 +02:00 committed by GitHub
parent 4447a93884
commit 03895ffaf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 3 deletions

View File

@ -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

View File

@ -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()