mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-08-25 07:21:13 +00:00
chore: maintenance to c and c++ simple examples (#3367)
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
## App description
|
||||
This is a very simple example that shows how to invoke libwaku functions from a C++ program.
|
||||
|
||||
## Build
|
||||
1. Open terminal
|
||||
2. cd to nwaku root folder
|
||||
3. make cppwaku_example -j8
|
||||
|
||||
This will create libwaku.so and cppwaku_example binary within the build folder.
|
||||
|
||||
## Run
|
||||
1. Open terminal
|
||||
2. cd to nwaku root folder
|
||||
3. export LD_LIBRARY_PATH=build
|
||||
4. `./build/cppwaku_example --host=0.0.0.0 --port=60001`
|
||||
|
||||
Use `./build/cppwaku_example --help` to see some other options.
|
||||
|
||||
+138
-78
@@ -16,12 +16,34 @@
|
||||
#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);
|
||||
}
|
||||
|
||||
void signal_cond() {
|
||||
pthread_mutex_lock(&mutex);
|
||||
callback_executed = 1;
|
||||
pthread_cond_signal(&cond);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
}
|
||||
|
||||
#define WAKU_CALL(call) \
|
||||
do { \
|
||||
int ret = call; \
|
||||
if (ret != 0) { \
|
||||
std::cout << "Failed the call to: " << #call << ". Code: " << ret << "\n"; \
|
||||
} \
|
||||
waitForCallback(); \
|
||||
} while (0)
|
||||
|
||||
struct ConfigNode {
|
||||
@@ -78,6 +100,24 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void event_handler(const char* msg, size_t len) {
|
||||
printf("Receiving event: %s\n", msg);
|
||||
}
|
||||
|
||||
void handle_error(const char* msg, size_t len) {
|
||||
printf("handle_error: %s\n", msg);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
template <class F>
|
||||
auto cify(F&& f) {
|
||||
static F fn = std::forward<F>(f);
|
||||
return [](int callerRet, const char* msg, size_t len, void* userData) {
|
||||
signal_cond();
|
||||
return fn(msg, len);
|
||||
};
|
||||
}
|
||||
|
||||
static struct argp argp = { options, parse_opt, args_doc, doc, 0, 0, 0 };
|
||||
|
||||
// Beginning of UI program logic
|
||||
@@ -98,7 +138,7 @@ void show_main_menu() {
|
||||
printf("\t3.) Publish a message\n");
|
||||
}
|
||||
|
||||
void handle_user_input() {
|
||||
void handle_user_input(void* ctx) {
|
||||
char cmd[1024];
|
||||
memset(cmd, 0, 1024);
|
||||
int numRead = read(0, cmd, 1024);
|
||||
@@ -106,9 +146,6 @@ void handle_user_input() {
|
||||
return;
|
||||
}
|
||||
|
||||
int c;
|
||||
while ( (c = getchar()) != '\n' && c != EOF ) { }
|
||||
|
||||
switch (atoi(cmd))
|
||||
{
|
||||
case SUBSCRIBE_TOPIC_MENU:
|
||||
@@ -116,10 +153,14 @@ void handle_user_input() {
|
||||
printf("Indicate the Pubsubtopic to subscribe:\n");
|
||||
char pubsubTopic[128];
|
||||
scanf("%127s", pubsubTopic);
|
||||
// if (!waku_relay_subscribe(pubsubTopic, &mResp)) {
|
||||
// printf("Error subscribing to PubsubTopic: %s\n", mResp->data);
|
||||
// }
|
||||
// printf("Waku Relay subscription response: %s\n", mResp->data);
|
||||
|
||||
WAKU_CALL( waku_relay_subscribe(ctx,
|
||||
pubsubTopic,
|
||||
cify([&](const char* msg, size_t len) {
|
||||
event_handler(msg, len);
|
||||
}),
|
||||
nullptr) );
|
||||
printf("The subscription went well\n");
|
||||
|
||||
show_main_menu();
|
||||
}
|
||||
@@ -130,41 +171,51 @@ void handle_user_input() {
|
||||
printf("e.g.: /ip4/127.0.0.1/tcp/60001/p2p/16Uiu2HAmVFXtAfSj4EiR7mL2KvL4EE2wztuQgUSBoj2Jx2KeXFLN\n");
|
||||
char peerAddr[512];
|
||||
scanf("%511s", peerAddr);
|
||||
// if (!waku_connect(peerAddr, 10000 /* timeoutMs */, &mResp)) {
|
||||
// printf("Couldn't connect to the remote peer: %s\n", mResp->data);
|
||||
// }
|
||||
WAKU_CALL( waku_connect(ctx,
|
||||
peerAddr,
|
||||
10000 /* timeoutMs */,
|
||||
cify([&](const char* msg, size_t len) {
|
||||
event_handler(msg, len);
|
||||
}),
|
||||
nullptr));
|
||||
show_main_menu();
|
||||
break;
|
||||
|
||||
case PUBLISH_MESSAGE_MENU:
|
||||
{
|
||||
printf("Indicate the Pubsubtopic:\n");
|
||||
char pubsubTopic[128];
|
||||
scanf("%127s", pubsubTopic);
|
||||
|
||||
printf("Type the message tp publish:\n");
|
||||
printf("Type the message to publish:\n");
|
||||
char msg[1024];
|
||||
scanf("%1023s", msg);
|
||||
|
||||
char jsonWakuMsg[1024];
|
||||
char jsonWakuMsg[2048];
|
||||
std::vector<char> msgPayload;
|
||||
b64_encode(msg, strlen(msg), msgPayload);
|
||||
|
||||
// waku_content_topic("appName",
|
||||
// 1,
|
||||
// "contentTopicName",
|
||||
// "encoding",
|
||||
// &mResp);
|
||||
std::string contentTopic;
|
||||
waku_content_topic(ctx,
|
||||
"appName",
|
||||
1,
|
||||
"contentTopicName",
|
||||
"encoding",
|
||||
cify([&contentTopic](const char* msg, size_t len) {
|
||||
contentTopic = msg;
|
||||
}),
|
||||
nullptr);
|
||||
|
||||
// snprintf(jsonWakuMsg,
|
||||
// 1024,
|
||||
// "{\"payload\":\"%s\",\"content_topic\":\"%s\"}",
|
||||
// msgPayload, mResp->data);
|
||||
snprintf(jsonWakuMsg,
|
||||
2048,
|
||||
"{\"payload\":\"%s\",\"contentTopic\":\"%s\"}",
|
||||
msgPayload.data(), contentTopic.c_str());
|
||||
|
||||
// free(msgPayload);
|
||||
WAKU_CALL( waku_relay_publish(ctx,
|
||||
"/waku/2/rs/16/32",
|
||||
jsonWakuMsg,
|
||||
10000 /*timeout ms*/,
|
||||
cify([&](const char* msg, size_t len) {
|
||||
event_handler(msg, len);
|
||||
}),
|
||||
nullptr) );
|
||||
|
||||
// waku_relay_publish(pubsubTopic, jsonWakuMsg, 10000 /*timeout ms*/, &mResp);
|
||||
// printf("waku relay response [%s]\n", mResp->data);
|
||||
show_main_menu();
|
||||
}
|
||||
break;
|
||||
@@ -181,23 +232,6 @@ void show_help_and_exit() {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void event_handler(const char* msg, size_t len) {
|
||||
printf("Receiving message %s\n", msg);
|
||||
}
|
||||
|
||||
void handle_error(const char* msg, size_t len) {
|
||||
printf("Error: %s\n", msg);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
template <class F>
|
||||
auto cify(F&& f) {
|
||||
static F fn = std::forward<F>(f);
|
||||
return [](const char* msg, size_t len) {
|
||||
return fn(msg, len);
|
||||
};
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
struct ConfigNode cfgNode;
|
||||
// default values
|
||||
@@ -212,60 +246,86 @@ int main(int argc, char** argv) {
|
||||
show_help_and_exit();
|
||||
}
|
||||
|
||||
char jsonConfig[1024];
|
||||
snprintf(jsonConfig, 1024, "{ \
|
||||
char jsonConfig[2048];
|
||||
snprintf(jsonConfig, 2048, "{ \
|
||||
\"host\": \"%s\", \
|
||||
\"port\": %d, \
|
||||
\"key\": \"%s\", \
|
||||
\"relay\": %s, \
|
||||
\"logLevel\": \"DEBUG\" \
|
||||
\"relay\": true, \
|
||||
\"clusterId\": 16, \
|
||||
\"shards\": [ 1, 32, 64, 128, 256 ], \
|
||||
\"logLevel\": \"FATAL\", \
|
||||
\"discv5Discovery\": true, \
|
||||
\"discv5BootstrapNodes\": \
|
||||
[\"enr:-QESuEB4Dchgjn7gfAvwB00CxTA-nGiyk-aALI-H4dYSZD3rUk7bZHmP8d2U6xDiQ2vZffpo45Jp7zKNdnwDUx6g4o6XAYJpZIJ2NIJpcIRA4VDAim11bHRpYWRkcnO4XAArNiZub2RlLTAxLmRvLWFtczMud2FrdS5zYW5kYm94LnN0YXR1cy5pbQZ2XwAtNiZub2RlLTAxLmRvLWFtczMud2FrdS5zYW5kYm94LnN0YXR1cy5pbQYfQN4DgnJzkwABCAAAAAEAAgADAAQABQAGAAeJc2VjcDI1NmsxoQOvD3S3jUNICsrOILlmhENiWAMmMVlAl6-Q8wRB7hidY4N0Y3CCdl-DdWRwgiMohXdha3UyDw\", \"enr:-QEkuEBIkb8q8_mrorHndoXH9t5N6ZfD-jehQCrYeoJDPHqT0l0wyaONa2-piRQsi3oVKAzDShDVeoQhy0uwN1xbZfPZAYJpZIJ2NIJpcIQiQlleim11bHRpYWRkcnO4bgA0Ni9ub2RlLTAxLmdjLXVzLWNlbnRyYWwxLWEud2FrdS5zYW5kYm94LnN0YXR1cy5pbQZ2XwA2Ni9ub2RlLTAxLmdjLXVzLWNlbnRyYWwxLWEud2FrdS5zYW5kYm94LnN0YXR1cy5pbQYfQN4DgnJzkwABCAAAAAEAAgADAAQABQAGAAeJc2VjcDI1NmsxoQKnGt-GSgqPSf3IAPM7bFgTlpczpMZZLF3geeoNNsxzSoN0Y3CCdl-DdWRwgiMohXdha3UyDw\"], \
|
||||
\"discv5UdpPort\": 9999, \
|
||||
\"dnsDiscoveryUrl\": \"enrtree://AMOJVZX4V6EXP7NTJPMAYJYST2QP6AJXYW76IU6VGJS7UVSNDYZG4@boot.prod.status.nodes.status.im\", \
|
||||
\"dnsDiscoveryNameServers\": [\"8.8.8.8\", \"1.0.0.1\"] \
|
||||
}", cfgNode.host,
|
||||
cfgNode.port,
|
||||
cfgNode.key,
|
||||
cfgNode.relay ? "true":"false");
|
||||
cfgNode.port);
|
||||
|
||||
WAKU_CALL(waku_new(jsonConfig, cify([](const char* msg, size_t len) {
|
||||
std::cout << "Error: " << msg << std::endl;
|
||||
exit(1);
|
||||
})));
|
||||
void* ctx =
|
||||
waku_new(jsonConfig,
|
||||
cify([](const char* msg, size_t len) {
|
||||
std::cout << "waku_new feedback: " << msg << std::endl;
|
||||
}
|
||||
),
|
||||
nullptr
|
||||
);
|
||||
waitForCallback();
|
||||
|
||||
// example on how to retrieve a value from the `libwaku` callback.
|
||||
std::string defaultPubsubTopic;
|
||||
WAKU_CALL(waku_default_pubsub_topic(cify([&defaultPubsubTopic](const char* msg, size_t len) {
|
||||
defaultPubsubTopic = msg;
|
||||
})));
|
||||
WAKU_CALL(
|
||||
waku_default_pubsub_topic(
|
||||
ctx,
|
||||
cify([&defaultPubsubTopic](const char* msg, size_t len) {
|
||||
defaultPubsubTopic = msg;
|
||||
}
|
||||
),
|
||||
nullptr));
|
||||
|
||||
std::cout << "Default pubsub topic: " << defaultPubsubTopic << std::endl;
|
||||
|
||||
WAKU_CALL(waku_version(cify([&](const char* msg, size_t len) {
|
||||
std::cout << "Git Version: " << msg << std::endl;
|
||||
})));
|
||||
WAKU_CALL(waku_version(ctx,
|
||||
cify([&](const char* msg, size_t len) {
|
||||
std::cout << "Git Version: " << msg << std::endl;
|
||||
}),
|
||||
nullptr));
|
||||
|
||||
printf("Bind addr: %s:%u\n", cfgNode.host, cfgNode.port);
|
||||
printf("Waku Relay enabled: %s\n", cfgNode.relay == 1 ? "YES": "NO");
|
||||
|
||||
std::string pubsubTopic;
|
||||
WAKU_CALL(waku_pubsub_topic("example", cify([&](const char* msg, size_t len) {
|
||||
pubsubTopic = msg;
|
||||
})));
|
||||
WAKU_CALL(waku_pubsub_topic(ctx,
|
||||
"example",
|
||||
cify([&](const char* msg, size_t len) {
|
||||
pubsubTopic = msg;
|
||||
}),
|
||||
nullptr));
|
||||
|
||||
std::cout << "Custom pubsub topic: " << pubsubTopic << std::endl;
|
||||
|
||||
waku_set_event_callback(event_handler);
|
||||
waku_start();
|
||||
waku_set_event_callback(ctx,
|
||||
cify([&](const char* msg, size_t len) {
|
||||
event_handler(msg, len);
|
||||
}),
|
||||
nullptr);
|
||||
|
||||
WAKU_CALL( waku_connect(cfgNode.peers,
|
||||
10000 /* timeoutMs */,
|
||||
handle_error) );
|
||||
WAKU_CALL( waku_start(ctx,
|
||||
cify([&](const char* msg, size_t len) {
|
||||
event_handler(msg, len);
|
||||
}),
|
||||
nullptr));
|
||||
|
||||
WAKU_CALL( waku_relay_subscribe(defaultPubsubTopic.c_str(),
|
||||
handle_error) );
|
||||
|
||||
std::cout << "Establishing connection with: " << cfgNode.peers << std::endl;
|
||||
WAKU_CALL(waku_connect(cfgNode.peers, 10000 /* timeoutMs */, handle_error));
|
||||
WAKU_CALL( waku_relay_subscribe(ctx,
|
||||
defaultPubsubTopic.c_str(),
|
||||
cify([&](const char* msg, size_t len) {
|
||||
event_handler(msg, len);
|
||||
}),
|
||||
nullptr) );
|
||||
|
||||
show_main_menu();
|
||||
while(1) {
|
||||
handle_user_input();
|
||||
handle_user_input(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user