Skip to content

Commit

Permalink
HelloInner near Hello
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Jun 11, 2024
1 parent 549ed67 commit 22828c8
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 54 deletions.
18 changes: 9 additions & 9 deletions commons/zenoh-codec/src/scouting/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ use zenoh_protocol::{
common::{imsg, ZExtUnknown},
core::{Locator, WhatAmI, ZenohIdInner},
scouting::{
hello::{flag, Hello},
hello::{flag, HelloInner},
id,
},
};

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

impl<W> WCodec<&Hello, &mut W> for Zenoh080
impl<W> WCodec<&HelloInner, &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: &HelloInner) -> Self::Output {
let HelloInner {
version,
whatami,
zid,
Expand Down Expand Up @@ -73,26 +73,26 @@ where
}
}

impl<R> RCodec<Hello, &mut R> for Zenoh080
impl<R> RCodec<HelloInner, &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<HelloInner, 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<HelloInner, &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<HelloInner, Self::Error> {
if imsg::mid(self.header) != id::HELLO {
return Err(DidntRead);
}
Expand Down Expand Up @@ -124,7 +124,7 @@ where
has_extensions = more;
}

Ok(Hello {
Ok(HelloInner {
version,
zid,
whatami,
Expand Down
2 changes: 1 addition & 1 deletion commons/zenoh-codec/tests/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ fn codec_scout() {

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

#[test]
Expand Down
43 changes: 40 additions & 3 deletions commons/zenoh-protocol/src/scouting/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// ZettaScale Zenoh Team, <[email protected]>
//
use alloc::vec::Vec;

use std::fmt;
use crate::core::{Locator, WhatAmI, ZenohIdInner};

/// # Hello message
Expand Down Expand Up @@ -99,14 +99,14 @@ pub mod flag {
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Hello {
pub struct HelloInner {
pub version: u8,
pub whatami: WhatAmI,
pub zid: ZenohIdInner,
pub locators: Vec<Locator>,
}

impl Hello {
impl HelloInner {
#[cfg(feature = "test")]
pub fn rand() -> Self {
use rand::Rng;
Expand All @@ -129,3 +129,40 @@ impl Hello {
}
}
}

/// A zenoh Hello message.
pub struct Hello(HelloInner);

impl Hello {
/// Get the locators of this Hello message.
pub fn locators(&self) -> &[Locator] {
&self.0.locators
}

/// Get the zenoh id of this Hello message.
pub fn zid(&self) -> ZenohIdInner {
self.0.zid
}

/// Get the whatami of this Hello message.
pub fn whatami(&self) -> WhatAmI {
self.0.whatami
}
}

impl From<HelloInner> for Hello {
fn from(inner: HelloInner) -> Self {
Hello(inner)
}
}

impl fmt::Display for Hello {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Hello")
.field("zid", &self.zid())
.field("whatami", &self.whatami())
.field("locators", &self.locators())
.finish()
}
}

10 changes: 5 additions & 5 deletions commons/zenoh-protocol/src/scouting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
pub mod hello;
pub mod scout;

pub use hello::Hello;
pub use hello::HelloInner;
pub use scout::Scout;

pub mod id {
Expand All @@ -27,7 +27,7 @@ pub mod id {
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScoutingBody {
Scout(Scout),
Hello(Hello),
Hello(HelloInner),
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -46,7 +46,7 @@ impl ScoutingMessage {

match rng.gen_range(0..2) {
0 => ScoutingBody::Scout(Scout::rand()),
1 => ScoutingBody::Hello(Hello::rand()),
1 => ScoutingBody::Hello(HelloInner::rand()),
_ => unreachable!(),
}
.into()
Expand All @@ -69,8 +69,8 @@ impl From<Scout> for ScoutingMessage {
}
}

impl From<Hello> for ScoutingMessage {
fn from(hello: Hello) -> Self {
impl From<HelloInner> for ScoutingMessage {
fn from(hello: HelloInner) -> Self {
ScoutingBody::Hello(hello).into()
}
}
34 changes: 2 additions & 32 deletions zenoh/src/api/scouting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::{

use tokio::net::UdpSocket;
use zenoh_core::{Resolvable, Wait};
use zenoh_protocol::core::WhatAmIMatcher;
use zenoh_protocol::{core::WhatAmIMatcher, scouting::hello::Hello};
use zenoh_result::ZResult;
use zenoh_task::TerminatableTask;

Expand All @@ -30,36 +30,6 @@ use crate::{
net::runtime::{orchestrator::Loop, Runtime},
};

/// A zenoh Hello message.
pub struct Hello(zenoh_protocol::scouting::Hello);

impl Hello {
/// Get the locators of this Hello message.
pub fn locators(&self) -> &[zenoh_protocol::core::Locator] {
&self.0.locators
}

/// Get the zenoh id of this Hello message.
pub fn zid(&self) -> zenoh_protocol::core::ZenohIdInner {
self.0.zid
}

/// Get the whatami of this Hello message.
pub fn whatami(&self) -> zenoh_protocol::core::WhatAmI {
self.0.whatami
}
}

impl fmt::Display for Hello {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Hello")
.field("zid", &self.zid())
.field("whatami", &self.whatami())
.field("locators", &self.locators())
.finish()
}
}

/// A builder for initializing a [`Scout`].
///
/// # Examples
Expand Down Expand Up @@ -354,7 +324,7 @@ fn _scout(
let scout = Runtime::scout(&sockets, what, &addr, move |hello| {
let callback = callback.clone();
async move {
callback(Hello(hello));
callback(hello.into());
Loop::Continue
}
});
Expand Down
3 changes: 2 additions & 1 deletion zenoh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ pub mod handlers {

/// Scouting primitives
pub mod scouting {
pub use crate::api::scouting::{scout, Hello, Scout, ScoutBuilder};
pub use zenoh_protocol::scouting::hello::Hello;
pub use crate::api::scouting::{scout, Scout, ScoutBuilder};
}

/// Liveliness primitives
Expand Down
6 changes: 3 additions & 3 deletions zenoh/src/net/runtime/orchestrator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use zenoh_config::{
use zenoh_link::{Locator, LocatorInspector};
use zenoh_protocol::{
core::{whatami::WhatAmIMatcher, EndPoint, WhatAmI, ZenohIdInner},
scouting::{Hello, Scout, ScoutingBody, ScoutingMessage},
scouting::{HelloInner, Scout, ScoutingBody, ScoutingMessage},
};
use zenoh_result::{bail, zerror, ZResult};

Expand Down Expand Up @@ -829,7 +829,7 @@ impl Runtime {
mcast_addr: &SocketAddr,
f: F,
) where
F: Fn(Hello) -> Fut + std::marker::Send + std::marker::Sync + Clone,
F: Fn(HelloInner) -> Fut + std::marker::Send + std::marker::Sync + Clone,
Fut: Future<Output = Loop> + std::marker::Send,
Self: Sized,
{
Expand Down Expand Up @@ -1104,7 +1104,7 @@ impl Runtime {
let codec = Zenoh080::new();

let zid = self.manager().zid();
let hello: ScoutingMessage = Hello {
let hello: ScoutingMessage = HelloInner {
version: zenoh_protocol::VERSION,
whatami: self.whatami(),
zid,
Expand Down

0 comments on commit 22828c8

Please sign in to comment.