Skip to content

Commit

Permalink
examples/nodejs: adapting code to latest callback and ctx/userData de…
Browse files Browse the repository at this point in the history
…finitions
  • Loading branch information
Ivansete-status committed Dec 18, 2023
1 parent 574cdf5 commit 889c43a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
5 changes: 4 additions & 1 deletion examples/nodejs/waku.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@ function event_handler(event) {
console.log("evento NodeJs: " + event)
}

wakuMod.wakuNew(cfg)

wakuMod.wakuVersion(function(msg){ console.log("Waku Version: " + msg) })

// Example on how to retrieve a value from the waku library
var defaultPubsubTopic = ""
wakuMod.wakuDefaultPubsubTopic(function(msg){ defaultPubsubTopic = msg })

console.log("Default pubsub topic: " + defaultPubsubTopic)

console.log("Setting callback event callback function")
wakuMod.wakuSetEventCallback(event_handler)
wakuMod.wakuNew(cfg)

wakuMod.wakuStart()

Expand Down
44 changes: 28 additions & 16 deletions examples/nodejs/waku_addon.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ do { \
} \
} while (0)

// libwaku Context
void* ctx;

// For the case of C language we don't need to store a particular userData
void* userData = NULL;

static napi_env my_env;

// This function is responsible for converting data coming in from the worker
Expand Down Expand Up @@ -80,7 +86,7 @@ static void CallJs(napi_env env, napi_value js_cb, void* context, void* data) {
free(data);
}

void handle_waku_version(const char* msg, size_t len) {
void handle_waku_version(int callerRet, const char* msg, size_t len) {
if (ref_version_callback == NULL) {
napi_throw_type_error(my_env, NULL, "ERROR in event_handler. ref_version_callback == NULL");
}
Expand All @@ -106,7 +112,7 @@ void handle_waku_version(const char* msg, size_t len) {

// This function is directly passed as a callback to the libwaku and it
// calls a NodeJs function if it has been set.
void event_handler(const char* msg, size_t len) {
void event_handler(int callerRet, const char* msg, size_t len) {
if (thsafe_fn == NULL) {
// if (ref_event_callback == NULL) {
napi_throw_type_error(my_env, NULL, "ERROR in event_handler. ref_event_callback == NULL");
Expand All @@ -118,7 +124,7 @@ void event_handler(const char* msg, size_t len) {
NAPI_CALL(napi_call_threadsafe_function(thsafe_fn, allocated_msg, napi_tsfn_nonblocking));
}

void handle_error(const char* msg, size_t len) {
void handle_error(int callerRet, const char* msg, size_t len) {
if (ref_on_error_callback == NULL) {
napi_throw_type_error(my_env, NULL, "ERROR in event_handler. ref_on_error_callback == NULL");
}
Expand All @@ -139,7 +145,7 @@ void handle_error(const char* msg, size_t len) {
}

char* contentTopic = NULL;
void handle_content_topic(const char* msg, size_t len) {
void handle_content_topic(int callerRet, const char* msg, size_t len) {
if (contentTopic != NULL) {
free(contentTopic);
}
Expand All @@ -148,7 +154,7 @@ void handle_content_topic(const char* msg, size_t len) {
strcpy(contentTopic, msg);
}

void handle_default_pubsub_topic(const char* msg, size_t len) {
void handle_default_pubsub_topic(int callerRet, const char* msg, size_t len) {
if (ref_def_pubsub_topic_callback == NULL) {
napi_throw_type_error(my_env, NULL,
"ERROR in event_handler. ref_def_pubsub_topic_callback == NULL");
Expand Down Expand Up @@ -186,6 +192,8 @@ static napi_value WakuNew(napi_env env, napi_callback_info info) {
return NULL;
}

ctx = waku_init(event_handler, userData);

size_t str_size;
size_t str_size_read;
napi_get_value_string_utf8(env, args[0], NULL, 0, &str_size);
Expand All @@ -194,7 +202,7 @@ static napi_value WakuNew(napi_env env, napi_callback_info info) {
str_size = str_size + 1;
napi_get_value_string_utf8(env, args[0], jsonConfig, str_size, &str_size_read);

WAKU_CALL( waku_new(jsonConfig, event_handler) );
WAKU_CALL( waku_new(&ctx, jsonConfig, event_handler, userData) );

free(jsonConfig);

Expand Down Expand Up @@ -228,7 +236,7 @@ static napi_value WakuVersion(napi_env env, napi_callback_info info) {

NAPI_CALL(napi_create_reference(env, cb, 1, &ref_version_callback));

WAKU_CALL( waku_version( handle_waku_version ) );
WAKU_CALL( waku_version(&ctx, handle_waku_version, userData) );

return NULL;
}
Expand Down Expand Up @@ -278,13 +286,13 @@ static napi_value WakuSetEventCallback(napi_env env, napi_callback_info info) {

// Inside 'event_handler', the event will be dispatched to the NodeJs
// if there is a proper napi_function (ref_event_callback) being set.
waku_set_event_callback(event_handler);
waku_set_event_callback(event_handler, userData);

return NULL;
}

static napi_value WakuStart(napi_env env, napi_callback_info info) {
waku_start();
waku_start(&ctx, event_handler, userData);
return NULL;
}

Expand Down Expand Up @@ -341,7 +349,7 @@ static napi_value WakuConnect(napi_env env, napi_callback_info info) {
my_env = env;
NAPI_CALL(napi_create_reference(env, cb, 1, &ref_on_error_callback));

WAKU_CALL(waku_connect(peers, timeoutMs, handle_error));
WAKU_CALL(waku_connect(&ctx, peers, timeoutMs, handle_error, userData));

// Free allocated memory
free(peers);
Expand Down Expand Up @@ -412,11 +420,13 @@ static napi_value WakuRelayPublish(napi_env env, napi_callback_info info) {
char *msgPayload = b64_encode((unsigned char*) msg, strlen(msg));

// TODO: move all the 'waku_content_topic' logic inside the libwaku
WAKU_CALL( waku_content_topic("appName",
WAKU_CALL( waku_content_topic(&ctx,
"appName",
1,
content_topic_name,
"encoding",
handle_content_topic) );
handle_content_topic,
userData) );
snprintf(jsonWakuMsg,
1024,
"{\"payload\":\"%s\",\"content_topic\":\"%s\"}",
Expand Down Expand Up @@ -449,10 +459,12 @@ static napi_value WakuRelayPublish(napi_env env, napi_callback_info info) {
NAPI_CALL(napi_create_reference(env, cb, 1, &ref_on_error_callback));

// Perform the actual 'publish'
WAKU_CALL( waku_relay_publish(pubsub_topic,
WAKU_CALL( waku_relay_publish(&ctx,
pubsub_topic,
jsonWakuMsg,
timeoutMs,
handle_error) );
handle_error,
userData) );
free(pubsub_topic);
free(content_topic_name);

Expand Down Expand Up @@ -486,7 +498,7 @@ static napi_value WakuDefaultPubsubTopic(napi_env env, napi_callback_info info)

NAPI_CALL(napi_create_reference(env, cb, 1, &ref_def_pubsub_topic_callback));

WAKU_CALL( waku_default_pubsub_topic(handle_default_pubsub_topic) );
WAKU_CALL( waku_default_pubsub_topic(&ctx, handle_default_pubsub_topic, userData) );

return NULL;
}
Expand Down Expand Up @@ -533,7 +545,7 @@ static napi_value WakuRelaySubscribe(napi_env env, napi_callback_info info) {
NAPI_CALL(napi_create_reference(env, cb, 1, &ref_on_error_callback));

// Calling the actual 'subscribe' waku function
WAKU_CALL( waku_relay_subscribe(pubsub_topic, handle_error) );
WAKU_CALL( waku_relay_subscribe(&ctx, pubsub_topic, handle_error, userData) );

free(pubsub_topic);

Expand Down

0 comments on commit 889c43a

Please sign in to comment.