-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
217 additions
and
366 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,22 +12,20 @@ | |
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
use crate::{LCodec, RCodec, WCodec, Zenoh080, Zenoh080Bounded}; | ||
use alloc::string::String; | ||
use zenoh_buffers::{ | ||
reader::{DidntRead, Reader}, | ||
writer::{DidntWrite, Writer}, | ||
}; | ||
use zenoh_protocol::{ | ||
common::imsg, | ||
core::encoding::{flag, Encoding, EncodingPrefix}, | ||
core::encoding::{flag, Encoding, EncodingId}, | ||
}; | ||
|
||
impl LCodec<&Encoding> for Zenoh080 { | ||
fn w_len(self, x: &Encoding) -> usize { | ||
let (prefix, suffix) = (x.prefix(), x.suffix()); | ||
let mut len = self.w_len((prefix as u32) << 1); | ||
if !suffix.is_empty() { | ||
len += self.w_len(x.suffix()); | ||
let mut len = self.w_len((x.id as u32) << 1); | ||
if let Some(schema) = x.schema.as_ref() { | ||
len += schema.len(); | ||
} | ||
len | ||
} | ||
|
@@ -40,17 +38,16 @@ where | |
type Output = Result<(), DidntWrite>; | ||
|
||
fn write(self, writer: &mut W, x: &Encoding) -> Self::Output { | ||
let mut prefix = (x.prefix() as u32) << 1; | ||
let suffix = x.suffix(); | ||
let mut id = (x.id as u32) << 1; | ||
|
||
if !suffix.is_empty() { | ||
prefix |= flag::S; | ||
if x.schema.is_some() { | ||
id |= flag::S; | ||
} | ||
let zodec = Zenoh080Bounded::<u32>::new(); | ||
zodec.write(&mut *writer, prefix)?; | ||
if !suffix.is_empty() { | ||
zodec.write(&mut *writer, id)?; | ||
if let Some(schema) = x.schema.as_ref() { | ||
let zodec = Zenoh080Bounded::<u8>::new(); | ||
zodec.write(&mut *writer, suffix)?; | ||
zodec.write(&mut *writer, schema)?; | ||
} | ||
Ok(()) | ||
} | ||
|
@@ -64,23 +61,19 @@ where | |
|
||
fn read(self, reader: &mut R) -> Result<Encoding, Self::Error> { | ||
let zodec = Zenoh080Bounded::<u32>::new(); | ||
let prefix: u32 = zodec.read(&mut *reader)?; | ||
let (prefix, has_suffix) = ( | ||
(prefix >> 1) as EncodingPrefix, | ||
imsg::has_flag(prefix as u8, flag::S as u8), | ||
let id: u32 = zodec.read(&mut *reader)?; | ||
let (id, has_suffix) = ( | ||
(id >> 1) as EncodingId, | ||
imsg::has_flag(id as u8, flag::S as u8), | ||
); | ||
|
||
let mut suffix = String::new(); | ||
let mut schema = None; | ||
if has_suffix { | ||
let zodec = Zenoh080Bounded::<u8>::new(); | ||
suffix = zodec.read(&mut *reader)?; | ||
} | ||
|
||
let mut encoding: Encoding = Encoding::new(prefix); | ||
if !suffix.is_empty() { | ||
encoding = encoding.with_suffix(suffix).map_err(|_| DidntRead)?; | ||
schema = Some(zodec.read(&mut *reader)?); | ||
} | ||
|
||
let encoding = Encoding { id, schema }; | ||
Ok(encoding) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,12 +11,10 @@ | |
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
use crate::core::CowStr; | ||
use alloc::borrow::Cow; | ||
use core::fmt::Debug; | ||
use zenoh_result::{bail, ZResult}; | ||
use zenoh_buffers::ZSlice; | ||
|
||
pub type EncodingPrefix = u16; | ||
pub type EncodingId = u16; | ||
|
||
/// [`Encoding`] is a metadata that indicates how the data payload should be interpreted. | ||
/// For wire-efficiency and extensibility purposes, Zenoh defines an [`Encoding`] as | ||
|
@@ -27,68 +25,31 @@ pub type EncodingPrefix = u16; | |
/// of the API as per user convenience. That mapping has no impact on the Zenoh protocol definition. | ||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub struct Encoding { | ||
prefix: EncodingPrefix, | ||
suffix: CowStr<'static>, | ||
pub id: EncodingId, | ||
pub schema: Option<ZSlice>, | ||
} | ||
|
||
/// # Encoding field | ||
/// | ||
/// ```text | ||
/// 7 6 5 4 3 2 1 0 | ||
/// +-+-+-+-+-+-+-+-+ | ||
/// ~ prefix: z16 |S~ | ||
/// ~ id: z16 |S~ | ||
/// +---------------+ | ||
/// ~suffix: <u8;z8>~ -- if S==1 | ||
/// ~schema: <u8;z8>~ -- if S==1 | ||
/// +---------------+ | ||
/// ``` | ||
pub mod flag { | ||
pub const S: u32 = 1; // 0x01 Suffix if S==1 then suffix is present | ||
} | ||
|
||
impl Encoding { | ||
pub const UNSPECIFIED: Self = Self::empty(); | ||
|
||
/// Returns a new [`Encoding`] object provided the prefix ID. | ||
pub const fn new(prefix: EncodingPrefix) -> Self { | ||
Self { | ||
prefix, | ||
suffix: CowStr::borrowed(""), | ||
} | ||
} | ||
|
||
/// Sets the suffix of the encoding. | ||
/// It will return an error when the suffix is longer than 255 characters. | ||
pub fn with_suffix<IntoCowStr>(mut self, suffix: IntoCowStr) -> ZResult<Self> | ||
where | ||
IntoCowStr: Into<Cow<'static, str>> + AsRef<str>, | ||
{ | ||
let s: Cow<'static, str> = suffix.into(); | ||
if s.as_bytes().len() > u8::MAX as usize { | ||
bail!("Suffix length is limited to 255 characters") | ||
} | ||
self.suffix = (self.suffix + s.as_ref()).into(); | ||
Ok(self) | ||
} | ||
|
||
/// Returns a new [`Encoding`] object with default empty prefix ID. | ||
pub const fn empty() -> Self { | ||
Self::new(0) | ||
} | ||
|
||
// Returns the numerical prefix | ||
pub const fn prefix(&self) -> EncodingPrefix { | ||
self.prefix | ||
} | ||
|
||
// Returns the suffix string | ||
pub fn suffix(&self) -> &str { | ||
self.suffix.as_str() | ||
} | ||
|
||
/// Returns `true` if the string representation of this encoding starts with | ||
/// the string representation of the other given encoding. | ||
pub fn starts_with(&self, with: &Encoding) -> bool { | ||
self.prefix() == with.prefix() && self.suffix().starts_with(with.suffix()) | ||
Self { | ||
id: 0, | ||
schema: None, | ||
} | ||
} | ||
} | ||
|
||
|
@@ -101,23 +62,17 @@ impl Default for Encoding { | |
impl Encoding { | ||
#[cfg(feature = "test")] | ||
pub fn rand() -> Self { | ||
use rand::{ | ||
distributions::{Alphanumeric, DistString}, | ||
Rng, | ||
}; | ||
use rand::Rng; | ||
|
||
const MIN: usize = 2; | ||
const MAX: usize = 16; | ||
|
||
let mut rng = rand::thread_rng(); | ||
|
||
let prefix: EncodingPrefix = rng.gen(); | ||
let suffix: String = if rng.gen_bool(0.5) { | ||
let len = rng.gen_range(MIN..MAX); | ||
Alphanumeric.sample_string(&mut rng, len) | ||
} else { | ||
String::new() | ||
}; | ||
Encoding::new(prefix).with_suffix(suffix).unwrap() | ||
let id: EncodingId = rng.gen(); | ||
let schema = rng | ||
.gen_bool(0.5) | ||
.then_some(ZSlice::rand(rng.gen_range(MIN..MAX))); | ||
Encoding { id, schema } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.