diff --git a/waku/node/v2/rpc/rpc_publish.nim b/waku/node/v2/rpc/rpc_publish.nim index 93f36eb4c..e96ad1268 100644 --- a/waku/node/v2/rpc/rpc_publish.nim +++ b/waku/node/v2/rpc/rpc_publish.nim @@ -1,9 +1,11 @@ import os, strutils, strformat, chronicles, json_rpc/[rpcclient, rpcserver], nimcrypto/sysrand, + stew/byteutils, libp2p/protobuf/minprotobuf, eth/common as eth_common, eth/keys, system, - options + options, + ../waku_types from strutils import rsplit template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0] @@ -18,8 +20,9 @@ if paramCount() < 1: let rpcPort = Port(parseInt(paramStr(1))) echo "Please enter your message:" -let message = readLine(stdin) -echo "Message is:", message +let raw_input = readLine(stdin) +let input = fmt"{raw_input}" +echo "Input is:", input var node = newRpcHttpClient() waitfor node.connect("localhost", rpcPort) @@ -27,5 +30,10 @@ waitfor node.connect("localhost", rpcPort) # Subscribe ourselves to topic #var res = node.wakuSubscribe("waku") -# TODO When RPC uses Node, create WakuMessage and pass instead -var res2 = waitfor node.wakuPublish("waku", cast[seq[byte]]("hello world")) +let pubSubTopic = "waku" +let contentTopic = "foobar" +var wakuMessage = WakuMessage(payload: input.toBytes(), contentTopic: contentTopic) +# XXX This should be WakuMessage type, but need to setup JSON-RPC mapping for that to work +var raw_bytes = wakuMessage.encode().buffer +var res = waitfor node.wakuPublish2(pubSubTopic, raw_bytes) +echo "Waku publish response: ", res diff --git a/waku/node/v2/rpc/wakucallsigs.nim b/waku/node/v2/rpc/wakucallsigs.nim index f398f5738..3ee5c9d75 100644 --- a/waku/node/v2/rpc/wakucallsigs.nim +++ b/waku/node/v2/rpc/wakucallsigs.nim @@ -1,6 +1,9 @@ # Alpha - Currently implemented in v2 proc waku_version(): string +# TODO Deprecate old waku_publish, requires adjust simulation code etc proc waku_publish(topic: string, message: seq[byte]): bool +# TODO This should be properly done with rpc types, etc. +proc waku_publish2(topic: string, message: seq[byte]): bool proc waku_subscribe(topic: string): bool #proc waku_subscribe(topic: string, handler: Topichandler): bool diff --git a/waku/node/v2/rpc/wakurpc.nim b/waku/node/v2/rpc/wakurpc.nim index a127f4225..73452704c 100644 --- a/waku/node/v2/rpc/wakurpc.nim +++ b/waku/node/v2/rpc/wakurpc.nim @@ -33,17 +33,35 @@ proc setupWakuRPC*(node: WakuNode, rpcsrv: RpcServer) = #if not result: # raise newException(ValueError, "Message could not be posted") - # TODO: Handler / Identifier logic - rpcsrv.rpc("waku_subscribe") do(topic: string) -> bool: - debug "waku_subscribe", topic=topic - let wakuRelay = cast[WakuRelay](node.switch.pubSub.get()) + rpcsrv.rpc("waku_publish2") do(topic: string, payload: seq[byte]) -> bool: + let msg = WakuMessage.init(payload) + if msg.isOk(): + debug "waku_publish", msg=msg + else: + warn "waku_publish decode error", msg=msg - # XXX: Hacky in-line handler - proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} = - info "Hit subscribe handler", topic=topic, data=data - - # TODO: Shouldn't we really be doing WakuNode subscribe here? - discard wakuRelay.subscribe(topic, handler) + debug "waku_publish", topic=topic, payload=payload, msg=msg[] + node.publish(topic, msg[]) + return true + #if not result: + # raise newException(ValueError, "Message could not be posted") + + # TODO: Handler / Identifier logic + rpcsrv.rpc("waku_subscribe") do(topic: string) -> bool: + debug "waku_subscribe", topic=topic + + # XXX: Hacky in-line handler + proc handler(topic: string, data: seq[byte]) {.async, gcsafe.} = + let msg = WakuMessage.init(data) + if msg.isOk(): + debug "waku_subscribe handler", msg=msg + else: + warn "waku_subscribe decode error", msg=msg + + var readable_str = cast[string](msg[].payload) + info "Hit subscribe handler", topic=topic, msg=msg[], payload=readable_str + + node.subscribe(topic, handler) return true #if not result: # raise newException(ValueError, "Message could not be posted")