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(cbindings): libwaku - run waku node in a secondary working thread #1865

Merged
merged 4 commits into from
Jul 31, 2023
Merged
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
58 changes: 58 additions & 0 deletions examples/cbindings/base64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

#include "base64.h"

// Base64 encoding
// source: https://nachtimwald.com/2017/11/18/base64-encode-and-decode-in-c/
size_t b64_encoded_size(size_t inlen)
{
size_t ret;

ret = inlen;
if (inlen % 3 != 0)
ret += 3 - (inlen % 3);
ret /= 3;
ret *= 4;

return ret;
}

const char b64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

char *b64_encode(const unsigned char *in, size_t len)
{
char *out;
size_t elen;
size_t i;
size_t j;
size_t v;

if (in == NULL || len == 0)
return NULL;

elen = b64_encoded_size(len);
out = malloc(elen+1);
out[elen] = '\0';

for (i=0, j=0; i<len; i+=3, j+=4) {
v = in[i];
v = i+1 < len ? v << 8 | in[i+1] : v << 8;
v = i+2 < len ? v << 8 | in[i+2] : v << 8;

out[j] = b64chars[(v >> 18) & 0x3F];
out[j+1] = b64chars[(v >> 12) & 0x3F];
if (i+1 < len) {
out[j+2] = b64chars[(v >> 6) & 0x3F];
} else {
out[j+2] = '=';
}
if (i+2 < len) {
out[j+3] = b64chars[v & 0x3F];
} else {
out[j+3] = '=';
}
}

return out;
}

// End of Base64 encoding
11 changes: 11 additions & 0 deletions examples/cbindings/base64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#ifndef _BASE64_H_
#define _BASE64_H_

#include <stdlib.h>

size_t b64_encoded_size(size_t inlen);

char *b64_encode(const unsigned char *in, size_t len);

#endif
21 changes: 10 additions & 11 deletions examples/cbindings/waku_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,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 };

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

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

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

void handle_error(char* msg, size_t len) {
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, char* msg) {
void publish_message(char* pubsubTopic, const char* msg) {
char jsonWakuMsg[MAX_MSG_SIZE];
char *msgPayload = b64_encode(msg, strlen(msg));

Expand Down Expand Up @@ -138,15 +138,15 @@ void show_help_and_exit() {
exit(1);
}

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

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

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

Expand Down Expand Up @@ -243,8 +243,6 @@ void handle_user_input() {

int main(int argc, char** argv) {

waku_init_lib();

struct ConfigNode cfgNode;
// default values
snprintf(cfgNode.host, 128, "0.0.0.0");
Expand Down Expand Up @@ -272,12 +270,13 @@ int main(int argc, char** argv) {

WAKU_CALL( waku_default_pubsub_topic(print_default_pubsub_topic) );
WAKU_CALL( waku_version(print_waku_version) );

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_set_relay_callback(event_handler);
waku_set_event_callback(event_handler);
waku_start();

printf("Establishing connection with: %s\n", cfgNode.peers);
Expand All @@ -291,6 +290,6 @@ int main(int argc, char** argv) {
show_main_menu();
while(1) {
handle_user_input();
waku_poll();
// waku_poll();
}
}
7 changes: 7 additions & 0 deletions library/alloc.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

proc alloc*(str: cstring): cstring =
# Byte allocation from the given address.
# There should be the corresponding manual deallocation with deallocShared !
let ret = cast[cstring](allocShared(len(str) + 1))
copyMem(ret, str, len(str) + 1)
return ret
10 changes: 2 additions & 8 deletions library/libwaku.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
#define RET_ERR 1
#define RET_MISSING_CALLBACK 2

typedef void (*WakuCallBack) (char* msg, size_t len_0);

// This should only be called once.
// It initializes the nim runtime and GC.
void waku_init_lib(void);
typedef void (*WakuCallBack) (const char* msg, size_t len_0);

// Creates a new instance of the waku node.
// Sets up the waku node from the given configuration.
Expand All @@ -27,7 +23,7 @@ void waku_stop(void);

int waku_version(WakuCallBack onOkCb);

void waku_set_relay_callback(WakuCallBack callback);
void waku_set_event_callback(WakuCallBack callback);

int waku_content_topic(const char* appName,
unsigned int appVersion,
Expand Down Expand Up @@ -56,6 +52,4 @@ int waku_connect(const char* peerMultiAddr,
unsigned int timeoutMs,
WakuCallBack onErrCb);

void waku_poll(void);

#endif /* __libwaku__ */
Loading