2023-05-12 16:08:41 +00:00
|
|
|
|
2023-07-31 07:52:04 +00:00
|
|
|
{.pragma: exported, exportc, cdecl, raises: [].}
|
|
|
|
{.pragma: callback, cdecl, raises: [], gcsafe.}
|
|
|
|
{.passc: "-fPIC".}
|
|
|
|
|
2023-05-12 16:08:41 +00:00
|
|
|
import
|
2023-07-07 08:53:00 +00:00
|
|
|
std/[json,sequtils,times,strformat,options,atomics,strutils],
|
2023-07-31 07:52:04 +00:00
|
|
|
strutils
|
2023-05-12 16:08:41 +00:00
|
|
|
import
|
|
|
|
chronicles,
|
2023-07-31 07:52:04 +00:00
|
|
|
chronos
|
2023-05-12 16:08:41 +00:00
|
|
|
import
|
2023-08-09 17:11:50 +00:00
|
|
|
../../waku/waku_core/message/message,
|
|
|
|
../../waku/node/waku_node,
|
|
|
|
../../waku/waku_core/topics/pubsub_topic,
|
|
|
|
../../../waku/waku_relay/protocol,
|
2023-07-31 07:52:04 +00:00
|
|
|
./events/json_message_event,
|
2023-08-02 08:45:15 +00:00
|
|
|
./waku_thread/waku_thread,
|
2023-07-31 07:52:04 +00:00
|
|
|
./waku_thread/inter_thread_communication/node_lifecycle_request,
|
|
|
|
./waku_thread/inter_thread_communication/peer_manager_request,
|
|
|
|
./waku_thread/inter_thread_communication/protocols/relay_request,
|
|
|
|
./alloc
|
2023-05-12 16:08:41 +00:00
|
|
|
|
|
|
|
################################################################################
|
|
|
|
### Wrapper around the waku node
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
### Exported types
|
2023-07-07 08:53:00 +00:00
|
|
|
|
|
|
|
const RET_OK: cint = 0
|
|
|
|
const RET_ERR: cint = 1
|
|
|
|
const RET_MISSING_CALLBACK: cint = 2
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2023-07-31 07:52:04 +00:00
|
|
|
type
|
|
|
|
WakuCallBack* = proc(msg: ptr cchar, len: csize_t) {.cdecl, gcsafe.}
|
|
|
|
|
2023-05-12 16:08:41 +00:00
|
|
|
### End of exported types
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
### Not-exported components
|
|
|
|
|
2023-07-07 08:53:00 +00:00
|
|
|
# May keep a reference to a callback defined externally
|
2023-07-31 07:52:04 +00:00
|
|
|
var extEventCallback*: WakuCallBack = nil
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2023-07-31 07:52:04 +00:00
|
|
|
proc relayEventCallback(pubsubTopic: PubsubTopic,
|
|
|
|
msg: WakuMessage): Future[void] {.async, gcsafe.} =
|
2023-05-12 16:08:41 +00:00
|
|
|
# Callback that hadles the Waku Relay events. i.e. messages or errors.
|
2023-07-31 07:52:04 +00:00
|
|
|
if not isNil(extEventCallback):
|
2023-05-12 16:08:41 +00:00
|
|
|
try:
|
2023-07-07 08:53:00 +00:00
|
|
|
let event = $JsonMessageEvent.new(pubsubTopic, msg)
|
2023-07-31 07:52:04 +00:00
|
|
|
extEventCallback(unsafeAddr event[0], cast[csize_t](len(event)))
|
2023-07-07 08:53:00 +00:00
|
|
|
except Exception,CatchableError:
|
2023-05-12 16:08:41 +00:00
|
|
|
error "Exception when calling 'eventCallBack': " &
|
|
|
|
getCurrentExceptionMsg()
|
|
|
|
else:
|
2023-07-31 07:52:04 +00:00
|
|
|
error "extEventCallback is nil"
|
2023-05-12 16:08:41 +00:00
|
|
|
|
|
|
|
### End of not-exported components
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
### Exported procs
|
|
|
|
|
2023-07-07 08:53:00 +00:00
|
|
|
proc waku_new(configJson: cstring,
|
|
|
|
onErrCb: WakuCallback): cint
|
|
|
|
{.dynlib, exportc, cdecl.} =
|
|
|
|
# Creates a new instance of the WakuNode.
|
|
|
|
# Notice that the ConfigNode type is also exported and available for users.
|
|
|
|
|
|
|
|
if isNil(onErrCb):
|
|
|
|
return RET_MISSING_CALLBACK
|
|
|
|
|
2023-08-02 08:45:15 +00:00
|
|
|
let createThRes = waku_thread.createWakuThread(configJson)
|
2023-07-31 07:52:04 +00:00
|
|
|
if createThRes.isErr():
|
|
|
|
let msg = "Error in createWakuThread: " & $createThRes.error
|
|
|
|
onErrCb(unsafeAddr msg[0], cast[csize_t](len(msg)))
|
2023-07-07 08:53:00 +00:00
|
|
|
return RET_ERR
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2023-07-07 08:53:00 +00:00
|
|
|
return RET_OK
|
|
|
|
|
|
|
|
proc waku_version(onOkCb: WakuCallBack): cint {.dynlib, exportc.} =
|
|
|
|
if isNil(onOkCb):
|
|
|
|
return RET_MISSING_CALLBACK
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2023-07-07 08:53:00 +00:00
|
|
|
onOkCb(cast[ptr cchar](WakuNodeVersionString),
|
|
|
|
cast[csize_t](len(WakuNodeVersionString)))
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2023-07-07 08:53:00 +00:00
|
|
|
return RET_OK
|
|
|
|
|
2023-07-31 07:52:04 +00:00
|
|
|
proc waku_set_event_callback(callback: WakuCallBack) {.dynlib, exportc.} =
|
|
|
|
extEventCallback = callback
|
2023-05-12 16:08:41 +00:00
|
|
|
|
|
|
|
proc waku_content_topic(appName: cstring,
|
2023-07-07 08:53:00 +00:00
|
|
|
appVersion: cuint,
|
2023-05-12 16:08:41 +00:00
|
|
|
contentTopicName: cstring,
|
|
|
|
encoding: cstring,
|
2023-07-07 08:53:00 +00:00
|
|
|
onOkCb: WakuCallBack): cint {.dynlib, exportc.} =
|
2023-05-12 16:08:41 +00:00
|
|
|
# https://rfc.vac.dev/spec/36/#extern-char-waku_content_topicchar-applicationname-unsigned-int-applicationversion-char-contenttopicname-char-encoding
|
|
|
|
|
2023-07-07 08:53:00 +00:00
|
|
|
if isNil(onOkCb):
|
|
|
|
return RET_MISSING_CALLBACK
|
|
|
|
|
|
|
|
let appStr = appName.alloc()
|
|
|
|
let ctnStr = contentTopicName.alloc()
|
|
|
|
let encodingStr = encoding.alloc()
|
|
|
|
|
|
|
|
let contentTopic = fmt"/{$appStr}/{appVersion}/{$ctnStr}/{$encodingStr}"
|
|
|
|
onOkCb(unsafeAddr contentTopic[0], cast[csize_t](len(contentTopic)))
|
|
|
|
|
|
|
|
deallocShared(appStr)
|
|
|
|
deallocShared(ctnStr)
|
|
|
|
deallocShared(encodingStr)
|
|
|
|
|
|
|
|
return RET_OK
|
|
|
|
|
|
|
|
proc waku_pubsub_topic(topicName: cstring,
|
|
|
|
onOkCb: WakuCallBack): cint {.dynlib, exportc, cdecl.} =
|
|
|
|
if isNil(onOkCb):
|
|
|
|
return RET_MISSING_CALLBACK
|
|
|
|
|
|
|
|
let topicNameStr = topicName.alloc()
|
|
|
|
|
2023-05-12 16:08:41 +00:00
|
|
|
# https://rfc.vac.dev/spec/36/#extern-char-waku_pubsub_topicchar-name-char-encoding
|
2023-07-07 08:53:00 +00:00
|
|
|
let outPubsubTopic = fmt"/waku/2/{$topicNameStr}"
|
|
|
|
onOkCb(unsafeAddr outPubsubTopic[0], cast[csize_t](len(outPubsubTopic)))
|
|
|
|
|
|
|
|
deallocShared(topicNameStr)
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2023-07-07 08:53:00 +00:00
|
|
|
return RET_OK
|
|
|
|
|
|
|
|
proc waku_default_pubsub_topic(onOkCb: WakuCallBack): cint {.dynlib, exportc.} =
|
2023-05-12 16:08:41 +00:00
|
|
|
# https://rfc.vac.dev/spec/36/#extern-char-waku_default_pubsub_topic
|
2023-07-07 08:53:00 +00:00
|
|
|
if isNil(onOkCb):
|
|
|
|
return RET_MISSING_CALLBACK
|
|
|
|
|
|
|
|
onOkCb(cast[ptr cchar](DefaultPubsubTopic),
|
|
|
|
cast[csize_t](len(DefaultPubsubTopic)))
|
|
|
|
|
|
|
|
return RET_OK
|
2023-05-12 16:08:41 +00:00
|
|
|
|
|
|
|
proc waku_relay_publish(pubSubTopic: cstring,
|
|
|
|
jsonWakuMessage: cstring,
|
2023-07-07 08:53:00 +00:00
|
|
|
timeoutMs: cuint,
|
|
|
|
onErrCb: WakuCallBack): cint
|
2023-05-12 16:08:41 +00:00
|
|
|
|
|
|
|
{.dynlib, exportc, cdecl.} =
|
|
|
|
# https://rfc.vac.dev/spec/36/#extern-char-waku_relay_publishchar-messagejson-char-pubsubtopic-int-timeoutms
|
|
|
|
|
2023-08-02 08:45:15 +00:00
|
|
|
if isNil(onErrCb):
|
2023-07-07 08:53:00 +00:00
|
|
|
return RET_MISSING_CALLBACK
|
|
|
|
|
|
|
|
let jwm = jsonWakuMessage.alloc()
|
2023-05-12 16:08:41 +00:00
|
|
|
var jsonContent:JsonNode
|
|
|
|
try:
|
2023-07-07 08:53:00 +00:00
|
|
|
jsonContent = parseJson($jwm)
|
2023-05-12 16:08:41 +00:00
|
|
|
except JsonParsingError:
|
2023-07-07 08:53:00 +00:00
|
|
|
deallocShared(jwm)
|
|
|
|
let msg = fmt"Error parsing json message: {getCurrentExceptionMsg()}"
|
|
|
|
onErrCb(unsafeAddr msg[0], cast[csize_t](len(msg)))
|
|
|
|
return RET_ERR
|
|
|
|
|
|
|
|
deallocShared(jwm)
|
2023-05-12 16:08:41 +00:00
|
|
|
|
|
|
|
var wakuMessage: WakuMessage
|
|
|
|
try:
|
|
|
|
var version = 0'u32
|
|
|
|
if jsonContent.hasKey("version"):
|
|
|
|
version = (uint32) jsonContent["version"].getInt()
|
|
|
|
|
|
|
|
wakuMessage = WakuMessage(
|
|
|
|
# Visit https://rfc.vac.dev/spec/14/ for further details
|
|
|
|
payload: jsonContent["payload"].getStr().toSeq().mapIt(byte (it)),
|
|
|
|
contentTopic: $jsonContent["content_topic"].getStr(),
|
|
|
|
version: version,
|
|
|
|
timestamp: getTime().toUnix(),
|
|
|
|
ephemeral: false
|
|
|
|
)
|
|
|
|
except KeyError:
|
2023-07-07 08:53:00 +00:00
|
|
|
let msg = fmt"Problem building the WakuMessage: {getCurrentExceptionMsg()}"
|
|
|
|
onErrCb(unsafeAddr msg[0], cast[csize_t](len(msg)))
|
|
|
|
return RET_ERR
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2023-07-07 08:53:00 +00:00
|
|
|
let pst = pubSubTopic.alloc()
|
|
|
|
|
|
|
|
let targetPubSubTopic = if len(pst) == 0:
|
2023-05-12 16:08:41 +00:00
|
|
|
DefaultPubsubTopic
|
|
|
|
else:
|
2023-07-07 08:53:00 +00:00
|
|
|
$pst
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2023-08-02 08:45:15 +00:00
|
|
|
let sendReqRes = waku_thread.sendRequestToWakuThread(
|
2023-07-31 07:52:04 +00:00
|
|
|
RelayRequest.new(RelayMsgType.PUBLISH,
|
|
|
|
PubsubTopic($pst),
|
|
|
|
WakuRelayHandler(relayEventCallback),
|
|
|
|
wakuMessage))
|
|
|
|
deallocShared(pst)
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2023-07-31 07:52:04 +00:00
|
|
|
if sendReqRes.isErr():
|
|
|
|
let msg = $sendReqRes.error
|
2023-07-07 08:53:00 +00:00
|
|
|
onErrCb(unsafeAddr msg[0], cast[csize_t](len(msg)))
|
|
|
|
return RET_ERR
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2023-07-31 07:52:04 +00:00
|
|
|
return RET_OK
|
|
|
|
|
2023-05-12 16:08:41 +00:00
|
|
|
proc waku_start() {.dynlib, exportc.} =
|
2023-08-02 08:45:15 +00:00
|
|
|
discard waku_thread.sendRequestToWakuThread(
|
2023-07-31 07:52:04 +00:00
|
|
|
NodeLifecycleRequest.new(
|
|
|
|
NodeLifecycleMsgType.START_NODE))
|
2023-05-12 16:08:41 +00:00
|
|
|
|
|
|
|
proc waku_stop() {.dynlib, exportc.} =
|
2023-08-02 08:45:15 +00:00
|
|
|
discard waku_thread.sendRequestToWakuThread(
|
2023-07-31 07:52:04 +00:00
|
|
|
NodeLifecycleRequest.new(
|
|
|
|
NodeLifecycleMsgType.STOP_NODE))
|
2023-05-12 16:08:41 +00:00
|
|
|
|
|
|
|
proc waku_relay_subscribe(
|
|
|
|
pubSubTopic: cstring,
|
2023-07-07 08:53:00 +00:00
|
|
|
onErrCb: WakuCallBack): cint
|
2023-05-12 16:08:41 +00:00
|
|
|
{.dynlib, exportc.} =
|
2023-07-07 08:53:00 +00:00
|
|
|
|
2023-07-31 07:52:04 +00:00
|
|
|
let pst = pubSubTopic.alloc()
|
2023-08-02 08:45:15 +00:00
|
|
|
let sendReqRes = waku_thread.sendRequestToWakuThread(
|
2023-07-31 07:52:04 +00:00
|
|
|
RelayRequest.new(RelayMsgType.SUBSCRIBE,
|
|
|
|
PubsubTopic($pst),
|
|
|
|
WakuRelayHandler(relayEventCallback)))
|
|
|
|
deallocShared(pst)
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2023-07-31 07:52:04 +00:00
|
|
|
if sendReqRes.isErr():
|
|
|
|
let msg = $sendReqRes.error
|
2023-07-07 08:53:00 +00:00
|
|
|
onErrCb(unsafeAddr msg[0], cast[csize_t](len(msg)))
|
|
|
|
return RET_ERR
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2023-07-07 08:53:00 +00:00
|
|
|
return RET_OK
|
2023-05-12 16:08:41 +00:00
|
|
|
|
|
|
|
proc waku_relay_unsubscribe(
|
|
|
|
pubSubTopic: cstring,
|
2023-07-07 08:53:00 +00:00
|
|
|
onErrCb: WakuCallBack): cint
|
2023-05-12 16:08:41 +00:00
|
|
|
{.dynlib, exportc.} =
|
2023-07-07 08:53:00 +00:00
|
|
|
|
2023-07-31 07:52:04 +00:00
|
|
|
let pst = pubSubTopic.alloc()
|
2023-08-02 08:45:15 +00:00
|
|
|
let sendReqRes = waku_thread.sendRequestToWakuThread(
|
2023-07-31 07:52:04 +00:00
|
|
|
RelayRequest.new(RelayMsgType.UNSUBSCRIBE,
|
|
|
|
PubsubTopic($pst),
|
|
|
|
WakuRelayHandler(relayEventCallback)))
|
|
|
|
deallocShared(pst)
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2023-07-31 07:52:04 +00:00
|
|
|
if sendReqRes.isErr():
|
|
|
|
let msg = $sendReqRes.error
|
2023-07-07 08:53:00 +00:00
|
|
|
onErrCb(unsafeAddr msg[0], cast[csize_t](len(msg)))
|
|
|
|
return RET_ERR
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2023-07-07 08:53:00 +00:00
|
|
|
return RET_OK
|
2023-05-12 16:08:41 +00:00
|
|
|
|
|
|
|
proc waku_connect(peerMultiAddr: cstring,
|
2023-07-07 08:53:00 +00:00
|
|
|
timeoutMs: cuint,
|
|
|
|
onErrCb: WakuCallBack): cint
|
2023-05-12 16:08:41 +00:00
|
|
|
{.dynlib, exportc.} =
|
|
|
|
|
2023-08-02 08:45:15 +00:00
|
|
|
let connRes = waku_thread.sendRequestToWakuThread(
|
2023-07-31 07:52:04 +00:00
|
|
|
PeerManagementRequest.new(
|
|
|
|
PeerManagementMsgType.CONNECT_TO,
|
|
|
|
$peerMultiAddr,
|
|
|
|
chronos.milliseconds(timeoutMs)))
|
|
|
|
if connRes.isErr():
|
|
|
|
let msg = $connRes.error
|
2023-07-07 08:53:00 +00:00
|
|
|
onErrCb(unsafeAddr msg[0], cast[csize_t](len(msg)))
|
|
|
|
return RET_ERR
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2023-07-07 08:53:00 +00:00
|
|
|
return RET_OK
|
2023-05-12 16:08:41 +00:00
|
|
|
|
|
|
|
### End of exported procs
|
2023-06-29 19:59:53 +00:00
|
|
|
################################################################################
|