Skip to content

Commit

Permalink
Fix c99 examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed May 15, 2024
1 parent 9932daa commit 5569bf8
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 42 deletions.
5 changes: 3 additions & 2 deletions examples/espidf/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ void wifi_init_sta(void) {
void data_handler(const z_loaned_sample_t* sample, void* arg) {
z_owned_str_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_loan(&keystr), (int)sample->payload.len,
sample->payload.start);
const z_loaned_payload_t* payload = z_sample_payload(sample);
printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)payload->len,
payload->start);
z_str_drop(z_str_move(&keystr));
}

Expand Down
2 changes: 1 addition & 1 deletion examples/mbed/z_get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void reply_handler(z_owned_reply_t *oreply, void *ctx) {
z_owned_str_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
const z_loaned_bytes_t *payload = z_sample_payload(sample);
printf(" >> Received ('%s': '%.*s')\n", z_str_loan(&keystr), (int)payload->len, payload->start);
printf(" >> Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)payload->len, payload->start);
z_str_drop(z_str_move(&keystr));
} else {
printf(" >> Received an error\n");
Expand Down
2 changes: 1 addition & 1 deletion examples/mbed/z_pull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
// void data_handler(const z_loaned_sample_t *sample, void *arg) {
// z_owned_str_t keystr;
// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
// printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_loan(&keystr), (int)sample->payload.len,
// printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)sample->payload.len,
// sample->payload.start);
// z_str_drop(z_str_move(&keystr));
// }
Expand Down
6 changes: 4 additions & 2 deletions examples/mbed/z_queryable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
(void)(ctx);
z_owned_str_t keystr;
z_keyexpr_to_string(z_query_keyexpr(query), &keystr);
z_bytes_t pred = z_query_parameters(query);
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_loan(&keystr), (int)pred.len, pred.start);
z_view_str_t pred;
z_query_parameters(query, &pred);
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_str_loan(&keystr)),
(int)z_view_str_loan(&pred)->len, z_view_str_loan(&pred)->val);
z_query_reply(query, z_keyexpr(KEYEXPR), (const unsigned char *)VALUE, strlen(VALUE), NULL);
z_str_drop(z_str_move(&keystr));
}
Expand Down
2 changes: 1 addition & 1 deletion examples/mbed/z_sub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) {
z_owned_str_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
const z_loaned_bytes_t *payload = z_sample_payload(sample);
printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_loan(&keystr), (int)payload->len,
printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)payload->len,
payload->start);
z_str_drop(z_str_move(&keystr));
}
Expand Down
6 changes: 3 additions & 3 deletions examples/unix/c99/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void reply_handler(z_owned_reply_t *reply, void *ctx) {
z_owned_str_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
const z_loaned_bytes_t *payload = z_sample_payload(sample);
printf(">> Received ('%s': '%.*s')\n", z_str_loan(&keystr), (int)payload->len, payload->start);
printf(">> Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)payload->len, payload->start);
z_str_drop(z_str_move(&keystr));
} else {
printf(">> Received an error\n");
Expand Down Expand Up @@ -107,8 +107,8 @@ int main(int argc, char **argv) {
return -1;
}

z_keyexpr_t ke = z_keyexpr(keyexpr);
if (!z_keyexpr_is_initialized(&ke)) {
const z_loaned_keyexpr_t *ke = z_keyexpr(keyexpr);
if (!z_keyexpr_is_initialized(ke)) {
printf("%s is not a valid key expression", keyexpr);
return -1;
}
Expand Down
13 changes: 8 additions & 5 deletions examples/unix/c99/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
static z_condvar_t cond;
static z_mutex_t mutex;

void callback(const z_sample_t* sample, void* context) {
void callback(const z_loaned_sample_t* sample, void* context) {
(void)sample;
(void)context;
z_condvar_signal(&cond);
Expand Down Expand Up @@ -80,18 +80,21 @@ int main(int argc, char** argv) {
return -1;
}

z_keyexpr_t ping = z_keyexpr_unchecked("test/ping");
// TODO(sashacmc):
// z_keyexpr_t ping = z_keyexpr_unchecked("test/ping");
z_owned_publisher_t pub;
if (z_declare_publisher(&pub, z_session_loan(&session), ping, NULL) < 0) {
if (z_declare_publisher(&pub, z_session_loan(&session), z_keyexpr("test/ping"), NULL) < 0) {
printf("Unable to declare publisher for key expression!\n");
return -1;
}

z_keyexpr_t pong = z_keyexpr_unchecked("test/pong");
// TODO(sashacmc):
// z_keyexpr_t pong = z_keyexpr_unchecked("test/pong");
z_owned_closure_sample_t respond;
z_closure_sample(&respond, callback, drop, NULL);
z_owned_subscriber_t sub;
if (z_declare_subscriber(&sub, z_session_loan(&session), pong, z_closure_sample_move(&respond), NULL) < 0) {
if (z_declare_subscriber(&sub, z_session_loan(&session), z_keyexpr("test/pong"), z_closure_sample_move(&respond),
NULL) < 0) {
printf("Unable to declare subscriber for key expression.\n");
return -1;
}
Expand Down
17 changes: 9 additions & 8 deletions examples/unix/c99/z_pong.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
#include "zenoh-pico/api/primitives.h"

#if Z_FEATURE_SUBSCRIPTION == 1 && Z_FEATURE_PUBLICATION == 1
void callback(const z_sample_t* sample, void* context) {
z_publisher_t pub = z_publisher_loan((z_owned_publisher_t*)context);
z_bytes_t payload = z_sample_payload(sample);
z_publisher_put(pub, payload.start, payload.len, NULL);
void callback(const z_loaned_sample_t* sample, void* context) {
const z_loaned_publisher_t* pub = z_publisher_loan((z_owned_publisher_t*)context);
const z_loaned_bytes_t* payload = z_sample_payload(sample);
z_publisher_put(pub, payload->start, payload->len, NULL);
}
void drop(void* context) {
z_owned_publisher_t* pub = (z_owned_publisher_t*)context;
Expand Down Expand Up @@ -50,18 +50,19 @@ int main(int argc, char** argv) {
return -1;
}

z_keyexpr_t pong = z_keyexpr_unchecked("test/pong");
// TODO(sashacmc): z_keyexpr_t pong = z_keyexpr_unchecked("test/pong");
z_owned_publisher_t pub;
if (z_declare_publisher(&pub, z_session_loan(&session), pong, NULL) < 0) {
if (z_declare_publisher(&pub, z_session_loan(&session), z_keyexpr("test/pong"), NULL) < 0) {
printf("Unable to declare publisher for key expression!\n");
return -1;
}

z_keyexpr_t ping = z_keyexpr_unchecked("test/ping");
// TODO(sashacmc): z_keyexpr_t ping = z_keyexpr_unchecked("test/ping");
z_owned_closure_sample_t respond;
z_closure_sample(&respond, callback, drop, (void*)z_publisher_move(&pub));
z_owned_subscriber_t sub;
if (z_declare_subscriber(&sub, z_session_loan(&session), ping, z_closure_sample_move(&respond), NULL) < 0) {
if (z_declare_subscriber(&sub, z_session_loan(&session), z_keyexpr("test/ping"), z_closure_sample_move(&respond),
NULL) < 0) {
printf("Unable to declare subscriber for key expression.\n");
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/unix/c99/z_pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
// (void)(ctx);
// z_owned_str_t keystr;
// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_loan(&keystr), (int)sample->payload.len,
// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)sample->payload.len,
// sample->payload.start);
// z_str_drop(z_str_move(&keystr));
// }
Expand Down
10 changes: 6 additions & 4 deletions examples/unix/c99/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
(void)(ctx);
z_owned_str_t keystr;
z_keyexpr_to_string(z_query_keyexpr(query), &keystr);
z_bytes_t pred = z_query_parameters(query);
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_loan(&keystr), (int)pred.len, pred.start);
z_view_str_t pred;
z_query_parameters(query, &pred);
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_str_loan(&keystr)),
(int)z_view_str_loan(&pred)->len, z_view_str_loan(&pred)->val);
z_query_reply_options_t options;
z_query_reply_options_default(&options);
options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL);
Expand Down Expand Up @@ -94,8 +96,8 @@ int main(int argc, char **argv) {
return -1;
}

z_keyexpr_t ke = z_keyexpr(keyexpr);
if (!z_keyexpr_is_initialized(&ke)) {
const z_loaned_keyexpr_t *ke = z_keyexpr(keyexpr);
if (!z_keyexpr_is_initialized(ke)) {
printf("%s is not a valid key expression", keyexpr);
return -1;
}
Expand Down
26 changes: 14 additions & 12 deletions examples/unix/c99/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,26 @@ void fprintwhatami(FILE *stream, unsigned int whatami) {

void fprintlocators(FILE *stream, const z_str_array_t *locs) {
fprintf(stream, "[");
for (unsigned int i = 0; i < z_str_array_len(locs); i++) {
fprintf(stream, "\"");
fprintf(stream, "%s", *z_str_array_get(locs, i));
fprintf(stream, "\"");
if (i < z_str_array_len(locs) - 1) {
fprintf(stream, ", ");
}
}
(void)locs;
// TODO(sashacmc):
// for (unsigned int i = 0; i < z_str_array_len(locs); i++) {
// fprintf(stream, "\"");
// fprintf(stream, "%s", *z_str_array_get(locs, i));
// fprintf(stream, "\"");
// if (i < z_str_array_len(locs) - 1) {
// fprintf(stream, ", ");
// }
//}
fprintf(stream, "]");
}

void fprinthello(FILE *stream, const z_hello_t hello) {
void fprinthello(FILE *stream, const z_loaned_hello_t *hello) {
fprintf(stream, "Hello { zid: ");
fprintzid(stream, hello.zid);
fprintzid(stream, hello->zid);
fprintf(stream, ", whatami: ");
fprintwhatami(stream, hello.whatami);
fprintwhatami(stream, hello->whatami);
fprintf(stream, ", locators: ");
fprintlocators(stream, &hello.locators);
fprintlocators(stream, &hello->locators);
fprintf(stream, " }");
}

Expand Down
2 changes: 1 addition & 1 deletion examples/unix/c99/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) {
z_owned_str_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
const z_loaned_bytes_t *payload = z_sample_payload(sample);
printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_loan(&keystr), (int)payload->len, payload->start);
printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)payload->len, payload->start);
z_str_drop(z_str_move(&keystr));
}

Expand Down
2 changes: 1 addition & 1 deletion examples/unix/c99/z_sub_st.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) {
z_owned_str_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
const z_loaned_bytes_t *payload = z_sample_payload(sample);
printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_loan(&keystr), (int)payload->len, payload->start);
printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)payload->len, payload->start);
z_str_drop(z_str_move(&keystr));
msg_nb++;
}
Expand Down

0 comments on commit 5569bf8

Please sign in to comment.