Skip to content

Commit

Permalink
chore: bindings return multiaddress array (#2461)
Browse files Browse the repository at this point in the history
* waku_example.c: adapt signature to new parameter 'void* userData'
* libwaku: add new DEBUG request handler to retrieve the list of listened multiaddresses
* waku_example.c: use example the new 'waku_listen_addresses'
* add debug_node_request.nim file
  • Loading branch information
Ivansete-status committed Feb 21, 2024
1 parent d01585e commit 7aea145
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 6 deletions.
12 changes: 7 additions & 5 deletions examples/cbindings/waku_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) {

static struct argp argp = { options, parse_opt, args_doc, doc, 0, 0, 0 };

void event_handler(int callerRet, const char* msg, size_t len) {
void event_handler(int callerRet, const char* msg, size_t len, void* userData) {
if (callerRet == RET_ERR) {
printf("Error: %s\n", msg);
exit(1);
Expand All @@ -102,7 +102,7 @@ void event_handler(int callerRet, const char* msg, size_t len) {
}

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

char* publishResponse = NULL;
void handle_publish_ok(int callerRet, const char* msg, size_t len) {
void handle_publish_ok(int callerRet, const char* msg, size_t len, void* userData) {
printf("Publish Ok: %s %lu\n", msg, len);

if (publishResponse != NULL) {
Expand Down Expand Up @@ -159,11 +159,11 @@ void show_help_and_exit() {
exit(1);
}

void print_default_pubsub_topic(int callerRet, const char* msg, size_t len) {
void print_default_pubsub_topic(int callerRet, const char* msg, size_t len, void* userData) {
printf("Default pubsub topic: %s\n", msg);
}

void print_waku_version(int callerRet, const char* msg, size_t len) {
void print_waku_version(int callerRet, const char* msg, size_t len, void* userData) {
printf("Git Version: %s\n", msg);
}

Expand Down Expand Up @@ -297,6 +297,8 @@ int main(int argc, char** argv) {
waku_set_event_callback(ctx, event_handler, userData);
waku_start(ctx, event_handler, userData);

waku_listen_addresses(ctx, event_handler, userData);

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

WAKU_CALL( waku_connect(ctx,
Expand Down
4 changes: 4 additions & 0 deletions library/libwaku.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ int waku_connect(void* ctx,
WakuCallBack callback,
void* userData);

int waku_listen_addresses(void* ctx,
WakuCallBack callback,
void* userData);

#ifdef __cplusplus
}
#endif
Expand Down
22 changes: 22 additions & 0 deletions library/libwaku.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import
./waku_thread/inter_thread_communication/requests/peer_manager_request,
./waku_thread/inter_thread_communication/requests/protocols/relay_request,
./waku_thread/inter_thread_communication/requests/protocols/store_request,
./waku_thread/inter_thread_communication/requests/debug_node_request,
./waku_thread/inter_thread_communication/waku_thread_request,
./alloc,
./callback
Expand Down Expand Up @@ -353,5 +354,26 @@ proc waku_store_query(ctx: ptr Context,

return RET_OK

proc waku_listen_addresses(ctx: ptr Context,
callback: WakuCallBack,
userData: pointer): cint
{.dynlib, exportc.} =

ctx[].userData = userData

let connRes = waku_thread.sendRequestToWakuThread(
ctx,
RequestType.DEBUG,
DebugNodeRequest.createShared(
DebugNodeMsgType.RETRIEVE_LISTENING_ADDRESSES))
if connRes.isErr():
let msg = $connRes.error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
else:
let msg = $connRes.value
callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_OK

### End of exported procs
################################################################################
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

import
std/[options,sequtils,strutils,json]
import
chronicles,
chronos,
stew/results,
stew/shims/net
import
../../../../waku/node/waku_node,
../../../alloc

type
DebugNodeMsgType* = enum
RETRIEVE_LISTENING_ADDRESSES

type
DebugNodeRequest* = object
operation: DebugNodeMsgType

proc createShared*(T: type DebugNodeRequest,
op: DebugNodeMsgType): ptr type T =

var ret = createShared(T)
ret[].operation = op
return ret

proc destroyShared(self: ptr DebugNodeRequest) =
deallocShared(self)

proc getMultiaddresses(node: WakuNode): seq[string] =
return node.info().listenAddresses

proc process*(self: ptr DebugNodeRequest,
node: WakuNode): Future[Result[string, string]] {.async.} =

defer: destroyShared(self)

case self.operation:
of RETRIEVE_LISTENING_ADDRESSES:
return ok($( %* node.getMultiaddresses()))

return err("unsupported operation in DebugNodeRequest")

Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ import
./requests/node_lifecycle_request,
./requests/peer_manager_request,
./requests/protocols/relay_request,
./requests/protocols/store_request
./requests/protocols/store_request,
./requests/debug_node_request

type
RequestType* {.pure.} = enum
LIFECYCLE,
PEER_MANAGER,
RELAY,
STORE,
DEBUG,

type
InterThreadRequest* = object
Expand Down Expand Up @@ -54,6 +56,8 @@ proc process*(T: type InterThreadRequest,
cast[ptr RelayRequest](request[].reqContent).process(node)
of STORE:
cast[ptr StoreRequest](request[].reqContent).process(node)
of DEBUG:
cast[ptr DebugNodeRequest](request[].reqContent).process(node[])

return await retFut

Expand Down

0 comments on commit 7aea145

Please sign in to comment.