Skip to content

Commit

Permalink
feat(c-bindings): support creating multiple instances (#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos authored Dec 15, 2023
1 parent 097123a commit 5e3c9fd
Show file tree
Hide file tree
Showing 28 changed files with 918 additions and 469 deletions.
31 changes: 17 additions & 14 deletions examples/c-bindings/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ void on_error(int ret, const char *result, void *user_data)
return;
}

printf("function execution failed. Returned code: %d\n", ret);
printf("function execution failed. Returned code: %d, %s\n", ret, result);
exit(1);
}

void on_response(int ret, const char *result, void *user_data)
{
if (ret != 0)
{
printf("function execution failed. Returned code: %d\n", ret);
printf("function execution failed. Returned code: %d, %s\n", ret, result);
exit(1);
}

Expand Down Expand Up @@ -119,26 +119,26 @@ void callBack(int ret, const char *signal, void *user_data)

int main(int argc, char *argv[])
{
// Set callback to be executed each time a message is received
waku_set_event_callback(callBack);

// configJSON can be NULL too to use defaults. Any value not defined will have
// a default set
char *configJSON = "{\"host\": \"0.0.0.0\", \"port\": 60000, "
"\"logLevel\":\"error\", \"store\":true}";
waku_new(configJSON, on_error, NULL);
void* ctx = waku_new(configJSON, on_error, NULL);

// Set callback to be executed each time a message is received
waku_set_event_callback(ctx, callBack);

// Start the node, enabling the waku protocols
waku_start(on_error, NULL);
waku_start(ctx, on_error, NULL);

// Obtain the node's peerID
char *peerID = NULL;
waku_peerid(on_response, (void *)&peerID);
waku_peerid(ctx, on_response, (void *)&peerID);
printf("PeerID: %s\n", peerID);

// Obtain the node's multiaddresses
char *addresses = NULL;
waku_listen_addresses(on_response, (void *)&addresses);
waku_listen_addresses(ctx, on_response, (void *)&addresses);
printf("Addresses: %s\n", addresses);

// Build a content topic
Expand All @@ -154,12 +154,12 @@ int main(int argc, char *argv[])

// To use dns discovery, and retrieve nodes from a enrtree url
char *discoveredNodes = NULL;
waku_dns_discovery("enrtree://AO47IDOLBKH72HIZZOXQP6NMRESAN7CHYWIBNXDXWRJRZWLODKII6@test.wakuv2.nodes.status.im",
waku_dns_discovery(ctx, "enrtree://AO47IDOLBKH72HIZZOXQP6NMRESAN7CHYWIBNXDXWRJRZWLODKII6@test.wakuv2.nodes.status.im",
"", 0, on_response, (void *)&discoveredNodes);
printf("Discovered nodes: %s\n", discoveredNodes);

// Connect to a node
waku_connect("/dns4/node-01.do-ams3.wakuv2.test.statusim.net/tcp/30303/"
waku_connect(ctx, "/dns4/node-01.do-ams3.wakuv2.test.statusim.net/tcp/30303/"
"p2p/16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ",
0, on_response, NULL);

Expand All @@ -176,7 +176,7 @@ int main(int argc, char *argv[])
sprintf(contentFilter,
"{\"pubsubTopic\":\"%s\",\"contentTopics\":[\"%s\"]}",
defaultPubsubTopic, contentTopic);
waku_relay_subscribe(contentFilter, on_error, NULL);
waku_relay_subscribe(ctx, contentFilter, on_error, NULL);

int i = 0;
int version = 1;
Expand All @@ -202,7 +202,7 @@ int main(int argc, char *argv[])

// Broadcast via waku relay
char *messageID = NULL;
waku_relay_publish(encodedMessage, defaultPubsubTopic, 0, on_response,
waku_relay_publish(ctx, encodedMessage, defaultPubsubTopic, 0, on_response,
(void *)&messageID);
printf("MessageID: %s\n", messageID);

Expand All @@ -221,7 +221,10 @@ int main(int argc, char *argv[])
// printf("%s\n", local_result);

// Stop the node's execution
waku_stop(on_response, NULL);
waku_stop(ctx, on_response, NULL);

// Release resources allocated to waku
waku_free(ctx, on_response, NULL);

// TODO: free all char*

Expand Down
Loading

0 comments on commit 5e3c9fd

Please sign in to comment.