Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(libwaku): async #3180

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 52 additions & 3 deletions examples/cbindings/waku_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <unistd.h>
#include <fcntl.h>
#include <stdint.h>
#include <pthread.h>

#include <sys/types.h>
#include <unistd.h>
Expand All @@ -13,13 +14,30 @@
#include "base64.h"
#include "../../library/libwaku.h"


// Shared synchronization variables
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int callback_executed = 0;

void waitForCallback() {
pthread_mutex_lock(&mutex);
while (!callback_executed) {
pthread_cond_wait(&cond, &mutex);
}
callback_executed = 0;
pthread_mutex_unlock(&mutex);
}


#define WAKU_CALL(call) \
do { \
int ret = call; \
if (ret != 0) { \
printf("Failed the call to: %s. Returned code: %d\n", #call, ret); \
exit(1); \
} \
waitForCallback(); \
} while (0)

struct ConfigNode {
Expand Down Expand Up @@ -99,6 +117,21 @@ void event_handler(int callerRet, const char* msg, size_t len, void* userData) {
else if (callerRet == RET_OK) {
printf("Receiving event: %s\n", msg);
}

pthread_mutex_lock(&mutex);
callback_executed = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}

void on_event_received(int callerRet, const char* msg, size_t len, void* userData) {
if (callerRet == RET_ERR) {
printf("Error: %s\n", msg);
exit(1);
}
else if (callerRet == RET_OK) {
printf("Receiving event: %s\n", msg);
}
}

char* contentTopic = NULL;
Expand Down Expand Up @@ -161,10 +194,20 @@ void show_help_and_exit() {

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

pthread_mutex_lock(&mutex);
callback_executed = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}

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

pthread_mutex_lock(&mutex);
callback_executed = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}

// Beginning of UI program logic
Expand Down Expand Up @@ -255,7 +298,7 @@ int main(int argc, char** argv) {
cfgNode.port = 60000;
cfgNode.relay = 1;

cfgNode.store = 1;
cfgNode.store = 0;
snprintf(cfgNode.storeNode, 2048, "");
snprintf(cfgNode.storeRetentionPolicy, 64, "time:6000000");
snprintf(cfgNode.storeDbUrl, 256, "postgres://postgres:test123@localhost:5432/postgres");
Expand Down Expand Up @@ -296,17 +339,20 @@ int main(int argc, char** argv) {
cfgNode.storeMaxNumDbConnections);

ctx = waku_new(jsonConfig, event_handler, userData);
waitForCallback();

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_set_event_callback(ctx, event_handler, userData);
waku_set_event_callback(ctx, on_event_received, userData);

waku_start(ctx, event_handler, userData);
waitForCallback();

waku_listen_addresses(ctx, event_handler, userData);
WAKU_CALL( waku_listen_addresses(ctx, event_handler, userData) );

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

Expand Down Expand Up @@ -334,4 +380,7 @@ int main(int argc, char** argv) {
while(1) {
handle_user_input();
}

pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
}
13 changes: 0 additions & 13 deletions library/callback.nim

This file was deleted.

30 changes: 30 additions & 0 deletions library/ffi_types.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
################################################################################
### Exported types

type WakuCallBack* = proc(
callerRet: cint, msg: ptr cchar, len: csize_t, userData: pointer
) {.cdecl, gcsafe, raises: [].}

const RET_OK*: cint = 0
const RET_ERR*: cint = 1
const RET_MISSING_CALLBACK*: cint = 2

### End of exported types
################################################################################

################################################################################
### FFI utils

template foreignThreadGc*(body: untyped) =
when declared(setupForeignThreadGc):
setupForeignThreadGc()

body

when declared(tearDownForeignThreadGc):
tearDownForeignThreadGc()

type onDone* = proc()

### End of FFI utils
################################################################################
Loading
Loading