Skip to content

Commit

Permalink
feat: create reply sample union type
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Jun 25, 2024
1 parent d67ea84 commit 23ad207
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 23 deletions.
94 changes: 92 additions & 2 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,7 @@ z_sample_kind_t z_sample_kind(const z_loaned_sample_t *sample);
z_qos_t z_sample_qos(const z_loaned_sample_t *sample);

/**
* Gets the attachment of a value by aliasing it.
* Gets the attachment of a sample by aliasing it.
*
* Parameters:
* sample: Pointer to a :c:type:`z_loaned_sample_t` to get the attachment from.
Expand All @@ -1283,6 +1283,83 @@ z_qos_t z_sample_qos(const z_loaned_sample_t *sample);
*/
const z_loaned_bytes_t *z_sample_attachment(const z_loaned_sample_t *sample);

/**
* Gets the keyexpr from a reply sample by aliasing it.
*
* Parameters:
* sample: Pointer to a :c:type:`zp_loaned_reply_sample_t` to get the keyexpr from.
*
* Return:
* The keyexpr wrapped as a :c:type:`z_loaned_keyexpr_t`.
*/
const z_loaned_keyexpr_t *zp_reply_sample_keyexpr(const zp_loaned_reply_sample_t *sample);

/**
* Gets the payload of a reply sample by aliasing it.
*
* Parameters:
* sample: Pointer to a :c:type:`zp_loaned_reply_sample_t` to get the payload from.
*
* Return:
* The payload wrapped as a :c:type:`z_loaned_bytes_t`.
*/
const z_loaned_bytes_t *zp_reply_sample_payload(const zp_loaned_reply_sample_t *sample);

/**
* Gets the timestamp of a reply sample by aliasing it.
*
* Parameters:
* sample: Pointer to a :c:type:`zp_loaned_reply_sample_t` to get the timestamp from.
*
* Return:
* The timestamp wrapped as a :c:type:`z_timestamp_t`.
*/
z_timestamp_t zp_reply_sample_timestamp(const zp_loaned_reply_sample_t *sample);

/**
* Gets the encoding of a reply sample by aliasing it.
*
* Parameters:
* sample: Pointer to a :c:type:`zp_loaned_reply_sample_t` to get the encoding from.
*
* Return:
* The encoding wrapped as a :c:type:`z_loaned_encoding_t*`.
*/
const z_loaned_encoding_t *zp_reply_sample_encoding(const zp_loaned_reply_sample_t *sample);

/**
* Gets the kind of a reply sample by aliasing it.
*
* Parameters:
* sample: Pointer to a :c:type:`zp_loaned_reply_sample_t` to get the kind from.
*
* Return:
* The sample kind wrapped as a :c:type:`z_sample_kind_t`.
*/
z_sample_kind_t zp_reply_sample_kind(const zp_loaned_reply_sample_t *sample);

/**
* Gets the qos value of a reply sample by aliasing it.
*
* Parameters:
* sample: Pointer to a :c:type:`zp_loaned_reply_sample_t` to get the qos from.
*
* Return:
* The qos wrapped as a :c:type:`z_qos_t`.
*/
z_qos_t zp_reply_sample_qos(const zp_loaned_reply_sample_t *sample);

/**
* Gets the attachment of a reply sample by aliasing it.
*
* Parameters:
* sample: Pointer to a :c:type:`zp_loaned_reply_sample_t` to get the attachment from.
*
* Return:
* Pointer to the attachment as a :c:type:`z_loaned_bytes_t`.
*/
const z_loaned_bytes_t *zp_reply_sample_attachment(const zp_loaned_reply_sample_t *sample);

#if Z_FEATURE_PUBLICATION == 1
/**
* Builds a :c:type:`z_put_options_t` with default values.
Expand Down Expand Up @@ -1446,7 +1523,7 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co
_Bool z_reply_is_ok(const z_loaned_reply_t *reply);

/**
* Gets the content of an OK reply.
* Gets the content of an OK reply refcounted.
*
* You should always make sure that :c:func:`z_reply_is_ok` returns ``true`` before calling this function.
*
Expand All @@ -1458,6 +1535,19 @@ _Bool z_reply_is_ok(const z_loaned_reply_t *reply);
*/
const z_loaned_sample_t *z_reply_ok(const z_loaned_reply_t *reply);

/**
* Gets the content of an OK reply.
*
* You should always make sure that :c:func:`z_reply_is_ok` returns ``true`` before calling this function.
*
* Parameters:
* reply: Pointer to a :c:type:`z_loaned_reply_t` to get content from.
*
* Return:
* The OK reply content wrapped as a :c:type:`zp_loaned_reply_sample_t`.
*/
const zp_loaned_reply_sample_t *zp_reply_ok(const z_loaned_reply_t *reply);

/**
* Gets the contents of an error reply.
*
Expand Down
6 changes: 5 additions & 1 deletion include/zenoh-pico/net/reply.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@
*
*/
typedef struct _z_reply_data_t {
_z_sample_rc_t sample;
union {
_z_sample_t base;
_z_sample_rc_t rc;
} sample;
_z_id_t replier_id;
_Bool has_sample_as_rc;
} _z_reply_data_t;

void _z_reply_data_clear(_z_reply_data_t *rd);
Expand Down
1 change: 1 addition & 0 deletions include/zenoh-pico/session/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ _Z_LIST_DEFINE(_z_resource, _z_resource_t)

// Forward declaration to avoid cyclical include
typedef struct _z_sample_rc_t z_loaned_sample_t;
typedef struct _z_sample_t zp_loaned_reply_sample_t;

/**
* The callback signature of the functions handling data messages.
Expand Down
30 changes: 28 additions & 2 deletions src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,18 @@ const z_loaned_bytes_t *z_sample_attachment(const z_loaned_sample_t *sample) {
return &_Z_RC_IN_VAL(sample).attachment;
}

const z_loaned_keyexpr_t *zp_reply_sample_keyexpr(const zp_loaned_reply_sample_t *sample) { return &sample->keyexpr; }
z_sample_kind_t zp_reply_sample_kind(const zp_loaned_reply_sample_t *sample) { return sample->kind; }
const z_loaned_bytes_t *zp_reply_sample_payload(const zp_loaned_reply_sample_t *sample) { return &sample->payload; }
z_timestamp_t zp_reply_sample_timestamp(const zp_loaned_reply_sample_t *sample) { return sample->timestamp; }
const z_loaned_encoding_t *zp_reply_sample_encoding(const zp_loaned_reply_sample_t *sample) {
return &sample->encoding;
}
z_qos_t zp_reply_sample_qos(const zp_loaned_reply_sample_t *sample) { return sample->qos; }
const z_loaned_bytes_t *zp_reply_sample_attachment(const zp_loaned_reply_sample_t *sample) {
return &sample->attachment;
}

const z_loaned_bytes_t *z_reply_err_payload(const z_loaned_reply_err_t *reply_err) { return &reply_err->payload; }
const z_loaned_encoding_t *z_reply_err_encoding(const z_loaned_reply_err_t *reply_err) { return &reply_err->encoding; }

Expand Down Expand Up @@ -1089,11 +1101,25 @@ int8_t z_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr, co
_Bool z_reply_is_ok(const z_loaned_reply_t *reply) {
_ZP_UNUSED(reply);
// For the moment always return TRUE.
// The support for reply errors will come in the next release.
// FIXME: The support for reply errors will come in the next release.
return true;
}

const z_loaned_sample_t *z_reply_ok(const z_loaned_reply_t *reply) { return &reply->in->val.data.sample; }
const z_loaned_sample_t *z_reply_ok(const z_loaned_reply_t *reply) {
// Convert sample to sample_rc if needed
if (!reply->in->val.data.has_sample_as_rc) {
reply->in->val.data.sample.rc = _z_sample_rc_new_from_val(reply->in->val.data.sample.base);
reply->in->val.data.has_sample_as_rc = true;
}
return &reply->in->val.data.sample.rc;
}

const zp_loaned_reply_sample_t *zp_reply_ok(const z_loaned_reply_t *reply) {
if (reply->in->val.data.has_sample_as_rc) {
return NULL;
}
return &reply->in->val.data.sample.base;
}

const z_loaned_reply_err_t *z_reply_err(const z_loaned_reply_t *reply) {
_ZP_UNUSED(reply);
Expand Down
36 changes: 21 additions & 15 deletions src/net/reply.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ _z_reply_t _z_reply_null(void) {
_z_reply_t r = {._tag = Z_REPLY_TAG_DATA,
.data = {
.replier_id = {.id = {0}},
.sample = {.in = NULL},
.has_sample_as_rc = false,
.sample.base = _z_sample_null(),
}};
return r;
}

#if Z_FEATURE_QUERY == 1
void _z_reply_data_clear(_z_reply_data_t *reply_data) {
_z_sample_rc_drop(&reply_data->sample);
if (reply_data->has_sample_as_rc) {
_z_sample_rc_drop(&reply_data->sample.rc);
} else {
_z_sample_clear(&reply_data->sample.base);
}
reply_data->replier_id = _z_id_empty();
}

Expand All @@ -37,15 +42,20 @@ void _z_reply_data_free(_z_reply_data_t **reply_data) {

if (ptr != NULL) {
_z_reply_data_clear(ptr);

z_free(ptr);
*reply_data = NULL;
}
}

void _z_reply_data_copy(_z_reply_data_t *dst, _z_reply_data_t *src) {
_z_sample_rc_copy(&dst->sample, &src->sample);
dst->replier_id = src->replier_id;
dst->has_sample_as_rc = src->has_sample_as_rc;

if (src->has_sample_as_rc) {
_z_sample_rc_copy(&dst->sample.rc, &src->sample.rc);
} else {
_z_sample_copy(&dst->sample.base, &src->sample.base);
}
}

_z_reply_t _z_reply_move(_z_reply_t *src_reply) {
Expand Down Expand Up @@ -91,17 +101,13 @@ _z_reply_t _z_reply_create(_z_keyexpr_t keyexpr, z_reply_tag_t tag, _z_id_t id,
reply._tag = tag;
if (tag == Z_REPLY_TAG_DATA) {
reply.data.replier_id = id;
// Create sample
_z_sample_t sample = _z_sample_null();
sample.keyexpr = keyexpr; // FIXME: call z_keyexpr_move or copy
sample.encoding = encoding; // FIXME: call z_encoding_move or copy
_z_slice_copy(&sample.payload._slice, payload);
sample.kind = kind;
sample.timestamp = _z_timestamp_duplicate(timestamp);
sample.attachment._slice = _z_slice_steal((_z_slice_t *)&att._slice);

// Create sample rc from value
reply.data.sample = _z_sample_rc_new_from_val(sample);
// Create reply sample
reply.data.sample.base.keyexpr = keyexpr; // FIXME: call z_keyexpr_move or copy
reply.data.sample.base.encoding = encoding; // FIXME: call z_encoding_move or copy
_z_slice_copy(&reply.data.sample.base.payload._slice, payload);
reply.data.sample.base.kind = kind;
reply.data.sample.base.timestamp = _z_timestamp_duplicate(timestamp);
reply.data.sample.base.attachment._slice = _z_slice_steal((_z_slice_t *)&att._slice);
}
return reply;
}
Expand Down
6 changes: 3 additions & 3 deletions src/session/query.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_zint_t id, cons
pen_rep = _z_pending_reply_list_head(pen_rps);

// Check if this is the same resource key
if (_z_str_eq(pen_rep->_reply.data.sample.in->val.keyexpr._suffix,
reply.data.sample.in->val.keyexpr._suffix) == true) {
if (_z_str_eq(pen_rep->_reply.data.sample.base.keyexpr._suffix, reply.data.sample.base.keyexpr._suffix) ==
true) {
if (msg->_commons._timestamp.time <= pen_rep->_tstamp.time) {
drop = true;
} else {
Expand All @@ -149,7 +149,7 @@ int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_zint_t id, cons
_z_reply_t partial_reply;
(void)memset(&partial_reply, 0,
sizeof(_z_reply_t)); // Avoid warnings on uninitialized values on the reply
partial_reply.data.sample.in->val.keyexpr = _z_keyexpr_duplicate(reply.data.sample.in->val.keyexpr);
partial_reply.data.sample.base.keyexpr = _z_keyexpr_duplicate(reply.data.sample.base.keyexpr);
pen_rep->_reply = partial_reply;
} else {
pen_rep->_reply = reply; // Store the whole reply in the latest mode
Expand Down

0 comments on commit 23ad207

Please sign in to comment.