Skip to content

Commit

Permalink
libwaku: simpler ctx mgmt. Param now receiving void* instead of void**
Browse files Browse the repository at this point in the history
This change is needed to that the interoperability with other languages
become simpler. Particularly, this simplification is needed from the
Python point of view, where it is tricky to pass a void** as a parameter
to a FFI function.
  • Loading branch information
Ivansete-status committed Feb 6, 2024
1 parent e4e147b commit f1d14e9
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 77 deletions.
10 changes: 5 additions & 5 deletions examples/cbindings/waku_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,24 +288,24 @@ int main(int argc, char** argv) {

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) );
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_set_event_callback(event_handler, userData);
waku_start(&ctx, event_handler, userData);
waku_start(ctx, event_handler, userData);

printf("Establishing connection with: %s\n", cfgNode.peers);

WAKU_CALL( waku_connect(&ctx,
WAKU_CALL( waku_connect(ctx,
cfgNode.peers,
10000 /* timeoutMs */,
event_handler,
userData) );

WAKU_CALL( waku_relay_subscribe(&ctx,
WAKU_CALL( waku_relay_subscribe(ctx,
"/waku/2/default-waku/proto",
event_handler,
userData) );
Expand Down
14 changes: 7 additions & 7 deletions examples/nodejs/waku_addon.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,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(&ctx, handle_waku_version, userData) );
WAKU_CALL( waku_version(ctx, handle_waku_version, userData) );

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

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

Expand Down Expand Up @@ -347,7 +347,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(&ctx, peers, timeoutMs, handle_error, userData));
WAKU_CALL(waku_connect(ctx, peers, timeoutMs, handle_error, userData));

// Free allocated memory
free(peers);
Expand Down Expand Up @@ -418,7 +418,7 @@ 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(&ctx,
WAKU_CALL( waku_content_topic(ctx,
"appName",
1,
content_topic_name,
Expand Down Expand Up @@ -457,7 +457,7 @@ 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(&ctx,
WAKU_CALL( waku_relay_publish(ctx,
pubsub_topic,
jsonWakuMsg,
timeoutMs,
Expand Down Expand Up @@ -496,7 +496,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(&ctx, handle_default_pubsub_topic, userData) );
WAKU_CALL( waku_default_pubsub_topic(ctx, handle_default_pubsub_topic, userData) );

return NULL;
}
Expand Down Expand Up @@ -543,7 +543,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(&ctx, pubsub_topic, handle_error, userData) );
WAKU_CALL( waku_relay_subscribe(ctx, pubsub_topic, handle_error, userData) );

free(pubsub_topic);

Expand Down
110 changes: 73 additions & 37 deletions examples/python/waku.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
""")
exit(-1)

def handle_event(event):
print("Event received: {}".format(event))
def handle_event(ret, msg, user_data):
print("Event received: %s" % msg)

def call_waku(func):
ret = func()
Expand Down Expand Up @@ -55,57 +55,93 @@ def call_waku(func):
args.key,
"true" if args.relay else "false")

callback_type = ctypes.CFUNCTYPE(None, ctypes.c_char_p, ctypes.c_size_t)
callback_type = ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_char_p, ctypes.c_size_t)

# Node creation
libwaku.waku_new.restype = ctypes.c_void_p
libwaku.waku_new.argtypes = [ctypes.c_char_p,
callback_type,
ctypes.c_void_p]

ctx = libwaku.waku_new(bytes(json_config, 'utf-8'),
callback_type(
#onErrCb
lambda ret, msg, len:
print("Error calling waku_new: %s",
msg.decode('utf-8'))
),
ctypes.c_void_p(0))

# Retrieve the current version of the library
libwaku.waku_version(callback_type(lambda msg, len:
libwaku.waku_version.argtypes = [ctypes.c_void_p,
callback_type,
ctypes.c_void_p]
libwaku.waku_version(ctx,
callback_type(lambda ret, msg, len:
print("Git Version: %s" %
msg.decode('utf-8'))))
msg.decode('utf-8'))),
ctypes.c_void_p(0))

# Retrieve the default pubsub topic
default_pubsub_topic = ""
libwaku.waku_default_pubsub_topic(callback_type(
lambda msg, len: (
globals().update(default_pubsub_topic = msg.decode('utf-8')),
print("Default pubsub topic: %s" % msg.decode('utf-8')))
))
libwaku.waku_default_pubsub_topic.argtypes = [ctypes.c_void_p,
callback_type,
ctypes.c_void_p]
libwaku.waku_default_pubsub_topic(ctx,
callback_type(
lambda ret, msg, len: (
globals().update(default_pubsub_topic = msg.decode('utf-8')),
print("Default pubsub topic: %s" % msg.decode('utf-8')))
),
ctypes.c_void_p(0))

print("Bind addr: {}:{}".format(args.host, args.port))
print("Waku Relay enabled: {}".format(args.relay))

# Node creation
libwaku.waku_new.argtypes = [ctypes.c_char_p,
callback_type]
# Set the event callback
callback = callback_type(handle_event) # This line is important so that the callback is not gc'ed

libwaku.waku_new(bytes(json_config, 'utf-8'),
callback_type(
#onErrCb
lambda msg, len:
print("Error calling waku_new: %s",
msg.decode('utf-8'))
))
# Start the node
libwaku.waku_start()
libwaku.waku_set_event_callback.argtypes = [callback_type, ctypes.c_void_p]
libwaku.waku_set_event_callback(callback, ctypes.c_void_p(0))

# Set the event callback
callback_type = ctypes.CFUNCTYPE(None, ctypes.c_char_p)
callback = callback_type(handle_event)
libwaku.waku_set_event_callback(callback)
# Start the node
libwaku.waku_start.argtypes = [ctypes.c_void_p,
callback_type,
ctypes.c_void_p]
libwaku.waku_start(ctx,
callback_type(lambda ret, msg, len:
print("Error in waku_start: %s" %
msg.decode('utf-8'))),
ctypes.c_void_p(0))

# Subscribe to the default pubsub topic
libwaku.waku_relay_subscribe(default_pubsub_topic.encode('utf-8'),
callback_type(
#onErrCb
lambda msg, len:
print("Error calling waku_new: %s",
libwaku.waku_relay_subscribe.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
callback_type,
ctypes.c_void_p]
libwaku.waku_relay_subscribe(ctx,
default_pubsub_topic.encode('utf-8'),
callback_type(
#onErrCb
lambda ret, msg, len:
print("Error calling waku_relay_subscribe: %s" %
msg.decode('utf-8'))
))

libwaku.waku_connect(args.peer.encode('utf-8'),
),
ctypes.c_void_p(0))

libwaku.waku_connect.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_int,
callback_type,
ctypes.c_void_p]
libwaku.waku_connect(ctx,
args.peer.encode('utf-8'),
10000,
callback_type(
# onErrCb
lambda msg, len:
print("Error calling waku_new: %s", msg.decode('utf-8'))))
callback_type(
lambda ret, msg, len:
print("Error calling waku_connect: %s" % msg.decode('utf-8'))),
ctypes.c_void_p(0))

# app = Flask(__name__)
# @app.route("/")
Expand Down
Loading

0 comments on commit f1d14e9

Please sign in to comment.