Skip to content

Commit

Permalink
Merge commit '8bf9cd90cd941f09f6ec6d59d122e254d5d46d76'
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowhatter committed May 28, 2024
2 parents 92ac140 + 8bf9cd9 commit e3d38d3
Show file tree
Hide file tree
Showing 10 changed files with 370 additions and 120 deletions.
4 changes: 4 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,13 @@ Functions
.. doxygenfunction:: z_bytes_encode_from_slice
.. doxygenfunction:: z_bytes_encode_from_string
.. doxygenfunction:: z_bytes_encode_from_slice_map
.. doxygenfunction:: z_bytes_encode_from_iter
.. doxygenfunction:: z_bytes_encode_from_pair
.. doxygenfunction:: z_bytes_decode_into_slice
.. doxygenfunction:: z_bytes_decode_into_string
.. doxygenfunction:: z_bytes_decode_into_slice_map
.. doxygenfunction:: z_bytes_decode_into_iter
.. doxygenfunction:: z_bytes_decode_into_pair

.. doxygenfunction:: z_bytes_clone
.. doxygenfunction:: z_bytes_loan
Expand Down
12 changes: 5 additions & 7 deletions examples/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int main(int argc, char **argv) {
expr = argv[1];
break;
case 1:
// Do nothing
value = "Test Value";
break;
}
z_view_keyexpr_t keyexpr;
Expand All @@ -49,7 +49,7 @@ int main(int argc, char **argv) {

printf("Opening session...\n");
z_owned_session_t s;
if (!z_open(&s, z_move(config))) {
if (z_open(&s, z_move(config))) {
printf("Unable to open session!\n");
exit(-1);
}
Expand All @@ -72,18 +72,16 @@ int main(int argc, char **argv) {

for (z_call(z_loan(channel.recv), &reply); z_check(reply); z_call(z_loan(channel.recv), &reply)) {
if (z_reply_is_ok(z_loan(reply))) {
const z_loaned_sample_t* sample = z_reply_ok(z_loan(reply));
const z_loaned_sample_t *sample = z_reply_ok(z_loan(reply));

z_view_str_t key_str;
z_keyexpr_to_string(z_sample_keyexpr(sample), &key_str);

z_owned_str_t reply_str;
z_bytes_decode_into_string(z_sample_payload(sample), &reply_str);

printf(">> Received ('%.*s': '%.*s')\n",
(int)z_str_len(z_loan(key_str)), z_str_data(z_loan(key_str)),
(int)z_str_len(z_loan(reply_str)), z_str_data(z_loan(reply_str))
);
printf(">> Received ('%.*s': '%.*s')\n", (int)z_str_len(z_loan(key_str)), z_str_data(z_loan(key_str)),
(int)z_str_len(z_loan(reply_str)), z_str_data(z_loan(reply_str)));
z_drop(z_move(reply_str));
} else {
printf("Received an error\n");
Expand Down
13 changes: 9 additions & 4 deletions examples/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
#define PING_TIMEOUT_SEC 1

#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
do { \
errno = en; \
perror(msg); \
exit(EXIT_FAILURE); \
} while (0)

z_owned_condvar_t cond;
z_owned_mutex_t mutex;
Expand All @@ -38,17 +42,17 @@ int main(int argc, char** argv) {
-w (optional, int, default=%d): the warmup time in ms during which pings will be emitted but not measured\n\
-c (optional, string): the path to a configuration file for the session. If this option isn't passed, the default configuration will be used.\n\
",
DEFAULT_PKT_SIZE, DEFAULT_PING_NB, DEFAULT_WARMUP_MS);
DEFAULT_PING_NB, DEFAULT_PKT_SIZE, DEFAULT_WARMUP_MS);
return 1;
}
z_mutex_init(&mutex);
z_condvar_init(&cond);
z_owned_config_t config;
z_owned_config_t config;
if (args.config_path) {
zc_config_from_file(&config, args.config_path);
} else {
z_config_default(&config);
}
}
z_owned_session_t session;
z_open(&session, z_move(config));
z_view_keyexpr_t ping;
Expand Down Expand Up @@ -85,6 +89,7 @@ int main(int argc, char** argv) {
}
unsigned long* results = z_malloc(sizeof(unsigned long) * args.number_of_pings);
for (int i = 0; i < args.number_of_pings; i++) {
z_bytes_encode_from_slice(&payload, data, args.size);
z_clock_t measure_start = z_clock_now();
z_publisher_put(z_loan(pub), z_move(payload), NULL);
int s = z_condvar_wait(z_loan(cond), z_loan_mut(mutex));
Expand Down
45 changes: 31 additions & 14 deletions examples/z_pub_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@

#include "zenoh.h"

typedef struct kv_pair_t {
const char* key;
const char* value;
} kv_pair_t;

typedef struct kv_pairs_t {
const kv_pair_t* data;
size_t len;
size_t current_idx;
} kv_pairs_t;

void create_attachment_iter(z_owned_bytes_t* kv_pair, void* context) {
kv_pairs_t *kvs = (kv_pairs_t*)(context);
z_owned_bytes_t k, v;
if (kvs->current_idx >= kvs->len) {
z_null(kv_pair);
} else {
z_bytes_encode_from_string(&k, kvs->data[kvs->current_idx].key);
z_bytes_encode_from_string(&v, kvs->data[kvs->current_idx].value);
z_bytes_encode_from_pair(kv_pair, z_move(k), z_move(v));
kvs->current_idx++;
}
};


int main(int argc, char **argv) {
char *keyexpr = "demo/example/zenoh-c-pub";
char *value = "Pub from C!";
Expand Down Expand Up @@ -54,14 +79,9 @@ int main(int argc, char **argv) {
z_publisher_put_options_t options;
z_publisher_put_options_default(&options);

// allocate attachment map
z_owned_slice_map_t map;
z_slice_map_new(&map);
z_view_slice_t src_key, src_value;
z_view_slice_from_str(&src_key, "source");
z_view_slice_from_str(&src_value, "C");
// add some value
z_slice_map_insert_by_alias(z_loan_mut(map), z_loan(src_key), z_loan(src_value));
// allocate attachment data
kv_pair_t kvs[2];
kvs[0] = (kv_pair_t){ .key = "source", .value = "C" };
// allocate attachment and payload
z_owned_bytes_t attachment;
z_owned_bytes_t payload;
Expand All @@ -73,11 +93,9 @@ int main(int argc, char **argv) {

// add some other attachment value
sprintf(buf_ind, "%d", idx);
z_view_slice_t index_key, index_value;
z_view_slice_from_str(&index_key, "index");
z_view_slice_from_str(&index_value, buf_ind);
z_slice_map_insert_by_alias(z_loan_mut(map), z_loan(index_key), z_loan(index_value));
z_bytes_encode_from_slice_map(&attachment, z_loan(map));
kvs[1] = (kv_pair_t){ .key = "index", .value = buf_ind };
kv_pairs_t ctx = (kv_pairs_t) { .data = kvs, .current_idx = 0, .len = 2 };
z_bytes_encode_from_iter(&attachment, create_attachment_iter, (void*)&ctx);
options.attachment = &attachment;

sprintf(buf, "[%4d] %s", idx, value);
Expand All @@ -90,7 +108,6 @@ int main(int argc, char **argv) {
z_undeclare_publisher(z_move(pub));

z_close(z_move(s));
z_drop(z_move(map));

return 0;
}
14 changes: 4 additions & 10 deletions examples/z_put.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ int main(int argc, char **argv) {
if (argc > 1) keyexpr = argv[1];
if (argc > 2) value = argv[2];

z_owned_slice_map_t attachment_map;
z_slice_map_new(&attachment_map);
z_view_slice_t map_key, map_value;
z_view_slice_from_str(&map_key, "hello");
z_view_slice_from_str(&map_value, "there");
z_slice_map_insert_by_alias(z_loan_mut(attachment_map), z_loan(map_key), z_loan(map_value));

z_owned_config_t config;
z_config_default(&config);

Expand Down Expand Up @@ -58,8 +51,10 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
z_bytes_encode_from_string(&payload, value);

z_owned_bytes_t attachment;
z_bytes_encode_from_slice_map(&attachment, z_loan(attachment_map));
z_owned_bytes_t attachment, key, val;
z_bytes_encode_from_string(&key, "hello");
z_bytes_encode_from_string(&val, "there");
z_bytes_encode_from_pair(&attachment, z_move(key), z_move(val));

z_put_options_t options;
z_put_options_default(&options);
Expand All @@ -71,6 +66,5 @@ int main(int argc, char **argv) {
}

z_close(z_move(s));
z_drop(z_move(attachment_map));
return 0;
}
29 changes: 12 additions & 17 deletions examples/z_sub_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@

const char *kind_to_str(z_sample_kind_t kind);

bool attachment_map_reader(const z_loaned_slice_t* key, const z_loaned_slice_t* val, void *ctx) {
printf(" attachment: %.*s: '%.*s'\n", (int)z_slice_len(key), z_slice_data(key),
(int)z_slice_len(val), z_slice_data(val)
z_error_t attachment_reader(const z_loaned_bytes_t* key_value, void *context) {
z_owned_bytes_t k, v;
z_owned_str_t key, value;
z_bytes_decode_into_pair(key_value, &k, &v);

z_bytes_decode_into_string(z_loan(k), &key);
z_bytes_decode_into_string(z_loan(v), &value);

printf(" attachment: %.*s: '%.*s'\n", (int)z_str_len(z_loan(key)), z_str_data(z_loan(key)),
(int)z_str_len(z_loan(value)), z_str_data(z_loan(value))
);
return false;
return 0;
}

void data_handler(const z_loaned_sample_t *sample, void *arg) {
Expand All @@ -40,19 +47,7 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) {
// checks if attachment exists
if (attachment != NULL) {
// reads full attachment
z_owned_slice_map_t attachment_map;
z_bytes_decode_into_slice_map(attachment, &attachment_map);

z_slice_map_iterate(z_loan(attachment_map), attachment_map_reader, NULL);

// reads particular attachment item
z_view_slice_t attachment_key;
z_view_slice_from_str(&attachment_key, "index");
const z_loaned_slice_t* index = z_slice_map_get(z_loan(attachment_map), z_loan(attachment_key));
if (index != NULL) {
printf(" message number: %.*s\n", (int)z_slice_len(index), z_slice_data(index));
}
z_drop(z_move(attachment_map));
z_bytes_decode_into_iter(attachment, attachment_reader, NULL);
}
z_drop(z_move(payload_string));
}
Expand Down
39 changes: 39 additions & 0 deletions include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -1417,6 +1417,26 @@ ZENOHC_API bool z_bytes_check(const struct z_owned_bytes_t *this_);
* Constructs an owned shallow copy of data in provided uninitialized memory location.
*/
ZENOHC_API void z_bytes_clone(const struct z_loaned_bytes_t *this_, struct z_owned_bytes_t *dst);
/**
* Decodes payload into an iterator to `z_loaned_bytes_t`.
* @param this_: Data to decode.
* @param iterator_body: Iterator body function, that will be called on each data item. Returning non-zero value is treated as iteration loop `break`.
* @param context: Arbitrary context that will be passed to iterator_body.
* @return last value returned by iterator_body (or 0 if there are no elements in the payload).
*/
ZENOHC_API
z_error_t z_bytes_decode_into_iter(const struct z_loaned_bytes_t *this_,
z_error_t (*iterator_body)(const struct z_loaned_bytes_t *data,
void *context),
void *context);
/**
* Decodes into a pair of `z_owned_bytes` objects.
* @return 0 in case of success, negative error code otherwise.
*/
ZENOHC_API
z_error_t z_bytes_decode_into_pair(const struct z_loaned_bytes_t *this_,
struct z_owned_bytes_t *first,
struct z_owned_bytes_t *second);
/**
* Decodes data into an owned slice.
*
Expand Down Expand Up @@ -1449,6 +1469,25 @@ z_error_t z_bytes_decode_into_string(const struct z_loaned_bytes_t *this_,
* created by `z_bytes_clone()`, they would still stay valid.
*/
ZENOHC_API void z_bytes_drop(struct z_owned_bytes_t *this_);
/**
* Constructs payload from an iterator to `z_owned_bytes_t`.
* @param this_: An uninitialized location in memery for `z_owned_bytes_t` will be constructed.
* @param iterator_body: Iterator body function, providing data items. Returning NULL
* @param context: Arbitrary context that will be passed to iterator_body.
* @return 0 in case of success, negative error code otherwise.
*/
ZENOHC_API
z_error_t z_bytes_encode_from_iter(struct z_owned_bytes_t *this_,
void (*iterator_body)(struct z_owned_bytes_t *data, void *context),
void *context);
/**
* Encodes a pair of `z_owned_bytes` objects which are consumed in the process.
* @return 0 in case of success, negative error code otherwise.
*/
ZENOHC_API
z_error_t z_bytes_encode_from_pair(struct z_owned_bytes_t *this_,
struct z_owned_bytes_t *first,
struct z_owned_bytes_t *second);
/**
* Encodes a slice by aliasing.
*/
Expand Down
Loading

0 comments on commit e3d38d3

Please sign in to comment.