Skip to content

Commit

Permalink
Keyexpr rework
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed May 17, 2024
1 parent d226db9 commit 275377e
Show file tree
Hide file tree
Showing 81 changed files with 440 additions and 312 deletions.
6 changes: 4 additions & 2 deletions examples/arduino/z_get.ino
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void reply_handler(z_owned_reply_t *oreply, void *ctx) {
std::string val((const char *)z_sample_payload(sample)->start, z_sample_payload(sample)->len);

Serial.print(" >> [Get listener] Received (");
Serial.print(z_str_loan(&keystr));
Serial.print(z_str_data(z_str_loan(&keystr)));
Serial.print(", ");
Serial.print(val.c_str());
Serial.println(")");
Expand Down Expand Up @@ -118,7 +118,9 @@ void loop() {
}
z_owned_closure_reply_t callback;
z_closure_reply(&callback, reply_handler, reply_dropper, NULL);
if (z_get(z_session_loan(&s), z_keyexpr(KEYEXPR), "", z_closure_reply_move(&callback), &opts) < 0) {
z_view_keyexpr_t ke;
z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR);
if (z_get(z_session_loan(&s), z_view_keyexpr_loan(&ke), "", z_closure_reply_move(&callback), &opts) < 0) {
Serial.println("Unable to send query.");
}
}
Expand Down
4 changes: 3 additions & 1 deletion examples/arduino/z_pub.ino
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ void setup() {
Serial.print("Declaring publisher for ");
Serial.print(KEYEXPR);
Serial.println("...");
if (z_declare_publisher(&pub, z_session_loan(&s), z_keyexpr(KEYEXPR), NULL) < 0) {
z_view_keyexpr_t ke;
z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR);
if (z_declare_publisher(&pub, z_session_loan(&s), z_view_keyexpr_loan(&ke), NULL) < 0) {
Serial.println("Unable to declare publisher for key expression!");
while (1) {
;
Expand Down
8 changes: 5 additions & 3 deletions examples/arduino/z_pull.ino
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
// std::string val((const char *)sample->payload.start, sample->payload.len);

// Serial.print(" >> [Subscription listener] Received (");
// Serial.print(z_str_loan(&keystr));
// Serial.print(z_str_data(z_str_loan(&keystr)));
// Serial.print(", ");
// Serial.print(val.c_str());
// Serial.println(")");
Expand Down Expand Up @@ -99,8 +99,10 @@ void setup() {
// z_owned_closure_sample_t callback;
// z_closure_sample(&callback, data_handler, NULL, NULL);
// @TODO
// sub = z_declare_pull_subscriber(z_session_loan(&s), z_keyexpr(KEYEXPR), z_closure_sample_move(&callback), NULL);
// if (!z_pull_subscriber_check(&sub)) {
// z_view_keyexpr_t ke;
// z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR);
// sub = z_declare_pull_subscriber(z_session_loan(&s), z_view_keyexpr_loan(&ke), z_closure_sample_move(&callback),
// NULL); if (!z_pull_subscriber_check(&sub)) {
// Serial.println("Unable to declare subscriber.");
// while (1) {
// ;
Expand Down
12 changes: 8 additions & 4 deletions examples/arduino/z_queryable.ino
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ void query_handler(const z_loaned_query_t *query, void *arg) {
z_keyexpr_to_string(z_query_keyexpr(query), &keystr);

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

z_query_reply(query, z_keyexpr(KEYEXPR), (const unsigned char *)VALUE, strlen(VALUE), NULL);
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_str_drop(z_str_move(&keystr));
}
Expand Down Expand Up @@ -96,8 +98,10 @@ void setup() {
z_owned_closure_query_t callback;
z_closure_query(&callback, query_handler, NULL, NULL);
z_owned_queryable_t qable;
if (z_declare_queryable(&qable, z_session_loan(&s), z_keyexpr(KEYEXPR), z_closure_query_move(&callback), NULL) <
0) {
z_view_keyexpr_t ke;
z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR);
if (z_declare_queryable(&qable, z_session_loan(&s), z_view_keyexpr_loan(&ke), z_closure_query_move(&callback),
NULL) < 0) {
Serial.println("Unable to declare queryable.");
while (1) {
;
Expand Down
28 changes: 15 additions & 13 deletions examples/arduino/z_scout.ino
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,27 @@ void fprintwhatami(unsigned int whatami) {

void fprintlocators(const z_str_array_t *locs) {
Serial.print("[");
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(", ");
}
}
(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(", ");
// }
//}
Serial.print("]");
}

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

Expand Down
8 changes: 5 additions & 3 deletions examples/arduino/z_sub.ino
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) {
std::string val((const char *)z_sample_payload(sample)->start, z_sample_payload(sample)->len);

Serial.print(" >> [Subscription listener] Received (");
Serial.print(z_str_loan(&keystr));
Serial.print(z_str_data(z_str_loan(&keystr)));
Serial.print(", ");
Serial.print(val.c_str());
Serial.println(")");
Expand Down Expand Up @@ -94,8 +94,10 @@ void setup() {
z_owned_closure_sample_t callback;
z_closure_sample(&callback, data_handler, NULL, NULL);
z_owned_subscriber_t sub;
if (z_declare_subscriber(&sub, z_session_loan(&s), z_keyexpr(KEYEXPR), z_closure_sample_move(&callback), NULL) <
0) {
z_view_keyexpr_t ke;
z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR);
if (z_declare_subscriber(&sub, z_session_loan(&s), z_view_keyexpr_loan(&ke), z_closure_sample_move(&callback),
NULL) < 0) {
Serial.println("Unable to declare subscriber.");
while (1) {
;
Expand Down
4 changes: 3 additions & 1 deletion examples/espidf/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ void app_main() {
}
z_owned_closure_reply_t callback;
z_closure(&callback, reply_handler, reply_dropper);
if (z_get(z_loan(s), z_keyexpr(KEYEXPR), "", z_move(callback), &opts) < 0) {
z_view_keyexpr_t ke;
z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR);
if (z_get(z_loan(s), z_loan(ke), "", z_move(callback), &opts) < 0) {
printf("Unable to send query.\n");
exit(-1);
}
Expand Down
4 changes: 3 additions & 1 deletion examples/espidf/z_pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ void app_main() {

printf("Declaring publisher for '%s'...", KEYEXPR);
z_owned_publisher_t pub;
if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(KEYEXPR), NULL) < 0) {
z_view_keyexpr_t ke;
z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR);
if (z_declare_publisher(&pub, z_loan(s), z_loan(ke), NULL) < 0) {
printf("Unable to declare publisher for key expression!\n");
exit(-1);
}
Expand Down
4 changes: 3 additions & 1 deletion examples/espidf/z_pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ void app_main() {
// z_closure(&callback, data_handler);
printf("Declaring Subscriber on '%s'...", KEYEXPR);
// @TODO
// z_owned_pull_subscriber_t sub = z_declare_pull_subscriber(z_loan(s), z_keyexpr(KEYEXPR), z_move(callback), NULL);
// z_view_keyexpr_t ke;
// z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR);
// z_owned_pull_subscriber_t sub = z_declare_pull_subscriber(z_loan(s), z_loan(ke), z_move(callback), NULL);
// if (!z_check(sub)) {
// printf("Unable to declare subscriber.\n");
// exit(-1);
Expand Down
18 changes: 12 additions & 6 deletions examples/espidf/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,17 @@ void wifi_init_sta(void) {
vEventGroupDelete(s_event_group_handler);
}

void query_handler(z_query_t *query, void *ctx) {
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_data(z_loan(keystr)), (int)pred.len, pred.start);
z_query_reply(query, z_keyexpr(KEYEXPR), (const unsigned char *)VALUE, strlen(VALUE), NULL);
// 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_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_drop(z_move(keystr));
}

Expand Down Expand Up @@ -154,7 +158,9 @@ void app_main() {
z_owned_closure_query_t callback;
z_closure(&callback, query_handler);
z_owned_queryable_t qable;
if (z_declare_queryable(&qable, z_loan(s), z_keyexpr(KEYEXPR), z_move(callback), NULL) < 0) {
z_view_keyexpr_t ke;
z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR);
if (z_declare_queryable(&qable, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) {
printf("Unable to declare queryable.\n");
exit(-1);
}
Expand Down
26 changes: 14 additions & 12 deletions examples/espidf/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,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): 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, ", ");
// }
//}
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
6 changes: 4 additions & 2 deletions examples/espidf/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ 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);
const z_loaned_payload_t* payload = z_sample_payload(sample);
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,
payload->start);
z_str_drop(z_str_move(&keystr));
Expand Down Expand Up @@ -151,7 +151,9 @@ void app_main() {
z_owned_closure_sample_t callback;
z_closure(&callback, data_handler);
z_owned_subscriber_t sub;
if (z_declare_subscriber(&sub, z_loan(s), z_keyexpr(KEYEXPR), z_move(callback), NULL) < 0) {
z_view_keyexpr_t ke;
z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR);
if (z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) {
printf("Unable to declare subscriber.\n");
exit(-1);
}
Expand Down
10 changes: 5 additions & 5 deletions examples/freertos_plus_tcp/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ void app_main(void) {
return;
}

z_keyexpr_t ke = z_keyexpr(KEYEXPR);
if (!z_check(ke)) {
printf("%s is not a valid key expression\n", KEYEXPR);
return;
z_view_keyexpr_t ke;
if (z_view_keyexpr_from_string(&ke, KEYEXPR) < 0) {
printf("%s is not a valid key expression", KEYEXPR);
return -1;
}

while (1) {
Expand All @@ -88,7 +88,7 @@ void app_main(void) {
}
z_owned_closure_reply_t callback;
z_closure(&callback, reply_handler, reply_dropper);
if (z_get(z_loan(s), ke, "", z_move(callback), &opts) < 0) {
if (z_get(z_loan(s), z_loan(ke), "", z_move(callback), &opts) < 0) {
printf("Unable to send query.\n");
return;
}
Expand Down
4 changes: 3 additions & 1 deletion examples/freertos_plus_tcp/z_pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ void app_main(void) {

printf("Declaring publisher for '%s'...\n", KEYEXPR);
z_owned_publisher_t pub;
if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(KEYEXPR), NULL) < 0) {
z_view_keyexpr_t ke;
z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR);
if (z_declare_publisher(&pub, z_loan(s), z_loan(ke), NULL) < 0) {
printf("Unable to declare publisher for key expression!\n");
return;
}
Expand Down
4 changes: 3 additions & 1 deletion examples/freertos_plus_tcp/z_pub_st.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ void app_main(void) {

printf("Declaring publisher for '%s'...\n", KEYEXPR);
z_owned_publisher_t pub;
if (z_declare_publisher(&pub, z_loan(s), z_keyexpr(KEYEXPR), NULL) < 0) {
z_view_keyexpr_t ke;
z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR);
if (z_declare_publisher(&pub, z_loan(s), z_loan(ke), NULL) < 0) {
printf("Unable to declare publisher for key expression!\n");
return;
}
Expand Down
4 changes: 3 additions & 1 deletion examples/freertos_plus_tcp/z_pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ void app_main(void) {
// z_closure(&callback, data_handler);
printf("Declaring Subscriber on '%s'...\n", KEYEXPR);
// @TODO
// z_owned_pull_subscriber_t sub = z_declare_pull_subscriber(z_loan(s), z_keyexpr(KEYEXPR), z_move(callback), NULL);
// z_view_keyexpr_t ke;
// z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR);
// z_owned_pull_subscriber_t sub = z_declare_pull_subscriber(z_loan(s), z_loan(ke), z_move(callback), NULL);
// if (!z_check(sub)) {
// printf("Unable to declare subscriber.\n");
// return;
Expand Down
4 changes: 3 additions & 1 deletion examples/freertos_plus_tcp/z_put.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ void app_main(void) {

printf("Declaring key expression '%s'...\n", KEYEXPR);
z_owned_keyexpr_t ke;
if (z_declare_keyexpr(&ke, z_loan(s), z_keyexpr(KEYEXPR)) < 0) {
z_view_keyexpr_t vke;
z_view_keyexpr_from_string_unchecked(&vke, KEYEXPR);
if (z_declare_keyexpr(&ke, z_loan(s), z_loan(vke)) < 0) {
printf("Unable to declare key expression!\n");
zp_stop_read_task(z_loan_mut(s));
zp_stop_lease_task(z_loan_mut(s));
Expand Down
25 changes: 13 additions & 12 deletions examples/freertos_plus_tcp/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ 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);
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);
}
// 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_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_keyexpr(KEYEXPR), (const unsigned char *)VALUE, strlen(VALUE), &options);
z_query_reply(query, z_query_keyexpr(query), (const unsigned char *)VALUE, strlen(VALUE), &options);
z_drop(z_move(keystr));
}

Expand All @@ -68,17 +69,17 @@ void app_main(void) {
return;
}

z_keyexpr_t ke = z_keyexpr(KEYEXPR);
if (!z_check(ke)) {
printf("%s is not a valid key expression\n", KEYEXPR);
return;
z_view_keyexpr_t ke;
if (z_view_keyexpr_from_string(&ke, KEYEXPR) < 0) {
printf("%s is not a valid key expression", KEYEXPR);
return -1;
}

printf("Creating Queryable on '%s'...\n", KEYEXPR);
z_owned_closure_query_t callback;
z_closure(&callback, query_handler);
z_owned_queryable_t qable;
if (z_declare_queryable(&qable, z_loan(s), ke, z_move(callback), NULL) < 0) {
if (z_declare_queryable(&qable, z_loan(s), z_loan(ke), z_move(callback), NULL) < 0) {
printf("Unable to create queryable.\n");
return;
}
Expand Down
Loading

0 comments on commit 275377e

Please sign in to comment.