Skip to content

Commit

Permalink
Add features to hide Zenoh unstable (#91)
Browse files Browse the repository at this point in the history
* Add features to hide Zenoh unstable

Signed-off-by: ChenYing Kuo <[email protected]>

* Add more description for feature zenoh-unstable

Signed-off-by: ChenYing Kuo <[email protected]>

---------

Signed-off-by: ChenYing Kuo <[email protected]>
  • Loading branch information
evshary authored Nov 16, 2024
1 parent 48e36cf commit 5817a6d
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 37 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/nightly.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ jobs:
check-msrv:
uses: ./.github/workflows/verify-msrv.yaml

test-all-features:
uses: ./.github/workflows/test-featurematrix.yaml

x-build:
# The jury is still out on whether this actually adds any value, besides simply being possible...
uses: ./.github/workflows/x-build.yaml
Expand Down
79 changes: 79 additions & 0 deletions .github/workflows/test-featurematrix.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# ********************************************************************************
# Copyright (c) 2024 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Apache License Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0
#
# SPDX-License-Identifier: Apache-2.0
# *******************************************************************************/

# Test all feature combinations on a range of OS platforms

name: Matrix-test

on:
workflow_call:
workflow_dispatch:

env:
RUST_TOOLCHAIN: ${{ vars.RUST_TOOLCHAIN || 'stable' }}
RUSTFLAGS: -Dwarnings
CARGO_TERM_COLOR: always

jobs:
# [impl->req~up-language-ci-test~1]
test-all-features:
name: all feature combinations
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
steps:
- uses: actions/checkout@v4
with:
submodules: "recursive"
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ env.RUST_TOOLCHAIN }}
- uses: Swatinem/rust-cache@v2
- name: Install cargo-all-features
run: |
cargo install cargo-all-features
- name: Show toolchain information
working-directory: ${{ github.workspace }}
run: |
rustup toolchain list
cargo --version
- name: cargo-check all possible feature combinations
run: |
cargo check-all-features
- name: cargo-test all possible feature combinations
run: |
cargo test-all-features
# Alternative to the full feature matrix tested in the step above - somewhat faster...
# multi-os-nextest:
# runs-on: ${{ matrix.os }}
# env:
# NEXTEST_EXPERIMENTAL_LIBTEST_JSON: 1
# strategy:
# matrix:
# os: [ubuntu-latest, windows-latest, macOS-latest]
# feature-flags: ["", "--no-default-features", "--all-features"]
# steps:
# - uses: actions/checkout@v4
# - uses: dtolnay/rust-toolchain@master
# with:
# toolchain: ${{ env.RUST_TOOLCHAIN }}
# - uses: Swatinem/rust-cache@v2
# - uses: taiki-e/install-action@nextest
# - name: Run cargo nextest
# run: |
# cargo nextest run --message-format libtest-json-plus ${{ matrix.feature-flags }}
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ tokio = { version = "1.35.1", default-features = false }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
up-rust = "0.2.0"
zenoh = { version = "1.0.0", features = ["unstable", "internal"] }
zenoh = { version = "1.0.0" }

[dev-dependencies]
chrono = "0.4.31"
clap = { version = "4.4.11", features = ["derive"] }
test-case = { version = "3.3" }

[features]
default = []
zenoh-unstable = ["zenoh/unstable", "zenoh/internal"]

[profile.release]
opt-level = 3
lto = "fat"
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ uProtocol transport implementation for Zenoh in Rust
cargo clippy --all-targets
# Build
cargo build
# Optional: Build with feature `zenoh-unstable` in uStreamer use case
cargo build --features zenoh-unstable
# Run test
cargo test
# Test coverage
Expand Down Expand Up @@ -45,4 +47,4 @@ The configuration file example is under the folder `config`.
The implementation follows the spec defined in [up-l1/zenoh](https://github.com/eclipse-uprotocol/up-spec/blob/main/up-l1/zenoh.adoc).

[^1]: Some PC configurations cannot connect locally. Add multicast to lo interface using
` $ sudo ip link set dev lo multicast on `
` $ sudo ip link set dev lo multicast on `
75 changes: 44 additions & 31 deletions examples/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ pub enum WhatAmIType {
Router,
}

#[cfg(not(feature = "zenoh-unstable"))]
#[derive(clap::Parser, Clone, PartialEq, Eq, Hash, Debug)]
pub struct Args {
#[arg(short, long)]
/// A configuration file.
config: Option<String>,
}

#[cfg(feature = "zenoh-unstable")]
#[derive(clap::Parser, Clone, PartialEq, Eq, Hash, Debug)]
pub struct Args {
#[arg(short, long)]
Expand All @@ -44,45 +53,49 @@ pub fn get_zenoh_config() -> zenoh_config::Config {
let args = Args::parse();

// Load the config from file path
#[allow(unused_mut)]
let mut zenoh_cfg = match &args.config {
Some(path) => zenoh_config::Config::from_file(path).unwrap(),
None => zenoh_config::Config::default(),
};

// You can choose from Router, Peer, Client
match args.mode {
Some(WhatAmIType::Peer) => zenoh_cfg.set_mode(Some(zenoh::config::WhatAmI::Peer)),
Some(WhatAmIType::Client) => zenoh_cfg.set_mode(Some(zenoh::config::WhatAmI::Client)),
Some(WhatAmIType::Router) => zenoh_cfg.set_mode(Some(zenoh::config::WhatAmI::Router)),
None => Ok(None),
}
.unwrap();
#[cfg(feature = "zenoh-unstable")]
{
// You can choose from Router, Peer, Client
match args.mode {
Some(WhatAmIType::Peer) => zenoh_cfg.set_mode(Some(zenoh::config::WhatAmI::Peer)),
Some(WhatAmIType::Client) => zenoh_cfg.set_mode(Some(zenoh::config::WhatAmI::Client)),
Some(WhatAmIType::Router) => zenoh_cfg.set_mode(Some(zenoh::config::WhatAmI::Router)),
None => Ok(None),
}
.unwrap();

// Set connection address
if !args.connect.is_empty() {
zenoh_cfg
.connect
.endpoints
.set(args.connect.iter().map(|v| v.parse().unwrap()).collect())
.unwrap();
}
// Set connection address
if !args.connect.is_empty() {
zenoh_cfg
.connect
.endpoints
.set(args.connect.iter().map(|v| v.parse().unwrap()).collect())
.unwrap();
}

// Set listener address
if !args.listen.is_empty() {
zenoh_cfg
.listen
.endpoints
.set(args.listen.iter().map(|v| v.parse().unwrap()).collect())
.unwrap();
}
// Set listener address
if !args.listen.is_empty() {
zenoh_cfg
.listen
.endpoints
.set(args.listen.iter().map(|v| v.parse().unwrap()).collect())
.unwrap();
}

// Set multicast configuration
if args.no_multicast_scouting {
zenoh_cfg
.scouting
.multicast
.set_enabled(Some(false))
.unwrap();
// Set multicast configuration
if args.no_multicast_scouting {
zenoh_cfg
.scouting
.multicast
.set_enabled(Some(false))
.unwrap();
}
}

zenoh_cfg
Expand Down
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ use tracing::error;
use up_rust::{ComparableListener, LocalUriProvider, UAttributes, UCode, UPriority, UStatus, UUri};
// Re-export Zenoh config
pub use zenoh::config as zenoh_config;
use zenoh::{
bytes::ZBytes, internal::runtime::Runtime as ZRuntime, pubsub::Subscriber, qos::Priority,
Session,
};
#[cfg(feature = "zenoh-unstable")]
use zenoh::internal::runtime::Runtime as ZRuntime;
use zenoh::{bytes::ZBytes, pubsub::Subscriber, qos::Priority, Session};

const UATTRIBUTE_VERSION: u8 = 1;
const THREAD_NUM: usize = 10;
Expand Down Expand Up @@ -88,6 +87,7 @@ impl UPTransportZenoh {
}

/// Create `UPTransportZenoh` by applying the Zenoh Runtime and local `UUri`. This can be used by uStreamer.
/// You need to enable feature `zenoh-unstable` to support this function.
///
/// # Arguments
///
Expand All @@ -96,6 +96,7 @@ impl UPTransportZenoh {
///
/// # Errors
/// Will return `Err` if unable to create `UPTransportZenoh`
#[cfg(feature = "zenoh-unstable")]
pub async fn new_with_runtime<U>(runtime: ZRuntime, uri: U) -> Result<UPTransportZenoh, UStatus>
where
U: TryInto<UUri>,
Expand Down

0 comments on commit 5817a6d

Please sign in to comment.