Skip to content

Commit

Permalink
Strings rework
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed May 22, 2024
1 parent 1fcd37f commit 6159305
Show file tree
Hide file tree
Showing 64 changed files with 411 additions and 465 deletions.
9 changes: 5 additions & 4 deletions examples/arduino/z_get.ino
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +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_str_t keystr;
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
std::string val((const char *)z_sample_payload(sample)->start, z_sample_payload(sample)->len);

Serial.print(" >> [Get listener] Received (");
Serial.print(z_str_data(z_str_loan(&keystr)));
Serial.print(z_str_data(z_string_loan(&keystr)));
Serial.print(", ");
Serial.print(val.c_str());
Serial.println(")");

z_str_drop(z_str_move(&keystr));
z_string_drop(z_string_move(&keystr));
} else {
Serial.println(" >> Received an error");
}
Expand Down Expand Up @@ -114,7 +114,8 @@ void loop() {
z_get_options_t opts;
z_get_options_default(&opts);
if (strcmp(VALUE, "") != 0) {
opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE));
// TODO(sashacmc): encoding
// opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE));
}
z_owned_closure_reply_t callback;
z_closure_reply(&callback, reply_handler, reply_dropper, NULL);
Expand Down
6 changes: 3 additions & 3 deletions examples/arduino/z_pull.ino
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@

// @TODO
// void data_handler(const z_loaned_sample_t *sample, void *arg) {
// z_owned_str_t keystr;
// z_owned_string_t keystr;
// z_keyexpr_to_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_str_data(z_str_loan(&keystr)));
// Serial.print(z_str_data(z_string_loan(&keystr)));
// Serial.print(", ");
// Serial.print(val.c_str());
// Serial.println(")");

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

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

void query_handler(const z_loaned_query_t *query, void *arg) {
z_owned_str_t keystr;
z_owned_string_t keystr;
z_keyexpr_to_string(z_query_keyexpr(query), &keystr);

Serial.print(" >> [Queryable handler] Replying Data ('");
Serial.print(z_str_data(z_str_loan(&keystr)));
Serial.print(z_str_data(z_string_loan(&keystr)));
Serial.print("': '");
Serial.print(VALUE);
Serial.println("')");

z_view_keyexpr_t ke;
z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR);
z_query_reply(query, z_view_keyexpr_loan(&ke), (const unsigned char *)VALUE, strlen(VALUE), NULL);
z_owned_bytes_t reply_payload;
// TODO(sashacmc): VALUE encoding
z_query_reply(query, z_view_keyexpr_loan(&ke), z_bytes_move(&reply_payload), NULL);

z_str_drop(z_str_move(&keystr));
z_string_drop(z_string_move(&keystr));
}

void setup() {
Expand Down
23 changes: 11 additions & 12 deletions examples/arduino/z_scout.ino
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,18 @@ void fprintwhatami(unsigned int whatami) {
}
}

void fprintlocators(const z_str_array_t *locs) {
void fprintlocators(const z_loaned_string_array_t *locs) {
Serial.print("[");
(void)locs;
// TODO(sashacmc): z_str_array_t
// size_t len = z_str_array_len(locs);
// for (unsigned int i = 0; i < len; i++) {
// Serial.print("'");
// Serial.print(*z_str_array_get(locs, i));
// Serial.print("'");
// if (i < len - 1) {
// Serial.print(", ");
// }
//}
size_t len = z_string_array_len(locs);
for (unsigned int i = 0; i < len; i++) {
Serial.print("'");
const z_loaned_string_t *str = z_string_array_get(locs, i);
Serial.print(str->val);
Serial.print("'");
if (i < len - 1) {
Serial.print(", ");
}
}
Serial.print("]");
}

Expand Down
6 changes: 3 additions & 3 deletions examples/arduino/z_sub.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@
#define KEYEXPR "demo/example/**"

void data_handler(const z_loaned_sample_t *sample, void *arg) {
z_owned_str_t keystr;
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
std::string val((const char *)z_sample_payload(sample)->start, z_sample_payload(sample)->len);

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

z_str_drop(z_str_move(&keystr));
z_string_drop(z_string_move(&keystr));
}

void setup() {
Expand Down
5 changes: 3 additions & 2 deletions examples/espidf/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ 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_str_t keystr;
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);

printf(" >> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)z_sample_payload(sample)->len,
Expand Down Expand Up @@ -161,7 +161,8 @@ void app_main() {
z_get_options_t opts;
z_get_options_default(&opts);
if (strcmp(VALUE, "") != 0) {
opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE));
// TODO(sashacmc): encoding
// opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE));
}
z_owned_closure_reply_t callback;
z_closure(&callback, reply_handler, reply_dropper);
Expand Down
2 changes: 1 addition & 1 deletion examples/espidf/z_pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void wifi_init_sta(void) {

// @TODO
// void data_handler(const z_loaned_sample_t* sample, void* arg) {
// z_owned_str_t keystr;
// z_owned_string_t keystr;
// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
// printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)),
// (int)sample->payload.len,
Expand Down
16 changes: 9 additions & 7 deletions examples/espidf/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,17 @@ void wifi_init_sta(void) {

void query_handler(const z_loaned_query_t *query, void *ctx) {
(void)(ctx);
z_owned_str_t keystr;
// TODO(sashacmc): z_query_parameters
// 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_data(z_loan(keystr)), (int)pred.len,
// pred.start);
z_owned_string_t keystr;
z_keyexpr_to_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_str_data(z_loan(keystr)), (int)z_loan(params)->len,
z_loan(params)->val);
z_view_keyexpr_t ke;
z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR);
z_query_reply(query, z_loan(ke), (const unsigned char *)VALUE, strlen(VALUE), NULL);
z_owned_bytes_t reply_payload;
// TODO(sashacmc): VALUE encoding
z_query_reply(query, z_loan(ke), z_move(reply_payload), NULL);
z_drop(z_move(keystr));
}

Expand Down
21 changes: 10 additions & 11 deletions examples/espidf/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,17 @@ void fprintwhatami(FILE *stream, unsigned int whatami) {
}
}

void fprintlocators(FILE *stream, const z_str_array_t *locs) {
void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) {
fprintf(stream, "[");
(void)locs;
// TODO(sashacmc): z_str_array missed
// 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, ", ");
// }
//}
for (unsigned int i = 0; i < z_string_array_len(locs); i++) {
fprintf(stream, "\"");
const z_loaned_string_t *str = z_string_array_get(locs, i);
fprintf(stream, "%.*s", (int)str->len, str->val);
fprintf(stream, "\"");
if (i < z_string_array_len(locs) - 1) {
fprintf(stream, ", ");
}
}
fprintf(stream, "]");
}

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

void data_handler(const z_loaned_sample_t* sample, void* arg) {
z_owned_str_t keystr;
z_owned_string_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_data(z_str_loan(&keystr)), (int)payload->len,
printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_str_data(z_string_loan(&keystr)), (int)payload->len,
payload->start);
z_str_drop(z_str_move(&keystr));
z_string_drop(z_string_move(&keystr));
}

void app_main() {
Expand Down
5 changes: 3 additions & 2 deletions examples/freertos_plus_tcp/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ 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_str_t keystr;
z_owned_string_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_data(z_loan(keystr)), (int)payload->len, payload->start);
Expand Down Expand Up @@ -84,7 +84,8 @@ void app_main(void) {
z_get_options_t opts;
z_get_options_default(&opts);
if (strcmp(VALUE, "") != 0) {
opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE));
// TODO(sashacmc): encoding
// opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE));
}
z_owned_closure_reply_t callback;
z_closure(&callback, reply_handler, reply_dropper);
Expand Down
2 changes: 1 addition & 1 deletion examples/freertos_plus_tcp/z_pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
// @TODO
// void data_handler(const z_loaned_sample_t *sample, void *ctx) {
// (void)(ctx);
// z_owned_str_t keystr;
// z_owned_string_t keystr;
// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)sample->payload.len,
// sample->payload.start);
Expand Down
22 changes: 12 additions & 10 deletions examples/freertos_plus_tcp/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@

void query_handler(const z_loaned_query_t *query, void *ctx) {
(void)(ctx);
z_owned_str_t keystr;
z_owned_string_t keystr;
z_keyexpr_to_string(z_query_keyexpr(query), &keystr);
// TODO(sashacmc): z_query_parameters
// z_bytes_t pred = z_query_parameters(query);
// z_value_t payload_value = z_query_value(query);
// printf(" >> [Queryable handler] Received Query '%s?%.*s'\n", z_str_data(z_loan(keystr)), (int)pred.len,
// pred.start); if (payload_value.payload.len > 0) {
// printf(" with value '%.*s'\n", (int)payload_value.payload.len, payload_value.payload.start);
// }
z_view_string_t params;
z_query_parameters(query, &params);
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), (int)z_loan(params)->len,
z_loan(params)->val);
const z_loaned_value_t *payload_value = z_query_value(query);
if (payload_value->payload.len > 0) {
printf(" with value '%.*s'\n", (int)payload_value->payload.len, payload_value->payload.start);
}
z_query_reply_options_t options;
z_query_reply_options_default(&options);
options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL);
z_query_reply(query, z_query_keyexpr(query), (const unsigned char *)VALUE, strlen(VALUE), &options);
z_owned_bytes_t reply_payload;
// TODO(sashacmc): VALUE encoding
z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options);
z_drop(z_move(keystr));
}

Expand Down
21 changes: 10 additions & 11 deletions examples/freertos_plus_tcp/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,17 @@ void fprintwhatami(FILE *stream, unsigned int whatami) {
}
}

void fprintlocators(FILE *stream, const z_str_array_t *locs) {
void fprintlocators(FILE *stream, const z_loaned_string_array_t *locs) {
fprintf(stream, "[");
(void)locs;
// TODO(sashacmc): z_str_array missed
// 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, ", ");
// }
//}
for (unsigned int i = 0; i < z_string_array_len(locs); i++) {
fprintf(stream, "\"");
const z_loaned_string_t *str = z_string_array_get(locs, i);
fprintf(stream, "%.*s", (int)str->len, str->val);
fprintf(stream, "\"");
if (i < z_string_array_len(locs) - 1) {
fprintf(stream, ", ");
}
}
fprintf(stream, "]");
}

Expand Down
2 changes: 1 addition & 1 deletion examples/freertos_plus_tcp/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

void data_handler(const z_loaned_sample_t *sample, void *ctx) {
(void)(ctx);
z_owned_str_t keystr;
z_owned_string_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_data(z_loan(keystr)), (int)payload->len, payload->start);
Expand Down
2 changes: 1 addition & 1 deletion examples/freertos_plus_tcp/z_sub_st.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int msg_nb = 0;

void data_handler(const z_loaned_sample_t *sample, void *ctx) {
(void)(ctx);
z_owned_str_t keystr;
z_owned_string_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_data(z_loan(keystr)), (int)payload->len, payload->start);
Expand Down
9 changes: 5 additions & 4 deletions examples/mbed/z_get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ 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_str_t keystr;
z_owned_string_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_data(z_str_loan(&keystr)), (int)payload->len, payload->start);
z_str_drop(z_str_move(&keystr));
printf(" >> Received ('%s': '%.*s')\n", z_str_data(z_string_loan(&keystr)), (int)payload->len, payload->start);
z_string_drop(z_string_move(&keystr));
} else {
printf(" >> Received an error\n");
}
Expand Down Expand Up @@ -80,7 +80,8 @@ int main(int argc, char **argv) {
z_get_options_t opts;
z_get_options_default(&opts);
if (strcmp(VALUE, "") != 0) {
opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE));
// TODO(sashacmc): encoding
// opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE));
}
z_owned_closure_reply_t callback;
z_closure_reply(&callback, reply_handler, reply_dropper, NULL);
Expand Down
6 changes: 3 additions & 3 deletions examples/mbed/z_pull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@

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

int main(int argc, char **argv) {
Expand Down
Loading

0 comments on commit 6159305

Please sign in to comment.