Skip to content

Commit

Permalink
make z_recv/z_try_recv return z_result_t to allow differentiating bet…
Browse files Browse the repository at this point in the history
…ween different cases without the need to call z_check on constructed object. (#573)
  • Loading branch information
DenisBiryukov91 authored Aug 6, 2024
1 parent 262b820 commit 0406e2e
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 63 deletions.
2 changes: 1 addition & 1 deletion examples/unix/c11/z_get_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ int main(int argc, char **argv) {

z_owned_reply_t reply;
z_null(&reply);
for (z_recv(z_loan(handler), &reply); z_check(reply); z_recv(z_loan(handler), &reply)) {
for (z_result_t res = z_recv(z_loan(handler), &reply); res == Z_OK; res = z_recv(z_loan(handler), &reply)) {
if (z_reply_is_ok(z_loan(reply))) {
const z_loaned_sample_t *sample = z_reply_ok(z_loan(reply));
z_view_string_t keystr;
Expand Down
11 changes: 8 additions & 3 deletions examples/unix/c11/z_pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ int main(int argc, char **argv) {
z_owned_sample_t sample;
z_null(&sample);
while (true) {
for (z_try_recv(z_loan(handler), &sample); z_check(sample); z_try_recv(z_loan(handler), &sample)) {
z_result_t res;
for (res = z_try_recv(z_loan(handler), &sample); res == Z_OK; res = z_try_recv(z_loan(handler), &sample)) {
z_view_string_t keystr;
z_keyexpr_as_view_string(z_sample_keyexpr(z_loan(sample)), &keystr);
z_owned_string_t value;
Expand All @@ -98,8 +99,12 @@ int main(int argc, char **argv) {
z_drop(z_move(value));
z_drop(z_move(sample));
}
printf(">> [Subscriber] Nothing to pull... sleep for %zu ms\n", interval);
z_sleep_ms(interval);
if (res == Z_CHANNEL_NODATA) {
printf(">> [Subscriber] Nothing to pull... sleep for %zu ms\n", interval);
z_sleep_ms(interval);
} else {
break;
}
}

z_undeclare_subscriber(z_move(sub));
Expand Down
2 changes: 1 addition & 1 deletion examples/unix/c11/z_queryable_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ int main(int argc, char **argv) {

z_owned_query_t query;
z_null(&query);
for (z_recv(z_loan(handler), &query); z_check(query); z_recv(z_loan(handler), &query)) {
for (z_result_t res = z_recv(z_loan(handler), &query); res == Z_OK; res = z_recv(z_loan(handler), &query)) {
const z_loaned_query_t *q = z_loan(query);
z_view_string_t keystr;
z_keyexpr_as_view_string(z_query_keyexpr(q), &keystr);
Expand Down
2 changes: 1 addition & 1 deletion examples/unix/c11/z_sub_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ int main(int argc, char **argv) {

z_owned_sample_t sample;
z_null(&sample);
for (z_recv(z_loan(handler), &sample); z_check(sample); z_recv(z_loan(handler), &sample)) {
for (z_result_t res = z_recv(z_loan(handler), &sample); res == Z_OK; res = z_recv(z_loan(handler), &sample)) {
z_view_string_t keystr;
z_keyexpr_as_view_string(z_sample_keyexpr(z_loan(sample)), &keystr);
z_owned_string_t value;
Expand Down
21 changes: 12 additions & 9 deletions include/zenoh-pico/api/handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "zenoh-pico/collections/fifo_mt.h"
#include "zenoh-pico/collections/ring_mt.h"
#include "zenoh-pico/utils/logging.h"
#include "zenoh-pico/utils/result.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -65,31 +66,33 @@ extern "C" {
_Z_ERROR("%s failed: %i", #collection_push_f, ret); \
} \
} \
static inline _Bool z_##handler_name##_recv(const z_loaned_##handler_name##_t *handler, elem_owned_type *elem) { \
static inline int8_t z_##handler_name##_recv(const z_loaned_##handler_name##_t *handler, elem_owned_type *elem) { \
elem_null_f(elem); \
int8_t ret = collection_pull_f(elem, (collection_type *)handler->collection, _z_##handler_name##_elem_move); \
if (ret == _Z_RES_CHANNEL_CLOSED) { \
return false; \
return Z_CHANNEL_DISCONNECTED; \
} \
if (ret != _Z_RES_OK) { \
_Z_ERROR("%s failed: %i", #collection_pull_f, ret); \
return false; \
return ret; \
} \
return true; \
return _Z_RES_OK; \
} \
static inline _Bool z_##handler_name##_try_recv(const z_loaned_##handler_name##_t *handler, \
elem_owned_type *elem) { \
static inline int8_t z_##handler_name##_try_recv(const z_loaned_##handler_name##_t *handler, \
elem_owned_type *elem) { \
elem_null_f(elem); \
int8_t ret = \
collection_try_pull_f(elem, (collection_type *)handler->collection, _z_##handler_name##_elem_move); \
if (ret == _Z_RES_CHANNEL_CLOSED) { \
return false; \
return Z_CHANNEL_DISCONNECTED; \
} else if (ret == _Z_RES_CHANNEL_NODATA) { \
return Z_CHANNEL_NODATA; \
} \
if (ret != _Z_RES_OK) { \
_Z_ERROR("%s failed: %i", #collection_try_pull_f, ret); \
return false; \
return ret; \
} \
return true; \
return _Z_RES_OK; \
} \
\
static inline void _z_##handler_name##_clear(handler_type *handler) { \
Expand Down
24 changes: 12 additions & 12 deletions include/zenoh-pico/api/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -517,42 +517,42 @@ inline void z_closure(
closure->_val.call = call;
};

inline bool z_try_recv(const z_loaned_fifo_handler_query_t* this_, z_owned_query_t* query) {
inline z_result_t z_try_recv(const z_loaned_fifo_handler_query_t* this_, z_owned_query_t* query) {
return z_fifo_handler_query_try_recv(this_, query);
};
inline bool z_try_recv(const z_loaned_fifo_handler_reply_t* this_, z_owned_reply_t* reply) {
inline z_result_t z_try_recv(const z_loaned_fifo_handler_reply_t* this_, z_owned_reply_t* reply) {
return z_fifo_handler_reply_try_recv(this_, reply);
};
inline bool z_try_recv(const z_loaned_fifo_handler_sample_t* this_, z_owned_sample_t* sample) {
inline z_result_t z_try_recv(const z_loaned_fifo_handler_sample_t* this_, z_owned_sample_t* sample) {
return z_fifo_handler_sample_try_recv(this_, sample);
};
inline bool z_try_recv(const z_loaned_ring_handler_query_t* this_, z_owned_query_t* query) {
inline z_result_t z_try_recv(const z_loaned_ring_handler_query_t* this_, z_owned_query_t* query) {
return z_ring_handler_query_try_recv(this_, query);
};
inline bool z_try_recv(const z_loaned_ring_handler_reply_t* this_, z_owned_reply_t* reply) {
inline z_result_t z_try_recv(const z_loaned_ring_handler_reply_t* this_, z_owned_reply_t* reply) {
return z_ring_handler_reply_try_recv(this_, reply);
};
inline bool z_try_recv(const z_loaned_ring_handler_sample_t* this_, z_owned_sample_t* sample) {
inline z_result_t z_try_recv(const z_loaned_ring_handler_sample_t* this_, z_owned_sample_t* sample) {
return z_ring_handler_sample_try_recv(this_, sample);
};


inline bool z_recv(const z_loaned_fifo_handler_query_t* this_, z_owned_query_t* query) {
inline z_result_t z_recv(const z_loaned_fifo_handler_query_t* this_, z_owned_query_t* query) {
return z_fifo_handler_query_recv(this_, query);
};
inline bool z_recv(const z_loaned_fifo_handler_reply_t* this_, z_owned_reply_t* reply) {
inline z_result_t z_recv(const z_loaned_fifo_handler_reply_t* this_, z_owned_reply_t* reply) {
return z_fifo_handler_reply_recv(this_, reply);
};
inline bool z_recv(const z_loaned_fifo_handler_sample_t* this_, z_owned_sample_t* sample) {
inline z_result_t z_recv(const z_loaned_fifo_handler_sample_t* this_, z_owned_sample_t* sample) {
return z_fifo_handler_sample_recv(this_, sample);
};
inline bool z_recv(const z_loaned_ring_handler_query_t* this_, z_owned_query_t* query) {
inline z_result_t z_recv(const z_loaned_ring_handler_query_t* this_, z_owned_query_t* query) {
return z_ring_handler_query_recv(this_, query);
};
inline bool z_recv(const z_loaned_ring_handler_reply_t* this_, z_owned_reply_t* reply) {
inline z_result_t z_recv(const z_loaned_ring_handler_reply_t* this_, z_owned_reply_t* reply) {
return z_ring_handler_reply_recv(this_, reply);
};
inline bool z_recv(const z_loaned_ring_handler_sample_t* this_, z_owned_sample_t* sample) {
inline z_result_t z_recv(const z_loaned_ring_handler_sample_t* this_, z_owned_sample_t* sample) {
return z_ring_handler_sample_recv(this_, sample);
};

Expand Down
3 changes: 3 additions & 0 deletions include/zenoh-pico/utils/result.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ typedef enum {
_Z_RES_OK = 0,
Z_OK = 0,
_Z_RES_CHANNEL_CLOSED = 1,
Z_CHANNEL_DISCONNECTED = 1,
_Z_RES_CHANNEL_NODATA = 2,
Z_CHANNEL_NODATA = 2,

_Z_ERR_MESSAGE_DESERIALIZATION_FAILED = -119,
_Z_ERR_MESSAGE_SERIALIZATION_FAILED = -118,
Expand Down
2 changes: 2 additions & 0 deletions src/collections/fifo_mt.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ int8_t _z_fifo_mt_try_pull(void *dst, void *context, z_element_move_f element_mo
element_move(dst, src);
} else if (f->is_closed) {
return _Z_RES_CHANNEL_CLOSED;
} else {
return _Z_RES_CHANNEL_NODATA;
}

return _Z_RES_OK;
Expand Down
2 changes: 2 additions & 0 deletions src/collections/ring_mt.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ int8_t _z_ring_mt_try_pull(void *dst, void *context, z_element_move_f element_mo
element_move(dst, src);
} else if (r->is_closed) {
return _Z_RES_CHANNEL_CLOSED;
} else {
return _Z_RES_CHANNEL_NODATA;
}
return _Z_RES_OK;
}
62 changes: 34 additions & 28 deletions tests/z_channels_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,24 @@
z_call(*z_loan(closure), &sample); \
} while (0);

#define _RECV(handler, method, buf) \
do { \
z_owned_sample_t sample; \
z_sample_null(&sample); \
if (!method(z_loan(handler), &sample)) { \
strcpy(buf, "closed"); \
} else { \
if (z_check(sample)) { \
z_owned_slice_t value; \
z_bytes_deserialize_into_slice(z_sample_payload(z_loan(sample)), &value); \
size_t value_len = z_slice_len(z_loan(value)); \
strncpy(buf, (const char *)z_slice_data(z_loan(value)), value_len); \
buf[value_len] = '\0'; \
z_drop(z_move(sample)); \
z_drop(z_move(value)); \
} else { \
buf[0] = '\0'; \
} \
} \
#define _RECV(handler, method, buf) \
do { \
z_owned_sample_t sample; \
z_sample_null(&sample); \
int8_t res = method(z_loan(handler), &sample); \
if (res == Z_CHANNEL_DISCONNECTED) { \
strcpy(buf, "closed"); \
} else if (res == Z_OK) { \
z_owned_slice_t value; \
z_bytes_deserialize_into_slice(z_sample_payload(z_loan(sample)), &value); \
size_t value_len = z_slice_len(z_loan(value)); \
strncpy(buf, (const char *)z_slice_data(z_loan(value)), value_len); \
buf[value_len] = '\0'; \
z_drop(z_move(sample)); \
z_drop(z_move(value)); \
} else if (res == Z_CHANNEL_NODATA) { \
strcpy(buf, "nodata"); \
} \
} while (0);

#define RECV(handler, buf) _RECV(handler, z_recv, buf)
Expand All @@ -84,8 +83,12 @@ void sample_fifo_channel_test(void) {
RECV(handler, buf)
assert(strcmp(buf, "v4444") == 0);

z_drop(z_move(handler));
z_drop(z_move(closure));

RECV(handler, buf)
assert(strcmp(buf, "closed") == 0);

z_drop(z_move(handler));
}

void sample_fifo_channel_test_try_recv(void) {
Expand All @@ -96,7 +99,7 @@ void sample_fifo_channel_test_try_recv(void) {
char buf[100];

TRY_RECV(handler, buf)
assert(strcmp(buf, "") == 0);
assert(strcmp(buf, "nodata") == 0);

SEND(closure, "v1")
SEND(closure, "v22")
Expand All @@ -112,10 +115,13 @@ void sample_fifo_channel_test_try_recv(void) {
TRY_RECV(handler, buf)
assert(strcmp(buf, "v4444") == 0);
TRY_RECV(handler, buf)
assert(strcmp(buf, "") == 0);
assert(strcmp(buf, "nodata") == 0);

z_drop(z_move(handler));
z_drop(z_move(closure));
TRY_RECV(handler, buf)
assert(strcmp(buf, "closed") == 0);

z_drop(z_move(handler));
}

void sample_ring_channel_test_in_size(void) {
Expand All @@ -126,7 +132,7 @@ void sample_ring_channel_test_in_size(void) {
char buf[100];

TRY_RECV(handler, buf)
assert(strcmp(buf, "") == 0);
assert(strcmp(buf, "nodata") == 0);

SEND(closure, "v1")
SEND(closure, "v22")
Expand All @@ -142,13 +148,13 @@ void sample_ring_channel_test_in_size(void) {
RECV(handler, buf)
assert(strcmp(buf, "v4444") == 0);
TRY_RECV(handler, buf)
assert(strcmp(buf, "") == 0);
assert(strcmp(buf, "nodata") == 0);

z_drop(z_move(closure));
RECV(handler, buf)
assert(strcmp(buf, "closed") == 0);

z_drop(z_move(handler));
z_drop(z_move(closure));
}

void sample_ring_channel_test_over_size(void) {
Expand All @@ -158,7 +164,7 @@ void sample_ring_channel_test_over_size(void) {

char buf[100];
TRY_RECV(handler, buf)
assert(strcmp(buf, "") == 0);
assert(strcmp(buf, "nodata") == 0);

SEND(closure, "v1")
SEND(closure, "v22")
Expand All @@ -172,7 +178,7 @@ void sample_ring_channel_test_over_size(void) {
RECV(handler, buf)
assert(strcmp(buf, "v4444") == 0);
TRY_RECV(handler, buf)
assert(strcmp(buf, "") == 0);
assert(strcmp(buf, "nodata") == 0);
z_drop(z_move(closure));
TRY_RECV(handler, buf)
assert(strcmp(buf, "closed") == 0);
Expand Down
5 changes: 2 additions & 3 deletions tests/z_client_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ int main(int argc, char **argv) {
for (unsigned int i = 0; i < SET; i++) idx[i] = i;

z_owned_session_t s1;
z_open(&s1, z_move(config));
assert(z_check(s1));
assert(z_open(&s1, z_move(config)) == Z_OK);
_z_string_t zid1 = format_id(&(_Z_RC_IN_VAL(z_loan(s1))->_local_zid));
printf("Session 1 with PID: %s\n", _z_string_data(&zid1));
_z_string_clear(&zid1);
Expand All @@ -148,7 +147,7 @@ int main(int argc, char **argv) {
zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, argv[1]);

z_owned_session_t s2;
z_open(&s2, z_move(config));
assert(z_open(&s2, z_move(config)) == Z_OK);
assert(z_check(s2));
_z_string_t zid2 = format_id(&(_Z_RC_IN_VAL(z_loan(s2))->_local_zid));
printf("Session 2 with PID: %s\n", _z_string_data(&zid2));
Expand Down
7 changes: 3 additions & 4 deletions tests/z_peer_multicast_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ int main(int argc, char **argv) {
for (unsigned int i = 0; i < SET; i++) idx[i] = i;

z_owned_session_t s1;
z_open(&s1, z_move(config));
assert(z_check(s1));
assert(z_open(&s1, z_move(config)) == Z_OK);
_z_slice_t id_as_bytes =
_z_slice_from_buf(_Z_RC_IN_VAL(z_loan(s1))->_local_zid.id, _z_id_len(_Z_RC_IN_VAL(z_loan(s1))->_local_zid));
_z_string_t zid1 = _z_string_convert_bytes(&id_as_bytes);
Expand All @@ -95,8 +94,8 @@ int main(int argc, char **argv) {
zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, argv[1]);

z_owned_session_t s2;
z_open(&s2, z_move(config));
assert(z_check(s2));
assert(z_open(&s2, z_move(config)) == Z_OK);

id_as_bytes =
_z_slice_from_buf(_Z_RC_IN_VAL(z_loan(s2))->_local_zid.id, _z_id_len(_Z_RC_IN_VAL(z_loan(s2))->_local_zid));
_z_string_t zid2 = _z_string_convert_bytes(&id_as_bytes);
Expand Down
2 changes: 1 addition & 1 deletion zenohpico.pc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ prefix=/usr/local
Name: zenohpico
Description:
URL:
Version: 1.0.20240801dev
Version: 1.0.20240805dev
Cflags: -I${prefix}/include
Libs: -L${prefix}/lib -lzenohpico

0 comments on commit 0406e2e

Please sign in to comment.