Skip to content

Commit

Permalink
feat: add encoding example
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Jun 26, 2024
1 parent 3050f54 commit bf490ef
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
7 changes: 7 additions & 0 deletions examples/unix/c11/z_pub_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ int main(int argc, char **argv) {
// Allocate buffer
char buf_ind[16];

// Create encoding
z_owned_encoding_t encoding;

// Publish data
printf("Press CTRL-C to quit...\n");
char buf[256];
Expand All @@ -172,6 +175,10 @@ int main(int argc, char **argv) {
zp_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&ctx, kv_pairs_size(&ctx));
options.attachment = z_move(attachment);

// Add encoding value
z_encoding_from_str(&encoding, "text/plain;utf8");
options.encoding = z_move(encoding);

z_publisher_put(z_loan(pub), z_move(payload), &options);
}
// Clean up
Expand Down
8 changes: 8 additions & 0 deletions examples/unix/c11/z_sub_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,14 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) {
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
z_owned_string_t value;
z_bytes_deserialize_into_string(z_sample_payload(sample), &value);
z_owned_string_t encoding;
z_encoding_to_string(z_sample_encoding(sample), &encoding);

printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value)));
// Check encoding
if (z_check(encoding)) {
printf(" with encoding: %s\n", z_string_data(z_loan(encoding)));
}
// Check attachment
kv_pairs_t kvp = {.current_idx = 0, .len = KVP_LEN, .data = (kv_pair_t *)malloc(KVP_LEN * sizeof(kv_pair_t))};
parse_attachment(&kvp, z_sample_attachment(sample));
Expand All @@ -82,6 +89,7 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) {
drop_attachment(&kvp);
z_drop(z_move(keystr));
z_drop(z_move(value));
z_drop(z_move(encoding));
msg_nb++;
}

Expand Down
2 changes: 1 addition & 1 deletion include/zenoh-pico/api/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ typedef enum { Z_SAMPLE_KIND_PUT = 0, Z_SAMPLE_KIND_DELETE = 1 } z_sample_kind_t
*
* Register your encoding metadata from a string with :c:func:`z_encoding_from_str`. To get the optimization, you need
* Z_FEATURE_ENCODING_VALUES to 1 and your string should follow the format: "<constant>;<optional additional data>"
*
*
* E.g: "text/plain;utf8"
*
* Here is the list of constants:
Expand Down

0 comments on commit bf490ef

Please sign in to comment.