mirror of https://github.com/waku-org/nwaku.git
JSON RPC: Use WakuNode instead of WakuRelay (#154)
* Using Node publish for JSON RPC Compiles, untested * Subscribe and print waku message * Readable string * Use actual input
This commit is contained in:
parent
34353cc96e
commit
4447a93884
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue