2023-07-31 07:52:04 +00:00
|
|
|
{.pragma: exported, exportc, cdecl, raises: [].}
|
|
|
|
{.pragma: callback, cdecl, raises: [], gcsafe.}
|
|
|
|
{.passc: "-fPIC".}
|
|
|
|
|
2024-05-23 14:05:53 +00:00
|
|
|
when defined(linux):
|
2024-05-22 01:00:22 +00:00
|
|
|
{.passl: "-Wl,-soname,libwaku.so".}
|
|
|
|
|
|
|
|
import std/[json, sequtils, atomics, times, strformat, options, atomics, strutils, os]
|
2024-03-15 23:08:47 +00:00
|
|
|
import chronicles, chronos
|
2023-05-12 16:08:41 +00:00
|
|
|
import
|
2024-02-20 20:00:03 +00:00
|
|
|
../../waku/common/base64,
|
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-09-18 07:21:50 +00:00
|
|
|
./waku_thread/inter_thread_communication/requests/node_lifecycle_request,
|
|
|
|
./waku_thread/inter_thread_communication/requests/peer_manager_request,
|
|
|
|
./waku_thread/inter_thread_communication/requests/protocols/relay_request,
|
2023-12-11 07:49:13 +00:00
|
|
|
./waku_thread/inter_thread_communication/requests/protocols/store_request,
|
2024-02-21 11:06:05 +00:00
|
|
|
./waku_thread/inter_thread_communication/requests/debug_node_request,
|
2024-05-21 16:37:50 +00:00
|
|
|
./waku_thread/inter_thread_communication/requests/discovery_request,
|
2023-09-18 07:21:50 +00:00
|
|
|
./waku_thread/inter_thread_communication/waku_thread_request,
|
2023-12-11 07:49:13 +00:00
|
|
|
./alloc,
|
|
|
|
./callback
|
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
|
|
|
|
|
|
|
### End of exported types
|
|
|
|
################################################################################
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
### Not-exported components
|
|
|
|
|
2024-05-22 01:00:22 +00:00
|
|
|
template foreignThreadGc(body: untyped) =
|
|
|
|
when declared(setupForeignThreadGc):
|
|
|
|
setupForeignThreadGc()
|
|
|
|
|
|
|
|
body
|
|
|
|
|
|
|
|
when declared(tearDownForeignThreadGc):
|
|
|
|
tearDownForeignThreadGc()
|
|
|
|
|
2024-02-13 14:22:22 +00:00
|
|
|
proc relayEventCallback(ctx: ptr Context): WakuRelayHandler =
|
2024-03-15 23:08:47 +00:00
|
|
|
return proc(
|
|
|
|
pubsubTopic: PubsubTopic, msg: WakuMessage
|
|
|
|
): Future[system.void] {.async.} =
|
2024-02-13 14:22:22 +00:00
|
|
|
# Callback that hadles the Waku Relay events. i.e. messages or errors.
|
2024-02-20 20:00:03 +00:00
|
|
|
if isNil(ctx[].eventCallback):
|
2024-02-13 14:22:22 +00:00
|
|
|
error "eventCallback is nil"
|
2024-02-20 20:00:03 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if isNil(ctx[].eventUserData):
|
|
|
|
error "eventUserData is nil"
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
let event = $JsonMessageEvent.new(pubsubTopic, msg)
|
2024-05-22 01:00:22 +00:00
|
|
|
foreignThreadGc:
|
|
|
|
cast[WakuCallBack](ctx[].eventCallback)(
|
|
|
|
RET_OK, unsafeAddr event[0], cast[csize_t](len(event)), ctx[].eventUserData
|
|
|
|
)
|
2024-03-15 23:08:47 +00:00
|
|
|
except Exception, CatchableError:
|
|
|
|
let msg = "Exception when calling 'eventCallBack': " & getCurrentExceptionMsg()
|
2024-05-22 01:00:22 +00:00
|
|
|
foreignThreadGc:
|
|
|
|
cast[WakuCallBack](ctx[].eventCallback)(
|
|
|
|
RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), ctx[].eventUserData
|
|
|
|
)
|
2023-05-12 16:08:41 +00:00
|
|
|
|
|
|
|
### End of not-exported components
|
|
|
|
################################################################################
|
|
|
|
|
2024-05-22 01:00:22 +00:00
|
|
|
################################################################################
|
|
|
|
### Library setup
|
|
|
|
|
|
|
|
# Every Nim library must have this function called - the name is derived from
|
|
|
|
# the `--nimMainPrefix` command line option
|
|
|
|
proc NimMain() {.importc.}
|
|
|
|
|
|
|
|
# To control when the library has been initialized
|
|
|
|
var initialized: Atomic[bool]
|
|
|
|
|
|
|
|
if defined(android):
|
|
|
|
# Redirect chronicles to Android System logs
|
|
|
|
when compiles(defaultChroniclesStream.outputs[0].writer):
|
|
|
|
defaultChroniclesStream.outputs[0].writer = proc(
|
|
|
|
logLevel: LogLevel, msg: LogOutputStr
|
|
|
|
) {.raises: [].} =
|
|
|
|
echo logLevel, msg
|
|
|
|
|
|
|
|
### End of library setup
|
|
|
|
################################################################################
|
|
|
|
|
2023-05-12 16:08:41 +00:00
|
|
|
################################################################################
|
|
|
|
### Exported procs
|
2024-05-22 01:00:22 +00:00
|
|
|
proc waku_setup() {.dynlib, exportc.} =
|
|
|
|
NimMain()
|
|
|
|
if not initialized.load:
|
|
|
|
initialized.store(true)
|
|
|
|
|
|
|
|
when declared(nimGC_setStackBottom):
|
|
|
|
var locals {.volatile, noinit.}: pointer
|
|
|
|
locals = addr(locals)
|
|
|
|
nimGC_setStackBottom(locals)
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc waku_new(
|
|
|
|
configJson: cstring, callback: WakuCallback, userData: pointer
|
|
|
|
): pointer {.dynlib, exportc, cdecl.} =
|
2023-12-15 12:32:12 +00:00
|
|
|
## Creates a new instance of the WakuNode.
|
|
|
|
if isNil(callback):
|
|
|
|
echo "error: missing callback in waku_new"
|
|
|
|
return nil
|
2023-07-07 08:53:00 +00:00
|
|
|
|
2023-09-01 06:37:02 +00:00
|
|
|
## Create the Waku thread that will keep waiting for req from the main thread.
|
2023-10-23 06:37:28 +00:00
|
|
|
var ctx = waku_thread.createWakuThread().valueOr:
|
2024-05-22 01:00:22 +00:00
|
|
|
foreignThreadGc:
|
|
|
|
let msg = "Error in createWakuThread: " & $error
|
|
|
|
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
2023-10-23 06:37:28 +00:00
|
|
|
return nil
|
|
|
|
|
2023-12-15 12:32:12 +00:00
|
|
|
ctx.userData = userData
|
2023-09-01 06:37:02 +00:00
|
|
|
|
2024-05-22 01:00:22 +00:00
|
|
|
waku_thread.sendRequestToWakuThread(
|
2024-03-15 23:08:47 +00:00
|
|
|
ctx,
|
|
|
|
RequestType.LIFECYCLE,
|
|
|
|
NodeLifecycleRequest.createShared(NodeLifecycleMsgType.CREATE_NODE, configJson),
|
2024-05-22 01:00:22 +00:00
|
|
|
).isOkOr:
|
|
|
|
foreignThreadGc:
|
|
|
|
let msg = $error
|
|
|
|
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
|
|
|
return nil
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2023-12-15 12:32:12 +00:00
|
|
|
return ctx
|
2023-07-07 08:53:00 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc waku_destroy(
|
|
|
|
ctx: ptr Context, callback: WakuCallBack, userData: pointer
|
|
|
|
): cint {.dynlib, exportc.} =
|
2024-03-07 17:53:03 +00:00
|
|
|
if isNil(callback):
|
|
|
|
return RET_MISSING_CALLBACK
|
|
|
|
|
|
|
|
waku_thread.stopWakuThread(ctx).isOkOr:
|
2024-05-22 01:00:22 +00:00
|
|
|
foreignThreadGc:
|
|
|
|
let msg = $error
|
|
|
|
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
|
|
|
return RET_ERR
|
2024-03-07 17:53:03 +00:00
|
|
|
|
|
|
|
return RET_OK
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc waku_version(
|
|
|
|
ctx: ptr Context, callback: WakuCallBack, userData: pointer
|
|
|
|
): cint {.dynlib, exportc.} =
|
2024-02-07 14:24:03 +00:00
|
|
|
ctx[].userData = userData
|
2023-10-23 06:37:28 +00:00
|
|
|
|
|
|
|
if isNil(callback):
|
2023-07-07 08:53:00 +00:00
|
|
|
return RET_MISSING_CALLBACK
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2024-05-22 01:00:22 +00:00
|
|
|
foreignThreadGc:
|
|
|
|
callback(
|
|
|
|
RET_OK,
|
|
|
|
cast[ptr cchar](WakuNodeVersionString),
|
|
|
|
cast[csize_t](len(WakuNodeVersionString)),
|
|
|
|
userData,
|
|
|
|
)
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2023-07-07 08:53:00 +00:00
|
|
|
return RET_OK
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc waku_set_event_callback(
|
|
|
|
ctx: ptr Context, callback: WakuCallBack, userData: pointer
|
|
|
|
) {.dynlib, exportc.} =
|
2024-02-13 14:22:22 +00:00
|
|
|
ctx[].eventCallback = cast[pointer](callback)
|
2024-02-20 20:00:03 +00:00
|
|
|
ctx[].eventUserData = userData
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc waku_content_topic(
|
|
|
|
ctx: ptr Context,
|
|
|
|
appName: cstring,
|
|
|
|
appVersion: cuint,
|
|
|
|
contentTopicName: cstring,
|
|
|
|
encoding: cstring,
|
|
|
|
callback: WakuCallBack,
|
|
|
|
userData: pointer,
|
|
|
|
): 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
|
|
|
|
|
2024-02-07 14:24:03 +00:00
|
|
|
ctx[].userData = userData
|
2023-10-23 06:37:28 +00:00
|
|
|
|
|
|
|
if isNil(callback):
|
2023-07-07 08:53:00 +00:00
|
|
|
return RET_MISSING_CALLBACK
|
|
|
|
|
|
|
|
let appStr = appName.alloc()
|
|
|
|
let ctnStr = contentTopicName.alloc()
|
|
|
|
let encodingStr = encoding.alloc()
|
|
|
|
|
|
|
|
let contentTopic = fmt"/{$appStr}/{appVersion}/{$ctnStr}/{$encodingStr}"
|
2024-03-15 23:08:47 +00:00
|
|
|
callback(
|
|
|
|
RET_OK, unsafeAddr contentTopic[0], cast[csize_t](len(contentTopic)), userData
|
|
|
|
)
|
2023-07-07 08:53:00 +00:00
|
|
|
|
|
|
|
deallocShared(appStr)
|
|
|
|
deallocShared(ctnStr)
|
|
|
|
deallocShared(encodingStr)
|
|
|
|
|
|
|
|
return RET_OK
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc waku_pubsub_topic(
|
|
|
|
ctx: ptr Context, topicName: cstring, callback: WakuCallBack, userData: pointer
|
|
|
|
): cint {.dynlib, exportc, cdecl.} =
|
2023-10-23 06:37:28 +00:00
|
|
|
# https://rfc.vac.dev/spec/36/#extern-char-waku_pubsub_topicchar-name-char-encoding
|
|
|
|
|
2024-02-07 14:24:03 +00:00
|
|
|
ctx[].userData = userData
|
2023-10-23 06:37:28 +00:00
|
|
|
|
|
|
|
if isNil(callback):
|
2023-07-07 08:53:00 +00:00
|
|
|
return RET_MISSING_CALLBACK
|
|
|
|
|
|
|
|
let topicNameStr = topicName.alloc()
|
|
|
|
|
|
|
|
let outPubsubTopic = fmt"/waku/2/{$topicNameStr}"
|
2024-03-15 23:08:47 +00:00
|
|
|
callback(
|
|
|
|
RET_OK, unsafeAddr outPubsubTopic[0], cast[csize_t](len(outPubsubTopic)), userData
|
|
|
|
)
|
2023-07-07 08:53:00 +00:00
|
|
|
|
|
|
|
deallocShared(topicNameStr)
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2023-07-07 08:53:00 +00:00
|
|
|
return RET_OK
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc waku_default_pubsub_topic(
|
|
|
|
ctx: ptr Context, callback: WakuCallBack, userData: pointer
|
|
|
|
): cint {.dynlib, exportc.} =
|
2023-05-12 16:08:41 +00:00
|
|
|
# https://rfc.vac.dev/spec/36/#extern-char-waku_default_pubsub_topic
|
2023-10-23 06:37:28 +00:00
|
|
|
|
2024-02-07 14:24:03 +00:00
|
|
|
ctx[].userData = userData
|
2023-10-23 06:37:28 +00:00
|
|
|
|
|
|
|
if isNil(callback):
|
2023-07-07 08:53:00 +00:00
|
|
|
return RET_MISSING_CALLBACK
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
callback(
|
|
|
|
RET_OK,
|
|
|
|
cast[ptr cchar](DefaultPubsubTopic),
|
|
|
|
cast[csize_t](len(DefaultPubsubTopic)),
|
|
|
|
userData,
|
|
|
|
)
|
2023-07-07 08:53:00 +00:00
|
|
|
|
|
|
|
return RET_OK
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc waku_relay_publish(
|
|
|
|
ctx: ptr Context,
|
|
|
|
pubSubTopic: cstring,
|
|
|
|
jsonWakuMessage: cstring,
|
|
|
|
timeoutMs: cuint,
|
|
|
|
callback: WakuCallBack,
|
|
|
|
userData: pointer,
|
|
|
|
): cint {.dynlib, exportc, cdecl.} =
|
2023-05-12 16:08:41 +00:00
|
|
|
# https://rfc.vac.dev/spec/36/#extern-char-waku_relay_publishchar-messagejson-char-pubsubtopic-int-timeoutms
|
|
|
|
|
2024-02-07 14:24:03 +00:00
|
|
|
ctx[].userData = userData
|
2023-10-23 06:37:28 +00:00
|
|
|
|
|
|
|
if isNil(callback):
|
2023-07-07 08:53:00 +00:00
|
|
|
return RET_MISSING_CALLBACK
|
|
|
|
|
|
|
|
let jwm = jsonWakuMessage.alloc()
|
2024-03-15 23:08:47 +00:00
|
|
|
var jsonMessage: JsonMessage
|
2023-05-12 16:08:41 +00:00
|
|
|
try:
|
2024-02-20 20:00:03 +00:00
|
|
|
let jsonContent = parseJson($jwm)
|
|
|
|
jsonMessage = JsonMessage.fromJsonNode(jsonContent)
|
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()}"
|
2024-02-13 14:22:22 +00:00
|
|
|
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
2023-07-07 08:53:00 +00:00
|
|
|
return RET_ERR
|
2024-02-20 20:00:03 +00:00
|
|
|
finally:
|
|
|
|
deallocShared(jwm)
|
2023-07-07 08:53:00 +00:00
|
|
|
|
2024-02-20 20:00:03 +00:00
|
|
|
let wakuMessage = jsonMessage.toWakuMessage().valueOr:
|
|
|
|
let msg = fmt"Problem building the WakuMessage: {error}"
|
2024-02-13 14:22:22 +00:00
|
|
|
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
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
|
|
|
let pst = pubSubTopic.alloc()
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
let targetPubSubTopic =
|
|
|
|
if len(pst) == 0:
|
|
|
|
DefaultPubsubTopic
|
|
|
|
else:
|
|
|
|
$pst
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2023-08-02 08:45:15 +00:00
|
|
|
let sendReqRes = waku_thread.sendRequestToWakuThread(
|
2024-03-15 23:08:47 +00:00
|
|
|
ctx,
|
|
|
|
RequestType.RELAY,
|
|
|
|
RelayRequest.createShared(
|
|
|
|
RelayMsgType.PUBLISH,
|
|
|
|
PubsubTopic($pst),
|
|
|
|
WakuRelayHandler(relayEventCallback(ctx)),
|
|
|
|
wakuMessage,
|
|
|
|
),
|
|
|
|
)
|
2023-07-31 07:52:04 +00:00
|
|
|
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
|
2024-02-13 14:22:22 +00:00
|
|
|
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
2023-07-07 08:53:00 +00:00
|
|
|
return RET_ERR
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2024-03-01 00:58:35 +00:00
|
|
|
let msgHash = $sendReqRes.value
|
|
|
|
callback(RET_OK, unsafeAddr msgHash[0], cast[csize_t](len(msgHash)), userData)
|
2023-07-31 07:52:04 +00:00
|
|
|
return RET_OK
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc waku_start(
|
|
|
|
ctx: ptr Context, callback: WakuCallBack, userData: pointer
|
|
|
|
): cint {.dynlib, exportc.} =
|
2024-02-07 14:24:03 +00:00
|
|
|
ctx[].userData = userData
|
2023-10-23 06:37:28 +00:00
|
|
|
## TODO: handle the error
|
2023-08-02 08:45:15 +00:00
|
|
|
discard waku_thread.sendRequestToWakuThread(
|
2024-03-15 23:08:47 +00:00
|
|
|
ctx,
|
|
|
|
RequestType.LIFECYCLE,
|
|
|
|
NodeLifecycleRequest.createShared(NodeLifecycleMsgType.START_NODE),
|
|
|
|
)
|
|
|
|
|
|
|
|
proc waku_stop(
|
|
|
|
ctx: ptr Context, callback: WakuCallBack, userData: pointer
|
|
|
|
): cint {.dynlib, exportc.} =
|
2024-02-07 14:24:03 +00:00
|
|
|
ctx[].userData = userData
|
2023-10-23 06:37:28 +00:00
|
|
|
## TODO: handle the error
|
2023-08-02 08:45:15 +00:00
|
|
|
discard waku_thread.sendRequestToWakuThread(
|
2024-03-15 23:08:47 +00:00
|
|
|
ctx,
|
|
|
|
RequestType.LIFECYCLE,
|
|
|
|
NodeLifecycleRequest.createShared(NodeLifecycleMsgType.STOP_NODE),
|
|
|
|
)
|
2023-05-12 16:08:41 +00:00
|
|
|
|
|
|
|
proc waku_relay_subscribe(
|
2024-03-15 23:08:47 +00:00
|
|
|
ctx: ptr Context, pubSubTopic: cstring, callback: WakuCallBack, userData: pointer
|
|
|
|
): cint {.dynlib, exportc.} =
|
2024-02-07 14:24:03 +00:00
|
|
|
ctx[].userData = userData
|
2023-10-23 06:37:28 +00:00
|
|
|
|
2023-07-31 07:52:04 +00:00
|
|
|
let pst = pubSubTopic.alloc()
|
2024-02-13 14:22:22 +00:00
|
|
|
var cb = relayEventCallback(ctx)
|
2023-08-02 08:45:15 +00:00
|
|
|
let sendReqRes = waku_thread.sendRequestToWakuThread(
|
2024-03-15 23:08:47 +00:00
|
|
|
ctx,
|
|
|
|
RequestType.RELAY,
|
|
|
|
RelayRequest.createShared(
|
|
|
|
RelayMsgType.SUBSCRIBE, PubsubTopic($pst), WakuRelayHandler(cb)
|
|
|
|
),
|
|
|
|
)
|
2023-07-31 07:52:04 +00:00
|
|
|
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
|
2024-02-13 14:22:22 +00:00
|
|
|
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
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
|
2023-05-12 16:08:41 +00:00
|
|
|
|
|
|
|
proc waku_relay_unsubscribe(
|
2024-03-15 23:08:47 +00:00
|
|
|
ctx: ptr Context, pubSubTopic: cstring, callback: WakuCallBack, userData: pointer
|
|
|
|
): cint {.dynlib, exportc.} =
|
2024-02-07 14:24:03 +00:00
|
|
|
ctx[].userData = userData
|
2023-10-23 06:37:28 +00:00
|
|
|
|
2023-07-31 07:52:04 +00:00
|
|
|
let pst = pubSubTopic.alloc()
|
2023-09-18 07:21:50 +00:00
|
|
|
|
2023-08-02 08:45:15 +00:00
|
|
|
let sendReqRes = waku_thread.sendRequestToWakuThread(
|
2024-03-15 23:08:47 +00:00
|
|
|
ctx,
|
|
|
|
RequestType.RELAY,
|
|
|
|
RelayRequest.createShared(
|
|
|
|
RelayMsgType.SUBSCRIBE,
|
|
|
|
PubsubTopic($pst),
|
|
|
|
WakuRelayHandler(relayEventCallback(ctx)),
|
|
|
|
),
|
|
|
|
)
|
2023-07-31 07:52:04 +00:00
|
|
|
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
|
2024-02-13 14:22:22 +00:00
|
|
|
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
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
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc waku_connect(
|
|
|
|
ctx: ptr Context,
|
|
|
|
peerMultiAddr: cstring,
|
|
|
|
timeoutMs: cuint,
|
|
|
|
callback: WakuCallBack,
|
|
|
|
userData: pointer,
|
|
|
|
): cint {.dynlib, exportc.} =
|
2024-02-07 14:24:03 +00:00
|
|
|
ctx[].userData = userData
|
2023-10-23 06:37:28 +00:00
|
|
|
|
2023-08-02 08:45:15 +00:00
|
|
|
let connRes = waku_thread.sendRequestToWakuThread(
|
2024-03-15 23:08:47 +00:00
|
|
|
ctx,
|
|
|
|
RequestType.PEER_MANAGER,
|
|
|
|
PeerManagementRequest.createShared(
|
|
|
|
PeerManagementMsgType.CONNECT_TO, $peerMultiAddr, chronos.milliseconds(timeoutMs)
|
|
|
|
),
|
|
|
|
)
|
2023-07-31 07:52:04 +00:00
|
|
|
if connRes.isErr():
|
|
|
|
let msg = $connRes.error
|
2024-02-13 14:22:22 +00:00
|
|
|
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
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
|
2023-05-12 16:08:41 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc waku_store_query(
|
|
|
|
ctx: ptr Context,
|
|
|
|
queryJson: cstring,
|
|
|
|
peerId: cstring,
|
|
|
|
timeoutMs: cint,
|
|
|
|
callback: WakuCallBack,
|
|
|
|
userData: pointer,
|
|
|
|
): cint {.dynlib, exportc.} =
|
2024-02-07 14:24:03 +00:00
|
|
|
ctx[].userData = userData
|
2023-12-11 07:49:13 +00:00
|
|
|
|
|
|
|
## TODO: implement the logic that make the "self" node to act as a Store client
|
|
|
|
|
|
|
|
# if sendReqRes.isErr():
|
|
|
|
# let msg = $sendReqRes.error
|
|
|
|
# callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)))
|
|
|
|
# return RET_ERR
|
|
|
|
|
|
|
|
return RET_OK
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc waku_listen_addresses(
|
|
|
|
ctx: ptr Context, callback: WakuCallBack, userData: pointer
|
|
|
|
): cint {.dynlib, exportc.} =
|
2024-02-21 11:06:05 +00:00
|
|
|
ctx[].userData = userData
|
|
|
|
|
|
|
|
let connRes = waku_thread.sendRequestToWakuThread(
|
2024-03-15 23:08:47 +00:00
|
|
|
ctx,
|
|
|
|
RequestType.DEBUG,
|
|
|
|
DebugNodeRequest.createShared(DebugNodeMsgType.RETRIEVE_LISTENING_ADDRESSES),
|
|
|
|
)
|
2024-02-21 11:06:05 +00:00
|
|
|
if connRes.isErr():
|
|
|
|
let msg = $connRes.error
|
|
|
|
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
|
|
|
return RET_ERR
|
|
|
|
else:
|
|
|
|
let msg = $connRes.value
|
|
|
|
callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
|
|
|
return RET_OK
|
|
|
|
|
2024-05-21 16:37:50 +00:00
|
|
|
proc waku_dns_discovery(
|
|
|
|
ctx: ptr Context,
|
|
|
|
entTreeUrl: cstring,
|
|
|
|
nameDnsServer: cstring,
|
|
|
|
timeoutMs: cint,
|
|
|
|
callback: WakuCallBack,
|
|
|
|
userData: pointer,
|
|
|
|
): cint {.dynlib, exportc.} =
|
|
|
|
ctx[].userData = userData
|
|
|
|
|
|
|
|
let bootstrapPeers = waku_thread.sendRequestToWakuThread(
|
|
|
|
ctx,
|
|
|
|
RequestType.DISCOVERY,
|
|
|
|
DiscoveryRequest.createRetrieveBootstrapNodesRequest(
|
|
|
|
DiscoveryMsgType.GET_BOOTSTRAP_NODES, entTreeUrl, nameDnsServer, timeoutMs
|
|
|
|
),
|
|
|
|
).valueOr:
|
|
|
|
let msg = $error
|
|
|
|
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
|
|
|
return RET_ERR
|
|
|
|
|
|
|
|
let msg = $bootstrapPeers
|
|
|
|
callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
|
|
|
return RET_OK
|
|
|
|
|
|
|
|
proc waku_discv5_update_bootnodes(
|
|
|
|
ctx: ptr Context, bootnodes: cstring, callback: WakuCallBack, userData: pointer
|
|
|
|
): cint {.dynlib, exportc.} =
|
|
|
|
## Updates the bootnode list used for discovering new peers via DiscoveryV5
|
|
|
|
## bootnodes - JSON array containing the bootnode ENRs i.e. `["enr:...", "enr:..."]`
|
|
|
|
ctx[].userData = userData
|
|
|
|
|
|
|
|
let resp = waku_thread.sendRequestToWakuThread(
|
|
|
|
ctx,
|
|
|
|
RequestType.DISCOVERY,
|
|
|
|
DiscoveryRequest.createUpdateBootstrapNodesRequest(
|
|
|
|
DiscoveryMsgType.UPDATE_DISCV5_BOOTSTRAP_NODES, bootnodes
|
|
|
|
),
|
|
|
|
).valueOr:
|
|
|
|
let msg = $error
|
|
|
|
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
|
|
|
return RET_ERR
|
|
|
|
|
|
|
|
let msg = $resp
|
|
|
|
callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
|
|
|
return RET_OK
|
|
|
|
|
2023-05-12 16:08:41 +00:00
|
|
|
### End of exported procs
|
2023-06-29 19:59:53 +00:00
|
|
|
################################################################################
|