libwaku: avoid using waku_init. Only use waku_new to create node and context (#2282)

This commit is contained in:
Ivan FB 2023-12-15 13:32:12 +01:00 committed by GitHub
parent 5847f49d3d
commit 701cb7032b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 27 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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,