Skip to content

Commit

Permalink
get/put/query_reply/etc options rework
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed May 3, 2024
1 parent 8e7ccad commit 75abe89
Show file tree
Hide file tree
Showing 17 changed files with 62 additions and 42 deletions.
3 changes: 2 additions & 1 deletion examples/arduino/z_get.ino
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ void loop() {
Serial.print("Sending Query ");
Serial.print(KEYEXPR);
Serial.println(" ...");
z_get_options_t opts = z_get_options_default();
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));
}
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 @@ -156,7 +156,8 @@ void app_main() {
while (1) {
sleep(5);
printf("Sending Query '%s'...\n", KEYEXPR);
z_get_options_t opts = z_get_options_default();
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));
}
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 @@ -80,7 +80,8 @@ void app_main(void) {
while (1) {
z_sleep_s(5);
printf("Sending Query '%s'...\n", KEYEXPR);
z_get_options_t opts = z_get_options_default();
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));
}
Expand Down
3 changes: 2 additions & 1 deletion examples/freertos_plus_tcp/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ void query_handler(const z_query_t *query, void *ctx) {
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();
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_drop(z_move(keystr));
Expand Down
3 changes: 2 additions & 1 deletion examples/mbed/z_get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ int main(int argc, char **argv) {
while (1) {
z_sleep_s(5);
printf("Sending Query '%s'...\n", KEYEXPR);
z_get_options_t opts = z_get_options_default();
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));
}
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 @@ -128,7 +128,8 @@ int main(int argc, char **argv) {

z_mutex_lock(&mutex);
printf("Sending Query '%s'...\n", keyexpr);
z_get_options_t opts = z_get_options_default();
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));
}
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 @@ -88,7 +88,8 @@ int main(int argc, char **argv) {
}

printf("Sending Query '%s'...\n", keyexpr);
z_get_options_t opts = z_get_options_default();
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));
}
Expand Down
3 changes: 2 additions & 1 deletion examples/unix/c11/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ void query_handler(const z_query_t *query, void *ctx) {
}
#endif

z_query_reply_options_t options = z_query_reply_options_default();
z_query_reply_options_t options;
z_query_reply_options_default(&options);
options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL);

#if Z_FEATURE_ATTACHMENT == 1
Expand Down
3 changes: 2 additions & 1 deletion examples/unix/c11/z_queryable_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ int main(int argc, char **argv) {
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();
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_keyexpr(keyexpr), (const unsigned char *)value, strlen(value), &options);
z_drop(z_move(keystr));
Expand Down
3 changes: 2 additions & 1 deletion examples/unix/c99/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ int main(int argc, char **argv) {

z_mutex_lock(&mutex);
printf("Sending Query '%s'...\n", keyexpr);
z_get_options_t opts = z_get_options_default();
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));
}
Expand Down
3 changes: 2 additions & 1 deletion examples/unix/c99/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ void query_handler(const z_query_t *query, void *ctx) {
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_query_reply_options_t options = z_query_reply_options_default();
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_str_drop(z_str_move(&keystr));
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 @@ -79,7 +79,8 @@ int main(int argc, char **argv) {

z_mutex_lock(&mutex);
printf("Sending Query '%s'...\n", keyexpr);
z_get_options_t opts = z_get_options_default();
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));
}
Expand Down
3 changes: 2 additions & 1 deletion examples/windows/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ void query_handler(const z_query_t *query, void *ctx) {
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();
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_drop(z_move(keystr));
Expand Down
3 changes: 2 additions & 1 deletion examples/zephyr/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ int main(int argc, char **argv) {
while (1) {
sleep(5);
printf("Sending Query '%s'...\n", KEYEXPR);
z_get_options_t opts = z_get_options_default();
z_get_options_t opts;
z_get_options_default(&opts);
opts.target = Z_QUERY_TARGET_ALL;
z_owned_closure_reply_t callback;
z_closure(&callback, reply_handler, reply_dropper);
Expand Down
8 changes: 4 additions & 4 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ int8_t z_publisher_delete(const z_publisher_t pub, const z_publisher_delete_opti
* Returns:
* Returns the constructed :c:type:`z_get_options_t`.
*/
z_get_options_t z_get_options_default(void);
void z_get_options_default(z_get_options_t *options);

/**
* Issues a distributed query for a given keyexpr.
Expand Down Expand Up @@ -1092,7 +1092,7 @@ z_value_t z_reply_err(const z_owned_reply_t *reply);
* Returns:
* Returns the constructed :c:type:`z_queryable_options_t`.
*/
z_queryable_options_t z_queryable_options_default(void);
void z_queryable_options_default(z_queryable_options_t *options);

/**
* Declares a queryable for the given keyexpr.
Expand Down Expand Up @@ -1143,7 +1143,7 @@ int8_t z_undeclare_queryable(z_owned_queryable_t *queryable);
* Returns:
* Returns the constructed :c:type:`z_query_reply_options_t`.
*/
z_query_reply_options_t z_query_reply_options_default(void);
void z_query_reply_options_default(z_query_reply_options_t *options);

/**
* Sends a reply to a query.
Expand Down Expand Up @@ -1221,7 +1221,7 @@ int8_t z_undeclare_keyexpr(z_session_t zs, z_owned_keyexpr_t *keyexpr);
* Returns:
* Returns the constructed :c:type:`z_subscriber_options_t`.
*/
z_subscriber_options_t z_subscriber_options_default(void);
void z_subscriber_options_default(z_subscriber_options_t *options);

/**
* Declares a (push) subscriber for the given keyexpr.
Expand Down
39 changes: 20 additions & 19 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -805,15 +805,15 @@ z_owned_keyexpr_t z_publisher_keyexpr(z_publisher_t publisher) {
#if Z_FEATURE_QUERY == 1
OWNED_FUNCTIONS_PTR_INTERNAL(z_reply_t, z_owned_reply_t, reply, _z_reply_free, _z_owner_noop_copy)

z_get_options_t z_get_options_default(void) {
return (z_get_options_t) {
.target = z_query_target_default(), .consolidation = z_query_consolidation_default(),
.value = {.encoding = z_encoding_default(), .payload = _z_bytes_empty()},
void z_get_options_default(z_get_options_t *options) {
options->target = z_query_target_default();
options->consolidation = z_query_consolidation_default();
options->value.encoding = z_encoding_default();
options->value.payload = _z_bytes_empty();
#if Z_FEATURE_ATTACHMENT == 1
.attachment = z_attachment_null(),
options->attachment = z_attachment_null();
#endif
.timeout_ms = Z_GET_TIMEOUT_DEFAULT
};
options->timeout_ms = Z_GET_TIMEOUT_DEFAULT;
}

typedef struct __z_reply_handler_wrapper_t {
Expand All @@ -835,7 +835,8 @@ int8_t z_get(z_session_t zs, z_keyexpr_t keyexpr, const char *parameters, z_owne
void *ctx = callback->context;
callback->context = NULL;

z_get_options_t opt = z_get_options_default();
z_get_options_t opt;
z_get_options_default(&opt);
if (options != NULL) {
opt.consolidation = options->consolidation;
opt.target = options->target;
Expand Down Expand Up @@ -894,9 +895,7 @@ OWNED_FUNCTIONS_PTR_COMMON(z_queryable_t, z_owned_queryable_t, queryable)
OWNED_FUNCTIONS_PTR_CLONE(z_queryable_t, z_owned_queryable_t, queryable, _z_owner_noop_copy)
void z_queryable_drop(z_owned_queryable_t *val) { z_undeclare_queryable(val); }

z_queryable_options_t z_queryable_options_default(void) {
return (z_queryable_options_t){.complete = _Z_QUERYABLE_COMPLETE_DEFAULT};
}
void z_queryable_options_default(z_queryable_options_t *options) { options->complete = _Z_QUERYABLE_COMPLETE_DEFAULT; }

z_owned_queryable_t z_declare_queryable(z_session_t zs, z_keyexpr_t keyexpr, z_owned_closure_query_t *callback,
const z_queryable_options_t *options) {
Expand All @@ -916,7 +915,8 @@ z_owned_queryable_t z_declare_queryable(z_session_t zs, z_keyexpr_t keyexpr, z_o
}
}

z_queryable_options_t opt = z_queryable_options_default();
z_queryable_options_t opt;
z_queryable_options_default(&opt);
if (options != NULL) {
opt.complete = options->complete;
}
Expand All @@ -933,13 +933,16 @@ int8_t z_undeclare_queryable(z_owned_queryable_t *queryable) {
return ret;
}

z_query_reply_options_t z_query_reply_options_default(void) {
return (z_query_reply_options_t){.encoding = z_encoding_default()};
}
void z_query_reply_options_default(z_query_reply_options_t *options) { options->encoding = z_encoding_default(); }

int8_t z_query_reply(const z_query_t *query, const z_keyexpr_t keyexpr, const uint8_t *payload, size_t payload_len,
const z_query_reply_options_t *options) {
z_query_reply_options_t opts = options == NULL ? z_query_reply_options_default() : *options;
z_query_reply_options_t opts;
if (options == NULL) {
z_query_reply_options_default(&opts);
} else {
opts = *options;
}
_z_value_t value = {.payload =
{
.start = payload,
Expand Down Expand Up @@ -989,9 +992,7 @@ OWNED_FUNCTIONS_PTR_COMMON(z_subscriber_t, z_owned_subscriber_t, subscriber)
OWNED_FUNCTIONS_PTR_CLONE(z_subscriber_t, z_owned_subscriber_t, subscriber, _z_owner_noop_copy)
void z_subscriber_drop(z_owned_subscriber_t *val) { z_undeclare_subscriber(val); }

z_subscriber_options_t z_subscriber_options_default(void) {
return (z_subscriber_options_t){.reliability = Z_RELIABILITY_DEFAULT};
}
void z_subscriber_options_default(z_subscriber_options_t *options) { options->reliability = Z_RELIABILITY_DEFAULT; }

int8_t z_declare_subscriber(z_owned_subscriber_t *sub, z_session_t zs, z_keyexpr_t keyexpr,
z_owned_closure_sample_t *callback, const z_subscriber_options_t *options) {
Expand Down
15 changes: 10 additions & 5 deletions tests/z_api_alignment_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ void query_handler(const z_query_t *query, void *arg) {
(void)(pred);
z_value_t payload_value = z_query_value(query);
(void)(payload_value);
z_query_reply_options_t _ret_qreply_opt = z_query_reply_options_default();
z_query_reply_options_t _ret_qreply_opt;
z_query_reply_options_default(&_ret_qreply_opt);
z_query_reply(query, z_keyexpr(z_loan(k_str)), (const uint8_t *)value, strlen(value), &_ret_qreply_opt);

z_drop(z_move(k_str));
Expand Down Expand Up @@ -287,7 +288,8 @@ int main(int argc, char **argv) {
printf("Declaring Subscriber...");
z_owned_closure_sample_t _ret_closure_sample;
z_closure(&_ret_closure_sample, data_handler, NULL, &ls1);
z_subscriber_options_t _ret_sub_opt = z_subscriber_options_default();
z_subscriber_options_t _ret_sub_opt;
z_subscriber_options_default(&_ret_sub_opt);
z_owned_subscriber_t _ret_sub;
_ret_int8 =
z_declare_subscriber(&_ret_sub, z_loan(s2), z_keyexpr(keyexpr_str), z_move(_ret_closure_sample), &_ret_sub_opt);
Expand All @@ -304,7 +306,8 @@ int main(int argc, char **argv) {
printf("Ok\n");

printf("Session Put...");
z_put_options_t _ret_put_opt = z_put_options_default();
z_put_options_t _ret_put_opt;
z_put_options_default(&_ret_put_opt);
_ret_put_opt.congestion_control = Z_CONGESTION_CONTROL_BLOCK;
z_encoding_t _ret_encoding = z_encoding_default();
(void)(_ret_encoding);
Expand Down Expand Up @@ -385,7 +388,8 @@ int main(int argc, char **argv) {
printf("Declaring Queryable...");
z_owned_closure_query_t _ret_closure_query;
z_closure(&_ret_closure_query, query_handler, NULL, &ls1);
z_queryable_options_t _ret_qle_opt = z_queryable_options_default();
z_queryable_options_t _ret_qle_opt;
z_queryable_options_default(&_ret_qle_opt);
z_owned_queryable_t qle =
z_declare_queryable(z_loan(s1), z_keyexpr(s1_res), z_move(_ret_closure_query), &_ret_qle_opt);
assert(z_check(qle));
Expand All @@ -397,7 +401,8 @@ int main(int argc, char **argv) {
z_session_t ls2 = z_loan(s2);
z_owned_closure_reply_t _ret_closure_reply;
z_closure(&_ret_closure_reply, reply_handler, NULL, &ls2);
z_get_options_t _ret_get_opt = z_get_options_default();
z_get_options_t _ret_get_opt;
z_get_options_default(&_ret_get_opt);
_ret_get_opt.target = z_query_target_default();
_ret_get_opt.consolidation = z_query_consolidation_auto();
(void)(_ret_get_opt.consolidation);
Expand Down

0 comments on commit 75abe89

Please sign in to comment.