Allow to provide user data for the filter subscribe callback
This commit is contained in:
parent
f0a5d8d83c
commit
cfdab7614d
|
@ -5,10 +5,10 @@ package main
|
||||||
#include "libnimbus.h"
|
#include "libnimbus.h"
|
||||||
|
|
||||||
// receiveHandler gateway function
|
// receiveHandler gateway function
|
||||||
void receiveHandler_cgo(received_message * msg)
|
void receiveHandler_cgo(received_message * msg, void* udata)
|
||||||
{
|
{
|
||||||
void receiveHandler(received_message*);
|
void receiveHandler(received_message* msg, void* udata);
|
||||||
receiveHandler(msg);
|
receiveHandler(msg, udata);
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
|
@ -45,7 +45,7 @@ typedef struct {
|
||||||
uint8_t topic[4];
|
uint8_t topic[4];
|
||||||
} topic;
|
} 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 */
|
/** Initialize Nim and the status library */
|
||||||
void NimMain();
|
void NimMain();
|
||||||
|
@ -88,7 +88,7 @@ int nimbus_get_symkey(const char* id, uint8_t* symkey);
|
||||||
|
|
||||||
/* Subscribe to given filter */
|
/* Subscribe to given filter */
|
||||||
const char* nimbus_subscribe_filter(filter_options* filter_options,
|
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);
|
int nimbus_unsubscribe_filter(const char* id);
|
||||||
/* Post Whisper message */
|
/* Post Whisper message */
|
||||||
int nimbus_post(post_message* msg);
|
int nimbus_post(post_message* msg);
|
||||||
|
|
|
@ -338,8 +338,8 @@ proc nimbus_post(message: ptr CPostMessage): bool {.exportc,foreignThreadGc.} =
|
||||||
powTarget = message.powTarget)
|
powTarget = message.powTarget)
|
||||||
|
|
||||||
proc nimbus_subscribe_filter(options: ptr CFilterOptions,
|
proc nimbus_subscribe_filter(options: ptr CFilterOptions,
|
||||||
handler: proc (msg: ptr CReceivedMessage) {.gcsafe, cdecl.}):
|
handler: proc (msg: ptr CReceivedMessage, udata: pointer) {.gcsafe, cdecl.},
|
||||||
cstring {.exportc, foreignThreadGc.} =
|
udata: pointer = nil): cstring {.exportc, foreignThreadGc.} =
|
||||||
## In case of a passed handler, the received msg needs to be copied before the
|
## In case of a passed handler, the received msg needs to be copied before the
|
||||||
## handler ends.
|
## handler ends.
|
||||||
## TODO: provide some user context passing here else this is rather useless?
|
## 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():
|
if msg.dst.isSome():
|
||||||
cmsg.recipientPublicKey = msg.decoded.src.get()
|
cmsg.recipientPublicKey = msg.decoded.src.get()
|
||||||
|
|
||||||
handler(addr cmsg)
|
handler(addr cmsg, udata)
|
||||||
|
|
||||||
result = node.subscribeFilter(filter, c_handler)
|
result = node.subscribeFilter(filter, c_handler)
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
void NimMain();
|
void NimMain();
|
||||||
|
|
||||||
void print_msg(received_message* msg) {
|
void print_msg(received_message* msg, void* udata) {
|
||||||
// Note: early null chars will terminate string early
|
// Note: early null chars will terminate string early
|
||||||
printf("received message %.*s\n", (int)msg->decodedLen, msg->decoded);
|
printf("received message %.*s\n", (int)msg->decodedLen, msg->decoded);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
#cgo LDFLAGS: -Wl,-rpath,'$ORIGIN' -L${SRCDIR}/../build -lnimbus -lm
|
#cgo LDFLAGS: -Wl,-rpath,'$ORIGIN' -L${SRCDIR}/../build -lnimbus -lm
|
||||||
#include "libnimbus.h"
|
#include "libnimbus.h"
|
||||||
|
|
||||||
void receiveHandler_cgo(received_message * msg); // Forward declaration.
|
void receiveHandler_cgo(received_message * msg, void* udata); // Forward declaration.
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
|
@ -30,10 +30,13 @@ func poll() {
|
||||||
}
|
}
|
||||||
|
|
||||||
//export receiveHandler
|
//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",
|
fmt.Printf("[nim-status] received message %s\n",
|
||||||
C.GoStringN((*C.char)(msg.decoded), (C.int)(msg.decodedLen)) )
|
C.GoStringN((*C.char)(msg.decoded), (C.int)(msg.decodedLen)) )
|
||||||
fmt.Printf("[nim-status] source public key %x\n", msg.source)
|
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() {
|
func Start() {
|
||||||
|
@ -51,11 +54,14 @@ func StatusListenAndPost(channel string) {
|
||||||
symKeyId := C.GoString(C.nimbus_add_symkey_from_password(C.CString(channel)))
|
symKeyId := C.GoString(C.nimbus_add_symkey_from_password(C.CString(channel)))
|
||||||
asymKeyId := C.GoString(C.nimbus_new_keypair())
|
asymKeyId := C.GoString(C.nimbus_new_keypair())
|
||||||
|
|
||||||
|
msgCount := 0
|
||||||
options := C.filter_options{symKeyID: C.CString(symKeyId),
|
options := C.filter_options{symKeyID: C.CString(symKeyId),
|
||||||
minPow: 0.002,
|
minPow: 0.002,
|
||||||
topic: C.nimbus_string_to_topic(C.CString(channel)).topic}
|
topic: C.nimbus_string_to_topic(C.CString(channel)).topic}
|
||||||
C.nimbus_subscribe_filter(&options,
|
filterId := C.GoString(C.nimbus_subscribe_filter(&options,
|
||||||
(C.received_msg_handler)(unsafe.Pointer(C.receiveHandler_cgo)))
|
(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),
|
postMessage := C.post_message{symKeyID: C.CString(symKeyId),
|
||||||
sourceID: C.CString(asymKeyId),
|
sourceID: C.CString(asymKeyId),
|
||||||
|
|
Loading…
Reference in New Issue