2026-03-03 19:17:54 +01:00
|
|
|
import std/[json, strutils]
|
|
|
|
|
import chronos, chronicles, results, confutils, confutils/std/net, ffi
|
2026-02-17 10:38:35 +01:00
|
|
|
import
|
|
|
|
|
waku/factory/waku,
|
|
|
|
|
waku/node/waku_node,
|
2026-03-03 19:17:54 +01:00
|
|
|
waku/api/[api, types],
|
2026-02-26 17:55:31 +01:00
|
|
|
waku/events/[message_events, health_events],
|
2026-03-03 19:17:54 +01:00
|
|
|
tools/confutils/cli_args,
|
2026-02-17 10:38:35 +01:00
|
|
|
../declare_lib,
|
|
|
|
|
../json_event
|
|
|
|
|
|
|
|
|
|
# Add JSON serialization for RequestId
|
|
|
|
|
proc `%`*(id: RequestId): JsonNode =
|
|
|
|
|
%($id)
|
|
|
|
|
|
|
|
|
|
registerReqFFI(CreateNodeRequest, ctx: ptr FFIContext[Waku]):
|
|
|
|
|
proc(configJson: cstring): Future[Result[string, string]] {.async.} =
|
2026-03-03 19:17:54 +01:00
|
|
|
## Parse the JSON configuration using fieldPairs approach (WakuNodeConf)
|
|
|
|
|
var conf = defaultWakuNodeConf().valueOr:
|
|
|
|
|
return err("Failed creating default conf: " & error)
|
|
|
|
|
|
|
|
|
|
var jsonNode: JsonNode
|
|
|
|
|
try:
|
|
|
|
|
jsonNode = parseJson($configJson)
|
|
|
|
|
except Exception:
|
|
|
|
|
return err(
|
|
|
|
|
"Failed to parse config JSON: " & getCurrentExceptionMsg() &
|
|
|
|
|
" configJson string: " & $configJson
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for confField, confValue in fieldPairs(conf):
|
|
|
|
|
if jsonNode.contains(confField):
|
|
|
|
|
let formattedString = ($jsonNode[confField]).strip(chars = {'\"'})
|
|
|
|
|
try:
|
|
|
|
|
confValue = parseCmdArg(typeof(confValue), formattedString)
|
|
|
|
|
except Exception:
|
|
|
|
|
return err(
|
|
|
|
|
"Failed to parse field '" & confField & "': " &
|
|
|
|
|
getCurrentExceptionMsg() & ". Value: " & formattedString
|
|
|
|
|
)
|
2026-02-17 10:38:35 +01:00
|
|
|
|
|
|
|
|
# Create the node
|
2026-03-03 19:17:54 +01:00
|
|
|
ctx.myLib[] = (await api.createNode(conf)).valueOr:
|
2026-02-17 10:38:35 +01:00
|
|
|
let errMsg = $error
|
|
|
|
|
chronicles.error "CreateNodeRequest failed", err = errMsg
|
|
|
|
|
return err(errMsg)
|
|
|
|
|
|
|
|
|
|
return ok("")
|
|
|
|
|
|
2026-03-02 18:56:39 +01:00
|
|
|
proc logosdelivery_destroy(
|
|
|
|
|
ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer
|
|
|
|
|
): cint {.dynlib, exportc, cdecl.} =
|
|
|
|
|
initializeLibrary()
|
|
|
|
|
checkParams(ctx, callback, userData)
|
|
|
|
|
|
|
|
|
|
ffi.destroyFFIContext(ctx).isOkOr:
|
|
|
|
|
let msg = "liblogosdelivery error: " & $error
|
|
|
|
|
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
|
|
|
|
return RET_ERR
|
|
|
|
|
|
|
|
|
|
## always need to invoke the callback although we don't retrieve value to the caller
|
|
|
|
|
callback(RET_OK, nil, 0, userData)
|
|
|
|
|
|
|
|
|
|
return RET_OK
|
|
|
|
|
|
2026-02-17 10:38:35 +01:00
|
|
|
proc logosdelivery_create_node(
|
|
|
|
|
configJson: cstring, callback: FFICallback, userData: pointer
|
|
|
|
|
): pointer {.dynlib, exportc, cdecl.} =
|
|
|
|
|
initializeLibrary()
|
|
|
|
|
|
|
|
|
|
if isNil(callback):
|
|
|
|
|
echo "error: missing callback in logosdelivery_create_node"
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
|
|
var ctx = ffi.createFFIContext[Waku]().valueOr:
|
|
|
|
|
let msg = "Error in createFFIContext: " & $error
|
|
|
|
|
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
|
|
ctx.userData = userData
|
|
|
|
|
|
|
|
|
|
ffi.sendRequestToFFIThread(
|
|
|
|
|
ctx, CreateNodeRequest.ffiNewReq(callback, userData, configJson)
|
|
|
|
|
).isOkOr:
|
|
|
|
|
let msg = "error in sendRequestToFFIThread: " & $error
|
|
|
|
|
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
2026-03-02 18:56:39 +01:00
|
|
|
# free allocated resources as they won't be available
|
|
|
|
|
ffi.destroyFFIContext(ctx).isOkOr:
|
|
|
|
|
chronicles.error "Error in destroyFFIContext after sendRequestToFFIThread during creation", err = $error
|
2026-02-17 10:38:35 +01:00
|
|
|
return nil
|
|
|
|
|
|
|
|
|
|
return ctx
|
|
|
|
|
|
|
|
|
|
proc logosdelivery_start_node(
|
|
|
|
|
ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer
|
|
|
|
|
) {.ffi.} =
|
|
|
|
|
requireInitializedNode(ctx, "START_NODE"):
|
|
|
|
|
return err(errMsg)
|
|
|
|
|
|
|
|
|
|
# setting up outgoing event listeners
|
|
|
|
|
let sentListener = MessageSentEvent.listen(
|
|
|
|
|
ctx.myLib[].brokerCtx,
|
|
|
|
|
proc(event: MessageSentEvent) {.async: (raises: []).} =
|
|
|
|
|
callEventCallback(ctx, "onMessageSent"):
|
|
|
|
|
$newJsonEvent("message_sent", event),
|
|
|
|
|
).valueOr:
|
|
|
|
|
chronicles.error "MessageSentEvent.listen failed", err = $error
|
|
|
|
|
return err("MessageSentEvent.listen failed: " & $error)
|
|
|
|
|
|
|
|
|
|
let errorListener = MessageErrorEvent.listen(
|
|
|
|
|
ctx.myLib[].brokerCtx,
|
|
|
|
|
proc(event: MessageErrorEvent) {.async: (raises: []).} =
|
|
|
|
|
callEventCallback(ctx, "onMessageError"):
|
|
|
|
|
$newJsonEvent("message_error", event),
|
|
|
|
|
).valueOr:
|
|
|
|
|
chronicles.error "MessageErrorEvent.listen failed", err = $error
|
|
|
|
|
return err("MessageErrorEvent.listen failed: " & $error)
|
|
|
|
|
|
|
|
|
|
let propagatedListener = MessagePropagatedEvent.listen(
|
|
|
|
|
ctx.myLib[].brokerCtx,
|
|
|
|
|
proc(event: MessagePropagatedEvent) {.async: (raises: []).} =
|
|
|
|
|
callEventCallback(ctx, "onMessagePropagated"):
|
|
|
|
|
$newJsonEvent("message_propagated", event),
|
|
|
|
|
).valueOr:
|
|
|
|
|
chronicles.error "MessagePropagatedEvent.listen failed", err = $error
|
|
|
|
|
return err("MessagePropagatedEvent.listen failed: " & $error)
|
|
|
|
|
|
2026-02-26 17:55:31 +01:00
|
|
|
let ConnectionStatusChangeListener = EventConnectionStatusChange.listen(
|
|
|
|
|
ctx.myLib[].brokerCtx,
|
|
|
|
|
proc(event: EventConnectionStatusChange) {.async: (raises: []).} =
|
|
|
|
|
callEventCallback(ctx, "onConnectionStatusChange"):
|
|
|
|
|
$newJsonEvent("connection_status_change", event),
|
|
|
|
|
).valueOr:
|
|
|
|
|
chronicles.error "ConnectionStatusChange.listen failed", err = $error
|
|
|
|
|
return err("ConnectionStatusChange.listen failed: " & $error)
|
|
|
|
|
|
2026-02-17 10:38:35 +01:00
|
|
|
(await startWaku(addr ctx.myLib[])).isOkOr:
|
|
|
|
|
let errMsg = $error
|
|
|
|
|
chronicles.error "START_NODE failed", err = errMsg
|
|
|
|
|
return err("failed to start: " & errMsg)
|
|
|
|
|
return ok("")
|
|
|
|
|
|
|
|
|
|
proc logosdelivery_stop_node(
|
|
|
|
|
ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer
|
|
|
|
|
) {.ffi.} =
|
|
|
|
|
requireInitializedNode(ctx, "STOP_NODE"):
|
|
|
|
|
return err(errMsg)
|
|
|
|
|
|
|
|
|
|
MessageErrorEvent.dropAllListeners(ctx.myLib[].brokerCtx)
|
|
|
|
|
MessageSentEvent.dropAllListeners(ctx.myLib[].brokerCtx)
|
|
|
|
|
MessagePropagatedEvent.dropAllListeners(ctx.myLib[].brokerCtx)
|
2026-02-26 17:55:31 +01:00
|
|
|
EventConnectionStatusChange.dropAllListeners(ctx.myLib[].brokerCtx)
|
2026-02-17 10:38:35 +01:00
|
|
|
|
|
|
|
|
(await ctx.myLib[].stop()).isOkOr:
|
|
|
|
|
let errMsg = $error
|
|
|
|
|
chronicles.error "STOP_NODE failed", err = errMsg
|
|
|
|
|
return err("failed to stop: " & errMsg)
|
|
|
|
|
return ok("")
|