zenoh_protocol/scouting/
mod.rs

1//
2// Copyright (c) 2023 ZettaScale Technology
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8//
9// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10//
11// Contributors:
12//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13//
14pub mod hello;
15pub mod scout;
16
17pub use hello::HelloProto;
18pub use scout::Scout;
19
20pub mod id {
21    // Scouting Messages
22    pub const SCOUT: u8 = 0x01;
23    pub const HELLO: u8 = 0x02;
24}
25
26// Zenoh messages at scouting level
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub enum ScoutingBody {
29    Scout(Scout),
30    Hello(HelloProto),
31}
32
33#[derive(Debug, Clone, PartialEq, Eq)]
34pub struct ScoutingMessage {
35    pub body: ScoutingBody,
36}
37
38impl ScoutingMessage {
39    #[cfg(feature = "test")]
40    #[doc(hidden)]
41    pub fn rand() -> Self {
42        use rand::Rng;
43
44        let mut rng = rand::thread_rng();
45
46        match rng.gen_range(0..2) {
47            0 => ScoutingBody::Scout(Scout::rand()),
48            1 => ScoutingBody::Hello(HelloProto::rand()),
49            _ => unreachable!(),
50        }
51        .into()
52    }
53}
54
55impl From<ScoutingBody> for ScoutingMessage {
56    fn from(body: ScoutingBody) -> Self {
57        Self { body }
58    }
59}
60
61impl From<Scout> for ScoutingMessage {
62    fn from(scout: Scout) -> Self {
63        ScoutingBody::Scout(scout).into()
64    }
65}
66
67impl From<HelloProto> for ScoutingMessage {
68    fn from(hello: HelloProto) -> Self {
69        ScoutingBody::Hello(hello).into()
70    }
71}