Skip to content

Commit

Permalink
Strings/bytes queries/reply rework
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed May 22, 2024
1 parent a73f119 commit 37592dc
Show file tree
Hide file tree
Showing 27 changed files with 154 additions and 138 deletions.
3 changes: 2 additions & 1 deletion examples/arduino/z_get.ino
Original file line number Diff line number Diff line change
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
4 changes: 3 additions & 1 deletion examples/arduino/z_queryable.ino
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ void query_handler(const z_loaned_query_t *query, void *arg) {

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_move(reply_payload), NULL);

z_str_drop(z_str_move(&keystr));
}
Expand Down
3 changes: 2 additions & 1 deletion examples/espidf/z_get.c
Original file line number Diff line number Diff line change
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
14 changes: 8 additions & 6 deletions examples/espidf/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,16 @@ void wifi_init_sta(void) {
void query_handler(const z_loaned_query_t *query, void *ctx) {
(void)(ctx);
z_owned_string_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_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
4 changes: 2 additions & 2 deletions examples/espidf/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ 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);
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
3 changes: 2 additions & 1 deletion examples/freertos_plus_tcp/z_get.c
Original file line number Diff line number Diff line change
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
20 changes: 11 additions & 9 deletions examples/freertos_plus_tcp/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@ 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);
// 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
4 changes: 2 additions & 2 deletions examples/mbed/z_get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ void reply_handler(const z_loaned_reply_t *oreply, void *ctx) {
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
4 changes: 2 additions & 2 deletions examples/mbed/z_pull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
// 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_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
9 changes: 6 additions & 3 deletions examples/mbed/z_queryable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
z_keyexpr_to_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_str_data(z_str_loan(&keystr)),
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_string_loan(&keystr)),
(int)z_view_str_loan(&pred)->len, z_view_str_loan(&pred)->val);
z_query_reply(query, z_query_keyexpr(query), (const unsigned char *)VALUE, strlen(VALUE), NULL);
z_str_drop(z_str_move(&keystr));

z_owned_bytes_t reply_payload;
// TODO(sashacmc): VALUE encoding
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
4 changes: 2 additions & 2 deletions examples/mbed/z_sub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ 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);
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));
}

int main(int argc, char **argv) {
Expand Down
3 changes: 2 additions & 1 deletion examples/unix/c11/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ int main(int argc, char **argv) {
z_get_options_t opts;
z_get_options_default(&opts);
if (value != NULL) {
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));
}
#if Z_FEATURE_ATTACHMENT == 1
z_owned_bytes_map_t map = z_bytes_map_new();
Expand Down
3 changes: 2 additions & 1 deletion examples/unix/c11/z_get_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ int main(int argc, char **argv) {
z_get_options_t opts;
z_get_options_default(&opts);
if (value != NULL) {
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_reply_ring_channel_t channel;
z_reply_ring_channel_new(&channel, 1);
Expand Down
19 changes: 11 additions & 8 deletions examples/unix/c11/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ 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);
// 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);
}
#if Z_FEATURE_ATTACHMENT == 1
z_attachment_t attachment = z_query_attachment(query);
if (z_attachment_check(&attachment)) {
Expand All @@ -61,7 +62,9 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
options.attachment = z_bytes_map_as_attachment(&map);
#endif

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));
msg_nb++;

Expand Down
20 changes: 11 additions & 9 deletions examples/unix/c11/z_queryable_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,19 @@ int main(int argc, char **argv) {
const z_loaned_query_t *q = z_loan(query);
z_owned_string_t keystr;
z_keyexpr_to_string(z_query_keyexpr(q), &keystr);
// TODO(sashacmc):
// z_bytes_t pred = z_query_parameters(&q);
// z_value_t payload_value = z_query_value(&q);
// 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(q, &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(q);
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(q, z_loan(ke), (const unsigned char *)value, strlen(value), &options);
z_owned_bytes_t reply_payload;
// TODO(sashacmc): value encoding
z_query_reply(q, z_query_keyexpr(q), z_move(reply_payload), &options);
z_drop(z_move(keystr));
z_drop(z_move(query));
}
Expand Down
7 changes: 4 additions & 3 deletions examples/unix/c99/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ void reply_handler(const z_loaned_reply_t *reply, void *ctx) {
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 @@ -118,7 +118,8 @@ int main(int argc, char **argv) {
z_get_options_t opts;
z_get_options_default(&opts);
if (value != NULL) {
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
4 changes: 2 additions & 2 deletions examples/unix/c99/z_pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
// (void)(ctx);
// z_owned_string_t keystr;
// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
// printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)sample->payload.len,
// printf(">> [Subscriber] 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
14 changes: 8 additions & 6 deletions examples/unix/c99/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ 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 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_view_string_t params;
z_query_parameters(query, &params);
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_string_loan(&keystr)),
(int)z_view_string_loan(&params)->len, z_view_string_loan(&params)->val);
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_str_drop(z_str_move(&keystr));
z_owned_bytes_t reply_payload;
// TODO(sashacmc): value encoding
z_query_reply(query, z_query_keyexpr(query), z_bytes_move(&reply_payload), &options);
z_string_drop(z_string_move(&keystr));
}

int main(int argc, char **argv) {
Expand Down
4 changes: 2 additions & 2 deletions examples/unix/c99/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ 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);
const z_loaned_bytes_t *payload = z_sample_payload(sample);
printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)payload->len,
printf(">> [Subscriber] 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));
}

int main(int argc, char **argv) {
Expand Down
4 changes: 2 additions & 2 deletions examples/unix/c99/z_sub_st.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ 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);
const z_loaned_bytes_t *payload = z_sample_payload(sample);
printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_data(z_str_loan(&keystr)), (int)payload->len,
printf(">> [Subscriber] 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));
msg_nb++;
}

Expand Down
3 changes: 2 additions & 1 deletion examples/windows/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ int main(int argc, char **argv) {
z_get_options_t opts;
z_get_options_default(&opts);
if (value != NULL) {
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
24 changes: 14 additions & 10 deletions examples/windows/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@ 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);
// TODO(sashacmc):
// 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
13 changes: 8 additions & 5 deletions examples/zephyr/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ 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);
// TODO(sashacmc): z_query_parameters
// 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_query_reply(query, z_query_keyexpr(query), (const unsigned char *)VALUE, strlen(VALUE), NULL);
z_string_view_t params;
z_query_parameters(query, params);
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_str_data(z_loan(keystr)), z_loan(params)->len,
z_loan(params)->val);

z_owned_bytes_t reply_payload;
// TODO(sashacmc): VALUE encoding
z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), NULL);
z_drop(z_move(keystr));
}

Expand Down
Loading

0 comments on commit 37592dc

Please sign in to comment.