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
This commit is contained in:
Ivan FB 2024-02-21 12:06:05 +01:00 committed by GitHub
parent d01585e9fa
commit 7aea145efe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 82 additions and 6 deletions

View File

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

View File

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

View File

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

View File

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

View File

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