Skip to content

Commit

Permalink
privatize sample
Browse files Browse the repository at this point in the history
  • Loading branch information
p-avital committed Feb 26, 2024
1 parent 501d129 commit db31f0d
Show file tree
Hide file tree
Showing 25 changed files with 311 additions and 275 deletions.
5 changes: 3 additions & 2 deletions examples/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ int main(int argc, char **argv) {
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(sample.keyexpr);
printf(">> Received ('%s': '%.*s')\n", z_loan(keystr), (int)sample.payload.len, sample.payload.start);
z_owned_str_t keystr = z_keyexpr_to_string(z_sample_keyexpr(&sample));
z_bytes_t payload = z_sample_payload(&sample);
printf(">> Received ('%s': '%.*s')\n", z_loan(keystr), (int)payload.len, payload.start);
z_drop(z_move(keystr));
} else {
printf("Received an error\n");
Expand Down
2 changes: 1 addition & 1 deletion examples/z_get_liveliness.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int main(int argc, char **argv) {
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(sample.keyexpr);
z_owned_str_t keystr = z_keyexpr_to_string(z_sample_keyexpr(&sample));
printf(">> Alive token ('%s')\n", z_loan(keystr));
z_drop(z_move(keystr));
} else {
Expand Down
5 changes: 3 additions & 2 deletions examples/z_non_blocking_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,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(sample.keyexpr);
printf(">> Received ('%s': '%.*s')\n", z_loan(keystr), (int)sample.payload.len, sample.payload.start);
z_owned_str_t keystr = z_keyexpr_to_string(z_sample_keyexpr(&sample));
z_bytes_t payload = z_sample_payload(&sample);
printf(">> Received ('%s': '%.*s')\n", z_loan(keystr), (int)payload.len, payload.start);
z_drop(z_move(keystr));
} else {
printf("Received an error\n");
Expand Down
10 changes: 6 additions & 4 deletions examples/z_pong.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
void callback(const z_sample_t* sample, void* context) {
z_publisher_t pub = z_loan(*(z_owned_publisher_t*)context);
#ifdef ZENOH_C // The zc_owned_payload_t API is exclusive to zenoh-c, but allows avoiding some copies.
zc_owned_payload_t payload = zc_sample_payload_rcinc(sample);
z_owned_buffer_t payload = z_sample_owned_payload(sample);
zc_publisher_put_owned(pub, z_move(payload), NULL);
#else
z_publisher_put(pub, sample->payload.start, sample->payload.len, NULL);
Expand All @@ -21,15 +21,17 @@ void drop(void* context) {
// valid.
}
struct args_t {
char* config_path; // -c
uint8_t help_requested; // -h
char* config_path; // -c
uint8_t help_requested; // -h
};
struct args_t parse_args(int argc, char** argv);

int main(int argc, char** argv) {
struct args_t args = parse_args(argc, argv);
if (args.help_requested) {
printf("-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");
printf(
"-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");
return 1;
}
z_owned_config_t config = args.config_path ? zc_config_from_file(args.config_path) : z_config_default();
Expand Down
7 changes: 4 additions & 3 deletions examples/z_pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
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(sample->keyexpr);
printf(">> [Subscriber] Received %s ('%s': '%.*s')\n", kind_to_str(sample->kind), z_loan(keystr),
(int)sample->payload.len, sample->payload.start);
z_owned_str_t keystr = z_keyexpr_to_string(z_sample_keyexpr(sample));
z_bytes_t payload = z_sample_payload(sample);
printf(">> [Subscriber] Received %s ('%s': '%.*s')\n", kind_to_str(z_sample_kind(sample)), z_loan(keystr),
(int)payload.len, payload.start);
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 @@ -23,9 +23,10 @@
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(sample->keyexpr);
printf(">> [Subscriber] Received %s ('%s': '%.*s')\n", kind_to_str(sample->kind), z_loan(keystr),
(int)sample->payload.len, sample->payload.start);
z_owned_str_t keystr = z_keyexpr_to_string(z_sample_keyexpr(sample));
z_bytes_t payload = z_sample_payload(sample);
printf(">> [Subscriber] Received %s ('%s': '%.*s')\n", kind_to_str(z_sample_kind(sample)), z_loan(keystr),
(int)payload.len, payload.start);
z_drop(z_move(keystr));
}

Expand Down
7 changes: 4 additions & 3 deletions examples/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
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(sample->keyexpr);
printf(">> [Subscriber] Received %s ('%s': '%.*s')\n", kind_to_str(sample->kind), z_loan(keystr),
(int)sample->payload.len, sample->payload.start);
z_owned_str_t keystr = z_keyexpr_to_string(z_sample_keyexpr(sample));
z_bytes_t payload = z_sample_payload(sample);
printf(">> [Subscriber] Received %s ('%s': '%.*s')\n", kind_to_str(z_sample_kind(sample)), z_loan(keystr),
(int)payload.len, payload.start);
z_drop(z_move(keystr));
}

Expand Down
14 changes: 8 additions & 6 deletions examples/z_sub_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ 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(sample->keyexpr);
printf(">> [Subscriber] Received %s ('%s': '%.*s')\n", kind_to_str(sample->kind), z_loan(keystr),
(int)sample->payload.len, sample->payload.start);
z_owned_str_t keystr = z_keyexpr_to_string(z_sample_keyexpr(sample));
z_bytes_t payload = z_sample_payload(sample);
printf(">> [Subscriber] Received %s ('%s': '%.*s')\n", kind_to_str(z_sample_kind(sample)), z_loan(keystr),
(int)payload.len, payload.start);

z_attachment_t attachment = z_sample_attachment(sample);
// checks if attachment exists
if (z_check(sample->attachment)) {
if (z_check(attachment)) {
// reads full attachment
z_attachment_iterate(sample->attachment, attachment_reader, NULL);
z_attachment_iterate(attachment, attachment_reader, NULL);

// reads particular attachment item
z_bytes_t index = z_attachment_get(sample->attachment, z_bytes_from_str("index"));
z_bytes_t index = z_attachment_get(attachment, z_bytes_from_str("index"));
if (z_check(index)) {
printf(" message number: %.*s\n", (int)index.len, index.start);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/z_sub_liveliness.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#include "zenoh.h"

void data_handler(const z_sample_t *sample, void *arg) {
z_owned_str_t keystr = z_keyexpr_to_string(sample->keyexpr);
switch (sample->kind) {
z_owned_str_t keystr = z_keyexpr_to_string(z_sample_keyexpr(sample));
switch (z_sample_kind(sample)) {
case Z_SAMPLE_KIND_PUT:
printf(">> [LivelinessSubscriber] New alive token ('%s')\n", z_loan(keystr));
break;
Expand Down
Loading

0 comments on commit db31f0d

Please sign in to comment.