Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User attachment #45

Merged
merged 16 commits into from
Feb 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feature(user attachment): adding comments
DariusIMP committed Feb 16, 2024
commit 7b83940fcd4a359be996850a13c9428e06314ea7
23 changes: 19 additions & 4 deletions zenoh-jni/src/utils.rs
Original file line number Diff line number Diff line change
@@ -113,17 +113,32 @@ pub(crate) fn load_on_close(
})
}

/// This function is used in conjunction with the Kotlin function
/// `decodeAttachment(attachmentBytes: ByteArray): Attachment` which takes a byte array with the
/// format <key size><key payload><value size><value payload>, repeating this
/// pattern for as many pairs there are in the attachment.
///
/// The kotlin function expects both key size and value size to be i32 integers expressed with
/// little endian format.
///
pub(crate) fn attachment_to_vec(attachment: Attachment) -> Vec<u8> {
let mut buffer: Vec<u8> = Vec::new();
for (key, value) in attachment.iter() {
buffer.extend(&i32::to_le_bytes(key.len().try_into().unwrap()));
buffer.extend((key.len() as i32).to_le_bytes());
buffer.extend(&key[..]);
buffer.extend(&i32::to_le_bytes(value.len().try_into().unwrap()));
buffer.extend((value.len() as i32).to_le_bytes());
buffer.extend(&value[..]);
}
buffer
}

/// This function is used in conjunction with the Kotlin function
/// `encodeAttachment(attachment: Attachment): ByteArray` which converts the attachment into a
/// ByteArray with the format <key size><key payload><value size><value payload>, repeating this
/// pattern for as many pairs there are in the attachment.
///
/// Both key size and value size are i32 integers with little endian format.
///
pub(crate) fn vec_to_attachment(bytes: Vec<u8>) -> Attachment {
let mut builder = AttachmentBuilder::new();
let mut idx = 0;
@@ -134,7 +149,7 @@ pub(crate) fn vec_to_attachment(bytes: Vec<u8>) -> Attachment {
slice_size = i32::from_le_bytes(
bytes[idx..idx + i32_size]
.try_into()
.expect("Error decoding i32."),
.expect("Error decoding i32 while processing attachment."), //This error should never happen.
);
idx += i32_size;

@@ -144,7 +159,7 @@ pub(crate) fn vec_to_attachment(bytes: Vec<u8>) -> Attachment {
slice_size = i32::from_le_bytes(
bytes[idx..idx + i32_size]
.try_into()
.expect("Error decoding i32."),
.expect("Error decoding i32 while processing attachment."), //This error should never happen.
);
idx += i32_size;

8 changes: 8 additions & 0 deletions zenoh-kotlin/src/commonMain/kotlin/io/zenoh/jni/JNIUtils.kt
Original file line number Diff line number Diff line change
@@ -54,6 +54,9 @@ internal fun decodeAttachment(attachmentBytes: ByteArray): Attachment {
return Attachment(pairs)
}

/**
* Converts an integer into a byte array with little endian format.
*/
fun Int.toByteArray(): ByteArray {
val result = ByteArray(UInt.SIZE_BYTES)
(0 until UInt.SIZE_BYTES).forEach {
@@ -62,5 +65,10 @@ fun Int.toByteArray(): ByteArray {
return result
}

/**
* To int. The byte array is expected to be in Little Endian format.
*
* @return The integer value.
*/
fun ByteArray.toInt(): Int =
(((this[3].toUInt() and 0xFFu) shl 24) or ((this[2].toUInt() and 0xFFu) shl 16) or ((this[1].toUInt() and 0xFFu) shl 8) or (this[0].toUInt() and 0xFFu)).toInt()
Original file line number Diff line number Diff line change
@@ -22,6 +22,8 @@ package io.zenoh.sample
*
* Attachments can be added to a message sent through Zenoh while performing puts, queries and replies.
*
* Using attachments will result in performance loss.
*
* @property values
* @constructor Create empty Attachment
*/