Skip to content

Commit

Permalink
Rework encoding (#452)
Browse files Browse the repository at this point in the history
* feat: add _z_string_check function

* feat: replace z_slice by z_string in encodings

* feat: update codec with encoding z_string

* feat: update encoding use with z_string

* fix: z_string related memory leak

* chore: update zenoh-pico.h

* feat: remove encoding enum from functions

* feat: remove encoding from most examples

* feat: remove encoding id enum

* feat: switch to encoding form string api

* feat: add encoding values config token

* doc: add encoding values documentation

* fix: remove encoding from z_pub

* feat: add wire-level optimization on encoding values

* feat: add encoding example

* fix: remove encoding from tests

* doc: update api doc

* test: update attachment test

* doc: update doc api

* fix: function name typo

* fix: segfault with default encoding
  • Loading branch information
jean-roland authored Jun 28, 2024
1 parent 7e0a107 commit 27000eb
Show file tree
Hide file tree
Showing 32 changed files with 384 additions and 232 deletions.
5 changes: 2 additions & 3 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ Enums
.. autocenum:: constants.h::z_whatami_t
.. autocenum:: constants.h::zp_keyexpr_canon_status_t
.. autocenum:: constants.h::z_sample_kind_t
.. autocenum:: constants.h::z_encoding_id_t
.. autocenum:: constants.h::z_consolidation_mode_t
.. autocenum:: constants.h::z_reliability_t
.. autocenum:: constants.h::z_reply_tag_t
Expand Down Expand Up @@ -282,14 +281,14 @@ Primitives
.. autocfunction:: primitives.h::z_scouting_config_from
.. autocfunction:: primitives.h::zp_scouting_config_get
.. autocfunction:: primitives.h::zp_scouting_config_insert
.. autocfunction:: primitives.h::zp_encoding_make
.. autocfunction:: primitives.h::zp_encoding_default
.. autocfunction:: primitives.h::z_encoding_check
.. autocfunction:: primitives.h::z_encoding_drop
.. autocfunction:: primitives.h::z_encoding_loan
.. autocfunction:: primitives.h::z_encoding_loan_mut
.. autocfunction:: primitives.h::z_encoding_move
.. autocfunction:: primitives.h::z_encoding_null
.. autocfunction:: primitives.h::z_encoding_from_str
.. autocfunction:: primitives.h::z_encoding_to_string
.. autocfunction:: primitives.h::z_reply_err_payload
.. autocfunction:: primitives.h::z_reply_err_encoding
.. autocfunction:: primitives.h::z_slice_data
Expand Down
9 changes: 1 addition & 8 deletions examples/unix/c11/z_pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,7 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
z_bytes_serialize_from_string(&payload, buf);

// Create encoding
z_owned_encoding_t encoding;
zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, "utf8");
z_publisher_put_options_t options;
z_publisher_put_options_default(&options);
options.encoding = z_move(encoding);

z_publisher_put(z_loan(pub), z_move(payload), &options);
z_publisher_put(z_loan(pub), z_move(payload), NULL);
}
// Clean up
z_undeclare_publisher(z_move(pub));
Expand Down
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
9 changes: 1 addition & 8 deletions examples/unix/c11/z_put.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,8 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
z_bytes_serialize_from_string(&payload, value);

// Create encoding
z_owned_encoding_t encoding;
zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL);

printf("Putting Data ('%s': '%s')...\n", keyexpr, value);
z_put_options_t options;
z_put_options_default(&options);
options.encoding = z_move(encoding);
if (z_put(z_loan(s), z_loan(ke), z_move(payload), &options) < 0) {
if (z_put(z_loan(s), z_loan(ke), z_move(payload), NULL) < 0) {
printf("Oh no! Put has failed...\n");
}
// Clean up
Expand Down
10 changes: 1 addition & 9 deletions examples/unix/c11/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,11 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
}
z_drop(z_move(payload_string));

// Create encoding
z_owned_encoding_t encoding;
zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL);

z_query_reply_options_t options;
z_query_reply_options_default(&options);
options.encoding = z_move(encoding);

// Reply value encoding
z_owned_bytes_t reply_payload;
z_bytes_serialize_from_string(&reply_payload, value);

z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options);
z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), NULL);
z_drop(z_move(keystr));
msg_nb++;
}
Expand Down
11 changes: 3 additions & 8 deletions examples/unix/c11/z_queryable_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,6 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
drop_attachment(&kvp);
z_drop(z_move(payload_string));

// Create encoding
z_owned_encoding_t encoding;
zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL);

z_query_reply_options_t options;
z_query_reply_options_default(&options);
options.encoding = z_move(encoding);

// Reply value encoding
z_owned_bytes_t reply_payload;
z_bytes_serialize_from_string(&reply_payload, value);
Expand All @@ -143,6 +135,9 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
kv_pairs_tx_t kv_ctx = (kv_pairs_tx_t){.data = kvs, .current_idx = 0, .len = 1};
z_owned_bytes_t attachment;
zp_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&kv_ctx, kv_pairs_size(&kv_ctx));

z_query_reply_options_t options;
z_query_reply_options_default(&options);
options.attachment = z_move(attachment);

z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options);
Expand Down
5 changes: 5 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,11 @@ 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)));
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 +86,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
9 changes: 1 addition & 8 deletions examples/unix/c99/z_pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,7 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
z_bytes_serialize_from_string(&payload, buf);

// Create encoding
z_owned_encoding_t encoding;
zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL);
z_publisher_put_options_t options;
z_publisher_put_options_default(&options);
options.encoding = z_encoding_move(&encoding);

z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), &options);
z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), NULL);
}
// Clean up
z_undeclare_publisher(z_publisher_move(&pub));
Expand Down
9 changes: 1 addition & 8 deletions examples/unix/c99/z_put.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,8 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
z_bytes_serialize_from_string(&payload, value);

// Create encoding
z_owned_encoding_t encoding;
zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL);

printf("Putting Data ('%s': '%s')...\n", keyexpr, value);
z_put_options_t options;
z_put_options_default(&options);
options.encoding = z_encoding_move(&encoding);
if (z_put(z_session_loan(&s), z_keyexpr_loan(&ke), z_bytes_move(&payload), &options) < 0) {
if (z_put(z_session_loan(&s), z_keyexpr_loan(&ke), z_bytes_move(&payload), NULL) < 0) {
printf("Oh no! Put has failed...\n");
}

Expand Down
9 changes: 1 addition & 8 deletions examples/unix/c99/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,11 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
}
z_string_drop(z_string_move(&payload_string));

// Create encoding
z_owned_encoding_t encoding;
zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL);

z_query_reply_options_t options;
z_query_reply_options_default(&options);
options.encoding = z_encoding_move(&encoding);
// Reply value encoding
z_owned_bytes_t reply_payload;
z_bytes_serialize_from_string(&reply_payload, value);

z_query_reply(query, z_query_keyexpr(query), z_bytes_move(&reply_payload), &options);
z_query_reply(query, z_query_keyexpr(query), z_bytes_move(&reply_payload), NULL);
z_string_drop(z_string_move(&keystr));
}

Expand Down
9 changes: 1 addition & 8 deletions examples/windows/z_pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,7 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
z_bytes_serialize_from_string(&payload, buf);

// Create encoding
z_owned_encoding_t encoding;
zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL);
z_publisher_put_options_t options;
z_publisher_put_options_default(&options);
options.encoding = z_move(encoding);

z_publisher_put(z_loan(pub), z_move(payload), &options);
z_publisher_put(z_loan(pub), z_move(payload), NULL);
}

// Clean-up
Expand Down
9 changes: 1 addition & 8 deletions examples/windows/z_put.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,8 @@ int main(int argc, char **argv) {
z_owned_bytes_t payload;
z_bytes_serialize_from_string(&payload, value);

// Create encoding
z_owned_encoding_t encoding;
zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL);

printf("Putting Data ('%s': '%s')...\n", keyexpr, value);
z_put_options_t options;
z_put_options_default(&options);
options.encoding = z_move(encoding);
if (z_put(z_loan(s), z_loan(ke), z_move(payload), &options) < 0) {
if (z_put(z_loan(s), z_loan(ke), z_move(payload), NULL) < 0) {
printf("Oh no! Put has failed...\n");
}

Expand Down
9 changes: 1 addition & 8 deletions examples/windows/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,11 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
}
z_drop(z_move(payload_string));

// Create encoding
z_owned_encoding_t encoding;
zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL);

z_query_reply_options_t options;
z_query_reply_options_default(&options);
options.encoding = z_move(encoding);
// Reply value encoding
z_owned_bytes_t reply_payload;
z_bytes_serialize_from_string(&reply_payload, value);

z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options);
z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), NULL);

z_drop(z_move(keystr));
}
Expand Down
6 changes: 3 additions & 3 deletions include/zenoh-pico.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
#ifndef ZENOH_PICO_H
#define ZENOH_PICO_H

#define ZENOH_PICO "0.11.0.0"
#define ZENOH_PICO_MAJOR 0
#define ZENOH_PICO_MINOR 11
#define ZENOH_PICO "1.0.0.0"
#define ZENOH_PICO_MAJOR 1
#define ZENOH_PICO_MINOR 0
#define ZENOH_PICO_PATCH 0
#define ZENOH_PICO_TWEAK 0

Expand Down
1 change: 1 addition & 0 deletions include/zenoh-pico.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define ZENOH_PICO_TWEAK @ZENOH_PICO_TWEAK@

#include "zenoh-pico/api/constants.h"
#include "zenoh-pico/api/handlers.h"
#include "zenoh-pico/api/macros.h"
#include "zenoh-pico/api/primitives.h"
#include "zenoh-pico/api/types.h"
Expand Down
Loading

0 comments on commit 27000eb

Please sign in to comment.