Skip to content

Commit

Permalink
Keyexpr alignment with zenoh-c (#516)
Browse files Browse the repository at this point in the history
* rename z_keyexpr_to_string into z_keyexpr_as_view_string

* added missing keyexpr constructors;
updated tests;

* added z_keyexpr_join and z_keyexpr_concat

* added z_keyexpr_relation_to

* c99 fixes

* c99 fixes

* typo fix

* fix z_keyexpr_from_substr_autocanonize api

* address review commens;
swap arguments of z_undeclare_keyexpr;

* .pc update

* if format
  • Loading branch information
DenisBiryukov91 authored Jul 9, 2024
1 parent 7e1a46b commit a83be07
Show file tree
Hide file tree
Showing 59 changed files with 459 additions and 216 deletions.
8 changes: 7 additions & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ Primitives
.. autocfunction:: primitives.h::z_view_keyexpr_from_str
.. autocfunction:: primitives.h::z_view_keyexpr_from_str_unchecked
.. autocfunction:: primitives.h::z_view_keyexpr_from_str_autocanonize
.. autocfunction:: primitives.h::z_keyexpr_to_string
.. autocfunction:: primitives.h::z_keyexpr_as_view_string
.. autocfunction:: primitives.h::z_keyexpr_is_initialized
.. autocfunction:: primitives.h::z_keyexpr_is_canon
.. autocfunction:: primitives.h::zp_keyexpr_is_canon_null_terminated
Expand All @@ -311,6 +311,9 @@ Primitives
.. autocfunction:: primitives.h::zp_keyexpr_intersect_null_terminated
.. autocfunction:: primitives.h::z_keyexpr_equals
.. autocfunction:: primitives.h::zp_keyexpr_equals_null_terminated
.. autocfunction:: primitives.h::z_keyexpr_relation_to
.. autocfunction:: primitives.h::z_keyexpr_concat
.. autocfunction:: primitives.h::z_keyexpr_join
.. autocfunction:: primitives.h::z_config_new
.. autocfunction:: primitives.h::z_config_default
.. autocfunction:: primitives.h::zp_config_get
Expand Down Expand Up @@ -422,6 +425,9 @@ Primitives
.. autocfunction:: primitives.h::z_query_reply_options_default
.. autocfunction:: primitives.h::z_query_reply
.. autocfunction:: primitives.h::z_keyexpr_from_str
.. autocfunction:: primitives.h::z_keyexpr_from_substr
.. autocfunction:: primitives.h::z_keyexpr_from_str_autocanonize
.. autocfunction:: primitives.h::z_keyexpr_from_substr_autocanonize
.. autocfunction:: primitives.h::z_declare_keyexpr
.. autocfunction:: primitives.h::z_undeclare_keyexpr
.. autocfunction:: primitives.h::z_subscriber_options_default
Expand Down
7 changes: 3 additions & 4 deletions examples/arduino/z_get.ino
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,17 @@ void reply_handler(const z_loaned_reply_t *oreply, void *ctx) {
(void)(ctx);
if (z_reply_is_ok(oreply)) {
const z_loaned_sample_t *sample = z_reply_ok(oreply);
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
z_view_string_t keystr;
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr);
z_owned_string_t replystr;
z_bytes_deserialize_into_string(z_sample_payload(sample), &replystr);

Serial.print(" >> [Get listener] Received (");
Serial.print(z_string_data(z_string_loan(&keystr)));
Serial.print(z_string_data(z_view_string_loan(&keystr)));
Serial.print(", ");
Serial.print(z_string_data(z_string_loan(&replystr)));
Serial.println(")");

z_string_drop(z_string_move(&keystr));
z_string_drop(z_string_move(&replystr));
} else {
Serial.println(" >> Received an error");
Expand Down
7 changes: 3 additions & 4 deletions examples/arduino/z_pull.ino
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,16 @@

// @TODO
// void data_handler(const z_loaned_sample_t *sample, void *arg) {
// z_owned_string_t keystr;
// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
// z_view_string_t keystr;
// z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr);
// std::string val((const char *)sample->payload.start, sample->payload.len);

// Serial.print(" >> [Subscription listener] Received (");
// Serial.print(z_string_data(z_string_loan(&keystr)));
// Serial.print(z_string_data(z_view_string_loan(&keystr)));
// Serial.print(", ");
// Serial.print(val.c_str());
// Serial.println(")");

// z_string_drop(z_string_move(&keystr));
// }

void setup() {
Expand Down
8 changes: 3 additions & 5 deletions examples/arduino/z_queryable.ino
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
#define VALUE "[ARDUINO]{ESP32} Queryable from Zenoh-Pico!"

void query_handler(const z_loaned_query_t *query, void *arg) {
z_owned_string_t keystr;
z_keyexpr_to_string(z_query_keyexpr(query), &keystr);
z_view_string_t keystr;
z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr);
Serial.print(" >> [Queryable handler] Received Query '");
Serial.print(z_string_data(z_string_loan(&keystr)));
Serial.print(z_string_data(z_view_string_loan(&keystr)));
Serial.println("'");

// Process value
Expand All @@ -60,8 +60,6 @@ void query_handler(const z_loaned_query_t *query, void *arg) {
z_bytes_serialize_from_str(&reply_payload, VALUE);

z_query_reply(query, z_view_keyexpr_loan(&ke), z_bytes_move(&reply_payload), NULL);

z_string_drop(z_string_move(&keystr));
}

void setup() {
Expand Down
7 changes: 3 additions & 4 deletions examples/arduino/z_sub.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,17 @@
#define KEYEXPR "demo/example/**"

void data_handler(const z_loaned_sample_t *sample, void *arg) {
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
z_view_string_t keystr;
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr);
z_owned_string_t value;
z_bytes_deserialize_into_string(z_sample_payload(sample), &value);

Serial.print(" >> [Subscription listener] Received (");
Serial.print(z_string_data(z_string_loan(&keystr)));
Serial.print(z_string_data(z_view_string_loan(&keystr)));
Serial.print(", ");
Serial.print(z_string_data(z_string_loan(&value)));
Serial.println(")");

z_string_drop(z_string_move(&keystr));
z_string_drop(z_string_move(&value));
}

Expand Down
5 changes: 2 additions & 3 deletions examples/espidf/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,12 @@ void reply_dropper(void *ctx) { printf(" >> Received query final notification\n"
void reply_handler(const z_loaned_reply_t *oreply, void *ctx) {
if (z_reply_is_ok(oreply)) {
const z_loaned_sample_t *sample = z_reply_ok(oreply);
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
z_view_string_t keystr;
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr);
z_owned_string_t replystr;
z_bytes_deserialize_into_string(z_sample_payload(sample), &replystr);

printf(" >> Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(replystr)));
z_drop(z_move(keystr));
z_drop(z_move(replystr));
} else {
printf(" >> Received an error\n");
Expand Down
5 changes: 2 additions & 3 deletions examples/espidf/z_pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,11 @@ void wifi_init_sta(void) {

// @TODO
// void data_handler(const z_loaned_sample_t* sample, void* arg) {
// z_owned_string_t keystr;
// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
// z_view_string_t keystr;
// z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr);
// printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)),
// (int)sample->payload.len,
// sample->payload.start);
// z_drop(z_move(keystr));
// }

void app_main() {
Expand Down
5 changes: 2 additions & 3 deletions examples/espidf/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ void wifi_init_sta(void) {

void query_handler(const z_loaned_query_t *query, void *ctx) {
(void)(ctx);
z_owned_string_t keystr;
z_keyexpr_to_string(z_query_keyexpr(query), &keystr);
z_view_string_t keystr;
z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr);
z_view_string_t params;
z_query_parameters(query, &params);
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), (int)z_loan(params)->len,
Expand All @@ -125,7 +125,6 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
z_bytes_serialize_from_str(&reply_payload, VALUE);

z_query_reply(query, z_loan(ke), z_move(reply_payload), NULL);
z_drop(z_move(keystr));
}

void app_main() {
Expand Down
7 changes: 3 additions & 4 deletions examples/espidf/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,12 @@ void wifi_init_sta(void) {
}

void data_handler(const z_loaned_sample_t* sample, void* arg) {
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
z_view_string_t keystr;
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr);
z_owned_string_t value;
z_bytes_deserialize_into_string(z_sample_payload(sample), &value);
printf(" >> [Subscriber handler] Received ('%s': '%s')\n", z_string_data(z_string_loan(&keystr)),
printf(" >> [Subscriber handler] Received ('%s': '%s')\n", z_string_data(z_view_string_loan(&keystr)),
z_string_data(z_string_loan(&value)));
z_string_drop(z_string_move(&keystr));
z_string_drop(z_string_move(&value));
}

Expand Down
5 changes: 2 additions & 3 deletions examples/freertos_plus_tcp/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) {
(void)(ctx);
if (z_reply_is_ok(reply)) {
const z_loaned_sample_t *sample = z_reply_ok(reply);
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
z_view_string_t keystr;
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr);
z_owned_string_t replystr;
z_bytes_deserialize_into_string(z_sample_payload(sample), &replystr);

printf(">> Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(replystr)));
z_drop(z_move(keystr));
z_drop(z_move(replystr));
} else {
printf(">> Received an error\n");
Expand Down
5 changes: 2 additions & 3 deletions examples/freertos_plus_tcp/z_pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@
// @TODO
// void data_handler(const z_loaned_sample_t *sample, void *ctx) {
// (void)(ctx);
// z_owned_string_t keystr;
// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
// z_view_string_t keystr;
// z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr);
// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)sample->payload.len,
// sample->payload.start);
// z_drop(z_move(keystr));
// }

void app_main(void) {
Expand Down
2 changes: 1 addition & 1 deletion examples/freertos_plus_tcp/z_put.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void app_main(void) {
}

// Clean up
z_undeclare_keyexpr(z_loan(s), z_move(ke));
z_undeclare_keyexpr(z_move(ke), z_loan(s));
zp_stop_read_task(z_loan_mut(s));
zp_stop_lease_task(z_loan_mut(s));
z_close(z_move(s));
Expand Down
5 changes: 2 additions & 3 deletions examples/freertos_plus_tcp/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@

void query_handler(const z_loaned_query_t *query, void *ctx) {
(void)(ctx);
z_owned_string_t keystr;
z_keyexpr_to_string(z_query_keyexpr(query), &keystr);
z_view_string_t keystr;
z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr);
z_view_string_t params;
z_query_parameters(query, &params);
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), (int)z_loan(params)->len,
Expand All @@ -52,7 +52,6 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
z_bytes_serialize_from_str(&reply_payload, VALUE);

z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options);
z_drop(z_move(keystr));
}

void app_main(void) {
Expand Down
5 changes: 2 additions & 3 deletions examples/freertos_plus_tcp/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@

void data_handler(const z_loaned_sample_t *sample, void *ctx) {
(void)(ctx);
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
z_view_string_t keystr;
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr);
z_owned_string_t value;
z_bytes_deserialize_into_string(z_sample_payload(sample), &value);
printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value)));
z_drop(z_move(keystr));
z_drop(z_move(value));
}

Expand Down
5 changes: 2 additions & 3 deletions examples/freertos_plus_tcp/z_sub_st.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ int msg_nb = 0;

void data_handler(const z_loaned_sample_t *sample, void *ctx) {
(void)(ctx);
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
z_view_string_t keystr;
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr);
z_owned_string_t value;
z_bytes_deserialize_into_string(z_sample_payload(sample), &value);
printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value)));
z_drop(z_move(keystr));
z_drop(z_move(value));
msg_nb++;
}
Expand Down
7 changes: 3 additions & 4 deletions examples/mbed/z_get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ void reply_dropper(void *ctx) { printf(" >> Received query final notification\n"
void reply_handler(const z_loaned_reply_t *oreply, void *ctx) {
if (z_reply_is_ok(oreply)) {
const z_loaned_sample_t *sample = z_reply_ok(oreply);
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
z_view_string_t keystr;
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr);
z_owned_string_t replystr;
z_bytes_deserialize_into_string(z_sample_payload(sample), &replystr);

printf(" >> Received ('%s': '%s')\n", z_string_data(z_string_loan(&keystr)),
printf(" >> Received ('%s': '%s')\n", z_string_data(z_view_string_loan(&keystr)),
z_string_data(z_string_loan(&replystr)));
z_string_drop(z_string_move(&keystr));
z_string_drop(z_string_move(&replystr));
} else {
printf(" >> Received an error\n");
Expand Down
7 changes: 3 additions & 4 deletions examples/mbed/z_pull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@

// @TODO
// void data_handler(const z_loaned_sample_t *sample, void *arg) {
// z_owned_string_t keystr;
// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
// printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_string_data(z_string_loan(&keystr)),
// z_view_string_t keystr;
// z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr);
// printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_string_data(z_view_string_loan(&keystr)),
// (int)sample->payload.len,
// sample->payload.start);
// z_string_drop(z_string_move(&keystr));
// }

int main(int argc, char **argv) {
Expand Down
7 changes: 3 additions & 4 deletions examples/mbed/z_queryable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@

void query_handler(const z_loaned_query_t *query, void *ctx) {
(void)(ctx);
z_owned_string_t keystr;
z_keyexpr_to_string(z_query_keyexpr(query), &keystr);
z_view_string_t keystr;
z_keyexpr_as_view_string(z_query_keyexpr(query), &keystr);
z_view_string_t pred;
z_query_parameters(query, &pred);
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_string_loan(&keystr)),
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_view_string_loan(&keystr)),
(int)z_view_string_loan(&pred)->len, z_view_string_loan(&pred)->val);
// Process value
z_owned_string_t payload_string;
Expand All @@ -52,7 +52,6 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
z_bytes_serialize_from_str(&reply_payload, VALUE);

z_query_reply(query, z_query_keyexpr(query), z_bytes_move(&reply_payload), NULL);
z_string_drop(z_string_move(&keystr));
}

int main(int argc, char **argv) {
Expand Down
7 changes: 3 additions & 4 deletions examples/mbed/z_sub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@
#define KEYEXPR "demo/example/**"

void data_handler(const z_loaned_sample_t *sample, void *arg) {
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
z_view_string_t keystr;
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr);
z_owned_string_t value;
z_bytes_deserialize_into_string(z_sample_payload(sample), &value);
printf(" >> [Subscriber handler] Received ('%s': '%s')\n", z_string_data(z_string_loan(&keystr)),
printf(" >> [Subscriber handler] Received ('%s': '%s')\n", z_string_data(z_view_string_loan(&keystr)),
z_string_data(z_string_loan(&value)));
z_string_drop(z_string_move(&keystr));
z_string_drop(z_string_move(&value));
}

Expand Down
5 changes: 2 additions & 3 deletions examples/unix/c11/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) {
(void)(ctx);
if (z_reply_is_ok(reply)) {
const z_loaned_sample_t *sample = z_reply_ok(reply);
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
z_view_string_t keystr;
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr);
z_owned_string_t replystr;
z_bytes_deserialize_into_string(z_sample_payload(sample), &replystr);

printf(">> Received %s ('%s': '%s')\n", kind_to_str(z_sample_kind(sample)), z_string_data(z_loan(keystr)),
z_string_data(z_loan(replystr)));
z_drop(z_move(keystr));
z_drop(z_move(replystr));
} else {
printf(">> Received an error\n");
Expand Down
5 changes: 2 additions & 3 deletions examples/unix/c11/z_get_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) {
(void)(ctx);
if (z_reply_is_ok(reply)) {
const z_loaned_sample_t *sample = z_reply_ok(reply);
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
z_view_string_t keystr;
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr);
z_owned_string_t replystr;
z_bytes_deserialize_into_string(z_sample_payload(sample), &replystr);
z_owned_string_t encoding;
Expand All @@ -121,7 +121,6 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) {
}
drop_attachment(&kvp);

z_drop(z_move(keystr));
z_drop(z_move(replystr));
z_drop(z_move(encoding));
} else {
Expand Down
5 changes: 2 additions & 3 deletions examples/unix/c11/z_get_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,12 @@ int main(int argc, char **argv) {
for (z_recv(z_loan(handler), &reply); z_check(reply); z_recv(z_loan(handler), &reply)) {
if (z_reply_is_ok(z_loan(reply))) {
const z_loaned_sample_t *sample = z_reply_ok(z_loan(reply));
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
z_view_string_t keystr;
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr);
z_owned_string_t replystr;
z_bytes_deserialize_into_string(z_sample_payload(sample), &replystr);

printf(">> Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(replystr)));
z_drop(z_move(keystr));
z_drop(z_move(replystr));
} else {
printf(">> Received an error\n");
Expand Down
Loading

0 comments on commit a83be07

Please sign in to comment.