Skip to content

Commit

Permalink
Add missed stuff, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed May 15, 2024
1 parent e933243 commit 9932daa
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 60 deletions.
1 change: 1 addition & 0 deletions include/zenoh-pico/api/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
z_owned_reply_t : z_reply_loan, \
z_owned_hello_t : z_hello_loan, \
z_owned_str_t : z_str_loan, \
z_view_str_t : z_view_str_loan, \
z_owned_str_array_t : z_str_array_loan, \
z_owned_sample_t : z_sample_loan, \
z_owned_query_t : z_query_loan \
Expand Down
6 changes: 3 additions & 3 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ _Bool zp_keyexpr_was_declared(const z_loaned_keyexpr_t *keyexpr);
* Returns:
* The string representation of a keyexpr for a given session.
*/
// TODO(sashacmc): not used, do we need it?
z_owned_str_t zp_keyexpr_resolve(z_loaned_session_t *zs, z_loaned_keyexpr_t *keyexpr);
int8_t zp_keyexpr_resolve(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, z_owned_str_t *str);

/**
* Checks if a given keyexpr is valid.
Expand Down Expand Up @@ -792,6 +791,7 @@ _OWNED_FUNCTIONS_CLOSURE(z_owned_closure_zid_t, closure_zid)
void z_view_##name##_null(viewtype *name);

_VIEW_FUNCTIONS(z_loaned_keyexpr_t, z_view_keyexpr_t, keyexpr)
_VIEW_FUNCTIONS(z_loaned_str_t, z_view_str_t, str)

// Gets internal value from refcountered type (e.g. z_loaned_session_t, z_query_t)
#define _Z_RC_IN_VAL(arg) ((arg)->in->val)
Expand Down Expand Up @@ -1029,7 +1029,7 @@ int8_t z_put(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co
* Returns:
* Returns ``0`` if the delete operation is successful, or a ``negative value`` otherwise.
*/
int8_t z_delete(z_loaned_session_t *zs, z_loaned_keyexpr_t *keyexpr, const z_delete_options_t *options);
int8_t z_delete(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, const z_delete_options_t *options);

/**
* Constructs the default values for the publisher entity.
Expand Down
23 changes: 11 additions & 12 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,16 @@ _Bool zp_keyexpr_was_declared(const z_keyexpr_t *keyexpr) {
}
return ret;
}
z_owned_str_t zp_keyexpr_resolve(z_loaned_session_t* zs, z_loaned_keyexpr_t *keyexpr) {
z_owned_str_t ret = {._val = NULL};
_z_keyexpr_t ekey = _z_get_expanded_key_from_key(&_Z_RC_IN_VAL(zs), &keyexpr);
ret._val = (char *)ekey._suffix; // ekey will be out of scope so
// - suffix can be safely casted as non-const
// - suffix does not need to be copied
return ret;
*/
int8_t zp_keyexpr_resolve(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, z_owned_str_t *str) {
_z_keyexpr_t ekey = _z_get_expanded_key_from_key(&_Z_RC_IN_VAL(zs), keyexpr);
*str->_val = _z_string_make((char *)ekey._suffix); // ekey will be out of scope so
// - suffix can be safely casted as non-const
// - suffix does not need to be copied
return _Z_RES_OK;
}

_Bool z_keyexpr_is_initialized(const z_keyexpr_t *keyexpr) {
_Bool z_keyexpr_is_initialized(const z_loaned_keyexpr_t *keyexpr) {
_Bool ret = false;

if ((keyexpr->_id != Z_RESOURCE_ID_NONE) || (keyexpr->_suffix != NULL)) {
Expand All @@ -116,7 +114,7 @@ _Bool z_keyexpr_is_initialized(const z_keyexpr_t *keyexpr) {

return ret;
}
*/

int8_t z_keyexpr_is_canon(const char *start, size_t len) { return _z_keyexpr_is_canon(start, len); }

int8_t zp_keyexpr_is_canon_null_terminated(const char *start) { return _z_keyexpr_is_canon(start, strlen(start)); }
Expand Down Expand Up @@ -412,6 +410,7 @@ OWNED_FUNCTIONS_PTR(_z_string_t, str, _z_string_copy, _z_string_free)

OWNED_FUNCTIONS_PTR(_z_keyexpr_t, keyexpr, _z_keyexpr_copy, _z_keyexpr_free)
VIEW_FUNCTIONS_PTR(_z_keyexpr_t, keyexpr)
VIEW_FUNCTIONS_PTR(_z_string_t, str)

OWNED_FUNCTIONS_PTR(_z_hello_t, hello, _z_owner_noop_copy, _z_hello_free)
OWNED_FUNCTIONS_PTR(_z_str_array_t, str_array, _z_owner_noop_copy, _z_str_array_free)
Expand Down Expand Up @@ -661,7 +660,7 @@ int8_t z_put(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co
return ret;
}

int8_t z_delete(z_loaned_session_t *zs, z_loaned_keyexpr_t *keyexpr, const z_delete_options_t *options) {
int8_t z_delete(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, const z_delete_options_t *options) {
int8_t ret = 0;

z_delete_options_t opt;
Expand Down
25 changes: 13 additions & 12 deletions tests/z_api_alignment_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,18 @@ void query_handler(const z_loaned_query_t *query, void *arg) {
z_keyexpr_to_string(z_query_keyexpr(query), &k_str);
#ifdef ZENOH_PICO
if (z_check(k_str) == false) {
k_str = zp_keyexpr_resolve(*(z_session_t *)arg, z_query_keyexpr(query));
zp_keyexpr_resolve(*(const z_loaned_session_t **)arg, z_query_keyexpr(query), &k_str);
}
#endif

z_bytes_t pred = z_query_parameters(query);
z_view_str_t pred;
z_query_parameters(query, &pred);
(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(&_ret_qreply_opt);
z_query_reply(query, z_keyexpr(z_loan(k_str)), (const uint8_t *)value, strlen(value), &_ret_qreply_opt);
z_query_reply(query, z_keyexpr(z_loan(k_str)->val), (const uint8_t *)value, strlen(value), &_ret_qreply_opt);

z_drop(z_move(k_str));
}
Expand All @@ -90,13 +91,13 @@ void reply_handler(z_owned_reply_t *reply, void *arg) {
replies++;

if (z_reply_is_ok(reply)) {
z_loaned_sample_t sample = z_reply_ok(reply);
const z_loaned_sample_t *sample = z_reply_ok(reply);

z_owned_str_t k_str;
z_keyexpr_to_string(sample.keyexpr, &k_str);
z_keyexpr_to_string(z_sample_keyexpr(sample), &k_str);
#ifdef ZENOH_PICO
if (z_check(k_str) == false) {
k_str = zp_keyexpr_resolve(*(z_session_t *)arg, sample.keyexpr);
zp_keyexpr_resolve(*(const z_loaned_session_t **)arg, z_sample_keyexpr(sample), &k_str);
}
#endif
z_drop(z_move(k_str));
Expand All @@ -118,7 +119,7 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) {
z_keyexpr_to_string(z_sample_keyexpr(sample), &k_str);
#ifdef ZENOH_PICO
if (z_check(k_str) == false) {
k_str = zp_keyexpr_resolve(*(z_session_t *)arg, keyexpr);
zp_keyexpr_resolve(*(const z_loaned_session_t **)arg, z_sample_keyexpr(sample), &k_str);
}
#endif
z_drop(z_move(k_str));
Expand All @@ -134,8 +135,8 @@ int main(int argc, char **argv) {
#endif

printf("Testing Keyexpr...");
z_keyexpr_t key = z_keyexpr("demo/example");
_Bool _ret_bool = z_keyexpr_is_initialized(&key);
const z_loaned_keyexpr_t *key = z_keyexpr("demo/example");
_Bool _ret_bool = z_keyexpr_is_initialized(key);
assert_eq(_ret_bool, true);

int8_t _ret_int8 = z_keyexpr_includes(z_keyexpr("demo/example/**"), z_keyexpr("demo/example/a"));
Expand Down Expand Up @@ -201,7 +202,7 @@ int main(int argc, char **argv) {
z_scouting_config_default(&_ret_sconfig);
assert(z_check(_ret_sconfig));
#ifdef ZENOH_PICO
_ret_int8 = zp_scouting_config_insert(z_loan(_ret_sconfig), Z_CONFIG_SCOUTING_TIMEOUT_KEY, SCOUTING_TIMEOUT);
_ret_int8 = zp_scouting_config_insert(z_loan_mut(_ret_sconfig), Z_CONFIG_SCOUTING_TIMEOUT_KEY, SCOUTING_TIMEOUT);
assert_eq(_ret_int8, 0);
_ret_cstr = zp_scouting_config_get(z_loan(_ret_sconfig), Z_CONFIG_SCOUTING_TIMEOUT_KEY);
assert_eq(strlen(_ret_cstr), strlen(SCOUTING_TIMEOUT));
Expand Down Expand Up @@ -285,7 +286,7 @@ int main(int argc, char **argv) {

sleep(SLEEP);

z_session_t ls1 = z_loan(s1);
const z_loaned_session_t *ls1 = z_loan(s1);
printf("Declaring Subscriber...");
z_owned_closure_sample_t _ret_closure_sample;
z_closure(&_ret_closure_sample, data_handler, NULL, &ls1);
Expand Down Expand Up @@ -400,7 +401,7 @@ int main(int argc, char **argv) {
sleep(SLEEP);

printf("Testing Consolidations...");
z_session_t ls2 = z_loan(s2);
const z_loaned_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;
Expand Down
55 changes: 28 additions & 27 deletions tests/z_client_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ void query_handler(const z_loaned_query_t *query, void *arg) {

z_owned_str_t k_str;
z_keyexpr_to_string(z_query_keyexpr(query), &k_str);
assert(_z_str_eq(z_loan(k_str), res) == true);
assert(_z_str_eq(z_loan(k_str)->val, res) == true);

z_bytes_t pred = z_query_parameters(query);
assert(pred.len == strlen(""));
assert(strncmp((const char *)pred.start, "", strlen("")) == 0);
z_view_str_t pred;
z_query_parameters(query, &pred);
assert(z_loan(pred)->len == strlen(""));
assert(strncmp((const char *)z_loan(pred)->val, "", strlen("")) == 0);

z_query_reply(query, z_keyexpr(res), (const uint8_t *)res, strlen(res), NULL);

Expand All @@ -72,14 +73,15 @@ void reply_handler(z_owned_reply_t *reply, void *arg) {
char *res = (char *)malloc(64);
snprintf(res, 64, "%s%u", uri, *(unsigned int *)arg);
if (z_reply_is_ok(reply)) {
z_loaned_sample_t sample = z_reply_ok(reply);
const z_loaned_sample_t *sample = z_reply_ok(reply);
printf(">> Received reply data: %s\t(%u/%u)\n", res, replies, total);

z_owned_str_t k_str;
z_keyexpr_to_string(sample.keyexpr, &k_str);
assert(sample.payload.len == strlen(res));
assert(strncmp(res, (const char *)sample.payload.start, strlen(res)) == 0);
assert(_z_str_eq(z_loan(k_str), res) == true);
z_keyexpr_to_string(z_sample_keyexpr(sample), &k_str);
const z_loaned_bytes_t *payload = z_sample_payload(sample);
assert(payload->len == strlen(res));
assert(strncmp(res, (const char *)payload->start, strlen(res)) == 0);
assert(_z_str_eq(z_loan(k_str)->val, res) == true);

replies++;
z_drop(z_move(k_str));
Expand All @@ -99,14 +101,14 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) {
z_keyexpr_to_string(z_sample_keyexpr(sample), &k_str);
const z_loaned_bytes_t *payload = z_sample_payload(sample);
assert((payload->len == MSG_LEN) || (payload->len == FRAGMENT_MSG_LEN));
assert(_z_str_eq(z_loan(k_str), res) == true);
assert(_z_str_eq(z_loan(k_str)->val, res) == true);

datas++;
z_drop(z_move(k_str));
free(res);
}

z_string_t format_id(const z_id_t *id) {
_z_string_t format_id(const z_id_t *id) {
_z_bytes_t id_as_bytes = _z_bytes_wrap(id->id, _z_id_len(*id));
return _z_string_from_bytes(&id_as_bytes);
}
Expand All @@ -128,7 +130,7 @@ int main(int argc, char **argv) {
z_owned_session_t s1;
z_open(&s1, z_move(config));
assert(z_check(s1));
z_string_t zid1 = format_id(&(_Z_RC_IN_VAL(z_loan(s1))._local_zid));
_z_string_t zid1 = format_id(&(_Z_RC_IN_VAL(z_loan(s1))._local_zid));
printf("Session 1 with PID: %s\n", zid1.val);
_z_string_clear(&zid1);

Expand All @@ -144,7 +146,7 @@ int main(int argc, char **argv) {
z_owned_session_t s2;
z_open(&s2, z_move(config));
assert(z_check(s2));
z_string_t zid2 = format_id(&(_Z_RC_IN_VAL(z_loan(s2))._local_zid));
_z_string_t zid2 = format_id(&(_Z_RC_IN_VAL(z_loan(s2))._local_zid));
printf("Session 2 with PID: %s\n", zid2.val);
_z_string_clear(&zid2);

Expand All @@ -160,7 +162,7 @@ int main(int argc, char **argv) {
snprintf(s1_res, 64, "%s%u", uri, i);
z_owned_keyexpr_t expr;
z_declare_keyexpr(&expr, z_loan(s1), z_keyexpr(s1_res));
printf("Declared resource on session 1: %u %s\n", z_loan(expr)._id, z_loan(expr)._suffix);
printf("Declared resource on session 1: %u %s\n", z_loan(expr)->_id, z_loan(expr)->_suffix);
rids1[i] = expr;
}

Expand All @@ -170,7 +172,7 @@ int main(int argc, char **argv) {
snprintf(s1_res, 64, "%s%u", uri, i);
z_owned_keyexpr_t expr;
z_declare_keyexpr(&expr, z_loan(s2), z_keyexpr(s1_res));
printf("Declared resource on session 2: %u %s\n", z_loan(expr)._id, z_loan(expr)._suffix);
printf("Declared resource on session 2: %u %s\n", z_loan(expr)->_id, z_loan(expr)->_suffix);
rids2[i] = expr;
}

Expand All @@ -183,8 +185,8 @@ int main(int argc, char **argv) {
z_owned_subscriber_t *sub = (z_owned_subscriber_t *)z_malloc(sizeof(z_owned_subscriber_t));
int8_t res = z_declare_subscriber(sub, z_loan(s2), z_loan(rids2[i]), &callback, NULL);
assert(res == _Z_RES_OK);
printf("Declared subscription on session 2: %ju %u %s\n", (uintmax_t)z_subscriber_loan(sub)._val->_entity_id,
z_loan(rids2[i])._id, "");
printf("Declared subscription on session 2: %ju %u %s\n", (uintmax_t)z_subscriber_loan(sub)->_entity_id,
z_loan(rids2[i])->_id, "");
subs2 = _z_list_push(subs2, sub);
}

Expand All @@ -196,8 +198,7 @@ int main(int argc, char **argv) {
z_closure(&callback, query_handler, NULL, &idx[i]);
z_owned_queryable_t *qle = (z_owned_queryable_t *)z_malloc(sizeof(z_owned_queryable_t));
assert(z_declare_queryable(qle, z_loan(s2), z_keyexpr(s1_res), &callback, NULL) == _Z_RES_OK);
printf("Declared queryable on session 2: %ju %zu %s\n", (uintmax_t)qle->_value->_entity_id, (z_zint_t)0,
s1_res);
printf("Declared queryable on session 2: %ju %zu %s\n", (uintmax_t)qle->_val->_entity_id, (z_zint_t)0, s1_res);
qles2 = _z_list_push(qles2, qle);
}

Expand All @@ -207,7 +208,7 @@ int main(int argc, char **argv) {
for (unsigned int i = 0; i < SET; i++) {
z_owned_publisher_t *pub = (z_owned_publisher_t *)z_malloc(sizeof(z_owned_publisher_t));
if (z_declare_publisher(pub, z_loan(s1), z_loan(rids1[i]), NULL) < 0) {
printf("Declared publisher on session 1: %zu\n", z_loan(*pub)._val->_id);
printf("Declared publisher on session 1: %zu\n", z_loan(*pub)->_id);
}
pubs1 = _z_list_push(pubs1, pub);
}
Expand All @@ -226,7 +227,7 @@ int main(int argc, char **argv) {
z_put_options_default(&opt);
opt.congestion_control = Z_CONGESTION_CONTROL_BLOCK;
z_put(z_loan(s1), z_loan(rids1[i]), (const uint8_t *)payload, len, &opt);
printf("Wrote data from session 1: %u %zu b\t(%u/%u)\n", z_loan(rids1[i])._id, len, n * SET + (i + 1),
printf("Wrote data from session 1: %u %zu b\t(%u/%u)\n", z_loan(rids1[i])->_id, len, n * SET + (i + 1),
total);
}
}
Expand Down Expand Up @@ -261,7 +262,7 @@ int main(int argc, char **argv) {
z_put_options_default(&opt);
opt.congestion_control = Z_CONGESTION_CONTROL_BLOCK;
z_put(z_loan(s1), z_loan(rids1[i]), (const uint8_t *)payload, len, &opt);
printf("Wrote fragment data from session 1: %u %zu b\t(%u/%u)\n", z_loan(rids1[i])._id, len,
printf("Wrote fragment data from session 1: %u %zu b\t(%u/%u)\n", z_loan(rids1[i])->_id, len,
n * SET + (i + 1), total);
}
}
Expand Down Expand Up @@ -323,7 +324,7 @@ int main(int argc, char **argv) {
// Undeclare publishers on first session
while (pubs1) {
z_owned_publisher_t *pub = _z_list_head(pubs1);
printf("Undeclared publisher on session 2: %zu\n", z_loan(*pub)._val->_id);
printf("Undeclared publisher on session 2: %zu\n", z_loan(*pub)->_id);
z_undeclare_publisher(z_move(*pub));
pubs1 = _z_list_pop(pubs1, _z_noop_elem_free, NULL);
}
Expand All @@ -333,7 +334,7 @@ int main(int argc, char **argv) {
// Undeclare subscribers and queryables on second session
while (subs2) {
z_owned_subscriber_t *sub = _z_list_head(subs2);
printf("Undeclared subscriber on session 2: %ju\n", (uintmax_t)sub->_value->_entity_id);
printf("Undeclared subscriber on session 2: %ju\n", (uintmax_t)sub->_val->_entity_id);
z_undeclare_subscriber(z_move(*sub));
subs2 = _z_list_pop(subs2, _z_noop_elem_free, NULL);
}
Expand All @@ -342,7 +343,7 @@ int main(int argc, char **argv) {

while (qles2) {
z_owned_queryable_t *qle = _z_list_head(qles2);
printf("Undeclared queryable on session 2: %ju\n", (uintmax_t)qle->_value->_entity_id);
printf("Undeclared queryable on session 2: %ju\n", (uintmax_t)qle->_val->_entity_id);
z_undeclare_queryable(z_move(*qle));
qles2 = _z_list_pop(qles2, _z_noop_elem_free, NULL);
}
Expand All @@ -351,14 +352,14 @@ int main(int argc, char **argv) {

// Undeclare resources on both sessions
for (unsigned int i = 0; i < SET; i++) {
printf("Undeclared resource on session 1: %u\n", z_loan(rids1[i])._id);
printf("Undeclared resource on session 1: %u\n", z_loan(rids1[i])->_id);
z_undeclare_keyexpr(z_loan(s1), z_move(rids1[i]));
}

z_sleep_s(SLEEP);

for (unsigned int i = 0; i < SET; i++) {
printf("Undeclared resource on session 2: %u\n", z_loan(rids2[i])._id);
printf("Undeclared resource on session 2: %u\n", z_loan(rids2[i])->_id);
z_undeclare_keyexpr(z_loan(s2), z_move(rids2[i]));
}

Expand Down
Loading

0 comments on commit 9932daa

Please sign in to comment.