Skip to content

Commit

Permalink
rename wrapped zenoh-protocol structures to ...Proto, wrap and expo…
Browse files Browse the repository at this point in the history
…se `EntityGlobalId` (#1118)

* zenohIdInner type added

* EntityGlobalIdInner

* HelloInner near Hello

* zenohId in info

* cargo fmt

* shm fix

* no-std fix

* internal doc test fix

* renamed Inner to Proto

* wrong return type fix

* wrappers in zenoh-config
  • Loading branch information
milyin authored Jun 12, 2024
1 parent f19741b commit c8537bc
Show file tree
Hide file tree
Showing 83 changed files with 575 additions and 478 deletions.
4 changes: 2 additions & 2 deletions commons/zenoh-codec/src/core/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use zenoh_buffers::{
reader::{DidntRead, Reader},
writer::{DidntWrite, Writer},
};
use zenoh_protocol::core::{Timestamp, ZenohId};
use zenoh_protocol::core::{Timestamp, ZenohIdProto};

use crate::{LCodec, RCodec, WCodec, Zenoh080};

Expand Down Expand Up @@ -53,7 +53,7 @@ where
if size > (uhlc::ID::MAX_SIZE) {
return Err(DidntRead);
}
let mut id = [0_u8; ZenohId::MAX_SIZE];
let mut id = [0_u8; ZenohIdProto::MAX_SIZE];
reader.read_exact(&mut id[..size])?;

let time = uhlc::NTP64(time);
Expand Down
36 changes: 18 additions & 18 deletions commons/zenoh-codec/src/core/zenohid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,70 +17,70 @@ use zenoh_buffers::{
reader::{DidntRead, Reader},
writer::{DidntWrite, Writer},
};
use zenoh_protocol::core::ZenohId;
use zenoh_protocol::core::ZenohIdProto;

use crate::{LCodec, RCodec, WCodec, Zenoh080, Zenoh080Length};

impl LCodec<&ZenohId> for Zenoh080 {
fn w_len(self, x: &ZenohId) -> usize {
impl LCodec<&ZenohIdProto> for Zenoh080 {
fn w_len(self, x: &ZenohIdProto) -> usize {
x.size()
}
}

impl<W> WCodec<&ZenohId, &mut W> for Zenoh080
impl<W> WCodec<&ZenohIdProto, &mut W> for Zenoh080
where
W: Writer,
{
type Output = Result<(), DidntWrite>;

fn write(self, writer: &mut W, x: &ZenohId) -> Self::Output {
fn write(self, writer: &mut W, x: &ZenohIdProto) -> Self::Output {
self.write(&mut *writer, &x.to_le_bytes()[..x.size()])
}
}

impl<R> RCodec<ZenohId, &mut R> for Zenoh080
impl<R> RCodec<ZenohIdProto, &mut R> for Zenoh080
where
R: Reader,
{
type Error = DidntRead;

fn read(self, reader: &mut R) -> Result<ZenohId, Self::Error> {
fn read(self, reader: &mut R) -> Result<ZenohIdProto, Self::Error> {
let size: usize = self.read(&mut *reader)?;
if size > ZenohId::MAX_SIZE {
if size > ZenohIdProto::MAX_SIZE {
return Err(DidntRead);
}
let mut id = [0; ZenohId::MAX_SIZE];
let mut id = [0; ZenohIdProto::MAX_SIZE];
reader.read_exact(&mut id[..size])?;
ZenohId::try_from(&id[..size]).map_err(|_| DidntRead)
ZenohIdProto::try_from(&id[..size]).map_err(|_| DidntRead)
}
}

impl<W> WCodec<&ZenohId, &mut W> for Zenoh080Length
impl<W> WCodec<&ZenohIdProto, &mut W> for Zenoh080Length
where
W: Writer,
{
type Output = Result<(), DidntWrite>;

fn write(self, writer: &mut W, x: &ZenohId) -> Self::Output {
if self.length > ZenohId::MAX_SIZE {
fn write(self, writer: &mut W, x: &ZenohIdProto) -> Self::Output {
if self.length > ZenohIdProto::MAX_SIZE {
return Err(DidntWrite);
}
writer.write_exact(&x.to_le_bytes()[..x.size()])
}
}

impl<R> RCodec<ZenohId, &mut R> for Zenoh080Length
impl<R> RCodec<ZenohIdProto, &mut R> for Zenoh080Length
where
R: Reader,
{
type Error = DidntRead;

fn read(self, reader: &mut R) -> Result<ZenohId, Self::Error> {
if self.length > ZenohId::MAX_SIZE {
fn read(self, reader: &mut R) -> Result<ZenohIdProto, Self::Error> {
if self.length > ZenohIdProto::MAX_SIZE {
return Err(DidntRead);
}
let mut id = [0; ZenohId::MAX_SIZE];
let mut id = [0; ZenohIdProto::MAX_SIZE];
reader.read_exact(&mut id[..self.length])?;
ZenohId::try_from(&id[..self.length]).map_err(|_| DidntRead)
ZenohIdProto::try_from(&id[..self.length]).map_err(|_| DidntRead)
}
}
4 changes: 2 additions & 2 deletions commons/zenoh-codec/src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use zenoh_buffers::{
};
use zenoh_protocol::{
common::{imsg, ZExtZ64, ZExtZBufHeader},
core::{EntityId, Reliability, ZenohId},
core::{EntityId, Reliability, ZenohIdProto},
network::{ext::EntityGlobalIdType, *},
};

Expand Down Expand Up @@ -265,7 +265,7 @@ where
let length = 1 + ((flags >> 4) as usize);

let lodec = Zenoh080Length::new(length);
let zid: ZenohId = lodec.read(&mut *reader)?;
let zid: ZenohIdProto = lodec.read(&mut *reader)?;

let eid: EntityId = self.codec.read(&mut *reader)?;

Expand Down
22 changes: 11 additions & 11 deletions commons/zenoh-codec/src/scouting/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ use zenoh_buffers::{
};
use zenoh_protocol::{
common::{imsg, ZExtUnknown},
core::{Locator, WhatAmI, ZenohId},
core::{Locator, WhatAmI, ZenohIdProto},
scouting::{
hello::{flag, Hello},
hello::{flag, HelloProto},
id,
},
};

use crate::{RCodec, WCodec, Zenoh080, Zenoh080Header, Zenoh080Length};

impl<W> WCodec<&Hello, &mut W> for Zenoh080
impl<W> WCodec<&HelloProto, &mut W> for Zenoh080
where
W: Writer,
{
type Output = Result<(), DidntWrite>;

fn write(self, writer: &mut W, x: &Hello) -> Self::Output {
let Hello {
fn write(self, writer: &mut W, x: &HelloProto) -> Self::Output {
let HelloProto {
version,
whatami,
zid,
Expand Down Expand Up @@ -73,26 +73,26 @@ where
}
}

impl<R> RCodec<Hello, &mut R> for Zenoh080
impl<R> RCodec<HelloProto, &mut R> for Zenoh080
where
R: Reader,
{
type Error = DidntRead;

fn read(self, reader: &mut R) -> Result<Hello, Self::Error> {
fn read(self, reader: &mut R) -> Result<HelloProto, Self::Error> {
let header: u8 = self.read(&mut *reader)?;
let codec = Zenoh080Header::new(header);
codec.read(reader)
}
}

impl<R> RCodec<Hello, &mut R> for Zenoh080Header
impl<R> RCodec<HelloProto, &mut R> for Zenoh080Header
where
R: Reader,
{
type Error = DidntRead;

fn read(self, reader: &mut R) -> Result<Hello, Self::Error> {
fn read(self, reader: &mut R) -> Result<HelloProto, Self::Error> {
if imsg::mid(self.header) != id::HELLO {
return Err(DidntRead);
}
Expand All @@ -108,7 +108,7 @@ where
};
let length = 1 + ((flags >> 4) as usize);
let lodec = Zenoh080Length::new(length);
let zid: ZenohId = lodec.read(&mut *reader)?;
let zid: ZenohIdProto = lodec.read(&mut *reader)?;

let locators = if imsg::has_flag(self.header, flag::L) {
let locs: Vec<Locator> = self.codec.read(&mut *reader)?;
Expand All @@ -124,7 +124,7 @@ where
has_extensions = more;
}

Ok(Hello {
Ok(HelloProto {
version,
zid,
whatami,
Expand Down
4 changes: 2 additions & 2 deletions commons/zenoh-codec/src/scouting/scout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use zenoh_buffers::{
};
use zenoh_protocol::{
common::{imsg, ZExtUnknown},
core::{whatami::WhatAmIMatcher, ZenohId},
core::{whatami::WhatAmIMatcher, ZenohIdProto},
scouting::{
id,
scout::{flag, Scout},
Expand Down Expand Up @@ -93,7 +93,7 @@ where
let zid = if imsg::has_flag(flags, flag::I) {
let length = 1 + ((flags >> 4) as usize);
let lodec = Zenoh080Length::new(length);
let zid: ZenohId = lodec.read(&mut *reader)?;
let zid: ZenohIdProto = lodec.read(&mut *reader)?;
Some(zid)
} else {
None
Expand Down
6 changes: 3 additions & 3 deletions commons/zenoh-codec/src/transport/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use zenoh_buffers::{
};
use zenoh_protocol::{
common::{iext, imsg},
core::{Resolution, WhatAmI, ZenohId},
core::{Resolution, WhatAmI, ZenohIdProto},
transport::{
batch_size, id,
init::{ext, flag, InitAck, InitSyn},
Expand Down Expand Up @@ -160,7 +160,7 @@ where
};
let length = 1 + ((flags >> 4) as usize);
let lodec = Zenoh080Length::new(length);
let zid: ZenohId = lodec.read(&mut *reader)?;
let zid: ZenohIdProto = lodec.read(&mut *reader)?;

let mut resolution = Resolution::default();
let mut batch_size = batch_size::UNICAST.to_le_bytes();
Expand Down Expand Up @@ -373,7 +373,7 @@ where
};
let length = 1 + ((flags >> 4) as usize);
let lodec = Zenoh080Length::new(length);
let zid: ZenohId = lodec.read(&mut *reader)?;
let zid: ZenohIdProto = lodec.read(&mut *reader)?;

let mut resolution = Resolution::default();
let mut batch_size = batch_size::UNICAST.to_le_bytes();
Expand Down
4 changes: 2 additions & 2 deletions commons/zenoh-codec/src/transport/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use zenoh_buffers::{
};
use zenoh_protocol::{
common::{iext, imsg, ZExtZBufHeader},
core::{Priority, Resolution, WhatAmI, ZenohId},
core::{Priority, Resolution, WhatAmI, ZenohIdProto},
transport::{
batch_size, id,
join::{ext, flag, Join},
Expand Down Expand Up @@ -242,7 +242,7 @@ where
};
let length = 1 + ((flags >> 4) as usize);
let lodec = Zenoh080Length::new(length);
let zid: ZenohId = lodec.read(&mut *reader)?;
let zid: ZenohIdProto = lodec.read(&mut *reader)?;

let mut resolution = Resolution::default();
let mut batch_size = batch_size::MULTICAST.to_le_bytes();
Expand Down
6 changes: 3 additions & 3 deletions commons/zenoh-codec/src/zenoh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use zenoh_buffers::{
use zenoh_protocol::common::{iext, ZExtUnit};
use zenoh_protocol::{
common::{imsg, ZExtZBufHeader},
core::{Encoding, EntityGlobalId, EntityId, ZenohId},
core::{Encoding, EntityGlobalIdProto, EntityId, ZenohIdProto},
zenoh::{ext, id, PushBody, RequestBody, ResponseBody},
};

Expand Down Expand Up @@ -186,14 +186,14 @@ where
let length = 1 + ((flags >> 4) as usize);

let lodec = Zenoh080Length::new(length);
let zid: ZenohId = lodec.read(&mut *reader)?;
let zid: ZenohIdProto = lodec.read(&mut *reader)?;

let eid: EntityId = self.codec.read(&mut *reader)?;
let sn: u32 = self.codec.read(&mut *reader)?;

Ok((
ext::SourceInfoType {
id: EntityGlobalId { zid, eid },
id: EntityGlobalIdProto { zid, eid },
sn,
},
more,
Expand Down
6 changes: 3 additions & 3 deletions commons/zenoh-codec/tests/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ fn codec_string_bounded() {

#[test]
fn codec_zid() {
run!(ZenohId, ZenohId::default());
run!(ZenohIdProto, ZenohIdProto::default());
}

#[test]
Expand Down Expand Up @@ -348,7 +348,7 @@ fn codec_locator() {
fn codec_timestamp() {
run!(Timestamp, {
let time = uhlc::NTP64(thread_rng().gen());
let id = uhlc::ID::try_from(ZenohId::rand().to_le_bytes()).unwrap();
let id = uhlc::ID::try_from(ZenohIdProto::rand().to_le_bytes()).unwrap();
Timestamp::new(time, id)
});
}
Expand Down Expand Up @@ -447,7 +447,7 @@ fn codec_scout() {

#[test]
fn codec_hello() {
run!(Hello, Hello::rand());
run!(HelloProto, HelloProto::rand());
}

#[test]
Expand Down
42 changes: 2 additions & 40 deletions commons/zenoh-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//! Configuration to pass to `zenoh::open()` and `zenoh::scout()` functions and associated constants.
pub mod defaults;
mod include;
pub mod wrappers;

#[allow(unused_imports)]
use std::convert::TryFrom; // This is a false positive from the rust analyser
Expand All @@ -25,7 +26,6 @@ use std::{
io::Read,
net::SocketAddr,
path::Path,
str::FromStr,
sync::{Arc, Mutex, MutexGuard, Weak},
};

Expand All @@ -35,6 +35,7 @@ use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use validated_struct::ValidatedMapAssociatedTypes;
pub use validated_struct::{GetError, ValidatedMap};
use wrappers::ZenohId;
use zenoh_core::zlock;
pub use zenoh_protocol::core::{
whatami, EndPoint, Locator, WhatAmI, WhatAmIMatcher, WhatAmIMatcherVisitor,
Expand All @@ -52,45 +53,6 @@ pub use mode_dependent::*;
pub mod connection_retry;
pub use connection_retry::*;

/// The global unique id of a zenoh peer.
#[derive(
Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Debug, Default,
)]
#[repr(transparent)]
pub struct ZenohId(zenoh_protocol::core::ZenohId);

impl fmt::Display for ZenohId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

impl From<zenoh_protocol::core::ZenohId> for ZenohId {
fn from(id: zenoh_protocol::core::ZenohId) -> Self {
Self(id)
}
}

impl From<ZenohId> for zenoh_protocol::core::ZenohId {
fn from(id: ZenohId) -> Self {
id.0
}
}

impl From<ZenohId> for uhlc::ID {
fn from(zid: ZenohId) -> Self {
zid.0.into()
}
}

impl FromStr for ZenohId {
type Err = zenoh_result::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
zenoh_protocol::core::ZenohId::from_str(s).map(|zid| zid.into())
}
}

// Wrappers for secrecy of values
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct SecretString(String);
Expand Down
2 changes: 1 addition & 1 deletion commons/zenoh-config/src/mode_dependent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use serde::{
Deserialize, Serialize,
};
pub use zenoh_protocol::core::{
whatami, EndPoint, Locator, WhatAmI, WhatAmIMatcher, WhatAmIMatcherVisitor, ZenohId,
whatami, EndPoint, Locator, WhatAmI, WhatAmIMatcher, WhatAmIMatcherVisitor, ZenohIdProto,
};

pub trait ModeDependent<T> {
Expand Down
Loading

0 comments on commit c8537bc

Please sign in to comment.