Skip to content

Commit

Permalink
z_encoding API rework (#7)
Browse files Browse the repository at this point in the history
* refactor: reworking encoding prefix enum

* feat: create net/encoding module

* fix: missing include in logging module

* fix: update files with encoding module

* feat: rework z_encoding api

* fix: remove encoding from freertos examples

* feat: add encoding_loan_check function

* feat: update tests & examples with new encoding

* fix: bad size schema allocation

* feat: add _z_encoding clear and check functions

* feat: add _z_encoding copy and move functions

* fix: use _z_encoding_check for errors

* fix: set encoding id value

* fix: missing end of file newline

* fix: deactivate faulty workflow

* doc: improve encoding documentation

* fix: missing new encoding functions

* fix: pr corrections

* fix: update examples

* fix: remove z_encoding_loan_check

* feat: rework _z_encoding make/wrap functions

* fix: update alignment test

* fix: missing return statement

* fix: create encoding in loop as it's moved

* feat: add z_encoding_move function

* fix: compilation/test issue

* fix: missing z_move to alignment test

* fix: remove default encoding struct
  • Loading branch information
jean-roland authored Jun 4, 2024
1 parent a8c236d commit 506a668
Show file tree
Hide file tree
Showing 38 changed files with 399 additions and 184 deletions.
35 changes: 12 additions & 23 deletions .github/workflows/build-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ jobs:
sudo apt install -y ninja-build
FORCE_C99=ON CMAKE_GENERATOR=Ninja make
raweth_build:
name: Build raweth transport on ubuntu-latest
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Build raweth
run: |
sudo apt install -y ninja-build
Z_FEATURE_RAWETH_TRANSPORT=1 CMAKE_GENERATOR=Ninja make
zenoh_build:
name: Build Zenoh from source
runs-on: ubuntu-latest
Expand Down Expand Up @@ -115,29 +127,6 @@ jobs:
if: always()
run: kill ${{ steps.run-zenoh.outputs.zenohd-pid }}

raweth_build:
name: Build and test raweth transport on ubuntu-latest
runs-on: ubuntu-latest
strategy:
matrix:
feature_reth: [1, 0]
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Build project and run test
uses: nick-fields/retry@v3
with:
timeout_minutes: 5
max_attempts: 3
retry_on: error
command: |
sudo apt install -y ninja-build
CMAKE_GENERATOR=Ninja make
python3 ./build/tests/raweth.py --reth $Z_FEATURE_RAWETH_TRANSPORT
env:
Z_FEATURE_RAWETH_TRANSPORT: ${{ matrix.feature_reth }}

st_build:
needs: zenoh_build
name: Build and test in single thread on ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ 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_prefix_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 @@ -192,7 +192,7 @@ 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::z_encoding_default
.. autocfunction:: primitives.h::zp_encoding_default
.. autocfunction:: primitives.h::z_query_target_default
.. autocfunction:: primitives.h::z_query_consolidation_auto
.. autocfunction:: primitives.h::z_query_consolidation_default
Expand Down
5 changes: 1 addition & 4 deletions examples/freertos_plus_tcp/z_pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,13 @@ void app_main(void) {

z_publisher_put_options_t options;
z_publisher_put_options_default(&options);
options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL);
z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options);
}

// Clean-up
z_undeclare_publisher(z_move(pub));

// Stop read and lease tasks for zenoh-pico
zp_stop_read_task(z_loan_mut(s));
zp_stop_lease_task(z_loan_mut(s));

z_close(z_move(s));
}
#else
Expand Down
1 change: 0 additions & 1 deletion examples/freertos_plus_tcp/z_put.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ void app_main(void) {
printf("Putting Data ('%s': '%s')...\n", KEYEXPR, VALUE);
z_put_options_t options;
z_put_options_default(&options);
options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL);
if (z_put(z_loan(s), z_loan(ke), (const uint8_t *)VALUE, strlen(VALUE), &options) < 0) {
printf("Oh no! Put has failed...\n");
}
Expand Down
19 changes: 11 additions & 8 deletions examples/unix/c11/z_pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,15 @@ int main(int argc, char **argv) {
// Start read and lease tasks for zenoh-pico
if (zp_start_read_task(z_loan_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) {
printf("Unable to start read and lease tasks\n");
z_close(z_session_move(&s));
z_close(z_move(s));
return -1;
}
// Wait for joins in peer mode
if (strcmp(mode, "peer") == 0) {
printf("Waiting for joins...\n");
sleep(3);
}
// Declare publisher
printf("Declaring publisher for '%s'...\n", keyexpr);
z_owned_publisher_t pub;
z_view_keyexpr_t ke;
Expand All @@ -102,27 +103,29 @@ int main(int argc, char **argv) {
printf("Unable to declare publisher for key expression!\n");
return -1;
}

// Publish data
printf("Press CTRL-C to quit...\n");
char buf[256];
for (int idx = 0; idx < n; ++idx) {
sleep(1);
z_sleep_s(1);
sprintf(buf, "[%4d] %s", idx, value);
printf("Putting Data ('%s': '%s')...\n", keyexpr, 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_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL);
options.encoding = z_move(encoding);

z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options);
}

// Clean up
z_undeclare_publisher(z_move(pub));

// Stop read and lease tasks for zenoh-pico
zp_stop_read_task(z_loan_mut(s));
zp_stop_lease_task(z_loan_mut(s));

z_close(z_move(s));

return 0;
}
#else
Expand Down
6 changes: 5 additions & 1 deletion examples/unix/c11/z_put.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,14 @@ int main(int argc, char **argv) {
return -1;
}

// 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(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL);
options.encoding = z_move(encoding);
#if Z_FEATURE_ATTACHMENT == 1
z_owned_bytes_map_t map = z_bytes_map_new();
z_bytes_map_insert_by_alias(&map, z_bytes_from_str("hi"), z_bytes_from_str("there"));
Expand Down
6 changes: 5 additions & 1 deletion examples/unix/c11/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
}
#endif

// 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(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL);
options.encoding = z_move(encoding);

#if Z_FEATURE_ATTACHMENT == 1
// Add attachment
Expand Down
8 changes: 6 additions & 2 deletions examples/unix/c99/z_pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,17 @@ int main(int argc, char **argv) {
printf("Press CTRL-C to quit...\n");
char *buf = (char *)malloc(256);
for (int idx = 0; idx < n; ++idx) {
sleep(1);
z_sleep_s(1);
snprintf(buf, 256, "[%4d] %s", idx, value);
printf("Putting Data ('%s': '%s')...\n", keyexpr, 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(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL);
options.encoding = z_encoding_move(&encoding);

z_publisher_put(z_publisher_loan(&pub), (const uint8_t *)buf, strlen(buf), &options);
}
// Clean up
Expand Down
6 changes: 5 additions & 1 deletion examples/unix/c99/z_put.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,14 @@ int main(int argc, char **argv) {
return -1;
}

// 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(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL);
options.encoding = z_encoding_move(&encoding);
if (z_put(z_session_loan(&s), z_keyexpr_loan(&ke), (const uint8_t *)value, strlen(value), &options) < 0) {
printf("Oh no! Put has failed...\n");
}
Expand Down
6 changes: 5 additions & 1 deletion examples/unix/c99/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
printf(" with value '%s'\n", z_string_data(z_string_loan(&payload_string)));
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(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL);
options.encoding = z_encoding_move(&encoding);
// Reply value encoding
z_view_string_t reply_str;
z_view_str_wrap(&reply_str, value);
Expand Down
15 changes: 8 additions & 7 deletions examples/windows/z_pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int main(int argc, char **argv) {
// Start read and lease tasks for zenoh-pico
if (zp_start_read_task(z_loan_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) {
printf("Unable to start read and lease tasks\n");
z_close(z_session_move(&s));
z_close(z_move(s));
return -1;
}

Expand All @@ -60,24 +60,25 @@ int main(int argc, char **argv) {
printf("Press CTRL-C to quit...\n");
char *buf = (char *)malloc(256);
for (int idx = 0; 1; ++idx) {
Sleep(1);
z_sleep_s(1);
snprintf(buf, 256, "[%4d] %s", idx, value);
printf("Putting Data ('%s': '%s')...\n", keyexpr, 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(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL);
options.encoding = z_move(encoding);

z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options);
}

// Clean-up
z_undeclare_publisher(z_move(pub));

// Stop read and lease tasks for zenoh-pico
zp_stop_read_task(z_loan_mut(s));
zp_stop_lease_task(z_loan_mut(s));

z_close(z_move(s));

free(buf);
return 0;
}
Expand Down
5 changes: 4 additions & 1 deletion examples/windows/z_put.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ int main(int argc, char **argv) {
z_close(z_move(s));
return -1;
}
// 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(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL);
options.encoding = z_move(encoding);
if (z_put(z_loan(s), z_loan(ke), (const uint8_t *)value, strlen(value), &options) < 0) {
printf("Oh no! Put has failed...\n");
}
Expand Down
5 changes: 5 additions & 0 deletions examples/windows/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
printf(" with value '%s'\n", z_string_data(z_loan(payload_string)));
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_view_string_t reply_str;
z_view_str_wrap(&reply_str, value);
Expand Down
Loading

0 comments on commit 506a668

Please sign in to comment.