2025-12-19 17:00:43 +01:00
|
|
|
import std/[atomics, options, atomics, macros]
|
|
|
|
|
import chronicles, chronos, chronos/threadsync, ffi
|
2023-05-12 18:08:41 +02:00
|
|
|
import
|
2024-07-09 13:14:28 +02:00
|
|
|
waku/waku_core/message/message,
|
|
|
|
|
waku/waku_core/topics/pubsub_topic,
|
2024-12-24 11:47:38 +01:00
|
|
|
waku/waku_relay,
|
2025-06-24 23:20:08 +02:00
|
|
|
./events/json_message_event,
|
2025-12-19 17:00:43 +01:00
|
|
|
./events/json_topic_health_change_event,
|
|
|
|
|
./events/json_connection_change_event,
|
|
|
|
|
../waku/factory/app_callbacks,
|
|
|
|
|
waku/factory/waku,
|
|
|
|
|
waku/node/waku_node,
|
|
|
|
|
./declare_lib
|
2024-12-02 10:56:12 -04:00
|
|
|
|
2024-05-21 21:00:22 -04:00
|
|
|
################################################################################
|
2025-12-19 17:00:43 +01:00
|
|
|
## Include different APIs, i.e. all procs with {.ffi.} pragma
|
|
|
|
|
include
|
|
|
|
|
./kernel_api/peer_manager_api,
|
|
|
|
|
./kernel_api/discovery_api,
|
|
|
|
|
./kernel_api/node_lifecycle_api,
|
|
|
|
|
./kernel_api/debug_node_api,
|
|
|
|
|
./kernel_api/ping_api,
|
|
|
|
|
./kernel_api/protocols/relay_api,
|
|
|
|
|
./kernel_api/protocols/store_api,
|
|
|
|
|
./kernel_api/protocols/lightpush_api,
|
|
|
|
|
./kernel_api/protocols/filter_api
|
2024-05-21 21:00:22 -04:00
|
|
|
|
2023-05-12 18:08:41 +02:00
|
|
|
################################################################################
|
|
|
|
|
### Exported procs
|
|
|
|
|
|
2024-03-16 00:08:47 +01:00
|
|
|
proc waku_new(
|
2025-12-19 17:00:43 +01:00
|
|
|
configJson: cstring, callback: FFICallback, userData: pointer
|
2024-03-16 00:08:47 +01:00
|
|
|
): pointer {.dynlib, exportc, cdecl.} =
|
2024-12-02 10:56:12 -04:00
|
|
|
initializeLibrary()
|
|
|
|
|
|
2023-12-15 13:32:12 +01:00
|
|
|
## Creates a new instance of the WakuNode.
|
|
|
|
|
if isNil(callback):
|
|
|
|
|
echo "error: missing callback in waku_new"
|
|
|
|
|
return nil
|
2023-07-07 10:53:00 +02:00
|
|
|
|
2023-09-01 08:37:02 +02:00
|
|
|
## Create the Waku thread that will keep waiting for req from the main thread.
|
2025-12-19 17:00:43 +01:00
|
|
|
var ctx = ffi.createFFIContext[Waku]().valueOr:
|
|
|
|
|
let msg = "Error in createFFIContext: " & $error
|
2024-12-02 10:56:12 -04:00
|
|
|
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
2023-10-23 08:37:28 +02:00
|
|
|
return nil
|
|
|
|
|
|
2023-12-15 13:32:12 +01:00
|
|
|
ctx.userData = userData
|
2023-09-01 08:37:02 +02:00
|
|
|
|
2025-12-19 17:00:43 +01:00
|
|
|
proc onReceivedMessage(ctx: ptr FFIContext): WakuRelayHandler =
|
|
|
|
|
return proc(pubsubTopic: PubsubTopic, msg: WakuMessage) {.async.} =
|
|
|
|
|
callEventCallback(ctx, "onReceivedMessage"):
|
|
|
|
|
$JsonMessageEvent.new(pubsubTopic, msg)
|
|
|
|
|
|
|
|
|
|
proc onTopicHealthChange(ctx: ptr FFIContext): TopicHealthChangeHandler =
|
|
|
|
|
return proc(pubsubTopic: PubsubTopic, topicHealth: TopicHealth) {.async.} =
|
|
|
|
|
callEventCallback(ctx, "onTopicHealthChange"):
|
|
|
|
|
$JsonTopicHealthChangeEvent.new(pubsubTopic, topicHealth)
|
|
|
|
|
|
|
|
|
|
proc onConnectionChange(ctx: ptr FFIContext): ConnectionChangeHandler =
|
|
|
|
|
return proc(peerId: PeerId, peerEvent: PeerEventKind) {.async.} =
|
|
|
|
|
callEventCallback(ctx, "onConnectionChange"):
|
|
|
|
|
$JsonConnectionChangeEvent.new($peerId, peerEvent)
|
|
|
|
|
|
2024-12-24 11:47:38 +01:00
|
|
|
let appCallbacks = AppCallbacks(
|
|
|
|
|
relayHandler: onReceivedMessage(ctx),
|
|
|
|
|
topicHealthChangeHandler: onTopicHealthChange(ctx),
|
2025-01-08 18:53:00 +01:00
|
|
|
connectionChangeHandler: onConnectionChange(ctx),
|
2024-12-24 11:47:38 +01:00
|
|
|
)
|
2024-12-13 17:38:16 +01:00
|
|
|
|
2025-12-19 17:00:43 +01:00
|
|
|
ffi.sendRequestToFFIThread(
|
|
|
|
|
ctx, CreateNodeRequest.ffiNewReq(callback, userData, configJson, appCallbacks)
|
|
|
|
|
).isOkOr:
|
|
|
|
|
let msg = "error in sendRequestToFFIThread: " & $error
|
|
|
|
|
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
2024-12-02 10:56:12 -04:00
|
|
|
return nil
|
2023-05-12 18:08:41 +02:00
|
|
|
|
2023-12-15 13:32:12 +01:00
|
|
|
return ctx
|
2023-07-07 10:53:00 +02:00
|
|
|
|
2024-03-16 00:08:47 +01:00
|
|
|
proc waku_destroy(
|
2025-12-19 17:00:43 +01:00
|
|
|
ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer
|
|
|
|
|
): cint {.dynlib, exportc, cdecl.} =
|
2024-12-02 10:56:12 -04:00
|
|
|
initializeLibrary()
|
2025-12-19 17:00:43 +01:00
|
|
|
checkParams(ctx, callback, userData)
|
2024-03-07 13:53:03 -04:00
|
|
|
|
2025-12-19 17:00:43 +01:00
|
|
|
ffi.destroyFFIContext(ctx).isOkOr:
|
2024-12-02 10:56:12 -04:00
|
|
|
let msg = "libwaku error: " & $error
|
|
|
|
|
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
|
|
|
|
return RET_ERR
|
|
|
|
|
|
2025-01-03 16:13:26 +01:00
|
|
|
## always need to invoke the callback although we don't retrieve value to the caller
|
|
|
|
|
callback(RET_OK, nil, 0, userData)
|
|
|
|
|
|
2024-12-02 10:56:12 -04:00
|
|
|
return RET_OK
|
2024-03-07 13:53:03 -04:00
|
|
|
|
2025-12-19 17:00:43 +01:00
|
|
|
# ### End of exported procs
|
|
|
|
|
# ################################################################################
|