From cfdab7614d550761eb127f29ea0e22a611b988e0 Mon Sep 17 00:00:00 2001 From: kdeme Date: Wed, 30 Oct 2019 12:18:11 +0100 Subject: [PATCH] Allow to provide user data for the filter subscribe callback --- wrappers/cfuncs.go | 6 +++--- wrappers/libnimbus.h | 4 ++-- wrappers/libnimbus.nim | 6 +++--- wrappers/wrapper_example.c | 2 +- wrappers/wrapper_whisper_example.go | 14 ++++++++++---- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/wrappers/cfuncs.go b/wrappers/cfuncs.go index d85c1d6f1..87372c776 100644 --- a/wrappers/cfuncs.go +++ b/wrappers/cfuncs.go @@ -5,10 +5,10 @@ package main #include "libnimbus.h" // receiveHandler gateway function -void receiveHandler_cgo(received_message * msg) +void receiveHandler_cgo(received_message * msg, void* udata) { - void receiveHandler(received_message*); - receiveHandler(msg); + void receiveHandler(received_message* msg, void* udata); + receiveHandler(msg, udata); } */ import "C" \ No newline at end of file diff --git a/wrappers/libnimbus.h b/wrappers/libnimbus.h index b3cc72349..9045ac297 100644 --- a/wrappers/libnimbus.h +++ b/wrappers/libnimbus.h @@ -45,7 +45,7 @@ typedef struct { uint8_t topic[4]; } topic; -typedef void (*received_msg_handler)(received_message* msg); +typedef void (*received_msg_handler)(received_message* msg, void* udata); /** Initialize Nim and the status library */ void NimMain(); @@ -88,7 +88,7 @@ int nimbus_get_symkey(const char* id, uint8_t* symkey); /* Subscribe to given filter */ const char* nimbus_subscribe_filter(filter_options* filter_options, - received_msg_handler msg); + received_msg_handler msg, void* udata); int nimbus_unsubscribe_filter(const char* id); /* Post Whisper message */ int nimbus_post(post_message* msg); diff --git a/wrappers/libnimbus.nim b/wrappers/libnimbus.nim index eb1c520fa..f761d3953 100644 --- a/wrappers/libnimbus.nim +++ b/wrappers/libnimbus.nim @@ -338,8 +338,8 @@ proc nimbus_post(message: ptr CPostMessage): bool {.exportc,foreignThreadGc.} = powTarget = message.powTarget) proc nimbus_subscribe_filter(options: ptr CFilterOptions, - handler: proc (msg: ptr CReceivedMessage) {.gcsafe, cdecl.}): - cstring {.exportc, foreignThreadGc.} = + handler: proc (msg: ptr CReceivedMessage, udata: pointer) {.gcsafe, cdecl.}, + udata: pointer = nil): cstring {.exportc, foreignThreadGc.} = ## In case of a passed handler, the received msg needs to be copied before the ## handler ends. ## TODO: provide some user context passing here else this is rather useless? @@ -379,7 +379,7 @@ proc nimbus_subscribe_filter(options: ptr CFilterOptions, if msg.dst.isSome(): cmsg.recipientPublicKey = msg.decoded.src.get() - handler(addr cmsg) + handler(addr cmsg, udata) result = node.subscribeFilter(filter, c_handler) diff --git a/wrappers/wrapper_example.c b/wrappers/wrapper_example.c index d65abe772..8422f9ba5 100644 --- a/wrappers/wrapper_example.c +++ b/wrappers/wrapper_example.c @@ -9,7 +9,7 @@ void NimMain(); -void print_msg(received_message* msg) { +void print_msg(received_message* msg, void* udata) { // Note: early null chars will terminate string early printf("received message %.*s\n", (int)msg->decodedLen, msg->decoded); } diff --git a/wrappers/wrapper_whisper_example.go b/wrappers/wrapper_whisper_example.go index 52a40b14a..f742c75b3 100644 --- a/wrappers/wrapper_whisper_example.go +++ b/wrappers/wrapper_whisper_example.go @@ -11,7 +11,7 @@ import ( #cgo LDFLAGS: -Wl,-rpath,'$ORIGIN' -L${SRCDIR}/../build -lnimbus -lm #include "libnimbus.h" -void receiveHandler_cgo(received_message * msg); // Forward declaration. +void receiveHandler_cgo(received_message * msg, void* udata); // Forward declaration. */ import "C" @@ -30,10 +30,13 @@ func poll() { } //export receiveHandler -func receiveHandler(msg *C.received_message) { +func receiveHandler(msg *C.received_message, udata unsafe.Pointer) { fmt.Printf("[nim-status] received message %s\n", C.GoStringN((*C.char)(msg.decoded), (C.int)(msg.decodedLen)) ) fmt.Printf("[nim-status] source public key %x\n", msg.source) + msgCount := (*int)(udata) + *msgCount += 1 + fmt.Printf("[nim-status] message count %d\n", *msgCount) } func Start() { @@ -51,11 +54,14 @@ func StatusListenAndPost(channel string) { symKeyId := C.GoString(C.nimbus_add_symkey_from_password(C.CString(channel))) asymKeyId := C.GoString(C.nimbus_new_keypair()) + msgCount := 0 options := C.filter_options{symKeyID: C.CString(symKeyId), minPow: 0.002, topic: C.nimbus_string_to_topic(C.CString(channel)).topic} - C.nimbus_subscribe_filter(&options, - (C.received_msg_handler)(unsafe.Pointer(C.receiveHandler_cgo))) + filterId := C.GoString(C.nimbus_subscribe_filter(&options, + (C.received_msg_handler)(unsafe.Pointer(C.receiveHandler_cgo)), + unsafe.Pointer(&msgCount))) + fmt.Printf("[nim-status] filter subscribed, id: %s\n", filterId) postMessage := C.post_message{symKeyID: C.CString(symKeyId), sourceID: C.CString(asymKeyId),