Ivan FB e3dd6203ae
Start using nim-ffi to implement libwaku (#3656)
* deep changes in libwaku to adap to nim-ffi
* start using ffi pragma in library
* update some binding examples
* add missing declare_lib.nim file
* properly rename api files in library folder
2025-12-19 17:00:43 +01:00

57 lines
1.6 KiB
C++

#include <QObject>
#include <QDebug>
#include <QString>
#include "../../library/libwaku.h"
class WakuHandler : public QObject {
Q_OBJECT
private:
static void event_handler(int callerRet, const char* msg, size_t len, void* userData) {
printf("Receiving message %s\n", msg);
}
static 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);
}
}
public:
WakuHandler() : QObject(), ctx(nullptr) {}
void initialize(const QString& jsonConfig, WakuCallBack event_handler, void* userData) {
ctx = waku_new(jsonConfig.toUtf8().constData(), WakuCallBack(event_handler), userData);
set_event_callback(ctx, on_event_received, userData);
qDebug() << "Waku context initialized, ready to start.";
}
Q_INVOKABLE void start() {
if (ctx) {
waku_start(ctx, event_handler, nullptr);
qDebug() << "Waku start called with event_handler and userData.";
} else {
qDebug() << "Context is not initialized in start.";
}
}
Q_INVOKABLE void stop() {
if (ctx) {
waku_stop(ctx, event_handler, nullptr);
qDebug() << "Waku stop called with event_handler and userData.";
} else {
qDebug() << "Context is not initialized in stop.";
}
}
virtual ~WakuHandler() {}
private:
void* ctx;
};