Skip to content

Commit

Permalink
Owned sample draft
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Mar 27, 2024
1 parent 84f22e6 commit bca6208
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 39 deletions.
6 changes: 3 additions & 3 deletions examples/unix/c11/z_pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ int main(int argc, char **argv) {
z_owned_sample_t sample = z_sample_null();
while (true) {
for (z_call(channel.recv, &sample); z_check(sample); z_call(channel.recv, &sample)) {
z_owned_str_t keystr = z_keyexpr_to_string(z_loan(sample.keyexpr));
printf(">> [Subscriber] Pulled ('%s': '%.*s')\n", z_loan(keystr), (int)sample.payload.len,
sample.payload.start);
z_owned_str_t keystr = z_keyexpr_to_string(z_loan(sample).keyexpr);
printf(">> [Subscriber] Pulled ('%s': '%.*s')\n", z_loan(keystr), (int)z_loan(sample).payload.len,
z_loan(sample).payload.start);
z_drop(z_move(keystr));
z_drop(z_move(sample));
}
Expand Down
6 changes: 3 additions & 3 deletions examples/unix/c11/z_sub_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ int main(int argc, char **argv) {

z_owned_sample_t sample = z_sample_null();
for (z_call(channel.recv, &sample); z_check(sample); z_call(channel.recv, &sample)) {
z_owned_str_t keystr = z_keyexpr_to_string(z_loan(sample.keyexpr));
printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_loan(keystr), (int)sample.payload.len,
sample.payload.start);
z_owned_str_t keystr = z_keyexpr_to_string(z_loan(sample).keyexpr);
printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_loan(keystr), (int)z_loan(sample).payload.len,
z_loan(sample).payload.start);
z_drop(z_move(keystr));
z_drop(z_move(sample));
sample = z_sample_null();
Expand Down
36 changes: 4 additions & 32 deletions include/zenoh-pico/api/handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,7 @@
#include "zenoh-pico/collections/ring.h"
#include "zenoh-pico/system/platform.h"

// -- Owned sample
// @TODO: define it via dedicated macros and move it to the appropriate place in types.
typedef struct {
z_owned_keyexpr_t keyexpr;
z_bytes_t payload;
// @TODO: implement the rest of the fields
} z_owned_sample_t;

static inline z_owned_sample_t z_sample_null(void) {
z_owned_sample_t sample;
sample.keyexpr = z_keyexpr_null();
sample.payload = z_bytes_null();
return sample;
}

static inline z_owned_sample_t *z_sample_move(z_owned_sample_t *sample) { return sample; }
static inline z_owned_sample_t *z_sample_loan(z_owned_sample_t *sample) { return sample; }

static inline bool z_sample_check(const z_owned_sample_t *sample) {
return z_keyexpr_check(&sample->keyexpr) && z_bytes_check(&sample->payload);
}

// TODO(sashacmc): move/rename?
static inline z_owned_sample_t z_sample_to_owned(const z_sample_t *src) {
z_owned_sample_t dst = z_sample_null();

Expand All @@ -57,18 +36,11 @@ static inline z_owned_sample_t z_sample_to_owned(const z_sample_t *src) {
if (ke == NULL) {
return dst;
}
*ke = _z_keyexpr_duplicate(src->keyexpr);
dst.keyexpr._value = ke;
dst.payload = _z_bytes_duplicate(&src->payload);

return dst;
}
dst._value->keyexpr = _z_keyexpr_duplicate(src->keyexpr);
dst._value->payload = _z_bytes_duplicate(&src->payload);

static inline void z_sample_drop(z_owned_sample_t *s) {
if (s != NULL) {
z_keyexpr_drop(&s->keyexpr);
_z_bytes_clear(&s->payload);
}
return dst;
}

_Z_ELEM_DEFINE(_z_owned_sample, z_owned_sample_t, _z_noop_size, z_sample_drop, _z_noop_copy)
Expand Down
4 changes: 4 additions & 0 deletions include/zenoh-pico/api/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ template<> struct zenoh_drop_type<z_owned_closure_query_t> { typedef void type;
template<> struct zenoh_drop_type<z_owned_closure_reply_t> { typedef void type; };
template<> struct zenoh_drop_type<z_owned_closure_hello_t> { typedef void type; };
template<> struct zenoh_drop_type<z_owned_closure_zid_t> { typedef void type; };
template<> struct zenoh_drop_type<z_owned_sample_t> { typedef void type; };

template<> inline int8_t z_drop(z_owned_session_t* v) { return z_close(v); }
template<> inline int8_t z_drop(z_owned_publisher_t* v) { return z_undeclare_publisher(v); }
Expand All @@ -262,6 +263,7 @@ template<> inline void z_drop(z_owned_closure_query_t* v) { z_closure_query_drop
template<> inline void z_drop(z_owned_closure_reply_t* v) { z_closure_reply_drop(v); }
template<> inline void z_drop(z_owned_closure_hello_t* v) { z_closure_hello_drop(v); }
template<> inline void z_drop(z_owned_closure_zid_t* v) { z_closure_zid_drop(v); }
template<> inline void z_drop(z_owned_sample_t* v) { z_closure_sample_drop(v); }

inline void z_null(z_owned_session_t& v) { v = z_session_null(); }
inline void z_null(z_owned_publisher_t& v) { v = z_publisher_null(); }
Expand All @@ -278,6 +280,7 @@ inline void z_null(z_owned_closure_query_t& v) { v = z_closure_query_null(); }
inline void z_null(z_owned_closure_reply_t& v) { v = z_closure_reply_null(); }
inline void z_null(z_owned_closure_hello_t& v) { v = z_closure_hello_null(); }
inline void z_null(z_owned_closure_zid_t& v) { v = z_closure_zid_null(); }
inline void z_null(z_owned_sample_t& v) { v = z_closure_sample_null(); }

inline bool z_check(const z_owned_session_t& v) { return z_session_check(&v); }
inline bool z_check(const z_owned_publisher_t& v) { return z_publisher_check(&v); }
Expand All @@ -291,6 +294,7 @@ inline bool z_check(const z_owned_queryable_t& v) { return z_queryable_check(&v)
inline bool z_check(const z_owned_reply_t& v) { return z_reply_check(&v); }
inline bool z_check(const z_owned_hello_t& v) { return z_hello_check(&v); }
inline bool z_check(const z_owned_str_t& v) { return z_str_check(&v); }
inline bool z_check(const z_owned_str_t& v) { return z_sample_check(&v); }

inline void z_call(const z_owned_closure_sample_t &closure, const z_sample_t *sample)
{ z_closure_sample_call(&closure, sample); }
Expand Down
1 change: 1 addition & 0 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@ _OWNED_FUNCTIONS(z_queryable_t, z_owned_queryable_t, queryable)
_OWNED_FUNCTIONS(z_hello_t, z_owned_hello_t, hello)
_OWNED_FUNCTIONS(z_reply_t, z_owned_reply_t, reply)
_OWNED_FUNCTIONS(z_str_array_t, z_owned_str_array_t, str_array)
_OWNED_FUNCTIONS(z_sample_t, z_owned_sample_t, sample)

#define _OWNED_FUNCTIONS_CLOSURE(ownedtype, name) \
_Bool z_##name##_check(const ownedtype *val); \
Expand Down
1 change: 1 addition & 0 deletions include/zenoh-pico/api/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ typedef struct {
* z_timestamp_t timestamp: The timestamp of this data sample.
*/
typedef _z_sample_t z_sample_t;
_OWNED_TYPE_PTR(z_sample_t, sample)

/**
* Represents the content of a `hello` message returned by a zenoh entity as a reply to a `scout` message.
Expand Down
3 changes: 3 additions & 0 deletions include/zenoh-pico/protocol/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ typedef struct {
z_attachment_t attachment;
#endif
} _z_sample_t;
static inline bool _z_sample_check(const _z_sample_t *sample) {
return _z_keyexpr_check(sample->keyexpr) && _z_bytes_check(sample->payload);
}

/**
* Represents a Zenoh value.
Expand Down
3 changes: 2 additions & 1 deletion src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ OWNED_FUNCTIONS_PTR_DROP(z_scouting_config_t, z_owned_scouting_config_t, scoutin
OWNED_FUNCTIONS_PTR_INTERNAL(z_keyexpr_t, z_owned_keyexpr_t, keyexpr, _z_keyexpr_free, _z_keyexpr_copy)
OWNED_FUNCTIONS_PTR_INTERNAL(z_hello_t, z_owned_hello_t, hello, _z_hello_free, _z_owner_noop_copy)
OWNED_FUNCTIONS_PTR_INTERNAL(z_str_array_t, z_owned_str_array_t, str_array, _z_str_array_free, _z_owner_noop_copy)
OWNED_FUNCTIONS_PTR_INTERNAL(z_sample_t, z_owned_sample_t, sample, _z_sample_free, _z_owner_noop_copy)

_Bool z_session_check(const z_owned_session_t *val) { return val->_value.in != NULL; }
z_session_t z_session_loan(const z_owned_session_t *val) { return (z_session_t){._val = val->_value}; }
Expand Down Expand Up @@ -1258,4 +1259,4 @@ z_owned_bytes_map_t z_bytes_map_new(void) { return (z_owned_bytes_map_t){._inner
z_owned_bytes_map_t z_bytes_map_null(void) { return (z_owned_bytes_map_t){._inner = NULL}; }
z_bytes_t z_bytes_from_str(const char *str) { return z_bytes_wrap((const uint8_t *)str, strlen(str)); }
z_bytes_t z_bytes_null(void) { return (z_bytes_t){.len = 0, ._is_alloc = false, .start = NULL}; }
#endif
#endif

0 comments on commit bca6208

Please sign in to comment.