From 7aea145efe37dd3b8e5d623807016b1c82e97695 Mon Sep 17 00:00:00 2001 From: Ivan FB <128452529+Ivansete-status@users.noreply.github.com> Date: Wed, 21 Feb 2024 12:06:05 +0100 Subject: [PATCH] chore: bindings return multiaddress array (#2461) * 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 --- examples/cbindings/waku_example.c | 12 ++--- library/libwaku.h | 4 ++ library/libwaku.nim | 22 ++++++++++ .../requests/debug_node_request.nim | 44 +++++++++++++++++++ .../waku_thread_request.nim | 6 ++- 5 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 library/waku_thread/inter_thread_communication/requests/debug_node_request.nim diff --git a/examples/cbindings/waku_example.c b/examples/cbindings/waku_example.c index 7053dd71c..67e1bf277 100644 --- a/examples/cbindings/waku_example.c +++ b/examples/cbindings/waku_example.c @@ -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); @@ -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); } @@ -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) { @@ -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); } @@ -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, diff --git a/library/libwaku.h b/library/libwaku.h index 4a30a8ad5..1dc7cf0ae 100644 --- a/library/libwaku.h +++ b/library/libwaku.h @@ -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 diff --git a/library/libwaku.nim b/library/libwaku.nim index 3ef27f0d3..b1c43a648 100644 --- a/library/libwaku.nim +++ b/library/libwaku.nim @@ -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 @@ -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 ################################################################################ diff --git a/library/waku_thread/inter_thread_communication/requests/debug_node_request.nim b/library/waku_thread/inter_thread_communication/requests/debug_node_request.nim new file mode 100644 index 000000000..b0a759882 --- /dev/null +++ b/library/waku_thread/inter_thread_communication/requests/debug_node_request.nim @@ -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") + diff --git a/library/waku_thread/inter_thread_communication/waku_thread_request.nim b/library/waku_thread/inter_thread_communication/waku_thread_request.nim index a38c09dba..4113e5d20 100644 --- a/library/waku_thread/inter_thread_communication/waku_thread_request.nim +++ b/library/waku_thread/inter_thread_communication/waku_thread_request.nim @@ -13,7 +13,8 @@ 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 @@ -21,6 +22,7 @@ type PEER_MANAGER, RELAY, STORE, + DEBUG, type InterThreadRequest* = object @@ -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