Skip to content

Commit

Permalink
Merge pull request #4 from jean-roland/ft_value_encoding
Browse files Browse the repository at this point in the history
Add value encoding function
  • Loading branch information
jean-roland authored May 27, 2024
2 parents fc93fe5 + 2858314 commit 39bf7de
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 10 deletions.
8 changes: 6 additions & 2 deletions examples/unix/c11/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,13 @@ int main(int argc, char **argv) {
printf("Sending Query '%s'...\n", keyexpr);
z_get_options_t opts;
z_get_options_default(&opts);

if (value != NULL) {
// TODO(sashacmc): encoding
// opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value));
z_view_string_t value_str;
z_view_str_wrap(&value_str, value);
z_owned_bytes_t payload;
z_bytes_encode_from_string(&payload, z_loan(value_str));
opts.payload = &payload;
}
#if Z_FEATURE_ATTACHMENT == 1
z_owned_bytes_map_t map = z_bytes_map_new();
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 @@ -62,8 +62,12 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
options.attachment = z_bytes_map_as_attachment(&map);
#endif

// Reply value encoding
z_view_string_t reply_str;
z_view_str_wrap(&reply_str, value);
z_owned_bytes_t reply_payload;
// TODO(sashacmc): value encoding
z_bytes_encode_from_string(&reply_payload, z_loan(reply_str));

z_query_reply(query, z_query_keyexpr(query), z_move(reply_payload), &options);
z_drop(z_move(keystr));
msg_nb++;
Expand Down
9 changes: 7 additions & 2 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,11 @@ z_encoding_t z_encoding(z_encoding_prefix_t prefix, const char *suffix);
*/
z_encoding_t z_encoding_default(void);

/**
* Encodes string by aliasing.
*/
int8_t z_bytes_encode_from_string(z_owned_bytes_t *buffer, const z_loaned_string_t *s);

/**
*
* Checks validity of the timestamp
Expand Down Expand Up @@ -820,10 +825,10 @@ _OWNED_FUNCTIONS_CLOSURE(z_owned_closure_zid_t, closure_zid)
_VIEW_FUNCTIONS(z_loaned_keyexpr_t, z_view_keyexpr_t, keyexpr)
_VIEW_FUNCTIONS(z_loaned_string_t, z_view_string_t, string)

// Gets internal value from refcountered type (e.g. z_loaned_session_t, z_query_t)
// Gets internal value from refcounted type (e.g. z_loaned_session_t, z_query_t)
#define _Z_RC_IN_VAL(arg) ((arg)->in->val)

// Gets internal value from refcountered owned type (e.g. z_owned_session_t, z_owned_query_t)
// Gets internal value from refcounted owned type (e.g. z_owned_session_t, z_owned_query_t)
#define _Z_OWNED_RC_IN_VAL(arg) ((arg)->_rc.in->val)

// TODO(sashacmc): comments, docs, etc.
Expand Down
1 change: 1 addition & 0 deletions include/zenoh-pico/collections/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ typedef struct {
} _z_string_t;

_z_string_t _z_string_make(const char *value);
_z_string_t _z_string_wrap(char *value);
_z_string_t *_z_string_make_as_ptr(const char *value);

size_t _z_string_size(const _z_string_t *s);
Expand Down
14 changes: 13 additions & 1 deletion src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
/********* Data Types Handlers *********/

int8_t z_view_str_wrap(z_view_string_t *str, const char *value) {
str->_val = _z_string_make(value);
str->_val = _z_string_wrap((char *)value);
return _Z_RES_OK;
}

Expand Down Expand Up @@ -266,6 +266,18 @@ z_encoding_t z_encoding(z_encoding_prefix_t prefix, const char *suffix) {

z_encoding_t z_encoding_default(void) { return z_encoding(Z_ENCODING_PREFIX_DEFAULT, NULL); }

int8_t z_bytes_encode_from_string(z_owned_bytes_t *buffer, const z_loaned_string_t *s) {
// Init owned bytes
z_bytes_null(buffer);
buffer->_val = (_z_bytes_t *)z_malloc(sizeof(_z_bytes_t));
if (buffer->_val == NULL) {
return _Z_ERR_SYSTEM_OUT_OF_MEMORY;
}
// Encode data
*buffer->_val = _z_bytes_wrap((uint8_t *)s->val, s->len);
return _Z_RES_OK;
}

_Bool z_timestamp_check(z_timestamp_t ts) { return _z_timestamp_check(&ts); }

z_query_target_t z_query_target_default(void) { return Z_QUERY_TARGET_DEFAULT; }
Expand Down
7 changes: 7 additions & 0 deletions src/collections/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ _z_string_t _z_string_make(const char *value) {
return s;
}

_z_string_t _z_string_wrap(char *value) {
_z_string_t s;
s.val = value;
s.len = strlen(value);
return s;
}

_z_string_t *_z_string_make_as_ptr(const char *value) {
_z_string_t *s = (_z_string_t *)z_malloc(sizeof(_z_string_t));
s->val = _z_str_clone(value);
Expand Down
8 changes: 4 additions & 4 deletions tests/modularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ def query_and_queryable(args):
if args.attachment == 1:
z_query_expected_output = """Opening session...
Sending Query 'demo/example/**'...
>> Received ('demo/example/zenoh-pico-queryable': 'Queryable from Pico!')
>> Received ('demo/example/**': 'Queryable from Pico!')
Attachement found
>>> hello: world
>> Received query final notification"""
else:
z_query_expected_output = """Opening session...
Sending Query 'demo/example/**'...
>> Received ('demo/example/zenoh-pico-queryable': 'Queryable from Pico!')
>> Received ('demo/example/**': 'Queryable from Pico!')
>> Received query final notification"""
else:
z_query_expected_output = """Opening session...
Expand All @@ -189,15 +189,15 @@ def query_and_queryable(args):
z_queryable_expected_output = """Opening session...
Creating Queryable on 'demo/example/zenoh-pico-queryable'...
Press CTRL-C to quit...
>> [Queryable handler] Received Query 'demo/example/**?'
>> [Queryable handler] Received Query 'demo/example/**'
Attachement found
>>> hi: there
"""
else:
z_queryable_expected_output = """Opening session...
Creating Queryable on 'demo/example/zenoh-pico-queryable'...
Press CTRL-C to quit...
>> [Queryable handler] Received Query 'demo/example/**?'
>> [Queryable handler] Received Query 'demo/example/**'
"""
else:
z_queryable_expected_output = """Opening session...
Expand Down

0 comments on commit 39bf7de

Please sign in to comment.