├── .clang-format ├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.yml │ ├── config.yml │ ├── feature_request.yml │ └── release.yml ├── dependabot.yml ├── release.yml └── workflows │ ├── check-labels.yml │ ├── ci.yml │ ├── release.yml │ └── update-release-project.yml ├── .gitignore ├── .gitmodules ├── .markdownlint.yaml ├── .readthedocs.yaml ├── CHANGELOG.md ├── CMakeLists.txt ├── CONTRIBUTING.md ├── CONTRIBUTORS.md ├── LICENSE ├── NOTICE.md ├── README.md ├── ci └── scripts │ ├── build.bash │ └── bump-and-tag.bash ├── cmake ├── FindSphinx.cmake └── helpers.cmake ├── docs ├── CMakeLists.txt ├── Doxyfile ├── README.md ├── api.rst ├── channels.rst ├── commons.rst ├── conf.py ├── config.rst ├── error_handling.rst ├── examples.rst ├── ext.rst ├── genindex.rst ├── index.rst ├── interop.rst ├── keyexpr.rst ├── matching.rst ├── publish_subscribe.rst ├── pubsub.rst ├── query_reply.rst ├── queryable.rst ├── requirements.txt ├── scouting.rst ├── session.rst ├── session_ex.rst └── shared_memory.rst ├── examples ├── CMakeLists.txt ├── README.md ├── getargs.hxx ├── simple │ ├── Readme.md │ ├── universal │ │ ├── CMakeLists.txt │ │ └── z_simple.cxx │ ├── zenohc │ │ ├── CMakeLists.txt │ │ └── zc_simple.cxx │ └── zenohpico │ │ ├── CMakeLists.txt │ │ └── zp_simple.cxx ├── universal │ ├── proto │ │ └── entity.proto │ ├── z_bytes.cxx │ ├── z_delete.cxx │ ├── z_get.cxx │ ├── z_get_channel.cxx │ ├── z_get_channel_non_blocking.cxx │ ├── z_get_liveliness.cxx │ ├── z_info.cxx │ ├── z_liveliness.cxx │ ├── z_ping.cxx │ ├── z_pong.cxx │ ├── z_pub.cxx │ ├── z_pub_thr.cxx │ ├── z_put.cxx │ ├── z_querier.cxx │ ├── z_queryable.cxx │ ├── z_scout.cxx │ ├── z_storage.cxx │ ├── z_sub.cxx │ ├── z_sub_liveliness.cxx │ └── z_sub_thr.cxx └── zenohc │ ├── z_advanced_pub.cxx │ ├── z_advanced_sub.cxx │ ├── z_get_shm.cxx │ ├── z_ping_shm.cxx │ ├── z_pub_shm.cxx │ ├── z_pub_shm_thr.cxx │ ├── z_queryable_shm.cxx │ └── z_sub_shm.cxx ├── include ├── zenoh.hxx └── zenoh │ ├── api.hxx │ ├── api │ ├── base.hxx │ ├── bytes.hxx │ ├── channels.hxx │ ├── closures.hxx │ ├── config.hxx │ ├── encoding.hxx │ ├── enums.hxx │ ├── ext │ │ ├── advanced_publisher.hxx │ │ ├── advanced_subscriber.hxx │ │ ├── miss.hxx │ │ ├── publication_cache.hxx │ │ ├── querying_subscriber.hxx │ │ ├── serialization.hxx │ │ └── session_ext.hxx │ ├── hello.hxx │ ├── id.hxx │ ├── interop.hxx │ ├── keyexpr.hxx │ ├── liveliness.hxx │ ├── logging.hxx │ ├── matching.hxx │ ├── publisher.hxx │ ├── querier.hxx │ ├── query.hxx │ ├── query_consolidation.hxx │ ├── queryable.hxx │ ├── reply.hxx │ ├── sample.hxx │ ├── scout.hxx │ ├── session.hxx │ ├── shm │ │ ├── buffer │ │ │ ├── buffer.hxx │ │ │ ├── zshm.hxx │ │ │ └── zshmmut.hxx │ │ ├── cleanup.hxx │ │ ├── client │ │ │ ├── client.hxx │ │ │ ├── shm_client.hxx │ │ │ └── shm_segment.hxx │ │ ├── client_storage │ │ │ └── client_storage.hxx │ │ ├── common │ │ │ ├── common.hxx │ │ │ └── types.hxx │ │ ├── protocol_implementations │ │ │ ├── posix │ │ │ │ ├── posix.hxx │ │ │ │ ├── posix_shm_client.hxx │ │ │ │ └── posix_shm_provider.hxx │ │ │ └── protocol_implementations.hxx │ │ ├── provider │ │ │ ├── alloc_layout.hxx │ │ │ ├── chunk.hxx │ │ │ ├── provider.hxx │ │ │ ├── shm_provider.hxx │ │ │ ├── shm_provider_backend.hxx │ │ │ ├── types.hxx │ │ │ └── types_impl.hxx │ │ └── shm.hxx │ ├── source_info.hxx │ ├── subscriber.hxx │ └── timestamp.hxx │ ├── detail │ ├── availability_checks.hxx │ ├── closures.hxx │ ├── closures_concrete.hxx │ └── commons.hxx │ └── zenohc.hxx ├── install ├── CMakeLists.txt ├── PackageConfig.cmake.in ├── cpack_project_config.cmake └── zenohcxx.pc.in ├── scripts ├── build_local.sh ├── build_standalone_examples.sh ├── install_from_git.sh ├── install_from_local.sh └── install_local.sh ├── tests ├── CMakeLists.txt ├── build │ └── warnings.cxx ├── run_with_router.sh ├── universal │ ├── bytes.cxx │ ├── closures.cxx │ ├── details.cxx │ ├── network │ │ ├── keyexpr.cxx │ │ ├── liveliness.cxx │ │ ├── pub_sub.cxx │ │ └── queryable_get.cxx │ └── serialization.cxx ├── zenohc │ ├── advanced_pub_sub.cxx │ ├── config.cxx │ └── shm_api.cxx └── zenohpico │ ├── config.cxx │ └── network │ └── batching.cxx ├── version.txt ├── zenoh-c-branch.txt ├── zenoh-cpp-branch.txt └── zenoh-pico-branch.txt /.clang-format: -------------------------------------------------------------------------------- 1 | BasedOnStyle: Google 2 | IndentWidth: 4 3 | ColumnLimit: 120 -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.yml: -------------------------------------------------------------------------------- 1 | name: Report a bug 2 | description: | 3 | Create a bug report to help us improve Zenoh. 4 | title: "[Bug] " 5 | labels: ["bug"] 6 | body: 7 | - type: textarea 8 | id: summary 9 | attributes: 10 | label: "Describe the bug" 11 | description: | 12 | A clear and concise description of the expected behaviour and what the bug is. 13 | placeholder: | 14 | E.g. zenoh peers can not automatically establish a connection. 15 | validations: 16 | required: true 17 | - type: textarea 18 | id: reproduce 19 | attributes: 20 | label: To reproduce 21 | description: "Steps to reproduce the behavior:" 22 | placeholder: | 23 | 1. Start a subscriber "..." 24 | 2. Start a publisher "...." 25 | 3. See error 26 | validations: 27 | required: true 28 | - type: textarea 29 | id: system 30 | attributes: 31 | label: System info 32 | description: "Please complete the following information:" 33 | placeholder: | 34 | - Platform: [e.g. Ubuntu 20.04 64-bit] 35 | - CPU [e.g. AMD Ryzen 3800X] 36 | - Zenoh version/commit [e.g. 6f172ea985d42d20d423a192a2d0d46bb0ce0d11] 37 | validations: 38 | required: true 39 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | contact_links: 3 | - name: Ask a question 4 | url: https://github.com/eclipse-zenoh/roadmap/discussions/categories/zenoh 5 | about: Open to the Zenoh community. Share your feedback with the Zenoh team. 6 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.yml: -------------------------------------------------------------------------------- 1 | name: Request a feature 2 | description: | 3 | Suggest a new feature specific to this repository. NOTE: for generic Zenoh ideas use "Ask a question". 4 | body: 5 | - type: markdown 6 | attributes: 7 | value: | 8 | **Guidelines for a good issue** 9 | 10 | *Is your feature request related to a problem?* 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | *Describe the solution you'd like* 14 | A clear and concise description of what you want to happen. 15 | 16 | *Describe alternatives you've considered* 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | *Additional context* 20 | Add any other context about the feature request here. 21 | - type: textarea 22 | id: feature 23 | attributes: 24 | label: "Describe the feature" 25 | validations: 26 | required: true 27 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/release.yml: -------------------------------------------------------------------------------- 1 | name: Add an issue to the next release 2 | description: | 3 | Add an issue as part of next release. 4 | This will be added to the current release project. 5 | You must be a contributor to use this template. 6 | labels: ["release"] 7 | body: 8 | - type: markdown 9 | attributes: 10 | value: | 11 | **Guidelines for a good issue** 12 | 13 | *Is your release item related to a problem?* 14 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 15 | 16 | *Describe the solution you'd like* 17 | A clear and concise description of what you want to happen. 18 | 19 | *Describe alternatives you've considered* 20 | A clear and concise description of any alternative solutions or features you've considered. 21 | 22 | *Additional context* 23 | Add any other context about the release item request here. 24 | - type: textarea 25 | id: item 26 | attributes: 27 | label: "Describe the release item" 28 | validations: 29 | required: true 30 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "gitsubmodule" 4 | directory: "/" 5 | schedule: 6 | interval: "daily" -------------------------------------------------------------------------------- /.github/release.yml: -------------------------------------------------------------------------------- 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, 13 | # 14 | 15 | changelog: 16 | categories: 17 | - title: Breaking changes 💥 18 | labels: 19 | - breaking-change 20 | - title: New features 🎉 21 | labels: 22 | - enhancement 23 | - new feature 24 | exclude: 25 | labels: 26 | - internal 27 | - title: Bug fixes 🐞 28 | labels: 29 | - bug 30 | exclude: 31 | labels: 32 | - internal 33 | - title: Documentation 📝 34 | labels: 35 | - documentation 36 | exclude: 37 | labels: 38 | - internal 39 | - title: Dependencies 👷 40 | labels: 41 | - dependencies 42 | exclude: 43 | labels: 44 | - internal 45 | - title: Other changes 46 | labels: 47 | - "*" 48 | exclude: 49 | labels: 50 | - internal -------------------------------------------------------------------------------- /.github/workflows/check-labels.yml: -------------------------------------------------------------------------------- 1 | name: Check required labels 2 | 3 | on: 4 | pull_request_target: 5 | types: [opened, synchronize, reopened, labeled] 6 | branches: ["**"] 7 | 8 | jobs: 9 | check-labels: 10 | name: Check PR labels 11 | uses: eclipse-zenoh/ci/.github/workflows/check-labels.yml@main 12 | secrets: 13 | github-token: ${{ secrets.GITHUB_TOKEN }} 14 | permissions: 15 | pull-requests: write 16 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: ["**"] 6 | pull_request: 7 | branches: ["**"] 8 | schedule: 9 | - cron: "0 6 * * 1-5" 10 | workflow_dispatch: 11 | 12 | jobs: 13 | check_format: 14 | name: Check codebase format with clang-format 15 | runs-on: ubuntu-24.04 16 | steps: 17 | - name: Checkout code 18 | uses: actions/checkout@v4 19 | 20 | - name: Run clang-format dry-run 21 | shell: bash 22 | run: | 23 | clang-format --version 24 | find include/ tests/ examples/ -iname "*.hxx" -o -iname "*.cxx" | xargs clang-format -n -Werror 25 | 26 | build: 27 | name: Build on ${{ matrix.os }} in ${{ matrix.build_type }} unstable ${{ matrix.unstable }} shm ${{ matrix.shm }} 28 | runs-on: ${{ matrix.os }} 29 | strategy: 30 | fail-fast: false 31 | matrix: 32 | os: [ubuntu-latest, macOS-latest, windows-latest] 33 | shm: [FALSE, TRUE] 34 | unstable: [FALSE, TRUE] 35 | build_type: [Debug, Release] 36 | exclude: 37 | - os: windows-latest 38 | build_type: Debug 39 | shm: TRUE 40 | 41 | steps: 42 | - name: Clone this repository 43 | uses: actions/checkout@v4 44 | 45 | - name: install zenoh-cpp and its dependencies 46 | shell: bash 47 | run: | 48 | ./scripts/install_from_git.sh ~/local ${{ matrix.unstable }} ${{ matrix.shm }} ON ${{ matrix.build_type }} 49 | 50 | - name: make examples 51 | shell: bash 52 | run: | 53 | cd build 54 | cmake --build . --target examples --config ${{ matrix.build_type }} 55 | 56 | - name: make stand-alone examples 57 | shell: bash 58 | run: | 59 | ./scripts/build_standalone_examples.sh ~/local 60 | 61 | - name: make tests 62 | shell: bash 63 | run: | 64 | cd build 65 | cmake --build . --target tests --config ${{ matrix.build_type }} 66 | 67 | - name: run tests 68 | shell: bash 69 | run: | 70 | cd build 71 | ctest -C ${{ matrix.build_type }} --output-on-failure 72 | 73 | - name: Upload artifact 74 | uses: actions/upload-artifact@v4 75 | with: 76 | # Artifact name 77 | name: zenoh-cpp-${{ matrix.os }}-${{ matrix.build_type }} 78 | # Directory containing files to upload 79 | path: | 80 | target/release/examples 81 | 82 | markdown_lint: 83 | runs-on: ubuntu-latest 84 | steps: 85 | - uses: actions/checkout@v4 86 | - uses: DavidAnson/markdownlint-cli2-action@v18 87 | with: 88 | config: '.markdownlint.yaml' 89 | globs: '**/README.md' 90 | 91 | # NOTE: In GitHub repository settings, the "Require status checks to pass 92 | # before merging" branch protection rule ensures that commits are only merged 93 | # from branches where specific status checks have passed. These checks are 94 | # specified manually as a list of workflow job names. Thus we use this extra 95 | # job to signal whether all CI checks have passed. 96 | ci: 97 | name: CI status checks 98 | runs-on: ubuntu-latest 99 | needs: [build, markdown_lint] 100 | if: always() 101 | steps: 102 | - name: Check whether all jobs pass 103 | run: echo '${{ toJson(needs) }}' | jq -e 'all(.result == "success")' 104 | -------------------------------------------------------------------------------- /.github/workflows/update-release-project.yml: -------------------------------------------------------------------------------- 1 | name: Update release project 2 | 3 | on: 4 | issues: 5 | types: [opened, edited, labeled] 6 | pull_request_target: 7 | types: [closed] 8 | branches: 9 | - main 10 | 11 | jobs: 12 | main: 13 | uses: eclipse-zenoh/zenoh/.github/workflows/update-release-project.yml@main 14 | secrets: inherit 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | build/* 35 | examples/simple/*/build 36 | .vscode/* 37 | target/* 38 | Testing/* 39 | docs/xml 40 | docs/html 41 | docs/latex 42 | .DS_Store 43 | 44 | zenohcpp.pc 45 | .cache -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "zenoh-c"] 2 | path = zenoh-c 3 | url = https://github.com/eclipse-zenoh/zenoh-c.git 4 | [submodule "zenoh-pico"] 5 | path = zenoh-pico 6 | url = https://github.com/eclipse-zenoh/zenoh-pico.git 7 | -------------------------------------------------------------------------------- /.markdownlint.yaml: -------------------------------------------------------------------------------- 1 | { 2 | "MD013": false, # Line length limitation 3 | "MD033": false, # Enable Inline HTML 4 | "MD041": false, # Allow first line heading 5 | "MD045": false, # Allow Images have no alternate text 6 | } -------------------------------------------------------------------------------- /.readthedocs.yaml: -------------------------------------------------------------------------------- 1 | # .readthedocs.yaml 2 | # Read the Docs configuration file 3 | # See https://docs.readthedocs.io/en/stable/config-file/v2.html for details 4 | 5 | # Required 6 | version: 2 7 | 8 | # Set the version of Python and other tools you might need 9 | build: 10 | os: ubuntu-22.04 11 | tools: 12 | python: "3.11" 13 | 14 | # Build documentation in the docs/ directory with Sphinx 15 | sphinx: 16 | configuration: docs/conf.py 17 | 18 | # We recommend specifying your dependencies to enable reproducible builds: 19 | # https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html 20 | python: 21 | install: 22 | - requirements: docs/requirements.txt -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | All changes for each release are tracked via [GitHub Releases](https://github.com/eclipse-zenoh/zenoh-cpp/releases). 2 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16) 2 | 3 | file(READ ${CMAKE_CURRENT_SOURCE_DIR}/version.txt version) 4 | 5 | file(READ ${CMAKE_CURRENT_SOURCE_DIR}/zenoh-c-branch.txt zenoh_c_branch) 6 | file(READ ${CMAKE_CURRENT_SOURCE_DIR}/zenoh-pico-branch.txt zenoh_pico_branch) 7 | 8 | project( 9 | zenohcxx 10 | VERSION ${version} 11 | DESCRIPTION "C++ bindings for Zenoh" 12 | HOMEPAGE_URL "https://github.com/eclipse-zenoh/zenoh-cpp" 13 | LANGUAGES C CXX 14 | ) 15 | set(CMAKE_CXX_STANDARD 17) 16 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 17 | 18 | set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) 19 | include(helpers) 20 | 21 | set(project_version "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") 22 | if(PROJECT_VERSION_TWEAK STREQUAL "") 23 | set(project_version "${project_version}") 24 | elseif(PROJECT_VERSION_TWEAK EQUAL 0) 25 | set(project_version "${project_version}-dev") 26 | elseif(PROJECT_VERSION_TWEAK GREATER 0) 27 | set(project_version "${project_version}-pre.${PROJECT_VERSION_TWEAK}") 28 | endif() 29 | status_print(project_version) 30 | 31 | declare_cache_var(ZENOHCXX_ZENOHC ON BOOL "Build for Zenoh-c target") 32 | declare_cache_var(ZENOHCXX_ZENOHPICO OFF BOOL "Build for Zenoh-pico target") 33 | declare_cache_var(ZENOHCXX_EXAMPLES_PROTOBUF ON BOOL "Build Protobuf example (turn off if you have problems with installed Protobuf version)") 34 | declare_cache_var(ZENOHCXX_ENABLE_TESTS ON BOOL "Enable building tests") 35 | declare_cache_var(ZENOHCXX_ENABLE_EXAMPLES ON BOOL "Enable building examples") 36 | 37 | set_default_build_type(Release) 38 | 39 | # zenohcxx without dependencies 40 | add_library(zenohcxx INTERFACE) 41 | target_include_directories(zenohcxx INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include") 42 | 43 | if(ZENOHCXX_ZENOHPICO) 44 | if(NOT TARGET zenohpico::lib) # Only find if target is not already available 45 | find_package(zenohpico REQUIRED) 46 | endif() 47 | if(TARGET zenohpico::lib) 48 | message(STATUS "defined lib target zenohcxx::zenohpico for zenohpico::lib") 49 | add_library(zenohcxx_zenohpico INTERFACE) 50 | target_compile_definitions(zenohcxx_zenohpico INTERFACE ZENOHCXX_ZENOHPICO) 51 | target_include_directories(zenohcxx_zenohpico INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include") 52 | add_dependencies(zenohcxx_zenohpico zenohpico::lib) 53 | target_link_libraries(zenohcxx_zenohpico INTERFACE zenohpico::lib) 54 | add_library(zenohcxx::zenohpico ALIAS zenohcxx_zenohpico) 55 | else() 56 | message(FATAL_ERROR "zenohpico::lib target not found") 57 | endif() 58 | endif() 59 | 60 | if(ZENOHCXX_ZENOHC) 61 | if(NOT TARGET zenohc::lib) # Only find if target is not already available 62 | find_package(zenohc REQUIRED) 63 | endif() 64 | if(TARGET zenohc::lib) 65 | message(STATUS "defined lib target zenohcxx::zenohc::lib for zenohc::lib") 66 | add_library(zenohcxx_zenohc INTERFACE) 67 | target_compile_definitions(zenohcxx_zenohc INTERFACE ZENOHCXX_ZENOHC) 68 | target_include_directories(zenohcxx_zenohc INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include") 69 | add_dependencies(zenohcxx_zenohc zenohc::lib) 70 | target_link_libraries(zenohcxx_zenohc INTERFACE zenohc::lib) 71 | add_library(zenohcxx::zenohc ALIAS zenohcxx_zenohc) 72 | else() 73 | message(FATAL_ERROR "zenohc::lib target not found") 74 | endif() 75 | endif() 76 | 77 | 78 | add_subdirectory(install) 79 | 80 | if(ZENOHCXX_ENABLE_TESTS) 81 | enable_testing() 82 | add_subdirectory(tests) 83 | else() 84 | message(STATUS "Tests are disabled.") 85 | endif() 86 | if(ZENOHCXX_ENABLE_EXAMPLES) 87 | add_subdirectory(examples) 88 | else() 89 | message(STATUS "Examples are disabled.") 90 | endif() 91 | 92 | add_subdirectory(docs) 93 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to Eclipse zenoh 2 | 3 | Thanks for your interest in this project. 4 | 5 | ## Project description 6 | 7 | Eclipse zenoh provides is a stack designed to 8 | 1. minimize network overhead, 9 | 2. support extremely constrained devices, 10 | 3. supports devices with low duty-cycle by allowing the negotiation of data exchange modes and schedules, 11 | 4. provide a rich set of abstraction for distributing, querying and storing data along the entire system, and 12 | 5. provide extremely low latency and high throughput. 13 | 14 | * https://projects.eclipse.org/projects/iot.zenoh 15 | 16 | ## Developer resources 17 | 18 | Information regarding source code management, builds, coding standards, and 19 | more. 20 | 21 | * https://projects.eclipse.org/projects/iot.zenoh/developer 22 | 23 | The project maintains the following source code repositories 24 | 25 | * https://github.com/eclipse-zenoh 26 | 27 | ## Eclipse Contributor Agreement 28 | 29 | Before your contribution can be accepted by the project team contributors must 30 | electronically sign the Eclipse Contributor Agreement (ECA). 31 | 32 | * http://www.eclipse.org/legal/ECA.php 33 | 34 | Commits that are provided by non-committers must have a Signed-off-by field in 35 | the footer indicating that the author is aware of the terms by which the 36 | contribution has been provided to the project. The non-committer must 37 | additionally have an Eclipse Foundation account and must have a signed Eclipse 38 | Contributor Agreement (ECA) on file. 39 | 40 | For more information, please see the Eclipse Committer Handbook: 41 | https://www.eclipse.org/projects/handbook/#resources-commit 42 | 43 | ## Contact 44 | 45 | Contact the project developers via the project's "dev" list. 46 | 47 | * https://accounts.eclipse.org/mailing-list/zenoh-dev 48 | 49 | Or via the Gitter channel. 50 | 51 | * https://gitter.im/atolab/zenoh 52 | -------------------------------------------------------------------------------- /CONTRIBUTORS.md: -------------------------------------------------------------------------------- 1 | # Contributors to Eclipse zenoh 2 | 3 | These are the contributors to Eclipse zenoh (the initial contributors and the contributors listed in the Git log). 4 | 5 | 6 | | GitHub username | Name | 7 | | --------------- | ---------------------------------| 8 | | kydos | Angelo Corsaro (ZettaScale) | 9 | | JEnoch | Julien Enoch (ZettaScale) | 10 | | OlivierHecart | Olivier Hécart (ZettaScale) | 11 | | gabrik | Gabriele Baldoni (ZettaScale) | 12 | | Mallets | Luca Cominardi (ZettaScale) | 13 | | IvanPaez | Ivan Paez (ZettaScale) | 14 | -------------------------------------------------------------------------------- /NOTICE.md: -------------------------------------------------------------------------------- 1 | # Notices for Eclipse zenoh-cpp 2 | 3 | This content is produced and maintained by the Eclipse zenoh project. 4 | 5 | * Project home: https://projects.eclipse.org/projects/iot.zenoh 6 | 7 | ## Trademarks 8 | 9 | Eclipse zenoh is trademark of the Eclipse Foundation. 10 | Eclipse, and the Eclipse Logo are registered trademarks of the Eclipse Foundation. 11 | 12 | ## Copyright 13 | 14 | All content is the property of the respective authors or their employers. 15 | For more information regarding authorship of content, please consult the 16 | listed source code repository logs. 17 | 18 | ## Declared Project Licenses 19 | 20 | This program and the accompanying materials are made available under the 21 | terms of the Eclipse Public License 2.0 which is available at 22 | http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 23 | which is available at https://www.apache.org/licenses/LICENSE-2.0. 24 | 25 | SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 26 | 27 | ## Source Code 28 | 29 | The project maintains the following source code repositories: 30 | 31 | * https://github.com/eclipse-zenoh/zenoh.git 32 | * https://github.com/eclipse-zenoh/zenoh-c.git 33 | * https://github.com/eclipse-zenoh/zenoh-cpp.git 34 | * https://github.com/eclipse-zenoh/zenoh-java.git 35 | * https://github.com/eclipse-zenoh/zenoh-go.git 36 | * https://github.com/eclipse-zenoh/zenoh-python.git 37 | 38 | ## Third-party Content 39 | 40 | *To be completed...* 41 | 42 | -------------------------------------------------------------------------------- /ci/scripts/build.bash: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -xeo pipefail 4 | 5 | # Repository 6 | readonly repo=${REPO:?input REPO is required} 7 | # Release number 8 | readonly version=${VERSION:-''} 9 | 10 | readonly out=$GITHUB_WORKSPACE 11 | # readonly repo_name=${repo#*/} 12 | # temporary fix - make manual 13 | readonly repo_name=zenohcpp 14 | readonly archive_lib=$out/$repo_name-$version-standalone.zip 15 | readonly archive_deb=$out/$repo_name-$version-debian.zip 16 | readonly archive_rpm=$out/$repo_name-$version-rpm.zip 17 | 18 | # Make packages into build/packages 19 | mkdir -p build 20 | cd build 21 | # For now we only build for x86_64 22 | cmake -DZENOHCXX_ZENOHC=OFF .. 23 | cpack 24 | cpack -G DEB 25 | cpack -G RPM 26 | ls -R 27 | 28 | cd "$GITHUB_WORKSPACE" 29 | mv "$(readlink -f build/packages/*.zip)" "$archive_lib" 30 | zip --verbose --junk-paths "$archive_deb" build/packages/*.deb 31 | zip --verbose --junk-paths "$archive_rpm" build/packages/*.rpm 32 | 33 | { echo "archive-lib=$(basename "$archive_lib")"; 34 | echo "archive-deb=$(basename "$archive_deb")"; 35 | echo "archive-rpm=$(basename "$archive_rpm")"; 36 | } >> "$GITHUB_OUTPUT" 37 | -------------------------------------------------------------------------------- /ci/scripts/bump-and-tag.bash: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -xeo pipefail 4 | 5 | readonly live_run=${LIVE_RUN:-false} 6 | # Release number 7 | readonly version=${VERSION:?input VERSION is required} 8 | # Branch 9 | readonly branch=${BRANCH:?input BRANCH is required} 10 | # Branch of zenoh-c 11 | readonly zenoh_c_branch=${ZENOH_C_BRANCH:-''} 12 | # Branch of zenoh-pico 13 | readonly zenoh_pico_branch=${ZENOH_PICO_BRANCH:-''} 14 | # Git actor name 15 | readonly git_user_name=${GIT_USER_NAME:?input GIT_USER_NAME is required} 16 | # Git actor email 17 | readonly git_user_email=${GIT_USER_EMAIL:?input GIT_USER_EMAIL is required} 18 | 19 | export GIT_AUTHOR_NAME=$git_user_name 20 | export GIT_AUTHOR_EMAIL=$git_user_email 21 | export GIT_COMMITTER_NAME=$git_user_name 22 | export GIT_COMMITTER_EMAIL=$git_user_email 23 | 24 | # Bump CMake project version 25 | printf '%s' "$version" > version.txt 26 | 27 | # Set branches 28 | printf '%s' "$branch" > zenoh-cpp-branch.txt 29 | if [[ $zenoh_c_branch != '' ]]; then 30 | printf '%s' "$zenoh_c_branch" > zenoh-c-branch.txt 31 | fi 32 | if [[ $zenoh_pico_branch != '' ]]; then 33 | printf '%s' "$zenoh_pico_branch" > zenoh-pico-branch.txt 34 | fi 35 | 36 | git commit version.txt -m "chore: Bump version to $version" 37 | if [[ ${live_run} ]]; then 38 | git tag --force "$version" -m "v$version" 39 | fi 40 | git log -10 41 | git show-ref --tags 42 | git push --force origin 43 | git push --force origin "$version" 44 | -------------------------------------------------------------------------------- /cmake/FindSphinx.cmake: -------------------------------------------------------------------------------- 1 | #Look for an executable called sphinx-build 2 | find_program(SPHINX_EXECUTABLE 3 | NAMES sphinx-build 4 | DOC "Path to sphinx-build executable") 5 | 6 | include(FindPackageHandleStandardArgs) 7 | 8 | #Handle standard arguments to find_package like REQUIRED and QUIET 9 | find_package_handle_standard_args(Sphinx 10 | "Failed to find sphinx-build executable" 11 | SPHINX_EXECUTABLE) -------------------------------------------------------------------------------- /docs/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) 2 | cmake_minimum_required (VERSION 3.16) 3 | project(zenohcxx_docs) 4 | else() 5 | message(STATUS "zenoh-cxx docs") 6 | include(../cmake/helpers.cmake) 7 | endif() 8 | 9 | find_package(Doxygen) 10 | if(NOT DOXYGEN_FOUND) 11 | message(STATUS "Doxygen not found, skipping docs") 12 | return() 13 | endif() 14 | 15 | find_package(Sphinx) 16 | if(NOT SPHINX_FOUND) 17 | message(STATUS "Sphinx not found, skipping docs") 18 | return() 19 | endif() 20 | 21 | # 22 | # Doxygen target 23 | # 24 | 25 | set(DOXYGEN_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/docs/doxygen) 26 | set(DOXYGEN_INDEX_FILE ${DOXYGEN_OUTPUT_DIR}/xml/index.xml) 27 | 28 | message(STATUS "Doxygen output dir: ${DOXYGEN_OUTPUT_DIR}") 29 | 30 | # Input files are configured in "Doxyfile" with relative paths: 31 | # INPUT = ../include/zenohcxx/base.hxx ../include/zenohcxx/api.hxx 32 | # So on adding/changing header files, they should be added both here and in "Doxyfile" 33 | # 34 | # CMake script will copy the include files and Doxyfile to the build directory 35 | # 36 | # We do not use here "configure_file" from "Doxygen.in" to "Doxygen" scheme, 37 | # because doxygen should work being executed from the source directory. 38 | # This is the case for readthedocs.io where doxygen is executed by "conf.py", 39 | # without CMakeLists.txt. 40 | set(DOXYGEN_INPUT_FILES 41 | ${CMAKE_CURRENT_SOURCE_DIR}/../include/zenohcxx/base.hxx 42 | ${CMAKE_CURRENT_SOURCE_DIR}/../include/zenohcxx/api.hxx 43 | ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile 44 | ) 45 | file(MAKE_DIRECTORY ${DOXYGEN_OUTPUT_DIR}) 46 | file(MAKE_DIRECTORY ${DOXYGEN_OUTPUT_DIR}/../include) 47 | 48 | add_custom_target(Doxygen) 49 | 50 | add_custom_command(TARGET Doxygen PRE_BUILD 51 | COMMAND ${CMAKE_COMMAND} -E copy_directory 52 | ${CMAKE_CURRENT_SOURCE_DIR}/../include 53 | ${DOXYGEN_OUTPUT_DIR}/../include) 54 | 55 | add_custom_command(TARGET Doxygen PRE_BUILD 56 | COMMAND ${CMAKE_COMMAND} -E copy 57 | ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile 58 | ${DOXYGEN_OUTPUT_DIR}/Doxyfile) 59 | 60 | add_custom_command(TARGET Doxygen POST_BUILD 61 | BYPRODUCTS ${DOXYGEN_INDEX_FILE} 62 | DEPENDS ${DOXYGEN_INPUT_FILES} 63 | WORKING_DIRECTORY ${DOXYGEN_OUTPUT_DIR} 64 | COMMAND ${DOXYGEN_EXECUTABLE} 65 | COMMENT "Generating docs") 66 | 67 | # 68 | # Sphinx target 69 | # 70 | 71 | set(SPHINX_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}) 72 | set(SPHINX_BUILD ${CMAKE_CURRENT_BINARY_DIR}/docs/sphinx) 73 | 74 | file (GLOB_RECURSE RST_FILES *.rst) 75 | add_custom_target(Sphinx 76 | DEPENDS conf.py ${RST_FILES} ${DOXYGEN_INDEX_FILE} 77 | COMMAND ${SPHINX_EXECUTABLE} -b html 78 | # Tell Breathe where to find the Doxygen output 79 | -Dbreathe_projects.zenohcpp=${DOXYGEN_OUTPUT_DIR}/xml 80 | ${SPHINX_SOURCE} ${SPHINX_BUILD} 81 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} 82 | COMMENT "Generating documentation with Sphinx") 83 | 84 | add_custom_target(docs DEPENDS Sphinx) 85 | 86 | add_custom_command(TARGET docs POST_BUILD 87 | COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --cyan 88 | "See read-the-docs html in ${SPHINX_BUILD}/index.html") -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | # Building the documentation 2 | 3 | ## Install tools 4 | 5 | - Ubuntu 6 | 7 | ```bash 8 | sudo apt install doxygen 9 | ``` 10 | 11 | - MacOS 12 | 13 | ```bash 14 | brew install doxygen 15 | ``` 16 | 17 | ```bash 18 | pip3 install sphinx 19 | pip3 install breathe 20 | pip3 install sphinx-rtd-theme 21 | ``` 22 | 23 | ## Build documentation locally 24 | 25 | ```bash 26 | git clone https://github.com/eclipse-zenoh/zenoh-cpp.git 27 | mkdir build 28 | cmake -Szenoh-cpp -Bbuild 29 | ``` 30 | 31 | the output should be 32 | 33 | ```raw 34 | ... 35 | -- Found Doxygen: /usr/bin/doxygen (found version "1.9.1") found components: doxygen missing components: dot 36 | -- Found Sphinx: /home/username/.local/bin/sphinx-build 37 | ... 38 | ``` 39 | 40 | or something like this. If doxygen or sphinx are not found, check first step. If everything is ok, 41 | build the documentation with this command: 42 | 43 | ```bash 44 | cmake --build build --target docs 45 | ``` 46 | 47 | If everything is ok, the output is 48 | 49 | ```raw 50 | ... 51 | See read-the-docs html in /username/build/docs/docs/sphinx/index.html 52 | ... 53 | ``` 54 | -------------------------------------------------------------------------------- /docs/api.rst: -------------------------------------------------------------------------------- 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, 13 | .. 14 | 15 | ************* 16 | API Reference 17 | ************* 18 | 19 | .. toctree:: 20 | :maxdepth: 10 21 | 22 | commons 23 | error_handling 24 | keyexpr 25 | config 26 | session 27 | scouting 28 | publish_subscribe 29 | query_reply 30 | matching 31 | channels 32 | interop 33 | shared_memory 34 | ext -------------------------------------------------------------------------------- /docs/channels.rst: -------------------------------------------------------------------------------- 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, 13 | .. 14 | 15 | Channels 16 | ======== 17 | 18 | Classes providing stream interface for Zenoh messages. 19 | 20 | .. doxygenenum:: zenoh::channels::RecvError 21 | 22 | .. doxygenclass:: zenoh::channels::FifoChannel 23 | :members: 24 | 25 | .. doxygenclass:: zenoh::channels::FifoHandler 26 | :members: 27 | :membergroups: Constructors Operators Methods 28 | 29 | .. doxygenclass:: zenoh::channels::RingChannel 30 | :members: 31 | 32 | .. doxygenclass:: zenoh::channels::RingHandler 33 | :members: 34 | :membergroups: Constructors Operators Methods 35 | -------------------------------------------------------------------------------- /docs/commons.rst: -------------------------------------------------------------------------------- 1 | .. 2 | .. Copyright (c) 2024 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, 13 | .. 14 | 15 | Commonly used types 16 | =================== 17 | 18 | Enums 19 | ----- 20 | 21 | Enum types are C++ - style typedefs for corrresponding enums of `zenoh-c`_ / `zenoh-pico`_ C API. 22 | 23 | .. doxygentypedef:: zenoh::SampleKind 24 | .. doxygentypedef:: zenoh::ConsolidationMode 25 | 26 | .. doxygentypedef:: zenoh::Reliability 27 | 28 | .. doxygentypedef:: zenoh::CongestionControl 29 | 30 | .. doxygentypedef:: zenoh::Priority 31 | 32 | .. doxygentypedef:: zenoh::QueryTarget 33 | 34 | .. doxygentypedef:: zenoh::WhatAmI 35 | 36 | .. doxygentypedef:: zenoh::Locality 37 | 38 | .. doxygenfunction:: whatami_as_str 39 | 40 | .. _zenoh-c: https://zenoh-c.readthedocs.io 41 | .. _zenoh-pico: https://zenoh-pico.readthedocs.io 42 | 43 | Source Info 44 | ----------- 45 | 46 | .. doxygenclass:: zenoh::Id 47 | :members: 48 | :membergroups: Constructors Operators Methods 49 | 50 | .. doxygenfunction:: zenoh::operator<<(std::ostream &os, const Id &id) 51 | 52 | .. doxygenclass:: zenoh::EntityGlobalId 53 | :members: 54 | :membergroups: Constructors Operators Methods 55 | 56 | .. doxygenclass:: zenoh::SourceInfo 57 | :members: 58 | :membergroups: Constructors Operators Methods 59 | 60 | Timestamp 61 | --------- 62 | .. doxygenclass:: zenoh::Timestamp 63 | :members: 64 | :membergroups: Constructors Operators Methods 65 | 66 | 67 | Encoding 68 | -------- 69 | .. doxygenclass:: zenoh::Encoding 70 | :members: 71 | :membergroups: Constructors Operators Methods 72 | 73 | Sample 74 | ------ 75 | .. doxygenclass:: zenoh::Sample 76 | :members: 77 | :membergroups: Constructors Operators Methods 78 | 79 | Bytes 80 | ------ 81 | .. doxygenclass:: zenoh::Bytes 82 | :members: 83 | :membergroups: Constructors Operators Methods 84 | 85 | Logging 86 | ------- 87 | 88 | .. doxygenfunction:: try_init_log_from_env 89 | .. doxygenfunction:: init_log_from_env_or 90 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 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, 13 | # 14 | 15 | # -- Run doxygen if executing on readthedocs ------------------------------- 16 | # see https://breathe.readthedocs.io/en/latest/readthedocs.html 17 | import subprocess, os 18 | read_the_docs_build = os.environ.get('READTHEDOCS', None) == 'True' 19 | if read_the_docs_build: 20 | subprocess.call('doxygen', shell=True) 21 | 22 | # -- Project information ----------------------------------------------------- 23 | project = 'zenoh-cpp' 24 | copyright = '2017, 2023 ZettaScale Technology' 25 | author = 'ZettaScale Zenoh team' 26 | # Extract the release number from the version.txt file 27 | with open("../version.txt", "rt") as f: 28 | release = f.read() 29 | 30 | # -- General configuration --------------------------------------------------- 31 | master_doc = 'index' 32 | html_theme = 'sphinx_rtd_theme' 33 | cpp_index_common_prefix = ['z::', 'zenoh::', 'zenohcxx::', 'zenohc::', 'zenohpico::'] 34 | 35 | extensions = [ "breathe" ] 36 | 37 | breathe_default_project = "zenohcpp" 38 | breathe_projects = {"zenohcpp": "xml"} 39 | breathe_order_parameters_first = True 40 | -------------------------------------------------------------------------------- /docs/config.rst: -------------------------------------------------------------------------------- 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, 13 | .. 14 | 15 | Configuration 16 | ============= 17 | 18 | .. doxygenclass:: zenoh::Config 19 | :members: 20 | :membergroups: Constructors Operators Methods -------------------------------------------------------------------------------- /docs/error_handling.rst: -------------------------------------------------------------------------------- 1 | .. 2 | .. Copyright (c) 2024 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, 13 | .. 14 | 15 | Error Handling 16 | ============== 17 | 18 | All failable Zenoh methods accept pointer to zenoh::ZResult as an optional argument. 19 | If it is not provided (or set to ``nullptr``) a ``zenoh::ZException`` will be thrown in case of failure. 20 | Otherwise a error code will be written to provided pointer and no exception will be thrown from Zenoh side. 21 | If corresponding method is expected to return or consume (via ``std::move``) any objects, they will be reset to 22 | gravestone state (i.e. None of the functions or methods will work with the object in this state, except 23 | explicit conversion to ``bool``, which will return false). 24 | 25 | .. doxygenclass:: zenoh::ZException 26 | :members: 27 | :membergroups: Constructors Operators Methods 28 | 29 | .. doxygentypedef:: zenoh::ZResult 30 | -------------------------------------------------------------------------------- /docs/examples.rst: -------------------------------------------------------------------------------- 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, 13 | .. 14 | 15 | ******** 16 | Examples 17 | ******** 18 | 19 | .. toctree:: 20 | :maxdepth: 10 21 | 22 | session_ex 23 | pubsub 24 | queryable -------------------------------------------------------------------------------- /docs/ext.rst: -------------------------------------------------------------------------------- 1 | .. 2 | .. Copyright (c) 2024 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, 13 | .. 14 | 15 | Extensions 16 | ========== 17 | Extra functionality, which is not a part of core Zenoh API. 18 | 19 | Serialization / Deserialization 20 | ------------------------------- 21 | Support for data serialization and deserialization. 22 | 23 | .. doxygenclass:: zenoh::ext::Serializer 24 | :members: 25 | :membergroups: Constructors Operators Methods 26 | 27 | .. doxygenclass:: zenoh::ext::Deserializer 28 | :members: 29 | :membergroups: Constructors Operators Methods 30 | 31 | .. doxygenfunction:: zenoh::ext::serialize 32 | .. doxygenfunction:: zenoh::ext::deserialize 33 | 34 | 35 | Session Extension 36 | ----------------- 37 | Extra Zenoh entities. 38 | 39 | .. doxygenclass:: zenoh::ext::SessionExt 40 | :members: 41 | :membergroups: Constructors Operators Methods Fields 42 | 43 | .. doxygenclass:: zenoh::ext::PublicationCache 44 | :members: 45 | :membergroups: Constructors Operators Methods Fields 46 | 47 | .. doxygenclass:: zenoh::ext::QueryingSubscriber 48 | :members: 49 | :membergroups: Constructors Operators Methods Fields 50 | 51 | .. doxygenclass:: zenoh::ext::AdvancedPublisher 52 | :members: 53 | :membergroups: Constructors Operators Methods Fields 54 | 55 | .. doxygenclass:: zenoh::ext::AdvancedSubscriber 56 | :members: 57 | :membergroups: Constructors Operators Methods Fields 58 | 59 | .. doxygenstruct:: zenoh::ext::Miss 60 | :members: 61 | :membergroups: Constructors Operators Methods Fields 62 | 63 | .. doxygenclass:: zenoh::ext::SampleMissListener 64 | :members: 65 | :membergroups: Constructors Operators Methods -------------------------------------------------------------------------------- /docs/genindex.rst: -------------------------------------------------------------------------------- 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, 13 | .. 14 | 15 | Index 16 | ===== 17 | 18 | .. Dummy file, which is actually replaced with autogenerated genidex.html 19 | .. This hack found here: 20 | .. https://stackoverflow.com/questions/40556423/how-can-i-link-the-generated-index-page-in-readthedocs-navigation-bar 21 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 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, 13 | .. 14 | 15 | ********* 16 | zenoh-cpp 17 | ********* 18 | 19 | The *zenoh-cpp* library provides a client C++ API for the zenoh network protocol. 20 | 21 | An introduction to Zenoh and its concepts is available on `zenoh.io`_. Since the zenoh-cpp is a header-only wrapper 22 | library over the `zenoh-c`_ and `zenoh-pico`_ C libraries, it can be useful to reference the documentation of these libraries as well. 23 | The zenoh-c library is C interface to main implementation of zenoh in Rust (see `zenoh`_ Rust API api documentation for more information). 24 | 25 | .. toctree:: 26 | :maxdepth: 10 27 | 28 | examples 29 | api 30 | genindex 31 | 32 | .. _zenoh.io: https://zenoh.io 33 | .. _zenoh: https://docs.rs/zenoh 34 | .. _zenoh-c: https://zenoh-c.readthedocs.io 35 | .. _zenoh-pico: https://zenoh-pico.readthedocs.io -------------------------------------------------------------------------------- /docs/interop.rst: -------------------------------------------------------------------------------- 1 | .. 2 | .. Copyright (c) 2024 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, 13 | .. 14 | 15 | Interoperability with zenoh-c / zenoh-pico 16 | ========================================== 17 | This is the list of the functions that can be use for interoperability between zenoh-c/zenoh-pico and 18 | zenoh-cpp. These functions essentially perform conversion of c-structs into c++ classes and back. They should be used with 19 | care, and it is up to the user to ensure that all necessary invariants uphold. 20 | 21 | .. doxygennamespace:: zenoh::interop 22 | :content-only: 23 | -------------------------------------------------------------------------------- /docs/keyexpr.rst: -------------------------------------------------------------------------------- 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, 13 | .. 14 | 15 | Key Expression 16 | =============== 17 | 18 | .. doxygenclass:: zenoh::KeyExpr 19 | :members: 20 | :membergroups: Constructors Operators Methods 21 | -------------------------------------------------------------------------------- /docs/matching.rst: -------------------------------------------------------------------------------- 1 | Matching 2 | ======== 3 | Classes related to getting information about matching Zenoh entities. 4 | 5 | .. doxygenstruct:: zenoh::MatchingStatus 6 | :members: 7 | :membergroups: Constructors Operators Methods Fields 8 | 9 | .. doxygenclass:: zenoh::MatchingListener 10 | :members: 11 | :membergroups: Constructors Operators Methods -------------------------------------------------------------------------------- /docs/publish_subscribe.rst: -------------------------------------------------------------------------------- 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, 13 | .. 14 | 15 | Publish-Subscribe 16 | ================= 17 | Classes related to publish-subscribe pattern. 18 | 19 | .. doxygenclass:: zenoh::Publisher 20 | :members: 21 | :membergroups: Constructors Operators Methods Fields 22 | 23 | .. doxygenclass:: zenoh::Subscriber 24 | :members: 25 | :membergroups: Constructors Operators Methods -------------------------------------------------------------------------------- /docs/pubsub.rst: -------------------------------------------------------------------------------- 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, 13 | .. 14 | 15 | Publish / Subscribe 16 | =================== 17 | 18 | The publish / subscribe pattern is implemented with classes :cpp:class:`zenoh::Publisher` 19 | and :cpp:class:`zenoh::Subscriber`. 20 | 21 | Publisher example: 22 | 23 | .. code-block:: c++ 24 | 25 | #include "zenoh.hxx" 26 | 27 | using namespace zenoh; 28 | 29 | int main(int argc, char **argv) { 30 | Config config = Config::create_default(); 31 | auto session = Session::open(std::move(config)); 32 | // Publish without creating a Publisher object 33 | session.put(KeyExpr("demo/example/simple"), Bytes("Simple from session.put!")); 34 | 35 | // Publish from a Publisher object 36 | auto publisher = session.declare_publisher(KeyExpr("demo/example/simple")); 37 | publisher.put("Simple from publisher.put!"); 38 | } 39 | 40 | Subscriber example: 41 | 42 | .. code-block:: c++ 43 | 44 | #include "zenoh.hxx" 45 | #include 46 | 47 | using namespace zenoh; 48 | 49 | int main(int argc, char **argv) { 50 | Config config = Config::create_default(); 51 | auto session = Session::open(std::move(config)); 52 | auto subscriber = session.declare_subscriber( 53 | KeyExpr("demo/example/simple"), 54 | [](const Sample& sample) { 55 | std::cout << "Received: " << sample.get_payload().as_string() << std::endl; 56 | }, 57 | closures::none 58 | ); 59 | // Wait for a key press to exit 60 | char c = getchar(); 61 | } 62 | 63 | Subscriber example with non-blocking stream interface: 64 | 65 | .. code-block:: c++ 66 | 67 | #include "zenoh.hxx" 68 | #include 69 | #include 70 | #include 71 | 72 | using namespace zenoh; 73 | using namespace std::chrono_literals; 74 | 75 | int main(int argc, char **argv) { 76 | Config config = Config::create_default(); 77 | auto session = Session::open(std::move(config)); 78 | auto subscriber = session.declare_subscriber( 79 | KeyExpr("demo/example/simple"), 80 | channels::FifoChannel(16) // use FIFO buffer to store unprocessed messages 81 | ); 82 | while (true) { 83 | auto res = subscriber.handler().try_recv(); 84 | if (std::holds_alternative(res)) { 85 | std::cout << "Received: " << std::get(res).get_payload().as_string() << std::endl; 86 | } else if (std::get(res) == channels::RecvError::Z_NODATA) { 87 | std::this_thread::sleep_for(1s); // do some other work 88 | } else { 89 | break; // channel is closed 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /docs/query_reply.rst: -------------------------------------------------------------------------------- 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, 13 | .. 14 | 15 | Query-Reply 16 | =========== 17 | Classes related to query-reply pattern. 18 | 19 | .. doxygenclass:: zenoh::Querier 20 | :members: 21 | :membergroups: Constructors Operators Methods Fields 22 | 23 | .. doxygenclass:: zenoh::Queryable 24 | :members: 25 | :membergroups: Constructors Operators Methods 26 | 27 | .. doxygenclass:: zenoh::Query 28 | :members: 29 | :membergroups: Constructors Operators Methods 30 | 31 | .. doxygenclass:: zenoh::QueryConsolidation 32 | :members: 33 | :membergroups: Constructors Operators Methods 34 | 35 | .. doxygenclass:: zenoh::Reply 36 | :members: 37 | :membergroups: Constructors Operators Methods 38 | 39 | .. doxygenclass:: zenoh::ReplyError 40 | :members: 41 | :membergroups: Constructors Operators Methods -------------------------------------------------------------------------------- /docs/requirements.txt: -------------------------------------------------------------------------------- 1 | sphinx==7.2.6 2 | sphinx_rtd_theme 3 | breathe 4 | -------------------------------------------------------------------------------- /docs/scouting.rst: -------------------------------------------------------------------------------- 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, 13 | .. 14 | 15 | Scouting 16 | ======== 17 | 18 | .. doxygenfunction:: zenoh::scout 19 | 20 | .. doxygenclass:: zenoh::Hello 21 | :members: 22 | :membergroups: Constructors Operators Methods 23 | 24 | .. doxygenstruct:: zenoh::ScoutOptions 25 | :members: -------------------------------------------------------------------------------- /docs/session.rst: -------------------------------------------------------------------------------- 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, 13 | .. 14 | 15 | Session Management 16 | ================== 17 | 18 | The :cpp:class:`zenoh::Session` is the main zenoh object. The instance of the session reprsents a single 19 | zenoh node in the network. 20 | 21 | Session 22 | ------- 23 | 24 | .. doxygenclass:: zenoh::Session 25 | :members: 26 | :membergroups: Constructors Operators Methods Fields 27 | -------------------------------------------------------------------------------- /docs/session_ex.rst: -------------------------------------------------------------------------------- 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, 13 | .. 14 | 15 | Session 16 | ======= 17 | 18 | The zenoh session is created using the :cpp:func:`zenoh::Session::open` function, 19 | consuming the configuration object :cpp:class:`zenoh::Config`. 20 | Then a string is published on "demo/example/simple" key expression. 21 | 22 | .. code-block:: c++ 23 | 24 | #include "zenoh.hxx" 25 | using namespace zenoh; 26 | 27 | int main(int argc, char **argv) { 28 | try { 29 | Config config = Config::create_default(); 30 | auto session = Session::open(std::move(config)); 31 | session.put(KeyExpr("demo/example/simple"), "Simple!"); 32 | } catch (ZException e) { 33 | std::cout << "Received an error :" << e.what() << "\n"; 34 | } 35 | } -------------------------------------------------------------------------------- /docs/shared_memory.rst: -------------------------------------------------------------------------------- 1 | .. 2 | .. SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 3 | .. 4 | .. Contributors: 5 | .. ZettaScale Zenoh Team, 6 | .. 7 | 8 | Shared Memory 9 | ============= 10 | .. doxygenclass:: zenoh::ZShm 11 | :members: 12 | :membergroups: Constructors Operators Methods 13 | 14 | .. doxygenclass:: zenoh::ZShmMut 15 | :members: 16 | :membergroups: Constructors Operators Methods 17 | 18 | .. doxygenclass:: zenoh::CppShmClient 19 | :members: 20 | :membergroups: Constructors Operators Methods 21 | 22 | .. doxygenclass:: zenoh::ShmClient 23 | :members: 24 | :membergroups: Constructors Operators Methods 25 | 26 | .. doxygenclass:: zenoh::CppShmSegment 27 | :members: 28 | :membergroups: Constructors Operators Methods 29 | 30 | .. doxygenclass:: zenoh::ShmClientStorage 31 | :members: 32 | :membergroups: Constructors Operators Methods 33 | 34 | .. doxygenclass:: zenoh::PosixShmClient 35 | :members: 36 | :membergroups: Constructors Operators Methods 37 | 38 | .. doxygenclass:: zenoh::PosixShmProvider 39 | :members: 40 | :membergroups: Constructors Operators Methods 41 | 42 | .. doxygenclass:: zenoh::CppShmProvider 43 | :members: 44 | :membergroups: Constructors Operators Methods 45 | 46 | .. doxygenclass:: zenoh::AllocLayout 47 | :members: 48 | :membergroups: Constructors Operators Methods 49 | 50 | .. doxygenclass:: zenoh::MemoryLayout 51 | :members: 52 | :membergroups: Constructors Operators Methods 53 | 54 | .. doxygenclass:: zenoh::ChunkAllocResult 55 | :members: 56 | :membergroups: Constructors Operators Methods 57 | 58 | 59 | .. doxygentypedef:: zenoh::ProtocolId 60 | .. doxygentypedef:: zenoh::SegmentId 61 | .. doxygentypedef:: zenoh::ChunkId 62 | 63 | .. doxygentypedef:: zenoh::ChunkDescriptor 64 | .. doxygentypedef:: zenoh::AllocatedChunk 65 | 66 | .. doxygentypedef:: zenoh::AllocError 67 | .. doxygentypedef:: zenoh::LayoutError 68 | .. doxygentypedef:: zenoh::AllocAlignment 69 | 70 | .. doxygentypedef:: zenoh::BufAllocResult 71 | .. doxygentypedef:: zenoh::BufLayoutAllocResult 72 | 73 | .. doxygenfunction:: zenoh::cleanup_orphaned_shm_segments 74 | 75 | 76 | -------------------------------------------------------------------------------- /examples/simple/Readme.md: -------------------------------------------------------------------------------- 1 | # Simple CMake projects demonstrating usage of zenoh-cpp 2 | 3 | * [universal](universal) - Use same C++ source for both zenoh-c and zenoh-pico 4 | * [zenohc](zenohc) - Explicitly use Zenoh-C library 5 | * [zenohpico](zenohpico) - Explicitly use Zenoh-Pico library 6 | 7 | -------------------------------------------------------------------------------- /examples/simple/universal/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16) 2 | project(zenohcxx_examples LANGUAGES C CXX) 3 | 4 | set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake" ${CMAKE_MODULE_PATH}) 5 | include(helpers) 6 | 7 | if(CMAKE_BUILD_TYPE STREQUAL "") 8 | set(CMAKE_BUILD_TYPE Release) 9 | endif() 10 | 11 | find_package(zenohpico REQUIRED) 12 | find_package(zenohc REQUIRED) 13 | find_package(zenohcxx REQUIRED) 14 | 15 | add_executable(zp_simple z_simple.cxx) 16 | target_link_libraries(zp_simple PRIVATE zenohcxx::zenohpico) 17 | set_property(TARGET zp_simple PROPERTY LANGUAGE CXX) 18 | set_property(TARGET zp_simple PROPERTY CXX_STANDARD 17) 19 | copy_dlls(zp_simple) 20 | 21 | add_executable(zc_simple z_simple.cxx) 22 | target_link_libraries(zc_simple PRIVATE zenohcxx::zenohc) 23 | set_property(TARGET zc_simple PROPERTY LANGUAGE CXX) 24 | set_property(TARGET zc_simple PROPERTY CXX_STANDARD 17) 25 | copy_dlls(zc_simple) -------------------------------------------------------------------------------- /examples/simple/universal/z_simple.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | // 14 | 15 | #include 16 | 17 | #include "zenoh.hxx" 18 | using namespace zenoh; 19 | 20 | int main(int, char **) { 21 | #ifdef ZENOHCXX_ZENOHC 22 | init_log_from_env_or("error"); 23 | #endif 24 | Config config = Config::create_default(); 25 | auto session = Session::open(std::move(config)); 26 | #if (ZENOHCXX_ZENOHC) || \ 27 | Z_FEATURE_PUBLICATION == \ 28 | 1 // check if zenoh-pico is compiled with publication support (always included for zenoh-c) 29 | session.put("demo/example/simple", "Simple!"); 30 | #endif 31 | } 32 | -------------------------------------------------------------------------------- /examples/simple/zenohc/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16) 2 | project(zenohcxx_examples LANGUAGES C CXX) 3 | set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake" ${CMAKE_MODULE_PATH}) 4 | include(helpers) 5 | 6 | if(CMAKE_BUILD_TYPE STREQUAL "") 7 | set(CMAKE_BUILD_TYPE Release) 8 | endif() 9 | 10 | find_package(zenohc REQUIRED) 11 | find_package(zenohcxx REQUIRED) 12 | 13 | add_executable(zc_simple zc_simple.cxx) 14 | target_link_libraries(zc_simple PRIVATE zenohcxx::zenohc) 15 | set_property(TARGET zc_simple PROPERTY LANGUAGE CXX) 16 | set_property(TARGET zc_simple PROPERTY CXX_STANDARD 17) 17 | copy_dlls(zc_simple) -------------------------------------------------------------------------------- /examples/simple/zenohc/zc_simple.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | // 14 | 15 | #include 16 | 17 | #include "zenoh.hxx" 18 | using namespace zenoh; 19 | 20 | int main(int, char **) { 21 | init_log_from_env_or("error"); 22 | Config config = Config::create_default(); 23 | auto session = Session::open(std::move(config)); 24 | session.put("demo/example/simple", "Simple!"); 25 | } 26 | -------------------------------------------------------------------------------- /examples/simple/zenohpico/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16) 2 | project(zenohcxx_examples LANGUAGES C CXX) 3 | 4 | set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake" ${CMAKE_MODULE_PATH}) 5 | include(helpers) 6 | 7 | if(CMAKE_BUILD_TYPE STREQUAL "") 8 | set(CMAKE_BUILD_TYPE Release) 9 | endif() 10 | 11 | find_package(zenohpico REQUIRED) 12 | find_package(zenohcxx REQUIRED) 13 | 14 | add_executable(zp_simple zp_simple.cxx) 15 | target_link_libraries(zp_simple PRIVATE zenohcxx::zenohpico) 16 | set_property(TARGET zp_simple PROPERTY LANGUAGE CXX) 17 | set_property(TARGET zp_simple PROPERTY CXX_STANDARD 17) 18 | copy_dlls(zp_simple) 19 | -------------------------------------------------------------------------------- /examples/simple/zenohpico/zp_simple.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | // 14 | #include 15 | 16 | #include "zenoh.hxx" 17 | using namespace zenoh; 18 | 19 | int main(int, char **) { 20 | Config config = Config::create_default(); 21 | auto session = Session::open(std::move(config)); 22 | #if Z_FEATURE_PUBLICATION == 1 // check if zenoh-pico is compliled with publication support 23 | session.put("demo/example/simple", "Simple!"); 24 | #endif 25 | } 26 | -------------------------------------------------------------------------------- /examples/universal/proto/entity.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | message Entity { 4 | uint32 id = 1; 5 | string name = 2; 6 | } -------------------------------------------------------------------------------- /examples/universal/z_delete.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | // 14 | #include 15 | 16 | #ifdef ZENOHCXX_ZENOHC 17 | const char *default_keyexpr = "demo/example/zenoh-cpp-zenoh-c-put"; 18 | #elif ZENOHCXX_ZENOHPICO 19 | const char *default_keyexpr = "demo/example/zenoh-cpp-zenoh-pico-put"; 20 | #endif 21 | 22 | #include "../getargs.hxx" 23 | #include "stdio.h" 24 | #include "zenoh.hxx" 25 | using namespace zenoh; 26 | 27 | int _main(int argc, char **argv) { 28 | auto &&[config, args] = 29 | ConfigCliArgParser(argc, argv) 30 | .named_value({"k", "key"}, "KEY_EXPRESSION", "The key expression to write to", default_keyexpr) 31 | .run(); 32 | 33 | std::string_view keyexpr = args.value("key"); 34 | 35 | std::cout << "Opening session...\n"; 36 | auto session = Session::open(std::move(config)); 37 | 38 | std::cout << "Deleting resources matching '" << keyexpr << "'...\n"; 39 | session.delete_resource(KeyExpr(keyexpr)); 40 | return 0; 41 | } 42 | 43 | int main(int argc, char **argv) { 44 | try { 45 | #ifdef ZENOHCXX_ZENOHC 46 | init_log_from_env_or("error"); 47 | #endif 48 | _main(argc, argv); 49 | } catch (ZException e) { 50 | std::cout << "Received an error :" << e.what() << "\n"; 51 | } 52 | } -------------------------------------------------------------------------------- /examples/universal/z_get.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "../getargs.hxx" 23 | #include "zenoh.hxx" 24 | using namespace zenoh; 25 | 26 | int _main(int argc, char **argv) { 27 | auto &&[config, args] = 28 | ConfigCliArgParser(argc, argv) 29 | .named_value({"s", "selector"}, "SELECTOR", "Query selector (string)", "demo/example/**") 30 | .named_value({"p", "payload"}, "PAYLOAD", "Query payload (string)", "") 31 | .named_value({"t", "target"}, "TARGET", "Query target (BEST_MATCHING | ALL | ALL_COMPLETE)", 32 | "BEST_MATCHING") 33 | .named_value({"o", "timeout"}, "TIMEOUT", "Timeout in ms (number)", "10000") 34 | .run(); 35 | 36 | uint64_t timeout_ms = std::atoi(args.value("timeout").data()); 37 | QueryTarget query_target = parse_query_target(args.value("target")); 38 | Selector selector = parse_selector(args.value("selector")); 39 | auto payload = args.value("payload"); 40 | 41 | std::cout << "Opening session...\n"; 42 | auto session = Session::open(std::move(config)); 43 | 44 | std::cout << "Sending Query '" << args.value("selector") << "'...\n"; 45 | 46 | std::mutex m; 47 | std::condition_variable done_signal; 48 | bool done = false; 49 | 50 | auto on_reply = [](const Reply &reply) { 51 | if (reply.is_ok()) { 52 | const auto &sample = reply.get_ok(); 53 | std::cout << "Received ('" << sample.get_keyexpr().as_string_view() << "' : '" 54 | << sample.get_payload().as_string() << "')\n"; 55 | } else { 56 | std::cout << "Received an error :" << reply.get_err().get_payload().as_string() << "\n"; 57 | } 58 | }; 59 | 60 | auto on_done = [&m, &done, &done_signal]() { 61 | std::lock_guard lock(m); 62 | done = true; 63 | done_signal.notify_all(); 64 | }; 65 | 66 | Session::GetOptions options; 67 | options.target = query_target; 68 | if (!payload.empty()) { 69 | options.payload = payload; 70 | } 71 | options.timeout_ms = timeout_ms; 72 | session.get(selector.key_expr, selector.parameters, on_reply, on_done, std::move(options)); 73 | 74 | std::unique_lock lock(m); 75 | done_signal.wait(lock, [&done] { return done; }); 76 | 77 | return 0; 78 | } 79 | 80 | int main(int argc, char **argv) { 81 | try { 82 | #ifdef ZENOHCXX_ZENOHC 83 | init_log_from_env_or("error"); 84 | #endif 85 | _main(argc, argv); 86 | } catch (ZException e) { 87 | std::cout << "Received an error :" << e.what() << "\n"; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /examples/universal/z_get_channel.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | 20 | #include "../getargs.hxx" 21 | #include "zenoh.hxx" 22 | using namespace zenoh; 23 | 24 | int _main(int argc, char **argv) { 25 | auto &&[config, args] = 26 | ConfigCliArgParser(argc, argv) 27 | .named_value({"s", "selector"}, "SELECTOR", "Query selector (string)", "demo/example/**") 28 | .named_value({"p", "payload"}, "PAYLOAD", "Query payload (string)", "") 29 | .named_value({"t", "target"}, "TARGET", "Query target (BEST_MATCHING | ALL | ALL_COMPLETE)", 30 | "BEST_MATCHING") 31 | .named_value({"o", "timeout"}, "TIMEOUT", "Timeout in ms (number)", "10000") 32 | .run(); 33 | 34 | uint64_t timeout_ms = std::atoi(args.value("timeout").data()); 35 | QueryTarget query_target = parse_query_target(args.value("target")); 36 | Selector selector = parse_selector(args.value("selector")); 37 | auto payload = args.value("payload"); 38 | 39 | std::cout << "Opening session...\n"; 40 | auto session = Session::open(std::move(config)); 41 | 42 | std::cout << "Sending Query '" << args.value("selector") << "'...\n"; 43 | 44 | Session::GetOptions options; 45 | options.target = query_target; 46 | if (!payload.empty()) { 47 | options.payload = payload; 48 | } 49 | options.timeout_ms = timeout_ms; 50 | auto replies = session.get(selector.key_expr, selector.parameters, channels::FifoChannel(16), std::move(options)); 51 | 52 | for (auto res = replies.recv(); std::holds_alternative(res); res = replies.recv()) { 53 | const auto &sample = std::get(res).get_ok(); 54 | std::cout << "Received ('" << sample.get_keyexpr().as_string_view() << "' : '" 55 | << sample.get_payload().as_string() << "')\n"; 56 | } 57 | 58 | return 0; 59 | } 60 | 61 | int main(int argc, char **argv) { 62 | try { 63 | #ifdef ZENOHCXX_ZENOHC 64 | init_log_from_env_or("error"); 65 | #endif 66 | _main(argc, argv); 67 | } catch (ZException e) { 68 | std::cout << "Received an error :" << e.what() << "\n"; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /examples/universal/z_get_channel_non_blocking.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "../getargs.hxx" 23 | #include "zenoh.hxx" 24 | using namespace zenoh; 25 | using namespace std::chrono_literals; 26 | 27 | int _main(int argc, char **argv) { 28 | auto &&[config, args] = 29 | ConfigCliArgParser(argc, argv) 30 | .named_value({"s", "selector"}, "SELECTOR", "Query selector (string)", "demo/example/**") 31 | .named_value({"p", "payload"}, "PAYLOAD", "Query payload (string)", "") 32 | .named_value({"t", "target"}, "TARGET", "Query target (BEST_MATCHING | ALL | ALL_COMPLETE)", 33 | "BEST_MATCHING") 34 | .named_value({"o", "timeout"}, "TIMEOUT", "Timeout in ms (number)", "10000") 35 | .run(); 36 | 37 | uint64_t timeout_ms = std::atoi(args.value("timeout").data()); 38 | QueryTarget query_target = parse_query_target(args.value("target")); 39 | Selector selector = parse_selector(args.value("selector")); 40 | auto payload = args.value("payload"); 41 | 42 | std::cout << "Opening session...\n"; 43 | auto session = Session::open(std::move(config)); 44 | 45 | std::cout << "Sending Query '" << args.value("selector") << "'...\n"; 46 | 47 | Session::GetOptions options; 48 | options.target = query_target; 49 | if (!payload.empty()) { 50 | options.payload = payload; 51 | } 52 | options.timeout_ms = timeout_ms; 53 | auto replies = session.get(selector.key_expr, selector.parameters, channels::FifoChannel(16), std::move(options)); 54 | 55 | while (true) { 56 | auto res = replies.try_recv(); 57 | if (std::holds_alternative(res)) { 58 | if (std::get(res) == channels::RecvError::Z_NODATA) { 59 | std::cout << "."; 60 | std::this_thread::sleep_for(1s); 61 | continue; 62 | } else { // channel is closed - no more replies will be received 63 | break; 64 | } 65 | } 66 | const auto &sample = std::get(res).get_ok(); 67 | std::cout << "Received ('" << sample.get_keyexpr().as_string_view() << "' : '" 68 | << sample.get_payload().as_string() << "')\n"; 69 | } 70 | std::cout << std::endl; 71 | 72 | return 0; 73 | } 74 | 75 | int main(int argc, char **argv) { 76 | try { 77 | #ifdef ZENOHCXX_ZENOHC 78 | init_log_from_env_or("error"); 79 | #endif 80 | _main(argc, argv); 81 | } catch (ZException e) { 82 | std::cout << "Received an error :" << e.what() << "\n"; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /examples/universal/z_get_liveliness.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2024 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, 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | 20 | #include "../getargs.hxx" 21 | #include "zenoh.hxx" 22 | using namespace zenoh; 23 | 24 | int _main(int argc, char **argv) { 25 | auto &&[config, args] = 26 | ConfigCliArgParser(argc, argv) 27 | .named_value({"k", "key"}, "KEY_EXPRESSION", "Key expression to query (string)", "group1/**") 28 | .named_value({"o", "timeout"}, "TIMEOUT", "Timeout in ms (number)", "10000") 29 | .run(); 30 | 31 | uint64_t timeout_ms = std::atoi(args.value("timeout").data()); 32 | 33 | KeyExpr keyexpr(args.value("key")); 34 | 35 | std::cout << "Opening session...\n"; 36 | auto session = Session::open(std::move(config)); 37 | 38 | std::cout << "Sending Liveliness Query '" << keyexpr.as_string_view() << "'...\n"; 39 | Session::LivelinessGetOptions opts; 40 | opts.timeout_ms = timeout_ms; 41 | auto replies = session.liveliness_get(keyexpr, channels::FifoChannel(16), std::move(opts)); 42 | 43 | for (auto res = replies.recv(); std::holds_alternative(res); res = replies.recv()) { 44 | const Reply &reply = std::get(res); 45 | if (reply.is_ok()) { 46 | const auto &sample = reply.get_ok(); 47 | std::cout << "Alive token ('" << sample.get_keyexpr().as_string_view() << "')\n"; 48 | } else { 49 | std::cout << "Received an error\n"; 50 | } 51 | } 52 | 53 | return 0; 54 | } 55 | 56 | int main(int argc, char **argv) { 57 | try { 58 | #ifdef ZENOHCXX_ZENOHC 59 | init_log_from_env_or("error"); 60 | #endif 61 | _main(argc, argv); 62 | } catch (ZException e) { 63 | std::cout << "Received an error :" << e.what() << "\n"; 64 | } 65 | } -------------------------------------------------------------------------------- /examples/universal/z_info.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | #include 14 | 15 | #include 16 | 17 | #include "../getargs.hxx" 18 | #include "zenoh.hxx" 19 | using namespace zenoh; 20 | 21 | int _main(int argc, char** argv) { 22 | const char* value = "Get from C++"; 23 | auto&& [config, args] = ConfigCliArgParser(argc, argv).run(); 24 | 25 | std::cout << "Opening session...\n"; 26 | auto session = Session::open(std::move(config)); 27 | 28 | #if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API) 29 | std::cout << "own id: " << session.get_zid() << std::endl; 30 | 31 | std::cout << "routers ids:\n"; 32 | for (const auto zid : session.get_routers_z_id()) { 33 | std::cout << zid << "\n"; 34 | } 35 | 36 | std::cout << "peers ids:\n"; 37 | for (const auto zid : session.get_peers_z_id()) { 38 | std::cout << zid << "\n"; 39 | } 40 | #endif 41 | return 0; 42 | } 43 | 44 | int main(int argc, char** argv) { 45 | try { 46 | #ifdef ZENOHCXX_ZENOHC 47 | init_log_from_env_or("error"); 48 | #endif 49 | return _main(argc, argv); 50 | } catch (ZException e) { 51 | std::cout << "Received an error :" << e.what() << "\n"; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /examples/universal/z_liveliness.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "../getargs.hxx" 23 | #include "zenoh.hxx" 24 | 25 | using namespace zenoh; 26 | using namespace std::chrono_literals; 27 | 28 | const char *default_keyexpr = "group1/zenoh-cpp-c"; 29 | 30 | int _main(int argc, char **argv) { 31 | auto &&[config, args] = 32 | ConfigCliArgParser(argc, argv) 33 | .named_value({"k", "key"}, "KEY_EXPRESSION", "Key expression of the liveliness (string)", default_keyexpr) 34 | .run(); 35 | auto keyexpr = args.value("key"); 36 | 37 | std::cout << "Opening session...\n"; 38 | auto session = Session::open(std::move(config)); 39 | 40 | std::cout << "Declaring liveliness token on '" << keyexpr << "'...\n"; 41 | auto token = session.liveliness_declare_token(KeyExpr(keyexpr)); 42 | 43 | std::cout << "Press CTRL-C to undeclare liveliness token and quit..." << std::endl; 44 | while (true) { 45 | std::this_thread::sleep_for(1s); 46 | } 47 | 48 | return 0; 49 | } 50 | 51 | int main(int argc, char **argv) { 52 | try { 53 | #ifdef ZENOHCXX_ZENOHC 54 | init_log_from_env_or("error"); 55 | #endif 56 | _main(argc, argv); 57 | } catch (ZException e) { 58 | std::cout << "Received an error :" << e.what() << "\n"; 59 | } 60 | } -------------------------------------------------------------------------------- /examples/universal/z_ping.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | // 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "../getargs.hxx" 23 | #include "zenoh.hxx" 24 | 25 | using namespace zenoh; 26 | 27 | int _main(int argc, char** argv) { 28 | using namespace std::literals; 29 | std::mutex mutex; 30 | std::condition_variable condvar; 31 | 32 | auto&& [config, args] = 33 | ConfigCliArgParser(argc, argv) 34 | .positional("PAYLOAD_SIZE", "Size of the payload to publish (number)") 35 | .named_value({"n", "samples"}, "SAMPLES", "The number of pings to be attempted (number)", "100") 36 | .named_value({"w", "warmup"}, "WARMUP", 37 | "The warmup time in ms during which pings will be emitted but not measured (number)", "1000") 38 | .named_flag({"no-express"}, "Disable message batching") 39 | .run(); 40 | 41 | size_t payload_size = std::atoi(args.positional(0).data()); 42 | size_t number_of_pings = std::atoi(args.value("samples").data()); 43 | size_t warmup_ms = std::atoi(args.value("warmup").data()); 44 | std::cout << "Opening session...\n"; 45 | auto session = Session::open(std::move(config)); 46 | 47 | auto sub = session.declare_subscriber( 48 | KeyExpr("test/pong"), [&condvar](const Sample&) mutable { condvar.notify_one(); }, closures::none); 49 | 50 | Session::PublisherOptions opts; 51 | opts.is_express = !args.flag("no-express"); 52 | auto pub = session.declare_publisher(KeyExpr("test/ping"), std::move(opts)); 53 | std::vector data(payload_size); 54 | std::iota(data.begin(), data.end(), uint8_t{0}); 55 | Bytes payload = std::move(data); 56 | 57 | std::unique_lock lock(mutex); 58 | if (warmup_ms > 0) { 59 | auto end = std::chrono::steady_clock::now() + (1ms * warmup_ms); 60 | while (std::chrono::steady_clock::now() < end) { 61 | pub.put(payload.clone()); 62 | condvar.wait_for(lock, 1s); 63 | } 64 | } 65 | for (unsigned int i = 0; i < number_of_pings; i++) { 66 | auto start = std::chrono::steady_clock::now(); 67 | pub.put(payload.clone()); 68 | if (condvar.wait_for(lock, 1s) == std::cv_status::timeout) { 69 | std::cout << "TIMEOUT seq=" << i << "\n"; 70 | continue; 71 | } 72 | auto rtt = 73 | std::chrono::duration_cast(std::chrono::steady_clock::now() - start).count(); 74 | std::cout << payload_size << " bytes: seq=" << i << " rtt=" << rtt << "µs" << " lat=" << rtt / 2 << "µs\n"; 75 | } 76 | lock.unlock(); 77 | return 0; 78 | } 79 | 80 | int main(int argc, char** argv) { 81 | try { 82 | #ifdef ZENOHCXX_ZENOHC 83 | init_log_from_env_or("error"); 84 | #endif 85 | return _main(argc, argv); 86 | } catch (ZException e) { 87 | std::cout << "Received an error :" << e.what() << "\n"; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /examples/universal/z_pong.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | // 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #include "../getargs.hxx" 21 | #include "zenoh.hxx" 22 | 23 | using namespace zenoh; 24 | using namespace std::chrono_literals; 25 | 26 | int _main(int argc, char **argv) { 27 | auto &&[config, args] = ConfigCliArgParser(argc, argv).named_flag({"no-express"}, "Disable message batching").run(); 28 | 29 | std::cout << "Opening session...\n"; 30 | auto session = Session::open(std::move(config)); 31 | 32 | Session::PublisherOptions opts; 33 | opts.is_express = args.flag("no-express"); 34 | auto pub = session.declare_publisher(KeyExpr("test/pong"), std::move(opts)); 35 | session.declare_background_subscriber( 36 | KeyExpr("test/ping"), 37 | [pub = std::move(pub)](const Sample &sample) mutable { pub.put(sample.get_payload().clone()); }, 38 | closures::none); 39 | 40 | std::cout << "Pong ready, press CTRL-C to quit...\n"; 41 | while (true) { 42 | std::this_thread::sleep_for(1s); 43 | } 44 | return 0; 45 | } 46 | 47 | int main(int argc, char **argv) { 48 | try { 49 | #ifdef ZENOHCXX_ZENOHC 50 | init_log_from_env_or("error"); 51 | #endif 52 | _main(argc, argv); 53 | } catch (ZException e) { 54 | std::cout << "Received an error :" << e.what() << "\n"; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /examples/universal/z_pub.cxx: -------------------------------------------------------------------------------- 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, 13 | // 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "../getargs.hxx" 24 | #include "zenoh.hxx" 25 | 26 | using namespace zenoh; 27 | using namespace std::chrono_literals; 28 | 29 | #ifdef ZENOHCXX_ZENOHC 30 | const char *default_value = "Pub from C++ zenoh-c!"; 31 | const char *default_keyexpr = "demo/example/zenoh-cpp-zenoh-c-pub"; 32 | #elif ZENOHCXX_ZENOHPICO 33 | const char *default_value = "Pub from C++ zenoh-pico!"; 34 | const char *default_keyexpr = "demo/example/zenoh-cpp-zenoh-pico-pub"; 35 | #else 36 | #error "Unknown zenoh backend" 37 | #endif 38 | 39 | int _main(int argc, char **argv) { 40 | auto &&[config, args] = 41 | ConfigCliArgParser(argc, argv) 42 | .named_value({"k", "key"}, "KEY_EXPRESSION", "Key expression to publish to (string)", default_keyexpr) 43 | .named_value({"p", "payload"}, "PAYLOAD", "Payload to publish (string)", default_value) 44 | .named_value({"a", "attach"}, "ATTACHMENT", "Attachment to add to each put (string)", "") 45 | #if defined(Z_FEATURE_UNSTABLE_API) && (defined(ZENOHCXX_ZENOHC) || Z_FEATURE_MATCHING == 1) 46 | .named_flag({"add-matching-listener"}, "Add matching listener") 47 | #endif 48 | .run(); 49 | 50 | auto keyexpr = args.value("key"); 51 | auto payload = args.value("payload"); 52 | auto attachment = args.value("attach"); 53 | 54 | std::cout << "Opening session..." << std::endl; 55 | auto session = Session::open(std::move(config)); 56 | 57 | std::cout << "Declaring Publisher on '" << keyexpr << "'..." << std::endl; 58 | auto pub = session.declare_publisher(KeyExpr(keyexpr)); 59 | #if defined(Z_FEATURE_UNSTABLE_API) && (defined(ZENOHCXX_ZENOHC) || Z_FEATURE_MATCHING == 1) 60 | if (args.flag("add-matching-listener")) { 61 | pub.declare_background_matching_listener( 62 | [](const MatchingStatus &s) { 63 | if (s.matching) { 64 | std::cout << "Publisher has matching subscribers." << std::endl; 65 | } else { 66 | std::cout << "Publisher has NO MORE matching subscribers." << std::endl; 67 | } 68 | }, 69 | closures::none); 70 | } 71 | #endif 72 | 73 | std::cout << "Press CTRL-C to quit..." << std::endl; 74 | for (int idx = 0; idx < std::numeric_limits::max(); ++idx) { 75 | std::this_thread::sleep_for(1s); 76 | std::ostringstream ss; 77 | ss << "[" << idx << "] " << payload; 78 | auto s = ss.str(); 79 | std::cout << "Putting Data ('" << keyexpr << "': '" << s << "')...\n"; 80 | Publisher::PutOptions options; 81 | if (!attachment.empty()) { 82 | options.attachment = attachment; 83 | } 84 | 85 | pub.put(s, std::move(options)); 86 | } 87 | return 0; 88 | } 89 | 90 | int main(int argc, char **argv) { 91 | try { 92 | #ifdef ZENOHCXX_ZENOHC 93 | init_log_from_env_or("error"); 94 | #endif 95 | _main(argc, argv); 96 | } catch (ZException e) { 97 | std::cout << "Received an error :" << e.what() << "\n"; 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /examples/universal/z_pub_thr.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | // 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | 20 | #include "../getargs.hxx" 21 | #include "zenoh.hxx" 22 | using namespace zenoh; 23 | 24 | int _main(int argc, char **argv) { 25 | std::string_view keyexpr = "test/thr"; 26 | auto &&[config, args] = 27 | ConfigCliArgParser(argc, argv) 28 | .positional("PAYLOAD_SIZE", "Size of the payload to publish (number)") 29 | .named_value({"p", "priority"}, "PRIORITY", "Priority for sending data (number [1 - 7])", "5") 30 | .named_flag({"express"}, "Batch messages") 31 | .run(); 32 | 33 | auto len = std::atoi(args.positional(0).data()); 34 | auto priority = parse_priority(args.value("priority")); 35 | auto express = args.flag("express"); 36 | 37 | std::vector data(len); 38 | std::iota(data.begin(), data.end(), uint8_t{0}); 39 | Bytes payload = std::move(data); 40 | 41 | std::cout << "Opening session...\n"; 42 | auto session = Session::open(std::move(config)); 43 | 44 | std::cout << "Declaring Publisher on " << keyexpr << "...\n"; 45 | 46 | Session::PublisherOptions pub_options; 47 | pub_options.congestion_control = Z_CONGESTION_CONTROL_BLOCK; 48 | pub_options.priority = priority; 49 | pub_options.is_express = express; 50 | auto pub = session.declare_publisher(KeyExpr(keyexpr), std::move(pub_options)); 51 | 52 | printf("Press CTRL-C to quit...\n"); 53 | while (1) pub.put(payload.clone()); 54 | return 0; 55 | } 56 | 57 | int main(int argc, char **argv) { 58 | try { 59 | #ifdef ZENOHCXX_ZENOHC 60 | init_log_from_env_or("error"); 61 | #endif 62 | _main(argc, argv); 63 | } catch (ZException e) { 64 | std::cout << "Received an error :" << e.what() << "\n"; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /examples/universal/z_put.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | // 14 | #include 15 | 16 | #include "../getargs.hxx" 17 | #include "stdio.h" 18 | #include "zenoh.hxx" 19 | using namespace zenoh; 20 | 21 | #ifdef ZENOHCXX_ZENOHC 22 | const char *default_value = "Put from C++ zenoh-c!"; 23 | const char *default_keyexpr = "demo/example/zenoh-cpp-zenoh-c-put"; 24 | #elif ZENOHCXX_ZENOHPICO 25 | const char *default_value = "Put from C++ zenoh-pico!"; 26 | const char *default_keyexpr = "demo/example/zenoh-cpp-zenoh-pico-put"; 27 | #else 28 | #error "Unknown zenoh backend" 29 | #endif 30 | 31 | int _main(int argc, char **argv) { 32 | auto &&[config, args] = 33 | ConfigCliArgParser(argc, argv) 34 | .named_value({"k", "key"}, "KEY_EXPRESSION", "Key expression to publish to (string)", default_keyexpr) 35 | .named_value({"p", "payload"}, "PAYLOAD", "Payload to publish (string)", default_value) 36 | .run(); 37 | 38 | auto keyexpr = args.value("key"); 39 | auto payload = args.value("payload"); 40 | 41 | std::cout << "Opening session...\n"; 42 | auto session = Session::open(std::move(config)); 43 | 44 | std::cout << "Putting Data (" << "'" << keyexpr << "': '" << payload << "')...\n"; 45 | 46 | session.put(KeyExpr(keyexpr), payload); 47 | return 0; 48 | } 49 | 50 | int main(int argc, char **argv) { 51 | try { 52 | #ifdef ZENOHCXX_ZENOHC 53 | init_log_from_env_or("error"); 54 | #endif 55 | _main(argc, argv); 56 | } catch (ZException e) { 57 | std::cout << "Received an error :" << e.what() << "\n"; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /examples/universal/z_queryable.cxx: -------------------------------------------------------------------------------- 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, 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | 21 | #include "../getargs.hxx" 22 | #include "zenoh.hxx" 23 | 24 | using namespace zenoh; 25 | using namespace std::chrono_literals; 26 | 27 | #ifdef ZENOHCXX_ZENOHC 28 | const char *default_keyexpr = "demo/example/zenoh-cpp-zenoh-c-queryable"; 29 | const char *default_payload = "Queryable from C++ zenoh-c!"; 30 | #elif ZENOHCXX_ZENOHPICO 31 | const char *default_keyexpr = "demo/example/zenoh-cpp-zenoh-pico-queryable"; 32 | const char *default_payload = "Queryable from C++ zenoh-pico!"; 33 | #else 34 | #error "Unknown zenoh backend" 35 | #endif 36 | 37 | int _main(int argc, char **argv) { 38 | auto &&[config, args] = 39 | ConfigCliArgParser(argc, argv) 40 | .named_value({"k", "key"}, "KEY_EXPRESSION", "Key expression matching queries to reply to (string)", 41 | default_keyexpr) 42 | .named_value({"p", "payload"}, "PAYLOAD", "Value to reply to queries with (string)", default_payload) 43 | .named_flag({"complete"}, "Flag to indicate whether queryable is complete or not") 44 | .run(); 45 | 46 | auto keyexpr = args.value("key"); 47 | auto payload = args.value("payload"); 48 | 49 | std::cout << "Opening session...\n"; 50 | auto session = Session::open(std::move(config)); 51 | 52 | std::cout << "Declaring Queryable on '" << keyexpr << "'...\n"; 53 | 54 | auto on_query = [payload, keyexpr](const Query &query) { 55 | auto params = query.get_parameters(); 56 | auto query_payload = query.get_payload(); 57 | std::cout << ">> [Queryable ] Received Query '" << query.get_keyexpr().as_string_view() << "?" << params; 58 | if (query_payload.has_value()) { 59 | std::cout << "' with value = '" << query_payload->get().as_string(); 60 | } 61 | std::cout << "'\n"; 62 | std::cout << "[Queryable ] Responding ('" << keyexpr << "': '" << payload << "')\n"; 63 | query.reply(keyexpr, payload); 64 | }; 65 | 66 | auto on_drop_queryable = []() { std::cout << "Destroying queryable\n"; }; 67 | 68 | Session::QueryableOptions opts; 69 | opts.complete = args.flag("complete"); 70 | auto queryable = session.declare_queryable(keyexpr, on_query, on_drop_queryable, std::move(opts)); 71 | 72 | printf("Press CTRL-C to quit...\n"); 73 | while (true) { 74 | std::this_thread::sleep_for(1s); 75 | } 76 | 77 | return 0; 78 | } 79 | 80 | int main(int argc, char **argv) { 81 | try { 82 | #ifdef ZENOHCXX_ZENOHC 83 | init_log_from_env_or("error"); 84 | #endif 85 | _main(argc, argv); 86 | } catch (ZException e) { 87 | std::cout << "Received an error :" << e.what() << "\n"; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /examples/universal/z_scout.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | 14 | #include 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "../getargs.hxx" 23 | #include "zenoh.hxx" 24 | using namespace zenoh; 25 | using namespace std::chrono_literals; 26 | 27 | void printlocators(const std::vector &locs) { 28 | std::cout << "["; 29 | for (size_t i = 0; i < locs.size(); i++) { 30 | std::cout << "\"" << locs[i] << "\""; 31 | if (i < locs.size() - 1) std::cout << ", "; 32 | } 33 | std::cout << "]"; 34 | } 35 | 36 | void printhello(const Hello &hello) { 37 | std::cout << "Hello { "; 38 | #if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API) 39 | std::cout << "pid: " << hello.get_id() << ", "; 40 | #endif 41 | std::cout << "whatami: " << hello.get_whatami(); 42 | std::cout << ", locators: "; 43 | printlocators(hello.get_locators()); 44 | std::cout << " }"; 45 | } 46 | 47 | int _main(int argc, char **argv) { 48 | auto &&[config, args] = ConfigCliArgParser(argc, argv).run(); 49 | 50 | size_t count = 0; 51 | std::mutex m; 52 | std::condition_variable done_signal; 53 | bool done = false; 54 | 55 | auto on_hello = [&count](const Hello &hello) { 56 | printhello(hello); 57 | std::cout << std::endl; 58 | count++; 59 | }; 60 | 61 | auto on_end_scouting = [&m, &done, &done_signal, &count]() { 62 | if (count == 0) std::cout << "Did not find any zenoh process.\n"; 63 | std::lock_guard lock(m); 64 | done = true; 65 | done_signal.notify_all(); 66 | }; 67 | 68 | std::cout << "Scout starting" << std::endl; 69 | 70 | scout(std::move(config), on_hello, on_end_scouting); 71 | 72 | std::unique_lock lock(m); 73 | done_signal.wait(lock, [&done] { return done; }); 74 | 75 | std::cout << "Scout finished" << std::endl; 76 | 77 | return 0; 78 | } 79 | 80 | int main(int argc, char **argv) { 81 | try { 82 | #ifdef ZENOHCXX_ZENOHC 83 | init_log_from_env_or("error"); 84 | #endif 85 | _main(argc, argv); 86 | } catch (ZException e) { 87 | std::cout << "Received an error :" << e.what() << "\n"; 88 | } 89 | } -------------------------------------------------------------------------------- /examples/universal/z_sub.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | // 14 | #include 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | #include "../getargs.hxx" 21 | #include "zenoh.hxx" 22 | 23 | using namespace zenoh; 24 | using namespace std::chrono_literals; 25 | 26 | const char *kind_to_str(SampleKind kind) { 27 | switch (kind) { 28 | case SampleKind::Z_SAMPLE_KIND_PUT: 29 | return "PUT"; 30 | case SampleKind::Z_SAMPLE_KIND_DELETE: 31 | return "DELETE"; 32 | default: 33 | return "UNKNOWN"; 34 | } 35 | } 36 | 37 | int _main(int argc, char **argv) { 38 | auto &&[config, args] = 39 | ConfigCliArgParser(argc, argv) 40 | .named_value({"k", "key"}, "KEY_EXPRESSION", "Key expression to subscribe to (string)", "demo/example/**") 41 | .run(); 42 | 43 | KeyExpr keyexpr(args.value("key")); 44 | 45 | std::cout << "Opening session..." << std::endl; 46 | auto session = Session::open(std::move(config)); 47 | 48 | auto data_handler = [](const Sample &sample) { 49 | std::cout << ">> [Subscriber] Received " << kind_to_str(sample.get_kind()) << " ('" 50 | << sample.get_keyexpr().as_string_view() << "' : '" << sample.get_payload().as_string() << "')"; 51 | 52 | auto attachment = sample.get_attachment(); 53 | if (attachment.has_value()) { 54 | std::cout << " (" << attachment->get().as_string() << ")"; 55 | } 56 | std::cout << std::endl; 57 | }; 58 | 59 | std::cout << "Declaring Subscriber on '" << keyexpr.as_string_view() << "'..." << std::endl; 60 | auto subscriber = session.declare_subscriber(keyexpr, data_handler, closures::none); 61 | 62 | std::cout << "Press CTRL-C to quit...\n"; 63 | while (true) { 64 | std::this_thread::sleep_for(1s); 65 | } 66 | 67 | return 0; 68 | } 69 | 70 | int main(int argc, char **argv) { 71 | try { 72 | #ifdef ZENOHCXX_ZENOHC 73 | init_log_from_env_or("error"); 74 | #endif 75 | _main(argc, argv); 76 | } catch (ZException e) { 77 | std::cout << "Received an error :" << e.what() << "\n"; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /examples/universal/z_sub_liveliness.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2024 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, 13 | // 14 | 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | 21 | #include "../getargs.hxx" 22 | #include "zenoh.hxx" 23 | 24 | using namespace zenoh; 25 | using namespace std::chrono_literals; 26 | 27 | void data_handler(const Sample &sample) { 28 | if (sample.get_kind() == Z_SAMPLE_KIND_PUT) { 29 | std::cout << ">> [LivelinessSubscriber] New alive token ('" << sample.get_keyexpr().as_string_view() << "')\n"; 30 | } else if (sample.get_kind() == Z_SAMPLE_KIND_DELETE) { 31 | std::cout << ">> [LivelinessSubscriber] Dropped token ('" << sample.get_keyexpr().as_string_view() << "')\n"; 32 | } 33 | } 34 | 35 | int _main(int argc, char **argv) { 36 | auto &&[config, args] = 37 | ConfigCliArgParser(argc, argv) 38 | .named_value({"k", "key"}, "KEY_EXPRESSION", 39 | "The key expression matching liveliness tokens to subscribe t (string)", "group1/**") 40 | .named_flag({"history"}, "Get historical liveliness tokens") 41 | .run(); 42 | 43 | auto history = args.flag("history"); 44 | KeyExpr keyexpr(args.value("key")); 45 | 46 | std::cout << "Opening session..." << std::endl; 47 | auto session = Session::open(std::move(config)); 48 | 49 | std::cout << "Declaring Liveliness Subscriber on '" << keyexpr.as_string_view() << "'..." << std::endl; 50 | Session::LivelinessSubscriberOptions opts; 51 | opts.history = history; 52 | auto subscriber = session.liveliness_declare_subscriber(keyexpr, &data_handler, closures::none, std::move(opts)); 53 | 54 | std::cout << "Press CTRL-C to quit...\n"; 55 | while (true) { 56 | std::this_thread::sleep_for(1s); 57 | } 58 | 59 | return 0; 60 | } 61 | 62 | int main(int argc, char **argv) { 63 | try { 64 | #ifdef ZENOHCXX_ZENOHC 65 | init_log_from_env_or("error"); 66 | #endif 67 | _main(argc, argv); 68 | } catch (ZException e) { 69 | std::cout << "Received an error :" << e.what() << "\n"; 70 | } 71 | } -------------------------------------------------------------------------------- /examples/zenohc/z_advanced_pub.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2024 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, 13 | // 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #include "../getargs.hxx" 22 | #include "zenoh.hxx" 23 | 24 | using namespace zenoh; 25 | using namespace std::chrono_literals; 26 | 27 | #ifdef ZENOHCXX_ZENOHC 28 | const char *default_value = "Pub from C++ zenoh-c!"; 29 | const char *default_keyexpr = "demo/example/zenoh-cpp-zenoh-c-pub"; 30 | #elif ZENOHCXX_ZENOHPICO 31 | const char *default_value = "Pub from C++ zenoh-pico!"; 32 | const char *default_keyexpr = "demo/example/zenoh-cpp-zenoh-pico-pub"; 33 | #else 34 | #error "Unknown zenoh backend" 35 | #endif 36 | 37 | int _main(int argc, char **argv) { 38 | auto &&[config, args] = 39 | ConfigCliArgParser(argc, argv) 40 | .named_value({"k", "key"}, "KEY_EXPRESSION", "Key expression to publish to (string)", default_keyexpr) 41 | .named_value({"p", "payload"}, "PAYLOAD", "Payload to publish (string)", default_value) 42 | .named_value({"i", "history"}, "HISTORY_SIZE", "The number of publications to keep in cache (number)", "1") 43 | .run(); 44 | 45 | config.insert_json5(Z_CONFIG_ADD_TIMESTAMP_KEY, "true"); 46 | auto keyexpr = args.value("key"); 47 | auto payload = args.value("payload"); 48 | auto history = std::atoi(args.value("history").data()); 49 | 50 | std::cout << "Opening session..." << std::endl; 51 | auto session = Session::open(std::move(config)); 52 | 53 | ext::SessionExt::AdvancedPublisherOptions opts; 54 | opts.cache.emplace().max_samples = history; 55 | opts.publisher_detection = true; 56 | opts.sample_miss_detection.emplace().heartbeat = 57 | ext::SessionExt::AdvancedPublisherOptions::SampleMissDetectionOptions::HeartbeatPeriodic{1000}; 58 | // alternatively sample miss detection can be done in response to subscriber's periodic queries 59 | 60 | std::cout << "Declaring AdvancedPublisher on '" << keyexpr << "'..." << std::endl; 61 | auto pub = session.ext().declare_advanced_publisher(KeyExpr(keyexpr), std::move(opts)); 62 | 63 | std::cout << "Press CTRL-C to quit..." << std::endl; 64 | for (int idx = 0; idx < std::numeric_limits::max(); ++idx) { 65 | std::this_thread::sleep_for(1s); 66 | std::ostringstream ss; 67 | ss << "[" << idx << "] " << payload; 68 | auto s = ss.str(); 69 | std::cout << "Put Data ('" << keyexpr << "': '" << s << "')...\n"; 70 | pub.put(s); 71 | } 72 | return 0; 73 | } 74 | 75 | int main(int argc, char **argv) { 76 | try { 77 | #ifdef ZENOHCXX_ZENOHC 78 | init_log_from_env_or("error"); 79 | #endif 80 | _main(argc, argv); 81 | } catch (ZException e) { 82 | std::cout << "Received an error :" << e.what() << "\n"; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /examples/zenohc/z_advanced_sub.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2024 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, 13 | // 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #include "../getargs.hxx" 21 | #include "zenoh.hxx" 22 | 23 | using namespace zenoh; 24 | using namespace std::chrono_literals; 25 | 26 | const char *kind_to_str(SampleKind kind) { 27 | switch (kind) { 28 | case SampleKind::Z_SAMPLE_KIND_PUT: 29 | return "PUT"; 30 | case SampleKind::Z_SAMPLE_KIND_DELETE: 31 | return "DELETE"; 32 | default: 33 | return "UNKNOWN"; 34 | } 35 | } 36 | 37 | int _main(int argc, char **argv) { 38 | auto &&[config, args] = 39 | ConfigCliArgParser(argc, argv) 40 | .named_value({"k", "key"}, "KEY_EXPRESSION", "Key expression to subscriber to (string)", "demo/example/**") 41 | .run(); 42 | 43 | auto keyexpr = args.value("key"); 44 | 45 | std::cout << "Opening session..." << std::endl; 46 | auto session = Session::open(std::move(config)); 47 | 48 | ext::SessionExt::AdvancedSubscriberOptions opts; 49 | opts.history.emplace().detect_late_publishers = true; 50 | // enable recovery based on received heartbeats from ext::AdvancedPublisher 51 | opts.recovery.emplace().last_sample_miss_detection = 52 | ext::SessionExt::AdvancedSubscriberOptions::RecoveryOptions::Heartbeat{}; 53 | // alternatively recovery can be triggered based on missed sample detection via periodic queries: 54 | // opts.recovery.emplace().last_sample_miss_detection = 55 | // ext::SessionExt::AdvancedSubscriberOptions::RecoveryOptions::PeriodicQueriesOptions{1000}; 56 | opts.subscriber_detection = true; 57 | 58 | auto data_handler = [](const Sample &sample) { 59 | std::cout << ">> [Subscriber] Received " << kind_to_str(sample.get_kind()) << " ('" 60 | << sample.get_keyexpr().as_string_view() << "' : '" << sample.get_payload().as_string() << "')"; 61 | std::cout << std::endl; 62 | }; 63 | 64 | auto missed_sample_handler = [](const ext::Miss &miss) { 65 | std::cout << ">> [Subscriber] Missed " << miss.nb << " samples from '" << miss.source.id() << "' !!!" 66 | << std::endl; 67 | }; 68 | 69 | std::cout << "Declaring AdvancedSubscriber on '" << keyexpr << "'" << std::endl; 70 | auto advanced_subscriber = 71 | session.ext().declare_advanced_subscriber(keyexpr, data_handler, closures::none, std::move(opts)); 72 | 73 | advanced_subscriber.declare_background_sample_miss_listener(missed_sample_handler, closures::none); 74 | 75 | std::cout << "Press CTRL-C to quit..." << std::endl; 76 | while (true) { 77 | std::this_thread::sleep_for(1s); 78 | } 79 | 80 | return 0; 81 | } 82 | 83 | int main(int argc, char **argv) { 84 | try { 85 | init_log_from_env_or("error"); 86 | _main(argc, argv); 87 | } catch (ZException e) { 88 | std::cout << "Received an error :" << e.what() << "\n"; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /examples/zenohc/z_get_shm.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "../getargs.hxx" 23 | #include "zenoh.hxx" 24 | using namespace zenoh; 25 | 26 | int _main(int argc, char **argv) { 27 | auto &&[config, args] = 28 | ConfigCliArgParser(argc, argv) 29 | .named_value({"s", "selector"}, "SELECTOR", "Query selector (string)", "demo/example/**") 30 | .named_value({"p", "payload"}, "PAYLOAD", "Query payload (string)", "") 31 | .named_value({"t", "target"}, "TARGET", "Query target (BEST_MATCHING | ALL | ALL_COMPLETE)", 32 | "BEST_MATCHING") 33 | .named_value({"o", "timeout"}, "TIMEOUT", "Timeout in ms (number)", "10000") 34 | .run(); 35 | 36 | uint64_t timeout_ms = std::atoi(args.value("timeout").data()); 37 | QueryTarget query_target = parse_query_target(args.value("target")); 38 | Selector selector = parse_selector(args.value("selector")); 39 | auto payload = args.value("payload"); 40 | 41 | std::cout << "Opening session...\n"; 42 | auto session = Session::open(std::move(config)); 43 | 44 | std::mutex m; 45 | std::condition_variable done_signal; 46 | bool done = false; 47 | 48 | auto on_reply = [](const Reply &reply) { 49 | if (reply.is_ok()) { 50 | const auto &sample = reply.get_ok(); 51 | std::cout << "Received ('" << sample.get_keyexpr().as_string_view() << "' : '" 52 | << sample.get_payload().as_string() << "')\n"; 53 | } else { 54 | std::cout << "Received an error :" << reply.get_err().get_payload().as_string() << "\n"; 55 | } 56 | }; 57 | 58 | auto on_done = [&m, &done, &done_signal]() { 59 | std::lock_guard lock(m); 60 | done = true; 61 | done_signal.notify_all(); 62 | }; 63 | 64 | std::cout << "Preparing SHM Provider...\n"; 65 | PosixShmProvider provider(MemoryLayout(1024 * 1024, AllocAlignment({2}))); 66 | 67 | std::cout << "Allocating SHM buffer...\n"; 68 | const auto len = payload.size() + 1; 69 | auto alloc_result = provider.alloc_gc_defrag_blocking(len, AllocAlignment({0})); 70 | ZShmMut &&buf = std::get(std::move(alloc_result)); 71 | memcpy(buf.data(), payload.data(), len); 72 | 73 | std::cout << "Sending Query '" << args.value("selector") << "'...\n"; 74 | 75 | Session::GetOptions options; 76 | options.target = query_target; 77 | options.payload = std::move(buf); 78 | session.get(selector.key_expr, selector.parameters, on_reply, on_done, std::move(options)); 79 | 80 | std::unique_lock lock(m); 81 | done_signal.wait(lock, [&done] { return done; }); 82 | 83 | return 0; 84 | } 85 | 86 | int main(int argc, char **argv) { 87 | try { 88 | #ifdef ZENOHCXX_ZENOHC 89 | init_log_from_env_or("error"); 90 | #endif 91 | _main(argc, argv); 92 | } catch (ZException e) { 93 | std::cout << "Received an error :" << e.what() << "\n"; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /examples/zenohc/z_pub_shm.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | // 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "../getargs.hxx" 24 | #include "zenoh.hxx" 25 | 26 | using namespace zenoh; 27 | using namespace std::chrono_literals; 28 | 29 | const char *default_value = "Pub from C++ zenoh-c SHM!"; 30 | const char *default_keyexpr = "demo/example/zenoh-cpp-zenoh-c-pub"; 31 | 32 | int _main(int argc, char **argv) { 33 | auto &&[config, args] = 34 | ConfigCliArgParser(argc, argv) 35 | .named_value({"k", "key"}, "KEY_EXPRESSION", "Key expression to publish to (string)", default_keyexpr) 36 | .named_value({"p", "payload"}, "PAYLOAD", "Payload to publish (string)", default_value) 37 | #if defined(Z_FEATURE_UNSTABLE_API) 38 | .named_flag({"add-matching-listener"}, "Add matching listener") 39 | #endif 40 | .run(); 41 | 42 | auto keyexpr = args.value("key"); 43 | auto payload = args.value("payload"); 44 | 45 | std::cout << "Opening session..." << std::endl; 46 | auto session = Session::open(std::move(config)); 47 | 48 | std::cout << "Declaring Publisher on '" << keyexpr << "'..." << std::endl; 49 | auto pub = session.declare_publisher(KeyExpr(keyexpr)); 50 | 51 | #if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API) 52 | if (args.flag("add-matching-listener")) { 53 | pub.declare_background_matching_listener( 54 | [](const MatchingStatus &s) { 55 | if (s.matching) { 56 | std::cout << "Publisher has matching subscribers." << std::endl; 57 | } else { 58 | std::cout << "Publisher has NO MORE matching subscribers." << std::endl; 59 | } 60 | }, 61 | closures::none); 62 | } 63 | #endif 64 | 65 | std::cout << "Publisher on '" << keyexpr << "' declared" << std::endl; 66 | 67 | std::cout << "Preparing SHM Provider...\n"; 68 | PosixShmProvider provider(MemoryLayout(65536, AllocAlignment({2}))); 69 | 70 | std::cout << "Press CTRL-C to quit..." << std::endl; 71 | for (int idx = 0; idx < std::numeric_limits::max(); ++idx) { 72 | std::this_thread::sleep_for(1s); 73 | std::ostringstream ss; 74 | ss << "[" << idx << "] " << payload; 75 | auto s = ss.str(); 76 | std::cout << "Putting Data ('" << keyexpr << "': '" << s << "')...\n"; 77 | 78 | std::cout << "Allocating SHM buffer...\n"; 79 | const auto len = s.size() + 1; 80 | auto alloc_result = provider.alloc_gc_defrag_blocking(len, AllocAlignment({0})); 81 | ZShmMut &&buf = std::get(std::move(alloc_result)); 82 | memcpy(buf.data(), s.data(), len); 83 | 84 | pub.put(std::move(buf)); 85 | } 86 | return 0; 87 | } 88 | 89 | int main(int argc, char **argv) { 90 | try { 91 | #ifdef ZENOHCXX_ZENOHC 92 | init_log_from_env_or("error"); 93 | #endif 94 | _main(argc, argv); 95 | } catch (ZException e) { 96 | std::cout << "Received an error :" << e.what() << "\n"; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /examples/zenohc/z_pub_shm_thr.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | // 14 | #include 15 | #include 16 | 17 | #include 18 | 19 | #include "../getargs.hxx" 20 | #include "zenoh.hxx" 21 | using namespace zenoh; 22 | 23 | int _main(int argc, char **argv) { 24 | const char *keyexpr = "test/thr"; 25 | auto &&[config, args] = 26 | ConfigCliArgParser(argc, argv) 27 | .positional("PAYLOAD_SIZE", "Size of the payload to publish (number)") 28 | .named_value({"s", "shared-memory"}, "SHARED_MEMORY_SIZE", "Shared memory size in MBytes", "32") 29 | .run(); 30 | 31 | size_t len = std::atoi(args.positional(0).data()); 32 | size_t shared_memory_size_mb = std::atoi(args.value("s").data()); 33 | 34 | std::cout << "Opening session...\n"; 35 | auto session = Session::open(std::move(config)); 36 | 37 | std::cout << "Declaring Publisher on " << keyexpr << "...\n"; 38 | 39 | Session::PublisherOptions options; 40 | options.congestion_control = Z_CONGESTION_CONTROL_BLOCK; 41 | auto pub = session.declare_publisher(keyexpr, std::move(options)); 42 | 43 | std::cout << "Preparing SHM Provider...\n"; 44 | PosixShmProvider provider(MemoryLayout(shared_memory_size_mb * 1024 * 1024, AllocAlignment({2}))); 45 | 46 | std::cout << "Allocating SHM buffer...\n"; 47 | auto alloc_result = provider.alloc_gc_defrag_blocking(len, AllocAlignment({0})); 48 | ZShmMut &&buf_mut = std::get(std::move(alloc_result)); 49 | ZShm buf(std::move(buf_mut)); 50 | 51 | printf("Press CTRL-C to quit...\n"); 52 | while (1) pub.put(ZShm(buf)); 53 | } 54 | 55 | int main(int argc, char **argv) { 56 | try { 57 | #ifdef ZENOHCXX_ZENOHC 58 | init_log_from_env_or("error"); 59 | #endif 60 | _main(argc, argv); 61 | } catch (ZException e) { 62 | std::cout << "Received an error :" << e.what() << "\n"; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /examples/zenohc/z_queryable_shm.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | 21 | #include "../getargs.hxx" 22 | #include "zenoh.hxx" 23 | using namespace zenoh; 24 | using namespace std::chrono_literals; 25 | 26 | const char *default_keyexpr = "demo/example/zenoh-cpp-zenoh-c-queryable"; 27 | const char *default_payload = "Queryable from C++ zenoh-c SHM!"; 28 | 29 | const char *locator = nullptr; 30 | 31 | int _main(int argc, char **argv) { 32 | auto &&[config, args] = 33 | ConfigCliArgParser(argc, argv) 34 | .named_value({"k", "key"}, "KEY_EXPRESSION", "Key expression matching queries to reply to (string)", 35 | default_keyexpr) 36 | .named_value({"p", "payload"}, "PAYLOAD", "Value to reply to queries with (string)", default_payload) 37 | .named_flag({"complete"}, "Flag to indicate whether queryable is complete or not") 38 | .run(); 39 | 40 | auto keyexpr = args.value("key"); 41 | auto payload = args.value("payload"); 42 | auto complete = args.flag("complete"); 43 | 44 | printf("Opening session...\n"); 45 | auto session = Session::open(std::move(config)); 46 | 47 | std::cout << "Declaring Queryable on '" << keyexpr << "'...\n"; 48 | 49 | std::cout << "Preparing SHM Provider...\n"; 50 | PosixShmProvider provider(MemoryLayout(65536, AllocAlignment({0}))); 51 | 52 | auto on_query = [provider = std::move(provider), payload, keyexpr](const Query &query) { 53 | auto query_payload = query.get_payload(); 54 | 55 | const char *payload_type = ""; 56 | if (query_payload.has_value()) { 57 | if (query_payload->get().as_shm().has_value()) { 58 | payload_type = "SHM"; 59 | } else { 60 | payload_type = "RAW"; 61 | } 62 | } 63 | 64 | auto params = query.get_parameters(); 65 | std::cout << ">> [Queryable ] Received Query [" << payload_type << "] '" << query.get_keyexpr().as_string_view() 66 | << "?" << params; 67 | if (query_payload.has_value()) { 68 | std::cout << "' value = '" << query_payload->get().as_string(); 69 | } 70 | std::cout << "'\n"; 71 | 72 | const auto len = payload.size() + 1; // + NULL terminator 73 | auto alloc_result = provider.alloc_gc_defrag_blocking(len, AllocAlignment({0})); 74 | ZShmMut &&buf = std::get(std::move(alloc_result)); 75 | memcpy(buf.data(), payload.data(), len); 76 | 77 | std::cout << "[Queryable ] Responding ('" << keyexpr << "': '" << payload << "')\n"; 78 | query.reply(keyexpr, std::move(buf)); 79 | }; 80 | 81 | auto on_drop_queryable = []() { std::cout << "Destroying queryable\n"; }; 82 | 83 | Session::QueryableOptions opts; 84 | opts.complete = complete; 85 | auto queryable = 86 | session.declare_queryable(keyexpr, std::move(on_query), std::move(on_drop_queryable), std::move(opts)); 87 | 88 | std::cout << "Press CTRL-C to quit...\n"; 89 | while (true) { 90 | std::this_thread::sleep_for(1s); 91 | } 92 | 93 | return 0; 94 | } 95 | 96 | int main(int argc, char **argv) { 97 | try { 98 | #ifdef ZENOHCXX_ZENOHC 99 | init_log_from_env_or("error"); 100 | #endif 101 | _main(argc, argv); 102 | } catch (ZException e) { 103 | std::cout << "Received an error :" << e.what() << "\n"; 104 | } 105 | } -------------------------------------------------------------------------------- /examples/zenohc/z_sub_shm.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | // 14 | #include 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | #include "../getargs.hxx" 21 | #include "zenoh.hxx" 22 | 23 | using namespace zenoh; 24 | using namespace std::chrono_literals; 25 | 26 | const char *kind_to_str(SampleKind kind) { 27 | switch (kind) { 28 | case SampleKind::Z_SAMPLE_KIND_PUT: 29 | return "PUT"; 30 | case SampleKind::Z_SAMPLE_KIND_DELETE: 31 | return "DELETE"; 32 | default: 33 | return "UNKNOWN"; 34 | } 35 | } 36 | 37 | void data_handler(Sample &sample) { 38 | // if Zenoh is built without SHM support, the only buffer type it can receive is RAW 39 | #if !defined(Z_FEATURE_SHARED_MEMORY) 40 | const char *payload_type = "RAW"; 41 | #endif 42 | 43 | // if Zenoh is built with SHM support but without SHM API (that is unstable), it can 44 | // receive buffers of any type, but there is no way to detect the buffer type 45 | #if defined(Z_FEATURE_SHARED_MEMORY) && !defined(Z_FEATURE_UNSTABLE_API) 46 | const char *payload_type = "UNKNOWN"; 47 | #endif 48 | 49 | // if Zenoh is built with SHM support and with SHM API, we can detect the exact buffer type 50 | #if defined(Z_FEATURE_SHARED_MEMORY) && defined(Z_FEATURE_UNSTABLE_API) 51 | const char *payload_type = "RAW"; 52 | { 53 | // try to convert sample payload into SHM buffer. The conversion will succeed 54 | // only if payload is carrying underlying SHM buffer 55 | auto shm = sample.get_payload().as_shm(); 56 | if (shm.has_value()) { 57 | // try to get mutable access to SHM buffer 58 | payload_type = ZShm::try_mutate(shm.value()).has_value() ? "SHM (MUT)" : "SHM (IMMUT)"; 59 | } 60 | } 61 | #endif 62 | 63 | std::cout << ">> [Subscriber] Received [" << payload_type << "] " << kind_to_str(sample.get_kind()) << " ('" 64 | << sample.get_keyexpr().as_string_view() << "' : '" << sample.get_payload().as_string() << "')\n"; 65 | } 66 | 67 | int _main(int argc, char **argv) { 68 | auto &&[config, args] = 69 | ConfigCliArgParser(argc, argv) 70 | .named_value({"k", "key"}, "KEY_EXPRESSION", "Key expression to subscribe to (string)", "demo/example/**") 71 | .run(); 72 | 73 | KeyExpr keyexpr(args.value("key")); 74 | 75 | std::cout << "Opening session..." << std::endl; 76 | auto session = Session::open(std::move(config)); 77 | 78 | std::cout << "Declaring Subscriber on '" << keyexpr.as_string_view() << "'..." << std::endl; 79 | auto subscriber = session.declare_subscriber(keyexpr, &data_handler, closures::none); 80 | 81 | std::cout << "Press CTRL-C to quit...\n"; 82 | while (true) { 83 | std::this_thread::sleep_for(1s); 84 | } 85 | 86 | return 0; 87 | } 88 | 89 | int main(int argc, char **argv) { 90 | try { 91 | #ifdef ZENOHCXX_ZENOHC 92 | init_log_from_env_or("error"); 93 | #endif 94 | _main(argc, argv); 95 | } catch (ZException e) { 96 | std::cout << "Received an error :" << e.what() << "\n"; 97 | } 98 | } -------------------------------------------------------------------------------- /include/zenoh.hxx: -------------------------------------------------------------------------------- 1 | #pragma once 2 | // 3 | // Copyright (c) 2022 ZettaScale Technology 4 | // 5 | // This program and the accompanying materials are made available under the 6 | // terms of the Eclipse Public License 2.0 which is available at 7 | // http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 8 | // which is available at https://www.apache.org/licenses/LICENSE-2.0. 9 | // 10 | // SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 11 | // 12 | // Contributors: 13 | // ZettaScale Zenoh Team, 14 | 15 | #if (__cplusplus < 201703L) && (!defined(_MSVC_LANG) || (_MSVC_LANG < 201703L)) 16 | #error zenoh-cpp requires a C++17-compliant compiler 17 | #endif 18 | 19 | #include "zenoh/api.hxx" 20 | -------------------------------------------------------------------------------- /include/zenoh/api.hxx: -------------------------------------------------------------------------------- 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, 13 | 14 | #pragma once 15 | 16 | #include "api/bytes.hxx" 17 | #include "api/channels.hxx" 18 | #include "api/closures.hxx" 19 | #include "api/config.hxx" 20 | #include "api/encoding.hxx" 21 | #include "api/enums.hxx" 22 | #include "api/hello.hxx" 23 | #include "api/id.hxx" 24 | #include "api/keyexpr.hxx" 25 | #if defined(ZENOHCXX_ZENOHC) || Z_FEATURE_LIVELINESS == 1 26 | #include "api/liveliness.hxx" 27 | #endif 28 | #include "api/logging.hxx" 29 | #include "api/publisher.hxx" 30 | #include "api/query.hxx" 31 | #include "api/query_consolidation.hxx" 32 | #include "api/queryable.hxx" 33 | #include "api/reply.hxx" 34 | #include "api/sample.hxx" 35 | #include "api/scout.hxx" 36 | #include "api/session.hxx" 37 | #include "api/subscriber.hxx" 38 | #include "api/timestamp.hxx" 39 | #if (defined(ZENOHCXX_ZENOHC) || Z_FEATURE_QUERY == 1) && defined(Z_FEATURE_UNSTABLE_API) 40 | #include "api/querier.hxx" 41 | #endif 42 | #if defined(Z_FEATURE_SHARED_MEMORY) && defined(Z_FEATURE_UNSTABLE_API) 43 | #include "api/shm/shm.hxx" 44 | #endif 45 | #include "api/ext/serialization.hxx" 46 | #if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API) 47 | #include "api/ext/session_ext.hxx" 48 | #include "api/matching.hxx" 49 | #endif 50 | -------------------------------------------------------------------------------- /include/zenoh/api/base.hxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | 14 | #pragma once 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #include "../detail/availability_checks.hxx" 22 | #include "../zenohc.hxx" 23 | 24 | namespace zenoh { 25 | 26 | /// @brief Error code returned by Zenoh API 27 | typedef ::z_result_t ZResult; 28 | 29 | /// @brief Zenoh-specific Exception 30 | class ZException : public std::runtime_error { 31 | public: 32 | ZResult e; 33 | ZException(const std::string& message, ZResult err) 34 | : std::runtime_error(message + "(Error code: " + std::to_string(err) + " )"), e(err) {} 35 | }; 36 | 37 | #define __ZENOH_RESULT_CHECK(err, err_ptr, message) \ 38 | if (err_ptr == nullptr) { \ 39 | ZResult __ze = static_cast(err); \ 40 | if (__ze != Z_OK) throw ZException(message, __ze); \ 41 | } else { \ 42 | *err_ptr = static_cast(err); \ 43 | } 44 | 45 | // 46 | // Template base classes implementing common functionality 47 | // 48 | 49 | /// Base type for C++ wrappers of Zenoh copyable structures, like GetOptions, PutOptions, etc. 50 | /// @tparam ZC_COPYABLE_TYPE - zenoh-c structure type ::z_XXX_t 51 | template 52 | class Copyable { 53 | protected: 54 | typedef ZC_COPYABLE_TYPE InnerType; 55 | InnerType _0; 56 | 57 | InnerType& inner() { return this->_0; } 58 | const InnerType& inner() const { return this->_0; } 59 | explicit Copyable(const InnerType& v) : _0(v) {} 60 | }; 61 | 62 | /// Base type for C++ wrappers of Zenoh owned structures 63 | /// @tparam ZC_OWNED_TYPE - zenoh-c owned type ::z_owned_XXX_t 64 | template 65 | class Owned { 66 | protected: 67 | typedef ZC_OWNED_TYPE OwnedType; 68 | 69 | protected: 70 | /// Move constructor. 71 | Owned(Owned&& v) : Owned(nullptr) { *this = std::move(v); } 72 | /// Move assignment. 73 | Owned& operator=(Owned&& v) { 74 | if (this != &v) { 75 | ::z_drop(::z_move(this->_0)); 76 | if constexpr (detail::is_take_from_loaned_available_v) { 77 | if (::z_internal_check(v._0)) { 78 | ::z_take_from_loaned(&this->_0, ::z_loan_mut(v._0)); 79 | } else { 80 | ::z_internal_null(&this->_0); 81 | } 82 | } else { 83 | z_take(&this->_0, ::z_move(v._0)); 84 | } 85 | } 86 | return *this; 87 | } 88 | /// Destructor drops owned value using z_drop from zenoh API 89 | ~Owned() { ::z_drop(::z_move(_0)); } 90 | 91 | OwnedType _0; 92 | 93 | explicit Owned(OwnedType* pv) { 94 | if (pv != nullptr) { 95 | z_take(&this->_0, ::z_move(*pv)); 96 | } else { 97 | ::z_internal_null(&this->_0); 98 | } 99 | } 100 | }; 101 | 102 | } // namespace zenoh 103 | -------------------------------------------------------------------------------- /include/zenoh/api/closures.hxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2024 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, 13 | 14 | #pragma once 15 | namespace zenoh::closures { 16 | namespace detail { 17 | inline void none() {} 18 | } // namespace detail 19 | static auto none = &detail::none; 20 | using None = decltype(none); 21 | } // namespace zenoh::closures 22 | -------------------------------------------------------------------------------- /include/zenoh/api/ext/publication_cache.hxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2024 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, 13 | 14 | #pragma once 15 | 16 | #if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API) 17 | #include "../../detail/closures_concrete.hxx" 18 | #include "../base.hxx" 19 | #include "../interop.hxx" 20 | #include "../keyexpr.hxx" 21 | #include "../sample.hxx" 22 | 23 | namespace zenoh::ext { 24 | 25 | /// @warning This API is deprecated. Please use zenoh::ext::AdvancedPublisher. 26 | /// @brief A Zenoh publication cache. 27 | /// 28 | /// Used to store publications on intersecting key expressions. Can be queried later via `zenoh::Session::get` to 29 | /// retrieve this data. 30 | /// @note Zenoh-c only 31 | class PublicationCache : public Owned<::ze_owned_publication_cache_t> { 32 | PublicationCache(zenoh::detail::null_object_t) : Owned(nullptr){}; 33 | friend struct interop::detail::Converter; 34 | 35 | public: 36 | /// @name Methods 37 | 38 | /// @brief Get the key expression of the publication cache. 39 | const KeyExpr& get_keyexpr() const { 40 | return interop::as_owned_cpp_ref(::ze_publication_cache_keyexpr(interop::as_loaned_c_ptr(*this))); 41 | } 42 | 43 | /// @brief Undeclare publication cache. 44 | /// @param err if not null, the result code will be written to this location, otherwise ZException exception will be 45 | /// thrown in case of error. 46 | void undeclare(ZResult* err = nullptr) && { 47 | __ZENOH_RESULT_CHECK(::ze_undeclare_publication_cache(interop::as_moved_c_ptr(*this)), err, 48 | "Failed to undeclare Publication Cache"); 49 | } 50 | }; 51 | 52 | } // namespace zenoh::ext 53 | #endif 54 | -------------------------------------------------------------------------------- /include/zenoh/api/hello.hxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2024 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, 13 | 14 | #pragma once 15 | #include 16 | #include 17 | 18 | #include "../zenohc.hxx" 19 | #include "base.hxx" 20 | #include "enums.hxx" 21 | #include "id.hxx" 22 | #include "interop.hxx" 23 | 24 | namespace zenoh { 25 | /// ``Hello`` message returned by a zenoh entity as a reply to a "scout" 26 | /// message. 27 | class Hello : public Owned<::z_owned_hello_t> { 28 | public: 29 | /// @name Methods 30 | 31 | /// @brief Get ``Id`` of the entity. 32 | /// @return ``Id`` of the entity. 33 | Id get_id() const { return interop::into_copyable_cpp_obj(::z_hello_zid(interop::as_loaned_c_ptr(*this))); }; 34 | 35 | /// @brief Get the type of the entity. 36 | /// @return ``zenoh::WhatAmI`` of the entity. 37 | WhatAmI get_whatami() const { return ::z_hello_whatami(interop::as_loaned_c_ptr(*this)); } 38 | 39 | /// @brief Get the array of locators of the entity. 40 | /// @return the array of locators of the entity. 41 | std::vector get_locators() const { 42 | #ifdef ZENOHCXX_ZENOHC 43 | ::z_owned_string_array_t out; 44 | ::z_hello_locators(interop::as_loaned_c_ptr(*this), &out); 45 | auto out_loaned = ::z_loan(out); 46 | #else 47 | auto out_loaned = ::zp_hello_locators(interop::as_loaned_c_ptr(*this)); 48 | #endif 49 | std::vector locators(::z_string_array_len(out_loaned)); 50 | for (size_t i = 0; i < ::z_string_array_len(out_loaned); i++) { 51 | auto s = ::z_string_array_get(out_loaned, i); 52 | locators[i] = std::string_view(reinterpret_cast(::z_string_data(s)), ::z_string_len(s)); 53 | } 54 | #ifdef ZENOHCXX_ZENOHC 55 | z_drop(z_move(out)); 56 | #endif 57 | return locators; 58 | } 59 | 60 | /// @brief Copy constructor. 61 | Hello(const Hello& other) : Owned(nullptr) { ::z_hello_clone(&this->_0, interop::as_loaned_c_ptr(other)); }; 62 | 63 | /// @brief Move constructor. 64 | Hello(Hello&& other) = default; 65 | 66 | /// @name Operators 67 | 68 | /// @brief Assignment operator. 69 | Hello& operator=(const Hello& other) { 70 | if (this != &other) { 71 | ::z_drop(z_move(this->_0)); 72 | ::z_hello_clone(&this->_0, interop::as_loaned_c_ptr(other)); 73 | } 74 | return *this; 75 | }; 76 | 77 | /// @brief Move assignment operator. 78 | Hello& operator=(Hello&& other) = default; 79 | }; 80 | } // namespace zenoh 81 | -------------------------------------------------------------------------------- /include/zenoh/api/id.hxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2024 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, 13 | 14 | #pragma once 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #include "../zenohc.hxx" 22 | #include "base.hxx" 23 | #include "interop.hxx" 24 | 25 | namespace zenoh { 26 | /// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release. 27 | /// @brief A representation of a Zenoh ID. 28 | /// 29 | /// In general, valid Zenoh IDs are LSB-first 128bit unsigned and non-zero integers. 30 | class Id : public Copyable<::z_id_t> { 31 | using Copyable::Copyable; 32 | friend struct interop::detail::Converter; 33 | 34 | public: 35 | /// @name Methods 36 | 37 | /// Return the byte sequence of the ``Id``. 38 | const std::array& bytes() const { return *reinterpret_cast*>(&_0.id); } 39 | 40 | /// @brief Formats the ``Id`` into 16-digit hex string (LSB-first order). 41 | std::string to_string() const { 42 | ::z_owned_string_t s; 43 | ::z_id_to_string(interop::as_copyable_c_ptr(*this), &s); 44 | std::string ss(::z_string_data(::z_loan(s)), ::z_string_len(::z_loan(s))); 45 | ::z_drop(::z_move(s)); 46 | return ss; 47 | } 48 | }; 49 | 50 | /// @brief Print ``Id`` in the hex format. 51 | inline std::ostream& operator<<(std::ostream& os, const Id& id) { 52 | ::z_owned_string_t s; 53 | ::z_id_to_string(interop::as_copyable_c_ptr(id), &s); 54 | os << std::string_view(::z_string_data(::z_loan(s)), ::z_string_len(::z_loan(s))); 55 | ::z_drop(::z_move(s)); 56 | return os; 57 | } 58 | } // namespace zenoh -------------------------------------------------------------------------------- /include/zenoh/api/liveliness.hxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2024 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, 13 | 14 | #pragma once 15 | 16 | #include "base.hxx" 17 | #include "interop.hxx" 18 | 19 | namespace zenoh { 20 | 21 | class Session; 22 | 23 | /// @brief A liveliness token that can be used to provide the network with information about connectivity to its 24 | /// declarer. 25 | /// 26 | /// When constructed, a PUT sample will be received by liveliness subscribers on intersecting key expressions. 27 | /// 28 | /// A DELETE on the token's key expression will be received by subscribers if the token is destroyed, or if connectivity 29 | /// between the subscriber and the token's creator is lost. 30 | class LivelinessToken : public Owned<::z_owned_liveliness_token_t> { 31 | LivelinessToken(zenoh::detail::null_object_t) : Owned(nullptr){}; 32 | friend struct interop::detail::Converter; 33 | 34 | public: 35 | /// Undeclares liveliness token, resetting it to gravestone state. 36 | /// @param err if not null, the result code will be written to this location, otherwise ZException exception will be 37 | /// thrown in case of error. 38 | void undeclare(ZResult* err = nullptr) && { 39 | __ZENOH_RESULT_CHECK(::z_liveliness_undeclare_token(interop::as_moved_c_ptr(*this)), err, 40 | "Failed to undeclare liveliness token"); 41 | } 42 | }; 43 | 44 | } // namespace zenoh -------------------------------------------------------------------------------- /include/zenoh/api/logging.hxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2024 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, 13 | 14 | #pragma once 15 | #include "../zenohc.hxx" 16 | 17 | namespace zenoh { 18 | 19 | #ifdef ZENOHCXX_ZENOHC 20 | /// @brief Initializes the zenoh runtime logger, using rust environment settings. 21 | /// E.g.: `RUST_LOG=info` will enable logging at info level. Similarly, you can set the variable to `error` or `debug`. 22 | /// Note that if the environment variable is not set, then logging will not be enabled. 23 | /// See https://docs.rs/env_logger/latest/env_logger/index.html for accepted filter format. 24 | /// @note Zenoh-c only. 25 | inline void try_init_log_from_env() { ::zc_try_init_log_from_env(); } 26 | 27 | /// @brief Initializes the zenoh runtime logger, using rust environment settings or the provided fallback filter. 28 | /// E.g.: `RUST_LOG=info` will enable logging at info level. Similarly, you can set the variable to `error` or `debug`. 29 | /// 30 | /// Note that if the environment variable is not set, then fallback filter will be used instead. 31 | /// See https://docs.rs/env_logger/latest/env_logger/index.html for accepted filter format. 32 | /// 33 | /// @param fallback_filter: The fallback filter if the `RUST_LOG` environment variable is not set. 34 | /// @note Zenoh-c only. 35 | inline void init_log_from_env_or(const std::string& fallback_filter) { 36 | ::zc_init_log_from_env_or(fallback_filter.c_str()); 37 | } 38 | #endif 39 | 40 | } // namespace zenoh -------------------------------------------------------------------------------- /include/zenoh/api/query_consolidation.hxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2024 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, 13 | 14 | #pragma once 15 | 16 | #if defined(ZENOHCXX_ZENOHC) || Z_FEATURE_QUERY == 1 17 | 18 | #include "base.hxx" 19 | #include "enums.hxx" 20 | #include "interop.hxx" 21 | 22 | namespace zenoh { 23 | 24 | /// Replies consolidation mode to apply on replies of get operation. 25 | class QueryConsolidation : public Copyable<::z_query_consolidation_t> { 26 | using Copyable::Copyable; 27 | friend struct interop::detail::Converter; 28 | 29 | public: 30 | /// @name Constructors 31 | 32 | /// @brief Create a new default ``QueryConsolidation`` value. 33 | QueryConsolidation() : Copyable(::z_query_consolidation_default()) {} 34 | 35 | /// @brief Create a new ``QueryConsolidation`` value with the given consolidation mode. 36 | /// @param v ``zenoh::ConsolidationMode`` value. 37 | QueryConsolidation(ConsolidationMode v) : Copyable({v}) {} 38 | 39 | /// @name Methods 40 | 41 | /// @name Operators 42 | 43 | /// @brief Equality relation. 44 | /// @param other a value to compare with. 45 | /// @return ``true`` if the two values are equal (have the same consolidation mode). 46 | bool operator==(const QueryConsolidation& other) const { return this->_0.mode == other._0.mode; } 47 | 48 | /// @brief Inequality relation. 49 | /// @param other a value to compare with. 50 | /// @return ``true`` if the two values are not equal (have different consolidation mode) 51 | bool operator!=(const QueryConsolidation& other) const { return !operator==(other); } 52 | }; 53 | } // namespace zenoh 54 | #endif -------------------------------------------------------------------------------- /include/zenoh/api/scout.hxx: -------------------------------------------------------------------------------- 1 | 2 | 3 | #pragma once 4 | 5 | #include "../detail/closures_concrete.hxx" 6 | #include "base.hxx" 7 | #include "config.hxx" 8 | #include "enums.hxx" 9 | #include "interop.hxx" 10 | 11 | namespace zenoh { 12 | 13 | /// @brief Options to be passed to ``scout`` operation 14 | struct ScoutOptions { 15 | /// @name Fields 16 | 17 | /// @brief The maximum duration in ms the scouting can take. 18 | size_t timeout_ms = 1000; 19 | /// @brief Type of entities to scout for. 20 | What what = What::Z_WHAT_ROUTER_PEER; 21 | 22 | /// @name Methods 23 | 24 | /// @brief Create default option settings. 25 | static ScoutOptions create_default() { return {}; } 26 | }; 27 | 28 | /// @brief Scout for zenoh entities in the network. 29 | /// @param config ``Config`` to use for scouting. 30 | /// @param on_hello the callable to process each received ``Hello``message. 31 | /// @param on_drop the callable that will be called once all ``Hello`` messages are received. 32 | template 33 | void scout(Config&& config, C&& on_hello, D&& on_drop, ScoutOptions&& options = ScoutOptions::create_default(), 34 | ZResult* err = nullptr) { 35 | static_assert(std::is_invocable_r::value, 36 | "on_hello should be callable with the following signature: void on_hello(zenoh::Hello& hello)"); 37 | static_assert(std::is_invocable_r::value, 38 | "on_drop should be callable with the following signature: void on_drop()"); 39 | ::z_owned_closure_hello_t c_closure; 40 | using Cval = std::remove_reference_t; 41 | using Dval = std::remove_reference_t; 42 | using ClosureType = typename detail::closures::Closure; 43 | auto closure = ClosureType::into_context(std::forward(on_hello), std::forward(on_drop)); 44 | ::z_closure(&c_closure, detail::closures::_zenoh_on_hello_call, detail::closures::_zenoh_on_drop, closure); 45 | ::z_scout_options_t opts; 46 | opts.timeout_ms = options.timeout_ms; 47 | opts.what = options.what; 48 | 49 | __ZENOH_RESULT_CHECK(::z_scout(interop::as_moved_c_ptr(config), ::z_move(c_closure), &opts), err, 50 | "Failed to perform scout operation"); 51 | } 52 | 53 | } // namespace zenoh -------------------------------------------------------------------------------- /include/zenoh/api/shm/buffer/buffer.hxx: -------------------------------------------------------------------------------- 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, 13 | // 14 | 15 | #pragma once 16 | 17 | #include "zshm.hxx" 18 | #include "zshmmut.hxx" -------------------------------------------------------------------------------- /include/zenoh/api/shm/buffer/zshm.hxx: -------------------------------------------------------------------------------- 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, 13 | // 14 | 15 | #pragma once 16 | 17 | #include 18 | #include 19 | 20 | #include "../../base.hxx" 21 | #include "../../interop.hxx" 22 | #include "zshmmut.hxx" 23 | 24 | namespace zenoh { 25 | 26 | /// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release. 27 | /// @brief An immutable SHM buffer 28 | class ZShm : public Owned<::z_owned_shm_t> { 29 | friend class ZShmMut; 30 | ZShm(zenoh::detail::null_object_t) : Owned(nullptr) {} 31 | friend struct interop::detail::Converter; 32 | 33 | public: 34 | /// @name Constructors 35 | 36 | /// @brief Create a new ZShm from ZShmMut. 37 | /// @param mut mutable buffer. 38 | ZShm(ZShmMut&& mut) : Owned(nullptr) { ::z_shm_from_mut(&this->_0, ::z_move(mut._0)); } 39 | 40 | /// @brief Create a new ZShm from ZShm by performing a shallow SHM reference copy. 41 | /// @param other ZShm to copy 42 | ZShm(const ZShm& other) : Owned(nullptr) { ::z_shm_clone(&this->_0, interop::as_loaned_c_ptr(other)); } 43 | 44 | /// @name Methods 45 | 46 | /// @brief Get buffer's const data. It is completely unsafe to to modify SHM data without using ZShmMut interface. 47 | /// @return pointer to the underlying data. 48 | const uint8_t* data() const { return ::z_shm_data(interop::as_loaned_c_ptr(*this)); } 49 | 50 | /// @brief Get buffer's data size. 51 | /// @return underlying data size. 52 | std::size_t len() const { return ::z_shm_len(interop::as_loaned_c_ptr(*this)); } 53 | 54 | /// @brief Create a new ZShmMut from ZShm. 55 | /// @param immut immutable buffer, NOTE: the value will not be moved if nullopt returned. 56 | /// @return mutable buffer or empty option if buffer mutation is impossible. 57 | static std::optional try_mutate(ZShm&& immut) { 58 | z_owned_shm_mut_t mut_inner; 59 | if (Z_OK == ::z_shm_mut_try_from_immut(&mut_inner, z_move(immut._0), &immut._0)) { 60 | return std::move(interop::as_owned_cpp_ref(&mut_inner)); 61 | } 62 | return std::nullopt; 63 | } 64 | 65 | /// @brief Create a new ZShmMut& from ZShm&. 66 | /// @param immut immutable buffer, NOTE: the value will not be moved if nullopt returned. 67 | /// @return mutable buffer or empty option if buffer mutation is impossible. 68 | static std::optional> try_mutate(ZShm& immut) { 69 | if (z_loaned_shm_mut_t* shm_mut = ::z_shm_try_reloan_mut(z_loan_mut(immut._0))) { 70 | return std::ref(interop::as_owned_cpp_ref(shm_mut)); 71 | } 72 | return std::nullopt; 73 | } 74 | }; 75 | 76 | } // end of namespace zenoh 77 | -------------------------------------------------------------------------------- /include/zenoh/api/shm/buffer/zshmmut.hxx: -------------------------------------------------------------------------------- 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, 13 | // 14 | 15 | #pragma once 16 | 17 | #include "../../base.hxx" 18 | 19 | namespace zenoh { 20 | 21 | /// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release. 22 | /// @brief A mutable SHM buffer 23 | class ZShmMut : public Owned<::z_owned_shm_mut_t> { 24 | friend class ZShm; 25 | 26 | public: 27 | /// @name Methods 28 | 29 | /// @brief Get buffer's const data. 30 | /// @return pointer to the underlying data. 31 | const uint8_t* data() const { return ::z_shm_mut_data(interop::as_loaned_c_ptr(*this)); } 32 | 33 | /// @brief Get buffer's data. 34 | /// @return pointer to the underlying data. 35 | uint8_t* data() { return ::z_shm_mut_data_mut(interop::as_loaned_c_ptr(*this)); } 36 | 37 | /// @brief Get buffer's data size. 38 | /// @return underlying data size. 39 | std::size_t len() const { return ::z_shm_mut_len(interop::as_loaned_c_ptr(*this)); } 40 | }; 41 | 42 | } // end of namespace zenoh 43 | -------------------------------------------------------------------------------- /include/zenoh/api/shm/cleanup.hxx: -------------------------------------------------------------------------------- 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, 13 | // 14 | 15 | #pragma once 16 | 17 | namespace zenoh { 18 | 19 | /// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release. 20 | /// 21 | /// @brief Linux: Trigger cleanup for orphaned SHM segments. 22 | /// If process that created named SHM segment crashes or exits by a signal, the segment persists in the system 23 | /// disregarding if it is used by other Zenoh processes or not. This is the detail of POSIX specification for 24 | /// shared memory that is hard to bypass. To deal with this we developed a cleanup routine that enumerates all 25 | /// segments and tries to find processes that are using it. If no such process found, segment will be removed. 26 | /// There is no ideal signal to trigger this cleanup, so by default, zenoh triggers it in the following moments: 27 | /// - first POSIX SHM segment creation 28 | /// - process exit via exit() call or return from maint function 29 | /// It is OK to additionally trigger this function at any time, but be aware that this can be costly. 30 | /// 31 | /// For non-linux platforms this function currently does nothing. 32 | inline void cleanup_orphaned_shm_segments() { ::zc_cleanup_orphaned_shm_segments(); } 33 | 34 | } // end of namespace zenoh 35 | -------------------------------------------------------------------------------- /include/zenoh/api/shm/client/client.hxx: -------------------------------------------------------------------------------- 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, 13 | // 14 | 15 | #pragma once 16 | 17 | #include "shm_client.hxx" 18 | #include "shm_segment.hxx" -------------------------------------------------------------------------------- /include/zenoh/api/shm/client/shm_client.hxx: -------------------------------------------------------------------------------- 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, 13 | // 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "../../base.hxx" 20 | #include "../common/types.hxx" 21 | #include "shm_segment.hxx" 22 | 23 | namespace zenoh { 24 | 25 | /// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release. 26 | /// @brief An interface for making custom SHM clients 27 | class CppShmClient { 28 | public: 29 | virtual std::unique_ptr attach(SegmentId segment_id) = 0; 30 | virtual ProtocolId id() const = 0; 31 | virtual ~CppShmClient() = default; 32 | }; 33 | 34 | // Ensure that function pointers are defined with extern C linkage 35 | namespace shm::client::closures { 36 | extern "C" { 37 | inline bool _z_cpp_shm_client_attach_fn(struct z_shm_segment_t* out_segment, z_segment_id_t id, void* context) { 38 | if (auto segment = static_cast(context)->attach(id)) { 39 | out_segment->context.context.ptr = segment.release(); 40 | out_segment->context.delete_fn = &shm::segment::closures::_z_cpp_shm_segment_drop_fn; 41 | out_segment->callbacks.map_fn = &shm::segment::closures::_z_cpp_shm_segment_map_fn; 42 | return true; 43 | } 44 | return false; 45 | } 46 | 47 | inline ProtocolId _z_cpp_shm_client_id_fn(void* context) { return static_cast(context)->id(); } 48 | 49 | inline void _z_cpp_shm_client_drop_fn(void* context) { delete static_cast(context); } 50 | } 51 | } // namespace shm::client::closures 52 | 53 | /// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release. 54 | /// @brief An SHM client for reading shared memory buffers 55 | class ShmClient : public Owned<::z_owned_shm_client_t> { 56 | friend class ShmClientStorage; 57 | 58 | public: 59 | /// @name Constructors 60 | 61 | /// @brief Create a new CPP-defined ShmClient. 62 | ShmClient(std::unique_ptr&& cpp_interface) : Owned(nullptr) { 63 | zc_threadsafe_context_t context = {{cpp_interface.release()}, 64 | &shm::client::closures::_z_cpp_shm_client_drop_fn}; 65 | zc_shm_client_callbacks_t callbacks = { 66 | &shm::client::closures::_z_cpp_shm_client_attach_fn, 67 | &shm::client::closures::_z_cpp_shm_client_id_fn, 68 | }; 69 | z_shm_client_new(&this->_0, context, callbacks); 70 | } 71 | }; 72 | 73 | } // end of namespace zenoh 74 | -------------------------------------------------------------------------------- /include/zenoh/api/shm/client/shm_segment.hxx: -------------------------------------------------------------------------------- 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, 13 | // 14 | 15 | #pragma once 16 | 17 | namespace zenoh { 18 | 19 | /// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release. 20 | /// @brief An interface for accessing custom SHM segments 21 | class CppShmSegment { 22 | public: 23 | virtual uint8_t* map(z_chunk_id_t chunk_id) = 0; 24 | virtual ~CppShmSegment() = default; 25 | }; 26 | 27 | // Ensure that function pointers are defined with extern C linkage 28 | namespace shm::segment::closures { 29 | extern "C" { 30 | inline uint8_t* _z_cpp_shm_segment_map_fn(z_chunk_id_t chunk, void* context) { 31 | return static_cast(context)->map(chunk); 32 | } 33 | inline void _z_cpp_shm_segment_drop_fn(void* context) { delete static_cast(context); } 34 | } 35 | } // namespace shm::segment::closures 36 | 37 | } // end of namespace zenoh 38 | -------------------------------------------------------------------------------- /include/zenoh/api/shm/common/common.hxx: -------------------------------------------------------------------------------- 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, 13 | // 14 | 15 | #pragma once 16 | 17 | #include "types.hxx" -------------------------------------------------------------------------------- /include/zenoh/api/shm/common/types.hxx: -------------------------------------------------------------------------------- 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, 13 | // 14 | 15 | #pragma once 16 | 17 | namespace zenoh { 18 | 19 | /// Unique protocol identifier. 20 | /// Here is a contract: it is up to user to make sure that incompatible ShmClient 21 | /// and CppShmProviderBackend implementations will never use the same ProtocolID. 22 | typedef z_protocol_id_t ProtocolId; 23 | 24 | /// Unique segment identifier. 25 | typedef z_segment_id_t SegmentId; 26 | 27 | /// Chunk id within it's segment. 28 | typedef z_chunk_id_t ChunkId; 29 | 30 | } // end of namespace zenoh 31 | -------------------------------------------------------------------------------- /include/zenoh/api/shm/protocol_implementations/posix/posix.hxx: -------------------------------------------------------------------------------- 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, 13 | // 14 | 15 | #pragma once 16 | 17 | #include "posix_shm_client.hxx" 18 | #include "posix_shm_provider.hxx" 19 | -------------------------------------------------------------------------------- /include/zenoh/api/shm/protocol_implementations/posix/posix_shm_client.hxx: -------------------------------------------------------------------------------- 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, 13 | // 14 | #pragma once 15 | 16 | #include "../../client/shm_client.hxx" 17 | 18 | namespace zenoh { 19 | 20 | /// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release. 21 | /// @brief Client factory implementation for particular shared memory protocol 22 | class PosixShmClient : public ShmClient { 23 | public: 24 | /// @name Constructors 25 | 26 | /// @brief Create a new PosixShmClient. 27 | PosixShmClient() : ShmClient(nullptr) { ::z_posix_shm_client_new(&this->_0); } 28 | }; 29 | 30 | } // end of namespace zenoh 31 | -------------------------------------------------------------------------------- /include/zenoh/api/shm/protocol_implementations/posix/posix_shm_provider.hxx: -------------------------------------------------------------------------------- 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, 13 | // 14 | 15 | #pragma once 16 | 17 | #include "../../provider/shm_provider.hxx" 18 | 19 | namespace zenoh { 20 | 21 | /// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release. 22 | /// @brief An SHM provider implementing zenoh-standard POSIX shared memory protocol 23 | class PosixShmProvider : public ShmProvider { 24 | public: 25 | using ShmProvider::ShmProvider; 26 | 27 | /// @name Constructors 28 | 29 | /// @brief Create a new PosixShmProvider. 30 | /// @param size size of POSIX shared memory segment to be allocated and used by the provider 31 | /// @param err if not null, the result code will be written to this location, otherwise ZException exception will be 32 | /// thrown in case of error. 33 | PosixShmProvider(std::size_t size, ZResult* err = nullptr) : ShmProvider(zenoh::detail::null_object) { 34 | __ZENOH_RESULT_CHECK(::z_posix_shm_provider_new(&this->_0, size), err, "Failed to create POSIX SHM provider"); 35 | } 36 | 37 | /// @brief Create a new PosixShmProvider. 38 | /// @param layout layout for POSIX shared memory segment to be allocated and used by the provider 39 | /// @param err if not null, the result code will be written to this location, otherwise ZException exception will be 40 | /// thrown in case of error. 41 | PosixShmProvider(const MemoryLayout& layout, ZResult* err = nullptr) : ShmProvider(zenoh::detail::null_object) { 42 | __ZENOH_RESULT_CHECK(::z_posix_shm_provider_with_layout_new(&this->_0, interop::as_loaned_c_ptr(layout)), err, 43 | "Failed to create POSIX SHM provider"); 44 | } 45 | }; 46 | 47 | } // end of namespace zenoh 48 | -------------------------------------------------------------------------------- /include/zenoh/api/shm/protocol_implementations/protocol_implementations.hxx: -------------------------------------------------------------------------------- 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, 13 | // 14 | 15 | #pragma once 16 | 17 | #include "posix/posix.hxx" -------------------------------------------------------------------------------- /include/zenoh/api/shm/provider/chunk.hxx: -------------------------------------------------------------------------------- 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, 13 | // 14 | #pragma once 15 | 16 | namespace zenoh { 17 | 18 | /// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release. 19 | /// @brief A ChunkDescriptor. 20 | typedef z_chunk_descriptor_t ChunkDescriptor; 21 | 22 | /// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release. 23 | /// @brief An AllocatedChunk. 24 | typedef z_allocated_chunk_t AllocatedChunk; 25 | 26 | } // end of namespace zenoh 27 | -------------------------------------------------------------------------------- /include/zenoh/api/shm/provider/provider.hxx: -------------------------------------------------------------------------------- 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, 13 | // 14 | 15 | #pragma once 16 | 17 | #include "alloc_layout.hxx" 18 | #include "chunk.hxx" 19 | #include "shm_provider.hxx" 20 | #include "shm_provider_backend.hxx" 21 | #include "types.hxx" 22 | -------------------------------------------------------------------------------- /include/zenoh/api/shm/provider/shm_provider_backend.hxx: -------------------------------------------------------------------------------- 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, 13 | // 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "../../base.hxx" 20 | #include "../../interop.hxx" 21 | #include "chunk.hxx" 22 | #include "types.hxx" 23 | 24 | namespace zenoh { 25 | 26 | class CppShmProviderBackendIface { 27 | public: 28 | virtual ChunkAllocResult alloc(const MemoryLayout &layout) = 0; 29 | virtual void free(const ChunkDescriptor &chunk) = 0; 30 | virtual size_t defragment() = 0; 31 | virtual size_t available() const = 0; 32 | virtual void layout_for(MemoryLayout &layout) = 0; 33 | virtual ProtocolId id() const = 0; 34 | virtual ~CppShmProviderBackendIface() = default; 35 | }; 36 | 37 | class CppShmProviderBackend : public CppShmProviderBackendIface {}; 38 | 39 | class CppShmProviderBackendThreadsafe : public CppShmProviderBackend {}; 40 | 41 | // Ensure that function pointers are defined with extern C linkage 42 | namespace shm::provider_backend::closures { 43 | extern "C" { 44 | inline void _z_cpp_shm_provider_backend_drop_fn(void *context) { delete static_cast(context); } 45 | inline void _z_cpp_shm_provider_backend_alloc_fn(struct z_owned_chunk_alloc_result_t *out_result, 46 | const struct z_loaned_memory_layout_t *layout, void *context) { 47 | *out_result = interop::move_to_c_obj( 48 | static_cast(context)->alloc(interop::as_owned_cpp_ref(layout))); 49 | } 50 | inline void _z_cpp_shm_provider_backend_free_fn(const struct z_chunk_descriptor_t *chunk, void *context) { 51 | static_cast(context)->free(*chunk); 52 | } 53 | inline size_t _z_cpp_shm_provider_backend_defragment_fn(void *context) { 54 | return static_cast(context)->defragment(); 55 | } 56 | inline size_t _z_cpp_shm_provider_backend_available_fn(void *context) { 57 | return static_cast(context)->available(); 58 | } 59 | inline void _z_cpp_shm_provider_backend_layout_for_fn(struct z_owned_memory_layout_t *layout, void *context) { 60 | static_cast(context)->layout_for(interop::as_owned_cpp_ref(layout)); 61 | } 62 | inline ProtocolId _z_cpp_shm_provider_backend_id_fn(void *context) { 63 | return static_cast(context)->id(); 64 | } 65 | } 66 | } // namespace shm::provider_backend::closures 67 | 68 | } // end of namespace zenoh 69 | -------------------------------------------------------------------------------- /include/zenoh/api/shm/provider/types_impl.hxx: -------------------------------------------------------------------------------- 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, 13 | // 14 | 15 | #pragma once 16 | 17 | #include "../../interop.hxx" 18 | #include "types.hxx" 19 | 20 | namespace zenoh { 21 | 22 | struct Converters { 23 | static inline BufLayoutAllocResult from(z_buf_layout_alloc_result_t& c_result) { 24 | switch (c_result.status) { 25 | case zc_buf_layout_alloc_status_t::ZC_BUF_LAYOUT_ALLOC_STATUS_OK: 26 | return std::move(interop::as_owned_cpp_ref(&c_result.buf)); 27 | case zc_buf_layout_alloc_status_t::ZC_BUF_LAYOUT_ALLOC_STATUS_LAYOUT_ERROR: 28 | return c_result.layout_error; 29 | case zc_buf_layout_alloc_status_t::ZC_BUF_LAYOUT_ALLOC_STATUS_ALLOC_ERROR: 30 | default: 31 | return c_result.alloc_error; 32 | } 33 | } 34 | 35 | static inline BufAllocResult from(z_buf_alloc_result_t& c_result) { 36 | switch (c_result.status) { 37 | case zc_buf_alloc_status_t::ZC_BUF_ALLOC_STATUS_OK: 38 | return std::move(interop::as_owned_cpp_ref(&c_result.buf)); 39 | case zc_buf_alloc_status_t::ZC_BUF_ALLOC_STATUS_ALLOC_ERROR: 40 | default: 41 | return c_result.error; 42 | } 43 | } 44 | }; 45 | 46 | } // end of namespace zenoh 47 | -------------------------------------------------------------------------------- /include/zenoh/api/shm/shm.hxx: -------------------------------------------------------------------------------- 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, 13 | 14 | // 15 | // This file contains structures and classes API without implementations 16 | // 17 | 18 | // Do not add '#pragma once' and '#include` statements here 19 | // as this file is included multiple times into different namespaces 20 | 21 | #pragma once 22 | 23 | #include "buffer/buffer.hxx" 24 | #include "cleanup.hxx" 25 | #include "client/client.hxx" 26 | #include "client_storage/client_storage.hxx" 27 | #include "protocol_implementations/protocol_implementations.hxx" 28 | #include "provider/provider.hxx" -------------------------------------------------------------------------------- /include/zenoh/api/source_info.hxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2024 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, 13 | 14 | #pragma once 15 | 16 | #include "../zenohc.hxx" 17 | #include "base.hxx" 18 | #include "id.hxx" 19 | #include "interop.hxx" 20 | 21 | namespace zenoh { 22 | /// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release. 23 | /// @brief The global unique id of a Zenoh entity. 24 | /// @note Zenoh-c only. 25 | class EntityGlobalId : public Copyable<::z_entity_global_id_t> { 26 | using Copyable::Copyable; 27 | friend struct interop::detail::Converter; 28 | 29 | public: 30 | /// @name Methods 31 | 32 | /// Get Zenoh id. 33 | Id id() const { return interop::into_copyable_cpp_obj(::z_entity_global_id_zid(&this->inner())); } 34 | 35 | /// Get eid. 36 | uint32_t eid() const { return ::z_entity_global_id_eid(&this->inner()); } 37 | }; 38 | 39 | /// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release. 40 | /// @brief Informations on the Zenoh source. 41 | /// @note Zenoh-c only. 42 | class SourceInfo : public Owned<::z_owned_source_info_t> { 43 | public: 44 | /// @name Constructors 45 | 46 | /// @brief Construct from global id and sequence number. 47 | SourceInfo(const EntityGlobalId& id, uint32_t sn) : Owned(nullptr) { 48 | ::z_source_info_new(&this->_0, interop::as_copyable_c_ptr(id), sn); 49 | } 50 | 51 | /// @name Methods 52 | 53 | /// @brief Get the source id. 54 | EntityGlobalId id() const { 55 | return interop::into_copyable_cpp_obj(::z_source_info_id(interop::as_loaned_c_ptr(*this))); 56 | } 57 | 58 | /// @brief Get the sequence number of the sample from the given source. 59 | uint32_t sn() const { return ::z_source_info_sn(interop::as_loaned_c_ptr(*this)); } 60 | }; 61 | } // namespace zenoh 62 | -------------------------------------------------------------------------------- /include/zenoh/api/timestamp.hxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2024 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, 13 | 14 | #pragma once 15 | #include "../zenohc.hxx" 16 | #include "base.hxx" 17 | #include "id.hxx" 18 | #include "interop.hxx" 19 | 20 | namespace zenoh { 21 | /// Zenoh Timestamp. 22 | class Timestamp : public Copyable<::z_timestamp_t> { 23 | using Copyable::Copyable; 24 | friend struct interop::detail::Converter; 25 | 26 | public: 27 | /// @name Methods 28 | 29 | /// @brief Get the NTP64 time part of the timestamp. 30 | /// @return time in NTP64 format. 31 | uint64_t get_time() const { return ::z_timestamp_ntp64_time(&this->inner()); } 32 | 33 | /// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future 34 | /// release. 35 | /// @brief Get the unique id of the timestamp. 36 | /// @return session id associated with this timestamp. 37 | Id get_id() const { return interop::into_copyable_cpp_obj(::z_timestamp_id(&this->inner())); } 38 | }; 39 | 40 | } // namespace zenoh -------------------------------------------------------------------------------- /include/zenoh/detail/availability_checks.hxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2024 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, 13 | #pragma once 14 | 15 | #include 16 | #include 17 | 18 | #include "../zenohc.hxx" 19 | 20 | // namespace zenoh::detail 21 | namespace zenoh::detail { 22 | template 23 | struct is_loan_available : std::false_type {}; 24 | 25 | template 26 | struct is_loan_available()))>> : std::true_type {}; 27 | 28 | template 29 | inline constexpr bool is_loan_available_v = is_loan_available::value; 30 | 31 | template 32 | struct is_loan_mut_available : std::false_type {}; 33 | 34 | template 35 | struct is_loan_mut_available()))>> : std::true_type {}; 36 | 37 | template 38 | inline constexpr bool is_loan_mut_available_v = is_loan_mut_available::value; 39 | 40 | template 41 | struct is_take_from_loaned_available : std::false_type {}; 42 | 43 | template 44 | struct is_take_from_loaned_available< 45 | T, std::void_t(), std::declval::type*>()))>> : std::true_type {}; 47 | 48 | template 49 | inline constexpr bool is_take_from_loaned_available_v = is_take_from_loaned_available::value; 50 | 51 | struct null_object_t {}; 52 | inline constexpr null_object_t null_object{}; 53 | 54 | } // namespace zenoh::detail 55 | -------------------------------------------------------------------------------- /include/zenoh/detail/closures.hxx: -------------------------------------------------------------------------------- 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, 13 | 14 | #pragma once 15 | 16 | #include 17 | #include 18 | 19 | namespace zenoh::detail::closures { 20 | 21 | struct IDroppable { 22 | virtual void drop() = 0; 23 | virtual ~IDroppable(){}; 24 | 25 | static void delete_from_context(void* context) { 26 | reinterpret_cast(context)->drop(); 27 | delete reinterpret_cast(context); 28 | } 29 | 30 | void* as_context() { 31 | auto d = static_cast(this); 32 | return reinterpret_cast(d); 33 | } 34 | }; 35 | 36 | template 37 | struct IClosure : public IDroppable { 38 | virtual R call(Args... args) = 0; 39 | 40 | static R call_from_context(void* context, Args... args) { 41 | IDroppable* d = reinterpret_cast(context); 42 | return static_cast*>(d)->call(args...); 43 | } 44 | }; 45 | 46 | template 47 | class Droppable : public IDroppable { 48 | typename std::conditional_t, D, std::remove_reference_t> _drop; 49 | 50 | public: 51 | template 52 | Droppable(DD&& drop) : _drop(std::forward
(drop)) {} 53 | 54 | virtual void drop() override { return _drop(); } 55 | 56 | template 57 | static void* into_context(DD&& drop) { 58 | auto obj = new Droppable(std::forward
(drop)); 59 | return obj->as_context(); 60 | } 61 | }; 62 | 63 | template 64 | class Closure : public IClosure { 65 | typename std::conditional_t, C, std::remove_reference_t> _call; 66 | typename std::conditional_t, D, std::remove_reference_t> _drop; 67 | 68 | public: 69 | template 70 | Closure(CC&& call, DD&& drop) : _call(std::forward(call)), _drop(std::forward
(drop)) {} 71 | 72 | virtual R call(Args... args) override { return _call(std::forward(args)...); } 73 | 74 | virtual void drop() override { return _drop(); } 75 | 76 | template 77 | static void* into_context(CC&& call, DD&& drop) { 78 | auto obj = new Closure(std::forward(call), std::forward
(drop)); 79 | return obj->as_context(); 80 | } 81 | }; 82 | 83 | } // namespace zenoh::detail::closures -------------------------------------------------------------------------------- /include/zenoh/detail/closures_concrete.hxx: -------------------------------------------------------------------------------- 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, 13 | 14 | #pragma once 15 | 16 | #include "../api/hello.hxx" 17 | #include "../api/id.hxx" 18 | #include "../api/interop.hxx" 19 | #include "../api/query.hxx" 20 | #include "../api/reply.hxx" 21 | #include "../api/sample.hxx" 22 | #include "../zenohc.hxx" 23 | #include "closures.hxx" 24 | 25 | // Ensure that function pointers are defined with extern C linkage 26 | namespace zenoh::detail::closures { 27 | extern "C" { 28 | inline void _zenoh_on_drop(void* context) { IDroppable::delete_from_context(context); } 29 | #if defined(ZENOHCXX_ZENOHC) || Z_FEATURE_QUERY == 1 30 | inline void _zenoh_on_reply_call(::z_loaned_reply_t* reply, void* context) { 31 | IClosure::call_from_context(context, interop::as_owned_cpp_ref(reply)); 32 | } 33 | #endif 34 | inline void _zenoh_on_sample_call(::z_loaned_sample_t* sample, void* context) { 35 | IClosure::call_from_context(context, interop::as_owned_cpp_ref(sample)); 36 | } 37 | #if defined(ZENOHCXX_ZENOHC) || Z_FEATURE_QUERYABLE == 1 38 | inline void _zenoh_on_query_call(::z_loaned_query_t* query, void* context) { 39 | IClosure::call_from_context(context, interop::as_owned_cpp_ref(query)); 40 | } 41 | #endif 42 | inline void _zenoh_on_id_call(const ::z_id_t* z_id, void* context) { 43 | IClosure::call_from_context(context, interop::as_copyable_cpp_ref(z_id)); 44 | } 45 | 46 | inline void _zenoh_on_hello_call(::z_loaned_hello_t* hello, void* context) { 47 | IClosure::call_from_context(context, interop::as_owned_cpp_ref(hello)); 48 | } 49 | } 50 | } // namespace zenoh::detail::closures 51 | -------------------------------------------------------------------------------- /include/zenoh/detail/commons.hxx: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // 4 | // Copyright (c) 2024 ZettaScale Technology 5 | // 6 | // This program and the accompanying materials are made available under the 7 | // terms of the Eclipse Public License 2.0 which is available at 8 | // http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 9 | // which is available at https://www.apache.org/licenses/LICENSE-2.0. 10 | // 11 | // SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 12 | // 13 | // Contributors: 14 | // ZettaScale Zenoh Team, 15 | 16 | #include 17 | 18 | namespace zenoh::detail::commons { 19 | 20 | template 21 | class TransformIterator { 22 | It _it; 23 | using Ftype = typename std::conditional_t, F, std::remove_reference_t>; 24 | Ftype _f; 25 | 26 | public: 27 | template 28 | TransformIterator(It const& it, FF&& f) : _it(it), _f(std::forward(f)) {} 29 | 30 | using difference_type = typename std::iterator_traits::difference_type; 31 | using value_type = typename std::invoke_result::type; 32 | using pointer = void; 33 | using reference = void; 34 | using iterator_category = std::input_iterator_tag; 35 | 36 | bool operator==(TransformIterator const& other) { return _it == other._it; } 37 | bool operator!=(TransformIterator const& other) { return _it != other._it; } 38 | 39 | auto operator*() const { return _f(_it); } 40 | 41 | auto operator++() { 42 | ++_it; 43 | return *this; 44 | } 45 | auto operator++(int) { 46 | auto prev = *this; 47 | ++_it; 48 | return prev; 49 | } 50 | }; 51 | 52 | template 53 | auto make_transform_iterator(It const& it, F&& f) { 54 | return TransformIterator(it, std::forward(f)); 55 | } 56 | 57 | } // namespace zenoh::detail::commons -------------------------------------------------------------------------------- /include/zenoh/zenohc.hxx: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #if defined(ZENOHCXX_ZENOHPICO) && defined(ZENOHCXX_ZENOHC) 4 | #error("Only one of ZENOHCXX_ZENOHPICO and ZENOHCXX_ZENOHC should be defined.") 5 | #endif 6 | #if !defined(ZENOHCXX_ZENOHPICO) && !defined(ZENOHCXX_ZENOHC) 7 | #error("Either ZENOHCXX_ZENOHPICO or ZENOHCXX_ZENOHC should be defined") 8 | #endif 9 | 10 | #if defined(ZENOHCXX_ZENOHPICO) 11 | #include "zenoh-pico.h" 12 | // use same macro to check for unstable features 13 | #elif defined(ZENOHCXX_ZENOHC) 14 | #include "zenoh.h" 15 | #endif -------------------------------------------------------------------------------- /install/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file(READ ${CMAKE_CURRENT_SOURCE_DIR}/../version.txt version) 2 | 3 | status_print(CMAKE_INSTALL_PREFIX) 4 | 5 | include(GNUInstallDirs) 6 | include(CMakePackageConfigHelpers) 7 | 8 | # 9 | # Install headers 10 | # 11 | get_target_property(include_dirs zenohcxx INTERFACE_INCLUDE_DIRECTORIES) 12 | foreach(dir ${include_dirs}) 13 | install(DIRECTORY "${dir}/" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT dev) 14 | endforeach() 15 | 16 | # 17 | # Install package config files 18 | # 19 | 20 | configure_file("${CMAKE_CURRENT_SOURCE_DIR}/zenohcxx.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/zenohcxx.pc" @ONLY) 21 | if(APPLE OR UNIX) 22 | install(FILES "${CMAKE_CURRENT_BINARY_DIR}/zenohcxx.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig" OPTIONAL COMPONENT dev) 23 | endif() 24 | 25 | set(CMAKE_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/zenohcxx") 26 | 27 | # Generate Config.cmake 28 | include(CMakePackageConfigHelpers) 29 | configure_package_config_file( 30 | "PackageConfig.cmake.in" 31 | "zenohcxxConfig.cmake" 32 | INSTALL_DESTINATION "${CMAKE_INSTALL_CMAKEDIR}") 33 | 34 | # Generate Version.cmake 35 | write_basic_package_version_file( 36 | "zenohcxxConfigVersion.cmake" 37 | VERSION ${PROJECT_VERSION} 38 | COMPATIBILITY SameMajorVersion) 39 | 40 | install( 41 | FILES "${CMAKE_CURRENT_BINARY_DIR}/zenohcxxConfig.cmake" 42 | "${CMAKE_CURRENT_BINARY_DIR}/zenohcxxConfigVersion.cmake" 43 | DESTINATION "${CMAKE_INSTALL_CMAKEDIR}" 44 | COMPONENT dev) 45 | 46 | 47 | # Configure CPack 48 | # 49 | set(CPACK_PACKAGE_DIRECTORY "${CMAKE_BINARY_DIR}/packages") 50 | set(CPACK_PACKAGE_CHECKSUM MD5) 51 | set(CPACK_PACKAGE_VENDOR "The Eclipse Foundation") 52 | set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR}) 53 | set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR}) 54 | set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH}) 55 | set(CPACK_COMPONENT_DEV_DESCRIPTION "The C++ header wrapper library for Eclipse zenoh-c and zenoh-pico libraries") 56 | set(CPACK_COMPONENT_DEV_GROUP "dev") 57 | set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY OFF) 58 | 59 | set(CPACK_GENERATOR ZIP) 60 | # set package name to format -. 61 | # platform name doesn't matter for header-only library 62 | set(CPACK_PACKAGE_NAME zenohcpp) 63 | 64 | if(NOT CPACK_PACKAGE_VERSION) 65 | set(SEM_VER "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") 66 | if(PROJECT_VERSION_TWEAK STREQUAL "") 67 | set(CPACK_PACKAGE_VERSION ${SEM_VER}) 68 | elseif(PROJECT_VERSION_TWEAK EQUAL 0) 69 | set(CPACK_PACKAGE_VERSION "${SEM_VER}~dev-1") 70 | elseif(PROJECT_VERSION_TWEAK GREATER 0) 71 | set(CPACK_PACKAGE_VERSION "${SEM_VER}~pre.${PROJECT_VERSION_TWEAK}-1") 72 | endif() 73 | endif() 74 | 75 | set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}") 76 | set(CPACK_PROJECT_CONFIG_FILE "${CMAKE_CURRENT_SOURCE_DIR}/cpack_project_config.cmake") 77 | 78 | include(CPack) 79 | -------------------------------------------------------------------------------- /install/PackageConfig.cmake.in: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2022 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, 13 | # 14 | 15 | @PACKAGE_INIT@ 16 | 17 | # Compute the installation prefix relative to this file. 18 | get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH) 19 | get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) 20 | get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) 21 | get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH) 22 | if(_IMPORT_PREFIX STREQUAL "/") 23 | set(_IMPORT_PREFIX "") 24 | endif() 25 | 26 | if(NOT TARGET zenohcxx) 27 | add_library(zenohcxx INTERFACE IMPORTED) 28 | target_include_directories(zenohcxx INTERFACE "${_IMPORT_PREFIX}/@CMAKE_INSTALL_INCLUDEDIR@") 29 | endif() 30 | 31 | # zenohcxx for zenohpico 32 | if(TARGET zenohpico::lib AND NOT TARGET zenohcxx_zenohpico) 33 | message(STATUS "defined lib target zenohcxx::zenohpico for zenohpico::lib") 34 | add_library(zenohcxx_zenohpico INTERFACE IMPORTED) 35 | target_compile_definitions(zenohcxx_zenohpico INTERFACE ZENOHCXX_ZENOHPICO) 36 | target_include_directories(zenohcxx_zenohpico INTERFACE "${_IMPORT_PREFIX}/@CMAKE_INSTALL_INCLUDEDIR@") 37 | add_dependencies(zenohcxx_zenohpico zenohpico::lib) 38 | target_link_libraries(zenohcxx_zenohpico INTERFACE zenohpico::lib) 39 | add_library(zenohcxx::zenohpico ALIAS zenohcxx_zenohpico) 40 | endif() 41 | 42 | # zenohcxx for zenohc static/dynamic depending on ZENOHC_LIB_STATIC 43 | if(TARGET zenohc::lib AND NOT TARGET zenohcxx_zenohc) 44 | message(STATUS "defined lib target zenohcxx::zenohc for zenohc::lib") 45 | add_library(zenohcxx_zenohc INTERFACE IMPORTED) 46 | target_compile_definitions(zenohcxx_zenohc INTERFACE ZENOHCXX_ZENOHC) 47 | target_include_directories(zenohcxx_zenohc INTERFACE "${_IMPORT_PREFIX}/@CMAKE_INSTALL_INCLUDEDIR@") 48 | add_dependencies(zenohcxx_zenohc zenohc::lib) 49 | target_link_libraries(zenohcxx_zenohc INTERFACE zenohc::lib) 50 | add_library(zenohcxx::zenohc ALIAS zenohcxx_zenohc) 51 | endif() 52 | 53 | if(NOT TARGET zenohcxx_zenohpico AND NOT TARGET zenohcxx_zenohc) 54 | message(FATAL_ERROR "Failed to detect zenoh-cpp backend, you need to have either zenoh-c or zenoh-pico installed" ) 55 | endif() 56 | -------------------------------------------------------------------------------- /install/cpack_project_config.cmake: -------------------------------------------------------------------------------- 1 | if(CPACK_GENERATOR MATCHES "DEB") 2 | # DEB package 3 | message(STATUS "Configure DEB packaging for Linux ${DEBARCH}") 4 | set(CPACK_DEBIAN_PACKAGE_MAINTAINER "ZettaScale Zenoh Team, ") 5 | set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE all) 6 | set(CPACK_DEB_COMPONENT_INSTALL OFF) 7 | set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT) 8 | set(CPACK_DEBIAN_PACKAGE_NAME lib${CPACK_PACKAGE_NAME}-dev) 9 | set(CPACK_DEBIAN_PACKAGE_DEPENDS "") 10 | endif() 11 | 12 | if(CPACK_GENERATOR MATCHES "RPM") 13 | # RPM package 14 | # rpmbuild should be installed 15 | # apt install rpm 16 | message(STATUS "Configure RPM packaging for Linux ${RPMARCH}") 17 | set(CPACK_RPM_PACKAGE_ARCHITECTURE noarch) 18 | set(CPACK_RPM_COMPONENT_INSTALL OFF) 19 | set(CPACK_RPM_FILE_NAME RPM-DEFAULT) 20 | set(CPACK_RPM_PACKAGE_NAME ${CPACK_PACKAGE_NAME}-dev) 21 | endif() 22 | -------------------------------------------------------------------------------- /install/zenohcxx.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@CMAKE_INSTALL_PREFIX@ 2 | 3 | Name: @PROJECT_NAME@ 4 | Description: @CMAKE_PROJECT_DESCRIPTION@ 5 | URL: @CMAKE_PROJECT_HOMEPAGE_URL@ 6 | Version: @PROJECT_VERSION@ 7 | Cflags: -I${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ -------------------------------------------------------------------------------- /scripts/build_local.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | if [ "$#" -eq 0 ]; then 5 | echo "Usage: build_local PROJECT_PATH INSTALL_PATH [CMAKE_OPTIONS]" 6 | exit 7 | fi 8 | 9 | current_dir=$PWD 10 | project_folder=$1 11 | mkdir -p $2 12 | absolute_install_location=$(cd $2; pwd) 13 | 14 | echo $install_location 15 | cd "$project_folder" 16 | rm -rf ./build 17 | mkdir ./build 18 | cd ./build 19 | cmake .. "${@:3}" -DCMAKE_BUILD_TYPE=Release --install-prefix "$absolute_install_location" 20 | cmake --build . --config Release -------------------------------------------------------------------------------- /scripts/build_standalone_examples.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) 4 | 5 | if [ "$#" -eq 0 ]; then 6 | echo "Usage: build_standalone_examples INSTALL_PATH [BUILD_PICO]" 7 | exit 8 | fi 9 | BUILD_PICO="ON" 10 | if [ "$#" -ge 2 ]; then 11 | BUILD_PICO=$2 12 | fi 13 | 14 | absolute_install_location=$(cd $1; pwd) 15 | 16 | bash $SCRIPT_DIR/build_local.sh $SCRIPT_DIR/../examples/simple/zenohc $absolute_install_location 17 | 18 | if [ "$BUILD_PICO" == "ON" ] || [ "$BUILD_PICO" == "TRUE" ] || [ "$BUILD_PICO" == "1" ]; then 19 | #build examples requiring zenoh-pico 20 | bash $SCRIPT_DIR/build_local.sh $SCRIPT_DIR/../examples/simple/universal $absolute_install_location 21 | bash $SCRIPT_DIR/build_local.sh $SCRIPT_DIR/../examples/simple/zenohpico $absolute_install_location 22 | fi 23 | 24 | -------------------------------------------------------------------------------- /scripts/install_from_git.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) 4 | 5 | if [ "$#" -eq 0 ]; then 6 | echo "Usage: install_from_git INSTALL_PATH [BUILD_WITH_UNSTABLE_API] [BUILD_WITH_SHARED_MEMORY] [BUILD_PICO] [BUILD_TYPE]" 7 | exit 8 | fi 9 | 10 | USE_UNSTABLE="TRUE" 11 | USE_SHARED_MEMORY="TRUE" 12 | USE_UNSTABLE_PICO="0" 13 | BUILD_TYPE="Release" 14 | BUILD_PICO="ON" 15 | 16 | if [ "$#" -ge 2 ]; then 17 | USE_UNSTABLE=$2 18 | fi 19 | 20 | if [ "$#" -ge 3 ]; then 21 | USE_SHARED_MEMORY=$3 22 | fi 23 | 24 | if [ "$USE_UNSTABLE" == "TRUE" ] || [ "$USE_UNSTABLE" == "ON" ] || [ "$USE_UNSTABLE" == "1" ]; then 25 | USE_UNSTABLE_PICO="1" 26 | fi 27 | 28 | if [ "$#" -ge 4 ]; then 29 | BUILD_PICO=$4 30 | fi 31 | 32 | if [ "$#" -ge 5 ]; then 33 | BUILD_TYPE=$5 34 | fi 35 | 36 | 37 | 38 | 39 | git submodule init 40 | git submodule update 41 | 42 | mkdir -p $1 43 | absolute_install_location=$(cd $1; pwd) 44 | #build zenoh-c 45 | bash $SCRIPT_DIR/install_local.sh $SCRIPT_DIR/../zenoh-c $absolute_install_location $BUILD_TYPE -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DZENOHC_BUILD_WITH_UNSTABLE_API=$USE_UNSTABLE -DZENOHC_BUILD_WITH_SHARED_MEMORY=$USE_SHARED_MEMORY 46 | if [ "$BUILD_PICO" == "ON" ] || [ "$BUILD_PICO" == "TRUE" ] || [ "$BUILD_PICO" == "1" ]; then 47 | #build zenoh-pico 48 | bash $SCRIPT_DIR/install_local.sh $SCRIPT_DIR/../zenoh-pico $absolute_install_location $BUILD_TYPE -DZ_FEATURE_UNSTABLE_API=$USE_UNSTABLE_PICO -DZ_FEATURE_BATCHING=1 49 | fi 50 | 51 | rm -rf ./build 52 | mkdir ./build 53 | cd ./build 54 | cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DZENOHCXX_ZENOHPICO=$BUILD_PICO --install-prefix "$absolute_install_location" 55 | cmake --build . --target install --config $BUILD_TYPE 56 | -------------------------------------------------------------------------------- /scripts/install_from_local.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) 4 | 5 | if [ "$#" -eq 0 ]; then 6 | echo "Usage: install_from_local INSTALL_PATH" 7 | exit 8 | fi 9 | 10 | mkdir -p $1 11 | absolute_install_location=$(cd $1; pwd) 12 | #build zenoh-c 13 | bash $SCRIPT_DIR/install_local.sh $SCRIPT_DIR/../../zenoh-c $absolute_install_location Release -DZENOHC_BUILD_WITH_UNSTABLE_API=TRUE -DZENOHC_BUILD_WITH_SHARED_MEMORY=TRUE 14 | #build zenoh-pico 15 | bash $SCRIPT_DIR/install_local.sh $SCRIPT_DIR/../../zenoh-pico $absolute_install_location Release -DZ_FEATURE_UNSTABLE_API=1 -DZ_FEATURE_BATCHING=1 16 | 17 | rm -rf ./build 18 | mkdir ./build 19 | cd ./build 20 | cmake .. -DCMAKE_BUILD_TYPE=Release -DZENOHCXX_ZENOHPICO=ON --install-prefix "$absolute_install_location" 21 | cmake --build . --target install --config Release -------------------------------------------------------------------------------- /scripts/install_local.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | if [ "$#" -eq 0 ]; then 5 | echo "Usage: install_local PROJECT_PATH INSTALL_PATH [CONFIG] [CMAKE_ARGS]" 6 | exit 7 | fi 8 | 9 | CONFIG="Release" 10 | 11 | if [ "$#" -ge 3 ]; then 12 | CONFIG=$3 13 | fi 14 | 15 | 16 | current_dir=$PWD 17 | project_folder=$1 18 | mkdir -p $2 19 | absolute_install_location=$(cd $2; pwd) 20 | 21 | echo $install_location 22 | cd "$project_folder" 23 | rm -rf ./build 24 | mkdir ./build 25 | cd ./build 26 | cmake .. "${@:4}" --install-prefix "$absolute_install_location" 27 | cmake --build . --target install --config $CONFIG 28 | -------------------------------------------------------------------------------- /tests/build/warnings.cxx: -------------------------------------------------------------------------------- 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, 13 | // 14 | 15 | // Disable 'old-style-cast` warning for C headers only 16 | #ifdef _MSC_VER 17 | #pragma warning(push) 18 | #pragma warning(disable : 4996) 19 | #else 20 | #pragma GCC diagnostic push 21 | #pragma GCC diagnostic ignored "-Wold-style-cast" 22 | #endif 23 | 24 | #ifdef ZENOHCXX_ZENOHPICO 25 | #include "zenoh-pico.h" 26 | #endif 27 | #ifdef ZENOHCXX_ZENOHC 28 | #include "zenoh.h" 29 | #endif 30 | 31 | #ifdef _MSC_VER 32 | #pragma warning(pop) 33 | #else 34 | #pragma GCC diagnostic pop 35 | #endif 36 | 37 | #include "zenoh.hxx" 38 | 39 | using namespace zenoh; 40 | 41 | int main() { return 0; } -------------------------------------------------------------------------------- /tests/run_with_router.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Copyright (c) 2022 ZettaScale Technology 4 | # 5 | # This program and the accompanying materials are made available under the 6 | # terms of the Eclipse Public License 2.0 which is available at 7 | # http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 8 | # which is available at https://www.apache.org/licenses/LICENSE-2.0. 9 | # 10 | # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 11 | # 12 | # Contributors: 13 | # ZettaScale Zenoh Team, 14 | # 15 | 16 | TESTBIN="$1" 17 | TESTDIR=$(dirname "$0") 18 | ZENOH_BRANCH="$2" 19 | 20 | # get vinary name without extension 21 | TEST_NAME_WE=$(basename -- "$TESTBIN") 22 | TEST_NAME_WE="${TEST_NAME_WE%.*}" 23 | 24 | cd "$TESTDIR"|| exit 25 | 26 | echo "------------------ Running test $TESTBIN -------------------" 27 | 28 | sleep 5 29 | 30 | if [ ! -f zenohd ]; then 31 | git clone https://github.com/eclipse-zenoh/zenoh.git zenoh-git 32 | cd zenoh-git || exit 33 | git switch "$ZENOH_BRANCH" 34 | rustup show 35 | cargo build --lib --bin zenohd 36 | cp ./target/debug/zenohd "$TESTDIR"/ 37 | cd "$TESTDIR"|| exit 38 | fi 39 | 40 | chmod +x zenohd 41 | 42 | LOCATORS="tcp/127.0.0.1:7447" 43 | for LOCATOR in $(echo "$LOCATORS" | xargs); do 44 | sleep 1 45 | 46 | echo "> Running zenohd ... $LOCATOR" 47 | RUST_LOG=debug ./zenohd --plugin-search-dir "$TESTDIR/zenoh-git/target/debug" -l "$LOCATOR" > zenohd."$TEST_NAME_WE".log 2>&1 & 48 | ZPID=$! 49 | 50 | sleep 5 51 | 52 | echo "> Running $TESTBIN ..." 53 | "$TESTBIN" "$LOCATOR" > client."$TEST_NAME_WE".log 2>&1 54 | RETCODE=$? 55 | 56 | echo "> Logs of $TESTBIN ..." 57 | cat client."$TEST_NAME_WE".log 58 | 59 | echo "> Stopping zenohd ..." 60 | kill -9 "$ZPID" 61 | 62 | sleep 1 63 | 64 | echo "> Logs of zenohd ..." 65 | cat zenohd."$TEST_NAME_WE".log 66 | 67 | [ "$RETCODE" -lt 0 ] && exit "$RETCODE" 68 | done 69 | 70 | echo "> Done ($RETCODE)." 71 | exit "$RETCODE" 72 | -------------------------------------------------------------------------------- /tests/universal/closures.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2024 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, 13 | // 14 | #include "zenoh.hxx" 15 | using namespace zenoh; 16 | 17 | #undef NDEBUG 18 | #include 19 | 20 | #include 21 | 22 | void test_call_drop() { 23 | size_t calls_count = 0; 24 | size_t drop_count = 0; 25 | 26 | auto on_call = [&calls_count] { 27 | calls_count++; 28 | return calls_count; 29 | }; 30 | using OnCall = decltype(on_call); 31 | auto on_drop = [&drop_count] { drop_count++; }; 32 | 33 | using OnDrop = decltype(on_drop); 34 | 35 | auto c = detail::closures::Closure(on_call, on_drop); 36 | c.call(); 37 | c.call(); 38 | c.drop(); 39 | 40 | assert(calls_count = 2); 41 | assert(drop_count == 1); 42 | } 43 | 44 | void test_context() { 45 | size_t calls_count = 0; 46 | bool dropped = 0; 47 | 48 | auto on_call = [&calls_count](size_t c) { 49 | calls_count += c; 50 | return calls_count; 51 | }; 52 | using OnCall = decltype(on_call); 53 | auto on_drop = [&dropped] { dropped = true; }; 54 | 55 | using OnDrop = decltype(on_drop); 56 | 57 | auto context = detail::closures::Closure::into_context(on_call, on_drop); 58 | detail::closures::IClosure::call_from_context(context, 2); 59 | detail::closures::IDroppable::delete_from_context(context); 60 | 61 | assert(calls_count = 3); 62 | assert(dropped); 63 | } 64 | 65 | int main(int argc, char** argv) { 66 | test_call_drop(); 67 | test_context(); 68 | } -------------------------------------------------------------------------------- /tests/universal/details.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2025 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, 13 | // 14 | 15 | #include "zenoh.hxx" 16 | 17 | using namespace zenoh; 18 | 19 | static_assert(detail::is_take_from_loaned_available_v<::z_owned_session_t> == false); 20 | static_assert(detail::is_take_from_loaned_available_v<::z_owned_hello_t>); 21 | static_assert(detail::is_take_from_loaned_available_v<::z_owned_sample_t>); 22 | static_assert(detail::is_take_from_loaned_available_v<::z_owned_reply_t>); 23 | static_assert(detail::is_take_from_loaned_available_v<::z_owned_query_t>); 24 | 25 | int main() { return 0; } -------------------------------------------------------------------------------- /tests/universal/network/keyexpr.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2024 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, 13 | // 14 | #include "zenoh.hxx" 15 | using namespace zenoh; 16 | 17 | #undef NDEBUG 18 | #include 19 | 20 | void key_expr() { 21 | KeyExpr foo("FOO"); 22 | assert(interop::detail::check(foo)); 23 | assert(foo.as_string_view() == "FOO"); 24 | } 25 | 26 | void canonize() { 27 | ZResult err = 0; 28 | bool res; 29 | auto non_canon = "a/**/**/c"; 30 | auto canon = "a/**/c"; 31 | 32 | assert(KeyExpr::is_canon(canon)); 33 | assert(!KeyExpr::is_canon(non_canon)); 34 | 35 | // do not force canoniozation on keyexpr construction 36 | KeyExpr k_err(non_canon, false, &err); 37 | #ifdef ZENOHCXX_ZENOHC // Pico does not validate key expressions yet. 38 | assert(!interop::detail::check(k_err)); 39 | assert(err < 0); 40 | #endif 41 | 42 | // enforce canonization 43 | KeyExpr k_ok(non_canon, true, &err); 44 | assert(interop::detail::check(k_ok)); 45 | assert(err == 0); 46 | assert(k_ok.as_string_view() == canon); 47 | } 48 | 49 | void concat() { 50 | KeyExpr foo("FOO"); 51 | auto foobar1 = foo.concat("BAR"); 52 | assert(foobar1.as_string_view() == "FOOBAR"); 53 | } 54 | 55 | void join() { 56 | KeyExpr foo("FOO"); 57 | auto foobar = foo.join(KeyExpr("BAR")); 58 | assert(foobar.as_string_view() == "FOO/BAR"); 59 | } 60 | 61 | void equals() { 62 | KeyExpr foo("FOO"); 63 | KeyExpr foo2("FOO"); 64 | KeyExpr bar("BAR"); 65 | 66 | assert(foo != bar); 67 | assert(foo == foo); 68 | assert(foo == foo2); 69 | assert(foo == "FOO"); 70 | assert(foo != "BAR"); 71 | } 72 | 73 | void includes() { 74 | KeyExpr foostar("FOO/*"); 75 | KeyExpr foobar("FOO/BAR"); 76 | assert(foostar.includes(foobar)); 77 | assert(!foobar.includes(foostar)); 78 | } 79 | 80 | void intersects() { 81 | KeyExpr foostar("FOO/*"); 82 | KeyExpr foobar("FOO/BAR"); 83 | KeyExpr starbuz("*/BUZ"); 84 | KeyExpr foobuz("FOO/BUZ"); 85 | 86 | assert(foostar.intersects(foobar)); 87 | assert(!starbuz.intersects(foobar)); 88 | assert(foobuz.intersects(starbuz)); 89 | assert(starbuz.intersects(foobuz)); 90 | } 91 | 92 | void declare(Session& s) { 93 | KeyExpr foobar("FOO/BAR"); 94 | KeyExpr foostar("FOO/*"); 95 | auto declared = s.declare_keyexpr(foobar); 96 | assert(interop::detail::check(declared)); 97 | 98 | assert(declared.as_string_view() == "FOO/BAR"); 99 | assert(declared == foobar); 100 | assert(foostar.includes(declared)); 101 | assert(declared.intersects(foobar)); 102 | s.undeclare_keyexpr(std::move(declared)); 103 | assert(!interop::detail::check(declared)); 104 | } 105 | 106 | int main(int argc, char** argv) { 107 | key_expr(); 108 | canonize(); 109 | concat(); 110 | join(); 111 | equals(); 112 | includes(); 113 | intersects(); 114 | 115 | // Session based tests 116 | Config config = Config::create_default(); 117 | auto session = Session::open(std::move(config)); 118 | declare(session); 119 | } 120 | -------------------------------------------------------------------------------- /tests/universal/network/liveliness.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2024 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, 13 | // 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | #include "zenoh.hxx" 20 | 21 | using namespace zenoh; 22 | using namespace std::chrono_literals; 23 | 24 | #undef NDEBUG 25 | #include 26 | 27 | void test_liveliness_get() { 28 | KeyExpr ke("zenoh/liveliness/test/*"); 29 | KeyExpr token_ke("zenoh/liveliness/test/1"); 30 | auto session1 = Session::open(Config::create_default()); 31 | auto session2 = Session::open(Config::create_default()); 32 | auto token = session1.liveliness_declare_token(token_ke); 33 | std::this_thread::sleep_for(1s); 34 | 35 | auto replies = session2.liveliness_get(ke, channels::FifoChannel(3)); 36 | auto res = replies.recv(); 37 | assert(std::holds_alternative(res)); 38 | assert(std::get(res).is_ok()); 39 | assert(std::get(res).get_ok().get_keyexpr() == "zenoh/liveliness/test/1"); 40 | res = replies.recv(); 41 | assert(std::holds_alternative(res)); 42 | assert(std::get(res) == channels::RecvError::Z_DISCONNECTED); 43 | 44 | std::move(token).undeclare(); 45 | std::this_thread::sleep_for(1s); 46 | 47 | replies = session2.liveliness_get(ke, channels::FifoChannel(3)); 48 | res = replies.recv(); 49 | assert(std::holds_alternative(res)); 50 | assert(std::get(res) == channels::RecvError::Z_DISCONNECTED); 51 | } 52 | 53 | void test_liveliness_subscriber() { 54 | KeyExpr ke("zenoh/liveliness/test/*"); 55 | KeyExpr token_ke1("zenoh/liveliness/test/1"); 56 | KeyExpr token_ke2("zenoh/liveliness/test/2"); 57 | auto session1 = Session::open(Config::create_default()); 58 | auto session2 = Session::open(Config::create_default()); 59 | 60 | std::unordered_set put_tokens; 61 | std::unordered_set delete_tokens; 62 | 63 | auto subscriber = session1.liveliness_declare_subscriber( 64 | ke, 65 | [&put_tokens, &delete_tokens](const Sample& s) { 66 | if (s.get_kind() == Z_SAMPLE_KIND_PUT) { 67 | put_tokens.insert(std::string(s.get_keyexpr().as_string_view())); 68 | } else if (s.get_kind() == Z_SAMPLE_KIND_DELETE) { 69 | delete_tokens.insert(std::string(s.get_keyexpr().as_string_view())); 70 | } 71 | }, 72 | closures::none); 73 | std::this_thread::sleep_for(1s); 74 | 75 | auto token1 = session2.liveliness_declare_token(token_ke1); 76 | auto token2 = session2.liveliness_declare_token(token_ke2); 77 | std::this_thread::sleep_for(1s); 78 | assert(put_tokens.size() == 2); 79 | assert(put_tokens.count("zenoh/liveliness/test/1") == 1); 80 | assert(put_tokens.count("zenoh/liveliness/test/2") == 1); 81 | 82 | std::move(token1).undeclare(); 83 | std::this_thread::sleep_for(1s); 84 | 85 | assert(delete_tokens.size() == 1); 86 | assert(delete_tokens.count("zenoh/liveliness/test/1") == 1); 87 | 88 | std::move(token2).undeclare(); 89 | std::this_thread::sleep_for(1s); 90 | assert(delete_tokens.size() == 2); 91 | assert(delete_tokens.count("zenoh/liveliness/test/2") == 1); 92 | } 93 | 94 | int main(int argc, char** argv) { 95 | test_liveliness_get(); 96 | test_liveliness_subscriber(); 97 | }; 98 | -------------------------------------------------------------------------------- /tests/zenohc/config.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | // 14 | 15 | #include "zenoh.hxx" 16 | 17 | using namespace zenoh; 18 | 19 | #undef NDEBUG 20 | #include 21 | 22 | void test_config_insert() { 23 | std::vector peers = {"tcp/192.168.0.1", "tcp/10.0.0.1"}; 24 | auto config = Config::create_default(); 25 | config.insert_json5("mode", "\"client\""); 26 | config.insert_json5("connect/endpoints", "[\"tcp/192.168.0.1\", \"tcp/10.0.0.1\"]"); 27 | assert(config.get("mode") == "\"client\""); 28 | assert(config.get("connect/endpoints") == "[\"tcp/192.168.0.1\",\"tcp/10.0.0.1\"]"); 29 | } 30 | 31 | void test_config_to_string() { 32 | Config config = Config::create_default(); 33 | auto s = config.to_string(); 34 | assert(s.size() > 0); 35 | assert(s.find("{\"id\":") != std::string::npos); 36 | } 37 | 38 | int main(int argc, char** argv) { 39 | init_log_from_env_or("error"); 40 | test_config_insert(); 41 | test_config_to_string(); 42 | // TODO: add more tests 43 | }; 44 | -------------------------------------------------------------------------------- /tests/zenohpico/config.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2022 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, 13 | // 14 | 15 | #include "zenoh.hxx" 16 | 17 | using namespace zenoh; 18 | 19 | #undef NDEBUG 20 | #include 21 | 22 | void test_config_default() { 23 | Config config = Config::create_default(); 24 | assert(config.get(Z_CONFIG_MULTICAST_LOCATOR_KEY) != nullptr); 25 | } 26 | 27 | void test_config_insert() { 28 | Config config = Config::create_default(); 29 | ZResult err = Z_OK; 30 | config.insert(Z_CONFIG_USER_KEY, "foo", &err); 31 | assert(err == Z_OK); 32 | assert(std::string("foo") == config.get(Z_CONFIG_USER_KEY)); 33 | } 34 | 35 | int main(int argc, char** argv) { 36 | test_config_default(); 37 | test_config_insert(); 38 | }; 39 | -------------------------------------------------------------------------------- /tests/zenohpico/network/batching.cxx: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (c) 2025 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, 13 | // 14 | 15 | #include 16 | #include 17 | 18 | #include "zenoh.hxx" 19 | 20 | using namespace zenoh; 21 | using namespace std::chrono_literals; 22 | 23 | #undef NDEBUG 24 | #include 25 | 26 | void test_multiple_batching() { 27 | auto session1 = Session::open(Config::create_default()); 28 | auto batch_guard = session1.start_batching(); 29 | 30 | ZResult err = Z_OK; 31 | auto batch_guard2 = session1.start_batching(&err); 32 | assert(err != Z_OK); 33 | } 34 | 35 | void test_batching() { 36 | auto session1 = Session::open(Config::create_default()); 37 | auto session2 = Session::open(Config::create_default()); 38 | 39 | auto subscriber = session2.declare_subscriber("test/batching", channels::FifoChannel(3)); 40 | std::this_thread::sleep_for(1s); 41 | 42 | auto batch_guard = session1.start_batching(); 43 | 44 | session1.put("test/batching", "data1"); 45 | session1.put("*/batching", "data2"); 46 | std::this_thread::sleep_for(1s); 47 | 48 | auto res = subscriber.handler().try_recv(); 49 | assert(std::get(res) == channels::RecvError::Z_NODATA); 50 | 51 | batch_guard.flush(); 52 | std::this_thread::sleep_for(1s); 53 | 54 | assert(std::holds_alternative(subscriber.handler().try_recv())); 55 | assert(std::holds_alternative(subscriber.handler().try_recv())); 56 | } 57 | 58 | void test_batching_drop() { 59 | auto session1 = Session::open(Config::create_default()); 60 | auto session2 = Session::open(Config::create_default()); 61 | 62 | auto subscriber = session2.declare_subscriber("test/batching", channels::FifoChannel(3)); 63 | std::this_thread::sleep_for(1s); 64 | 65 | { 66 | auto batch_guard = session1.start_batching(); 67 | 68 | session1.put("test/batching", "data1"); 69 | session1.put("*/batching", "data2"); 70 | std::this_thread::sleep_for(1s); 71 | 72 | auto res = subscriber.handler().try_recv(); 73 | assert(std::get(res) == channels::RecvError::Z_NODATA); 74 | } 75 | 76 | std::this_thread::sleep_for(1s); 77 | 78 | assert(std::holds_alternative(subscriber.handler().try_recv())); 79 | assert(std::holds_alternative(subscriber.handler().try_recv())); 80 | } 81 | 82 | int main(int argc, char** argv) { 83 | test_multiple_batching(); 84 | test_batching(); 85 | test_batching_drop(); 86 | }; 87 | -------------------------------------------------------------------------------- /version.txt: -------------------------------------------------------------------------------- 1 | 1.4.0 -------------------------------------------------------------------------------- /zenoh-c-branch.txt: -------------------------------------------------------------------------------- 1 | main -------------------------------------------------------------------------------- /zenoh-cpp-branch.txt: -------------------------------------------------------------------------------- 1 | main -------------------------------------------------------------------------------- /zenoh-pico-branch.txt: -------------------------------------------------------------------------------- 1 | main --------------------------------------------------------------------------------