Skip to content

Commit

Permalink
reply with owned payload
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiryukov91 committed Apr 3, 2024
1 parent 206983e commit ed3a164
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 36 deletions.
3 changes: 2 additions & 1 deletion examples/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ void query_handler(const z_query_t *query, void *context) {
}
z_query_reply_options_t options = z_query_reply_options_default();
options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL);
z_query_reply(query, z_keyexpr((const char *)context), (const unsigned char *)value, strlen(value), &options);
zc_owned_payload_t reply_payload = zc_payload_encode_from_string(value);
z_query_reply(query, z_keyexpr((const char *)context), z_move(reply_payload), &options);
z_drop(z_move(keystr));
}

Expand Down
3 changes: 2 additions & 1 deletion examples/z_queryable_with_channels.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ int main(int argc, char **argv) {
}
z_query_reply_options_t options = z_query_reply_options_default();
options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL);
z_query_reply(&query, keyexpr, (const unsigned char *)value, strlen(value), &options);
zc_owned_payload_t reply_payload = zc_payload_encode_from_string(value);
z_query_reply(&query, keyexpr, z_move(reply_payload), &options);
z_drop(z_move(keystr));
z_drop(z_move(oquery));
}
Expand Down
4 changes: 1 addition & 3 deletions include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -1994,14 +1994,12 @@ struct z_bytes_t z_query_parameters(const struct z_query_t *query);
* query: The query to reply to.
* key: The key of this reply.
* payload: The value of this reply.
* len: The length of the value of this reply.
* options: The options of this reply.
*/
ZENOHC_API
int8_t z_query_reply(const struct z_query_t *query,
struct z_keyexpr_t key,
const uint8_t *payload,
size_t len,
zc_owned_payload_t *payload,
const struct z_query_reply_options_t *options);
/**
* Constructs the default value for :c:type:`z_query_reply_options_t`.
Expand Down
48 changes: 22 additions & 26 deletions src/queryable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::attachment::{
};
use crate::{
impl_guarded_transmute, z_buffer_t, z_bytes_t, z_closure_query_call, z_encoding_default,
z_encoding_t, z_keyexpr_t, z_owned_closure_query_t, z_session_t, z_value_t,
z_encoding_t, z_keyexpr_t, z_owned_closure_query_t, z_session_t, z_value_t, zc_owned_payload_t,
LOG_INVALID_SESSION,
};
use libc::c_void;
Expand Down Expand Up @@ -273,46 +273,42 @@ pub extern "C" fn z_queryable_check(qable: &z_owned_queryable_t) -> bool {
/// query: The query to reply to.
/// key: The key of this reply.
/// payload: The value of this reply.
/// len: The length of the value of this reply.
/// options: The options of this reply.
#[allow(clippy::missing_safety_doc)]
#[no_mangle]
pub unsafe extern "C" fn z_query_reply(
query: &z_query_t,
key: z_keyexpr_t,
payload: *const u8,
len: usize,
payload: Option<&mut zc_owned_payload_t>,
options: Option<&z_query_reply_options_t>,
) -> i8 {
let Some(query) = query.as_ref() else {
log::error!("Called `z_query_reply` with invalidated `query`");
return i8::MIN;
};
if let Some(key) = &*key {
let mut s = Sample::new(
key.clone().into_owned(),
std::slice::from_raw_parts(payload, len),
);
if let Some(o) = options {
s.encoding = o.encoding.into();
if z_attachment_check(&o.attachment) {
let mut attachment_builder = AttachmentBuilder::new();
z_attachment_iterate(
o.attachment,
insert_in_attachment_builder,
&mut attachment_builder as *mut AttachmentBuilder as *mut c_void,
);
s = s.with_attachment(attachment_builder.build());
};
if let Some(payload) = payload.and_then(|p| p.take()) {
let mut s = Sample::new(key.clone().into_owned(), payload);
if let Some(o) = options {
s.encoding = o.encoding.into();
if z_attachment_check(&o.attachment) {
let mut attachment_builder = AttachmentBuilder::new();
z_attachment_iterate(
o.attachment,
insert_in_attachment_builder,
&mut attachment_builder as *mut AttachmentBuilder as *mut c_void,
);
s = s.with_attachment(attachment_builder.build());
};
}
if let Err(e) = query.reply(Ok(s)).res_sync() {
log::error!("{}", e);
return e.errno().get();
}
return 0;
}
if let Err(e) = query.reply(Ok(s)).res_sync() {
log::error!("{}", e);
return e.errno().get();
}
0
} else {
i8::MIN
}
i8::MIN
}

/// Get a query's key by aliasing it.
Expand Down
3 changes: 2 additions & 1 deletion tests/z_api_alignment_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ void query_handler(const z_query_t *query, void *arg) {
z_value_t payload_value = z_query_value(query);
(void)(payload_value);
z_query_reply_options_t _ret_qreply_opt = z_query_reply_options_default();
z_query_reply(query, z_keyexpr(z_loan(k_str)), (const uint8_t *)value, strlen(value), &_ret_qreply_opt);
zc_owned_payload_t payload = zc_payload_encode_from_string(value);
z_query_reply(query, z_keyexpr(z_loan(k_str)), z_move(payload), &_ret_qreply_opt);

z_drop(z_move(k_str));
}
Expand Down
4 changes: 2 additions & 2 deletions tests/z_int_queryable_attachment_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ void query_handler(const z_query_t *query, void *context) {
z_query_reply_options_t options = z_query_reply_options_default();
options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL);
options.attachment = z_bytes_map_as_attachment(&map);
z_query_reply(query, z_keyexpr((const char *)context), (const uint8_t *)values[value_num],
strlen(values[value_num]), &options);
zc_owned_payload_t payload = zc_payload_encode_from_string(values[value_num]);
z_query_reply(query, z_keyexpr((const char *)context), z_move(payload), &options);
z_drop(z_move(keystr));
z_drop(z_move(map));

Expand Down
4 changes: 2 additions & 2 deletions tests/z_int_queryable_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ void query_handler(const z_query_t *query, void *context) {

z_query_reply_options_t options = z_query_reply_options_default();
options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL);
z_query_reply(query, z_keyexpr((const char *)context), (const uint8_t *)values[value_num],
strlen(values[value_num]), &options);
zc_owned_payload_t payload = zc_payload_encode_from_string(values[value_num]);
z_query_reply(query, z_keyexpr((const char *)context), z_move(payload), &options);
z_drop(z_move(keystr));

if (++value_num == values_count) {
Expand Down

0 comments on commit ed3a164

Please sign in to comment.