From 701cb7032bcf4125fc7b56ed9cef81e17b8d738d Mon Sep 17 00:00:00 2001 From: Ivan FB <128452529+Ivansete-status@users.noreply.github.com> Date: Fri, 15 Dec 2023 13:32:12 +0100 Subject: [PATCH] libwaku: avoid using waku_init. Only use waku_new to create node and context (#2282) --- examples/cbindings/waku_example.c | 5 ++--- library/libwaku.h | 7 ++----- library/libwaku.nim | 32 +++++++++++++------------------ 3 files changed, 17 insertions(+), 27 deletions(-) diff --git a/examples/cbindings/waku_example.c b/examples/cbindings/waku_example.c index dcb1d74d9..49942235b 100644 --- a/examples/cbindings/waku_example.c +++ b/examples/cbindings/waku_example.c @@ -267,8 +267,6 @@ int main(int argc, char** argv) { show_help_and_exit(); } - ctx = waku_init(event_handler, userData); - char jsonConfig[2048]; snprintf(jsonConfig, 2048, "{ \ \"host\": \"%s\", \ @@ -288,13 +286,14 @@ int main(int argc, char** argv) { cfgNode.storeRetentionPolicy, cfgNode.storeMaxNumDbConnections); + ctx = waku_new(jsonConfig, event_handler, userData); + WAKU_CALL( waku_default_pubsub_topic(&ctx, print_default_pubsub_topic, userData) ); WAKU_CALL( waku_version(&ctx, print_waku_version, userData) ); printf("Bind addr: %s:%u\n", cfgNode.host, cfgNode.port); printf("Waku Relay enabled: %s\n", cfgNode.relay == 1 ? "YES": "NO"); - WAKU_CALL( waku_new(&ctx, jsonConfig, event_handler, userData) ); waku_set_event_callback(event_handler, userData); waku_start(&ctx, event_handler, userData); diff --git a/library/libwaku.h b/library/libwaku.h index bbd7f3515..28e65400b 100644 --- a/library/libwaku.h +++ b/library/libwaku.h @@ -17,13 +17,10 @@ extern "C" { typedef void (*WakuCallBack) (int callerRet, const char* msg, size_t len); -// Initializes the waku library and returns a pointer to the Context. -void* waku_init(WakuCallBack callback, - void* userData); - // Creates a new instance of the waku node. // Sets up the waku node from the given configuration. -int waku_new(void* ctx, +// Returns a pointer to the Context needed by the rest of the API functions. +void* waku_new( const char* configJson, WakuCallBack callback, void* userData); diff --git a/library/libwaku.nim b/library/libwaku.nim index 3780073c1..721e4f44e 100644 --- a/library/libwaku.nim +++ b/library/libwaku.nim @@ -63,8 +63,15 @@ proc relayEventCallback(pubsubTopic: PubsubTopic, ################################################################################ ### Exported procs -proc waku_init(callback: WakuCallback): pointer {.dynlib, exportc, cdecl.} = - ## Initializes the waku library. +proc waku_new(configJson: cstring, + callback: WakuCallback, + userData: pointer): pointer + {.dynlib, exportc, cdecl.} = + ## Creates a new instance of the WakuNode. + + if isNil(callback): + echo "error: missing callback in waku_new" + return nil ## Create the Waku thread that will keep waiting for req from the main thread. var ctx = waku_thread.createWakuThread().valueOr: @@ -72,23 +79,10 @@ proc waku_init(callback: WakuCallback): pointer {.dynlib, exportc, cdecl.} = callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg))) return nil - return ctx - -proc waku_new(ctx: ptr ptr Context, - configJson: cstring, - callback: WakuCallback, - userData: pointer): cint - {.dynlib, exportc, cdecl.} = - ## Creates a new instance of the WakuNode. - ## Notice that the ConfigNode type is also exported and available for users. - - ctx[][].userData = userData - - if isNil(callback): - return RET_MISSING_CALLBACK + ctx.userData = userData let sendReqRes = waku_thread.sendRequestToWakuThread( - ctx[], + ctx, RequestType.LIFECYCLE, NodeLifecycleRequest.createShared( NodeLifecycleMsgType.CREATE_NODE, @@ -96,9 +90,9 @@ proc waku_new(ctx: ptr ptr Context, if sendReqRes.isErr(): let msg = $sendReqRes.error callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg))) - return RET_ERR + return nil - return RET_OK + return ctx proc waku_version(ctx: ptr ptr Context, callback: WakuCallBack,