Skip to content

Commit

Permalink
modified payload decoding functions to return error and take value by…
Browse files Browse the repository at this point in the history
… pointer
  • Loading branch information
DenisBiryukov91 committed Apr 4, 2024
1 parent ed3a164 commit 6d005bb
Show file tree
Hide file tree
Showing 19 changed files with 223 additions and 159 deletions.
9 changes: 5 additions & 4 deletions examples/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,19 @@ int main(int argc, char **argv) {
z_owned_reply_channel_t channel = zc_reply_fifo_new(16);
z_get_options_t opts = z_get_options_default();
if (value != NULL) {
opts.value.payload = zc_payload_encode_from_string(value);
opts.payload = zc_payload_encode_from_string(value);
}
z_get(z_loan(s), keyexpr, "", z_move(channel.send),
z_move(opts)); // here, the send is moved and will be dropped by zenoh when adequate
z_owned_reply_t reply = z_reply_null();
z_owned_str_t payload_value = z_str_null();
for (z_call(channel.recv, &reply); z_check(reply); z_call(channel.recv, &reply)) {
if (z_reply_is_ok(&reply)) {
z_sample_t sample = z_reply_ok(&reply);
z_owned_str_t keystr = z_keyexpr_to_string(z_sample_keyexpr(&sample));
z_owned_str_t payload = zc_payload_decode_into_string(z_sample_payload(&sample));
printf(">> Received ('%s': '%s')\n", z_loan(keystr), z_loan(payload));
z_drop(z_move(payload));
zc_payload_decode_into_string(z_sample_payload(&sample), &payload_value);
printf(">> Received ('%s': '%s')\n", z_loan(keystr), z_loan(payload_value));
z_drop(z_move(payload_value));
z_drop(z_move(keystr));
} else {
printf("Received an error\n");
Expand Down
7 changes: 4 additions & 3 deletions examples/z_non_blocking_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ int main(int argc, char **argv) {
z_get(z_loan(s), keyexpr, "", z_move(channel.send),
z_move(opts)); // here, the send is moved and will be dropped by zenoh when adequate
z_owned_reply_t reply = z_reply_null();
z_owned_str_t payload_value = z_str_null();
for (bool call_success = z_call(channel.recv, &reply); !call_success || z_check(reply);
call_success = z_call(channel.recv, &reply)) {
if (!call_success) {
Expand All @@ -59,9 +60,9 @@ int main(int argc, char **argv) {
if (z_reply_is_ok(&reply)) {
z_sample_t sample = z_reply_ok(&reply);
z_owned_str_t keystr = z_keyexpr_to_string(z_sample_keyexpr(&sample));
z_owned_str_t payload = zc_payload_decode_into_string(z_sample_payload(&sample));
printf(">> Received ('%s': '%s')\n", z_loan(keystr), z_loan(payload));
z_drop(z_move(payload));
zc_payload_decode_into_string(z_sample_payload(&sample), &payload_value);
printf(">> Received ('%s': '%s')\n", z_loan(keystr), z_loan(payload_value));
z_drop(z_move(payload_value));
z_drop(z_move(keystr));
} else {
printf("Received an error\n");
Expand Down
7 changes: 4 additions & 3 deletions examples/z_pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ const char *kind_to_str(z_sample_kind_t kind);

void data_handler(const z_sample_t *sample, void *arg) {
z_owned_str_t keystr = z_keyexpr_to_string(z_sample_keyexpr(sample));
z_owned_str_t payload = zc_payload_decode_into_string(z_sample_payload(sample));
z_owned_str_t payload_value = z_str_null();
zc_payload_decode_into_string(z_sample_payload(sample), &payload_value);
printf(">> [Subscriber] Received %s ('%s': '%s')\n", kind_to_str(z_sample_kind(sample)), z_loan(keystr),
z_loan(payload));
z_drop(z_move(payload));
z_loan(payload_value));
z_drop(z_move(payload_value));
z_drop(z_move(keystr));
}

Expand Down
7 changes: 4 additions & 3 deletions examples/z_query_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ const char *kind_to_str(z_sample_kind_t kind);

void data_handler(const z_sample_t *sample, void *arg) {
z_owned_str_t keystr = z_keyexpr_to_string(z_sample_keyexpr(sample));
z_owned_str_t payload = zc_payload_decode_into_string(z_sample_payload(sample));
z_owned_str_t payload_value = z_str_null();
zc_payload_decode_into_string(z_sample_payload(sample), &payload_value);
printf(">> [Subscriber] Received %s ('%s': '%s')\n", kind_to_str(z_sample_kind(sample)), z_loan(keystr),
z_loan(payload));
z_drop(z_move(payload));
z_loan(payload_value));
z_drop(z_move(payload_value));
z_drop(z_move(keystr));
}

Expand Down
8 changes: 5 additions & 3 deletions examples/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ void query_handler(const z_query_t *query, void *context) {
z_bytes_t pred = z_query_parameters(query);
zc_payload_t payload = z_query_value(query).payload;
if (zc_payload_len(payload) > 0) {
z_owned_str_t payload_string = zc_payload_decode_into_string(payload);
z_owned_str_t payload_value = z_str_null();
zc_payload_decode_into_string(payload, &payload_value);
printf(">> [Queryable ] Received Query '%s?%.*s' with value '%s'\n", z_loan(keystr), (int)pred.len,
pred.start, z_loan(payload_string));
z_drop(z_move(payload_string));
pred.start, z_loan(payload_value));
z_drop(z_move(payload_value));
} else {
printf(">> [Queryable ] Received Query '%s?%.*s'\n", z_loan(keystr), (int)pred.len, pred.start);
}
z_query_reply_options_t options = z_query_reply_options_default();
options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL);

zc_owned_payload_t reply_payload = zc_payload_encode_from_string(value);
z_query_reply(query, z_keyexpr((const char *)context), z_move(reply_payload), &options);
z_drop(z_move(keystr));
Expand Down
7 changes: 4 additions & 3 deletions examples/z_queryable_with_channels.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ int main(int argc, char **argv) {
z_bytes_t pred = z_query_parameters(&query);
zc_payload_t payload = z_query_value(&query).payload;
if (zc_payload_len(payload) > 0) {
z_owned_str_t payload_string = zc_payload_decode_into_string(payload);
z_owned_str_t payload_value = z_str_null();
zc_payload_decode_into_string(payload, &payload_value);
printf(">> [Queryable ] Received Query '%s?%.*s' with value '%s'\n", z_loan(keystr), (int)pred.len,
pred.start, z_loan(payload_string));
z_drop(z_move(payload_string));
pred.start, z_loan(payload_value));
z_drop(z_move(payload_value));
} else {
printf(">> [Queryable ] Received Query '%s?%.*s'\n", z_loan(keystr), (int)pred.len, pred.start);
}
Expand Down
7 changes: 4 additions & 3 deletions examples/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ const char *kind_to_str(z_sample_kind_t kind);

void data_handler(const z_sample_t *sample, void *arg) {
z_owned_str_t keystr = z_keyexpr_to_string(z_sample_keyexpr(sample));
z_owned_str_t payload = zc_payload_decode_into_string(z_sample_payload(sample));
z_owned_str_t payload_value = z_str_null();
zc_payload_decode_into_string(z_sample_payload(sample), &payload_value);
printf(">> [Subscriber] Received %s ('%s': '%s')\n", kind_to_str(z_sample_kind(sample)), z_loan(keystr),
z_loan(payload));
z_drop(z_move(payload));
z_loan(payload_value));
z_drop(z_move(payload_value));
z_drop(z_move(keystr));
}

Expand Down
10 changes: 6 additions & 4 deletions examples/z_sub_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ int8_t attachment_reader(z_bytes_t key, z_bytes_t val, void *ctx) {

void data_handler(const z_sample_t *sample, void *arg) {
z_owned_str_t keystr = z_keyexpr_to_string(z_sample_keyexpr(sample));
z_owned_str_t payload = zc_payload_decode_into_string(z_sample_payload(sample));
printf(">> [Subscriber] Received %s ('%s': '%s')\n", kind_to_str(z_sample_kind(sample)), z_loan(keystr), z_loan(payload));
z_owned_str_t payload_value = z_str_null();
zc_payload_decode_into_string(z_sample_payload(sample), &payload_value);
printf(">> [Subscriber] Received %s ('%s': '%s')\n", kind_to_str(z_sample_kind(sample)), z_loan(keystr),
z_loan(payload_value));

z_attachment_t attachment = z_sample_attachment(sample);
// checks if attachment exists
Expand All @@ -35,11 +37,11 @@ void data_handler(const z_sample_t *sample, void *arg) {

// reads particular attachment item
z_bytes_t index = z_attachment_get(attachment, z_bytes_from_str("index"));
if (z_check(index)) {
if (z_bytes_is_initialized(&index)) {
printf(" message number: %.*s\n", (int)index.len, index.start);
}
}
z_drop(z_move(payload));
z_drop(z_move(payload_value));
z_drop(z_move(keystr));
}

Expand Down
52 changes: 28 additions & 24 deletions include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ typedef struct ALIGN(8) z_owned_buffer_t {
typedef struct z_buffer_t {
struct z_owned_buffer_t *_inner;
} z_buffer_t;
typedef struct z_owned_bytes_t {
uint8_t *start;
size_t len;
} z_owned_bytes_t;
/**
* A map of maybe-owned vector of bytes to owned vector of bytes.
*
Expand Down Expand Up @@ -631,7 +635,7 @@ typedef struct z_encoding_t {
*/
typedef struct z_owned_encoding_t {
enum z_encoding_prefix_t prefix;
struct z_bytes_t suffix;
struct z_owned_bytes_t suffix;
bool _dropped;
} z_owned_encoding_t;
/**
Expand All @@ -646,17 +650,6 @@ typedef struct z_query_consolidation_t {
* The `payload` field may be modified, and Zenoh will take the new values into account.
*/
typedef struct z_owned_buffer_t zc_owned_payload_t;
/**
* An owned zenoh value.
*
* Members:
* zc_owned_payload_t payload: The payload of this zenoh value.
* z_owned_encoding_t encoding: The encoding of this zenoh value `payload`.
*/
typedef struct z_owned_value_t {
zc_owned_payload_t payload;
struct z_owned_encoding_t encoding;
} z_owned_value_t;
/**
* Options passed to the :c:func:`z_get` function.
*
Expand All @@ -670,7 +663,8 @@ typedef struct z_owned_value_t {
typedef struct z_get_options_t {
enum z_query_target_t target;
struct z_query_consolidation_t consolidation;
struct z_owned_value_t value;
zc_owned_payload_t payload;
struct z_encoding_t encoding;
struct z_attachment_t attachment;
uint64_t timeout_ms;
} z_get_options_t;
Expand Down Expand Up @@ -1102,7 +1096,7 @@ ZENOHC_API struct z_owned_buffer_t z_buffer_null(void);
/**
* Returns the `index`th slice of the buffer, aliasing it.
*
* Out of bounds accesses will return `z_bytes_null`.
* Out of bounds accesses will return `z_bytes_empty`.
*/
ZENOHC_API struct z_bytes_t z_buffer_slice_at(struct z_buffer_t buffer, size_t index);
/**
Expand All @@ -1114,13 +1108,23 @@ ZENOHC_API size_t z_buffer_slice_count(struct z_buffer_t buffer);
/**
* Returns ``true`` if `b` is initialized.
*/
ZENOHC_API bool z_bytes_check(const struct z_bytes_t *b);
ZENOHC_API bool z_bytes_check(const struct z_owned_bytes_t *b);
ZENOHC_API struct z_owned_bytes_t z_bytes_clone(const struct z_bytes_t *b);
/**
* Returns the gravestone value for `z_bytes_t`
*/
ZENOHC_API struct z_bytes_t z_bytes_empty(void);
/**
* Returns a view of `str` using `strlen` (this should therefore not be used with untrusted inputs).
*
* `str == NULL` will cause this to return `z_bytes_null()`
* `str == NULL` will cause this to return `z_bytes_empty()`
*/
ZENOHC_API struct z_bytes_t z_bytes_from_str(const char *str);
/**
* Returns ``true`` if `b` is initialized.
*/
ZENOHC_API bool z_bytes_is_initialized(const struct z_bytes_t *b);
ZENOHC_API struct z_bytes_t z_bytes_loan(const struct z_owned_bytes_t *b);
/**
* Aliases `this` into a generic `z_attachment_t`, allowing it to be passed to corresponding APIs.
*/
Expand Down Expand Up @@ -1211,14 +1215,14 @@ ZENOHC_API struct z_owned_bytes_map_t z_bytes_map_null(void);
/**
* Deprecated in favor of `z_bytes_from_str`: Returns a view of `str` using `strlen` (this should therefore not be used with untrusted inputs).
*
* `str == NULL` will cause this to return `z_bytes_null()`
* `str == NULL` will cause this to return `z_bytes_empty()`
*/
ZENOHC_API
struct z_bytes_t z_bytes_new(const char *str);
/**
* Returns the gravestone value for `z_bytes_t`
* Returns the gravestone value for `z_owned_bytes_t`
*/
ZENOHC_API struct z_bytes_t z_bytes_null(void);
ZENOHC_API struct z_owned_bytes_t z_bytes_null(void);
/**
* Constructs a `len` bytes long view starting at `start`.
*/
Expand Down Expand Up @@ -2270,10 +2274,6 @@ ZENOHC_API int8_t z_undeclare_queryable(struct z_owned_queryable_t *qable);
*/
ZENOHC_API
int8_t z_undeclare_subscriber(struct z_owned_subscriber_t *sub);
ZENOHC_API bool z_value_check(const struct z_owned_value_t *value);
ZENOHC_API void z_value_drop(struct z_owned_value_t *value);
ZENOHC_API struct z_value_t z_value_loan(const struct z_owned_value_t *value);
ZENOHC_API struct z_owned_value_t z_value_null(void);
/**
* Converts the kind of zenoh entity into a string.
*
Expand Down Expand Up @@ -2467,7 +2467,11 @@ ZENOHC_API zc_owned_payload_t zc_payload_clone(zc_payload_t payload);
/**
* Decodes payload into null-terminated string
*/
ZENOHC_API struct z_owned_str_t zc_payload_decode_into_string(zc_payload_t payload);
ZENOHC_API int8_t zc_payload_decode_into_bytes(zc_payload_t payload, struct z_owned_bytes_t *b);
/**
* Decodes payload into null-terminated string
*/
ZENOHC_API int8_t zc_payload_decode_into_string(zc_payload_t payload, struct z_owned_str_t *cstr);
/**
* Decrements `payload`'s backing refcount, releasing the memory if appropriate.
*/
Expand Down
4 changes: 2 additions & 2 deletions include/zenoh_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
z_keyexpr_t : z_keyexpr_is_initialized, \
z_owned_config_t : z_config_check, \
z_owned_scouting_config_t : z_scouting_config_check, \
z_bytes_t : z_bytes_check, \
z_owned_bytes_t : z_bytes_check, \
z_owned_subscriber_t : z_subscriber_check, \
z_owned_pull_subscriber_t : z_pull_subscriber_check, \
z_owned_queryable_t : z_queryable_check, \
Expand Down Expand Up @@ -257,7 +257,7 @@ inline bool z_check(const z_owned_keyexpr_t& v) { return z_keyexpr_check(&v); }
inline bool z_check(const z_keyexpr_t& v) { return z_keyexpr_is_initialized(&v); }
inline bool z_check(const z_owned_config_t& v) { return z_config_check(&v); }
inline bool z_check(const z_owned_scouting_config_t& v) { return z_scouting_config_check(&v); }
inline bool z_check(const z_bytes_t& v) { return z_bytes_check(&v); }
inline bool z_check(const z_owned_bytes_t& v) { return z_bytes_check(&v); }
inline bool z_check(const zc_owned_payload_t& v) { return zc_payload_check(&v); }
inline bool z_check(const zc_owned_shmbuf_t& v) { return zc_shmbuf_check(&v); }
inline bool z_check(const zc_owned_shm_manager_t& v) { return zc_shm_manager_check(&v); }
Expand Down
10 changes: 5 additions & 5 deletions src/attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{borrow::Cow, cell::UnsafeCell, collections::HashMap};

use libc::c_void;

use crate::{impl_guarded_transmute, z_bytes_null, z_bytes_t};
use crate::{impl_guarded_transmute, z_bytes_empty, z_bytes_t};

use zenoh::sample::{Attachment, AttachmentBuilder};

Expand Down Expand Up @@ -101,7 +101,7 @@ pub extern "C" fn z_attachment_get(this: z_attachment_t, key: z_bytes_t) -> z_by

let mut context = attachment_get_iterator_context {
key,
value: z_bytes_null(),
value: z_bytes_empty(),
};

if this.iteration_driver.map_or(false, |iteration_driver| {
Expand All @@ -113,7 +113,7 @@ pub extern "C" fn z_attachment_get(this: z_attachment_t, key: z_bytes_t) -> z_by
}) {
context.value
} else {
z_bytes_null()
z_bytes_empty()
}
}

Expand Down Expand Up @@ -237,12 +237,12 @@ pub extern "C" fn z_bytes_map_is_empty(this: &mut z_owned_bytes_map_t) -> bool {
pub extern "C" fn z_bytes_map_get(this: &z_owned_bytes_map_t, key: z_bytes_t) -> z_bytes_t {
let this = unsafe { &*this.get() };
let (Some(this), Some(key)) = (this.as_ref(), key.as_slice()) else {
return z_bytes_null();
return z_bytes_empty();
};
if let Some(value) = this.get(key) {
value.as_ref().into()
} else {
z_bytes_null()
z_bytes_empty()
}
}

Expand Down
Loading

0 comments on commit 6d005bb

Please sign in to comment.