diff --git a/examples/c-bindings/main.c b/examples/c-bindings/main.c index 00835c1d..e6a462c1 100644 --- a/examples/c-bindings/main.c +++ b/examples/c-bindings/main.c @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include "libgowaku.h" #include "nxjson.c" @@ -66,7 +68,7 @@ int main(int argc, char *argv[]) char *response; waku_set_event_callback(callBack); - char *configJSON = "{\"host\": \"0.0.0.0\", \"port\": 60000, \"logLevel\":\"error\"}"; + char *configJSON = "{\"host\": \"0.0.0.0\", \"port\": 60000, \"logLevel\":\"error\", \"store\":true}"; response = waku_new(configJSON); // configJSON can be NULL too to use defaults if (isError(response)) return 1; @@ -84,7 +86,7 @@ int main(int argc, char *argv[]) response = waku_connect("/dns4/node-01.gc-us-central1-a.wakuv2.test.statusim.net/tcp/30303/p2p/16Uiu2HAmJb2e28qLXxT5kZxVUUoJt72EMzNGXB47Rxx5hw3q4YjS", 0); // Connect to a node if (isError(response)) - return 1; + printf("Could not connect to node: %s\n", response); /* @@ -118,7 +120,7 @@ int main(int argc, char *argv[]) char wakuMsg[1000]; char *msgPayload = waku_utils_base64_encode("Hello World!"); char *contentTopic = waku_content_topic("example", 1, "default", "rfc26"); - sprintf(wakuMsg, "{\"payload\":\"%s\",\"contentTopic\":\"%s\",\"timestamp\":%d}", msgPayload, contentTopic, i); + sprintf(wakuMsg, "{\"payload\":\"%s\",\"contentTopic\":\"%s\",\"timestamp\":%lu}", msgPayload, contentTopic, nowInNanosecs()); free(msgPayload); free(contentTopic); @@ -134,6 +136,17 @@ int main(int argc, char *argv[]) sleep(1); } + + // To retrieve messages from local store, set store:true in the node config, and use waku_store_local_query + /* + char query[1000]; + sprintf(query, "{\"pubsubTopic\":\"%s\", \"pagingOptions\":{\"pageSize\": 40, \"forward\":false}}", waku_default_pubsub_topic()); + response = waku_store_local_query(query); + if (isError(response)) + return 1; + printf("%s\n",response); + */ + response = waku_stop(); if (isError(response)) return 1; diff --git a/examples/c-bindings/main.h b/examples/c-bindings/main.h index aab99066..31e9a95a 100644 --- a/examples/c-bindings/main.h +++ b/examples/c-bindings/main.h @@ -4,6 +4,29 @@ #include #include "nxjson.c" +/// Convert seconds to nanoseconds +#define SEC_TO_NS(sec) ((sec)*1000000000) + + +uint64_t nowInNanosecs(){ + uint64_t nanoseconds; + struct timespec ts; + int return_code = timespec_get(&ts, TIME_UTC); + if (return_code == 0) + { + printf("Failed to obtain timestamp.\n"); + nanoseconds = UINT64_MAX; // use this to indicate error + } + else + { + // `ts` now contains your timestamp in seconds and nanoseconds! To + // convert the whole struct to nanoseconds, do this: + nanoseconds = SEC_TO_NS((uint64_t)ts.tv_sec) + (uint64_t)ts.tv_nsec; + } + return nanoseconds; +} + + bool isError(char *input) { char *jsonStr = malloc(strlen(input) + 1);