mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-08-27 00:11:10 +00:00
refactor(cbindings): libwaku - run waku node in a secondary working thread (#1865)
* Refactoring to have a working waku_thread
This commit is contained in:
committed by
GitHub
parent
a3e93e5e4c
commit
bceaaa650d
@@ -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
|
||||
@@ -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
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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) {
|
||||
@@ -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));
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
@@ -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);
|
||||
@@ -291,6 +290,6 @@ int main(int argc, char** argv) {
|
||||
show_main_menu();
|
||||
while(1) {
|
||||
handle_user_input();
|
||||
waku_poll();
|
||||
// waku_poll();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user