Skip to content

Commit

Permalink
chore(cbindings): avoid using global var in libwaku.nim (waku-org#2118)
Browse files Browse the repository at this point in the history
* libwaku: Avoid global variable and changing callback signature

* Better signature for the callback. Two new parameters have been added:
  one aimed to allow passing the caller result code; the other
  param is to pass an optional userData pointer that might need
  to be linked locally with the Context object. For example, this is needed
  in Rust to make the passed closures live as
  long as the Context.

* waku_example.c: adaptation to the latest changes

* libwaku.h: removing 'waku_set_user_data' function

* libwaku.nim: renaming parameter in WakuCallBack (isOk -> callerRet)
  • Loading branch information
Ivansete-status authored and omahs committed Nov 21, 2023
1 parent f796819 commit a5a30a2
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 105 deletions.
77 changes: 48 additions & 29 deletions examples/cbindings/waku_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ struct ConfigNode {
char peers[2048];
};

// libwaku Context
void* ctx;

// For the case of C language we don't need to store a particular userData
void* userData = NULL;

// Arguments parsing
static char doc[] = "\nC example that shows how to use the waku library.";
static char args_doc[] = "";
Expand Down Expand Up @@ -78,8 +84,18 @@ 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) {
if (callerRet == RET_ERR) {
printf("Error: %s\n", msg);
exit(1);
}
else if (callerRet == RET_OK) {
printf("Receiving message %s\n", msg);
}
}

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

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

if (publishResponse != NULL) {
Expand All @@ -100,22 +116,19 @@ void handle_publish_ok(const char* msg, size_t len) {
strcpy(publishResponse, msg);
}

void handle_error(const char* msg, size_t len) {
printf("Error: %s\n", msg);
exit(1);
}

#define MAX_MSG_SIZE 65535

void publish_message(char* pubsubTopic, const char* msg) {
char jsonWakuMsg[MAX_MSG_SIZE];
char *msgPayload = b64_encode(msg, strlen(msg));

WAKU_CALL( waku_content_topic("appName",
WAKU_CALL( waku_content_topic(RET_OK,
"appName",
1,
"contentTopicName",
"encoding",
handle_content_topic) );
handle_content_topic,
userData) );

snprintf(jsonWakuMsg,
MAX_MSG_SIZE,
Expand All @@ -124,10 +137,12 @@ void publish_message(char* pubsubTopic, const char* msg) {

free(msgPayload);

WAKU_CALL( waku_relay_publish(pubsubTopic,
WAKU_CALL( waku_relay_publish(&ctx,
pubsubTopic,
jsonWakuMsg,
10000 /*timeout ms*/,
handle_error) );
event_handler,
userData) );

printf("waku relay response [%s]\n", publishResponse);
}
Expand All @@ -137,15 +152,11 @@ void show_help_and_exit() {
exit(1);
}

void event_handler(const char* msg, size_t len) {
printf("Receiving message %s\n", msg);
}

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

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

Expand Down Expand Up @@ -186,8 +197,10 @@ void handle_user_input() {
char pubsubTopic[128];
scanf("%127s", pubsubTopic);

WAKU_CALL( waku_relay_subscribe(pubsubTopic,
handle_error) );
WAKU_CALL( waku_relay_subscribe(&ctx,
pubsubTopic,
event_handler,
userData) );
printf("The subscription went well\n");

show_main_menu();
Expand All @@ -199,7 +212,7 @@ void handle_user_input() {
printf("e.g.: /ip4/127.0.0.1/tcp/60001/p2p/16Uiu2HAmVFXtAfSj4EiR7mL2KvL4EE2wztuQgUSBoj2Jx2KeXFLN\n");
char peerAddr[512];
scanf("%511s", peerAddr);
WAKU_CALL(waku_connect(peerAddr, 10000 /* timeoutMs */, handle_error));
WAKU_CALL(waku_connect(&ctx, peerAddr, 10000 /* timeoutMs */, event_handler, userData));
show_main_menu();
break;

Expand Down Expand Up @@ -239,6 +252,8 @@ int main(int argc, char** argv) {
show_help_and_exit();
}

ctx = waku_init(event_handler, userData);

char jsonConfig[1024];
snprintf(jsonConfig, 1024, "{ \
\"host\": \"%s\", \
Expand All @@ -250,25 +265,29 @@ int main(int argc, char** argv) {
cfgNode.key,
cfgNode.relay ? "true":"false");

WAKU_CALL( waku_default_pubsub_topic(print_default_pubsub_topic) );
WAKU_CALL( waku_version(print_waku_version) );
WAKU_CALL( waku_default_pubsub_topic(&ctx, print_default_pubsub_topic, userData) );
WAKU_CALL( waku_version(&ctx, print_waku_version, userData) );

printf("Bind addr: %s:%u\n", cfgNode.host, cfgNode.port);
printf("Waku Relay enabled: %s\n", cfgNode.relay == 1 ? "YES": "NO");

WAKU_CALL( waku_new(jsonConfig, handle_error) );
WAKU_CALL( waku_new(&ctx, jsonConfig, event_handler, userData) );

waku_set_event_callback(event_handler);
waku_start();
waku_set_event_callback(event_handler, userData);
waku_start(&ctx, event_handler, userData);

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

WAKU_CALL( waku_connect(cfgNode.peers,
WAKU_CALL( waku_connect(&ctx,
cfgNode.peers,
10000 /* timeoutMs */,
handle_error) );
event_handler,
userData) );

WAKU_CALL( waku_relay_subscribe("/waku/2/default-waku/proto",
handle_error) );
WAKU_CALL( waku_relay_subscribe(&ctx,
"/waku/2/default-waku/proto",
event_handler,
userData) );
show_main_menu();
while(1) {
handle_user_input();
Expand Down
66 changes: 47 additions & 19 deletions library/libwaku.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,73 @@
extern "C" {
#endif

typedef void (*WakuCallBack) (const char* msg, size_t len_0);
typedef void (*WakuCallBack) (int callerRet, const char* msg, size_t len);

// Initializes the waku library and returns a pointer to the Context.
void* waku_init(WakuCallBack callback,
void* userData);

// Creates a new instance of the waku node.
// Sets up the waku node from the given configuration.
int waku_new(const char* configJson, WakuCallBack onErrCb);
int waku_new(void* ctx,
const char* configJson,
WakuCallBack callback,
void* userData);

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

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

int waku_version(WakuCallBack onOkCb);
int waku_version(void* ctx,
WakuCallBack callback,
void* userData);

void waku_set_event_callback(WakuCallBack callback);
void waku_set_event_callback(WakuCallBack callback,
void* userData);

int waku_content_topic(const char* appName,
int waku_content_topic(void* ctx,
const char* appName,
unsigned int appVersion,
const char* contentTopicName,
const char* encoding,
WakuCallBack onOkCb);
WakuCallBack callback,
void* userData);

int waku_pubsub_topic(const char* topicName,
WakuCallBack onOkCb);
int waku_pubsub_topic(void* ctx,
const char* topicName,
WakuCallBack callback,
void* userData);

int waku_default_pubsub_topic(WakuCallBack onOkCb);
int waku_default_pubsub_topic(void* ctx,
WakuCallBack callback,
void* userData);

int waku_relay_publish(const char* pubSubTopic,
int waku_relay_publish(void* ctx,
const char* pubSubTopic,
const char* jsonWakuMessage,
unsigned int timeoutMs,
WakuCallBack onErrCb);
WakuCallBack callback,
void* userData);

int waku_relay_subscribe(const char* pubSubTopic,
WakuCallBack onErrCb);
int waku_relay_subscribe(void* ctx,
const char* pubSubTopic,
WakuCallBack callback,
void* userData);

int waku_relay_unsubscribe(const char* pubSubTopic,
WakuCallBack onErrCb);
int waku_relay_unsubscribe(void* ctx,
const char* pubSubTopic,
WakuCallBack callback,
void* userData);

int waku_connect(const char* peerMultiAddr,
int waku_connect(void* ctx,
const char* peerMultiAddr,
unsigned int timeoutMs,
WakuCallBack onErrCb);
WakuCallBack callback,
void* userData);

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit a5a30a2

Please sign in to comment.