Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ jobs:
- name: Build up-core-api conan package
shell: bash
run: |
conan create --version 1.6.0-alpha2 up-conan-recipes/up-core-api/release
conan create --version 1.6.0-alpha4 up-conan-recipes/up-core-api/release

- name: Build up-cpp conan package
shell: bash
run: |
conan create --version 1.0.1 --build=missing up-conan-recipes/up-cpp/release
conan create --version 1.0.1-dev --build=missing up-conan-recipes/up-cpp/developer

- name: Build zenohcpp conan package
shell: bash
Expand Down Expand Up @@ -163,7 +163,7 @@ jobs:
style: 'file' # read .clang-format for configuration
tidy-checks: '' # Read .clang-tidy for configuration
database: compile_commands.json
version: 12
version: 13

- name: Run linters on tests
id: test-linter
Expand All @@ -176,7 +176,7 @@ jobs:
style: 'file' # read .clang-format for configuration
tidy-checks: '' # Read .clang-tidy for configuration
database: compile_commands.json
version: 12
version: 13

- name: Report lint failure
if:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ up-transport-zenoh-cpp, follow the steps in the
### With Conan for dependencies

```
cd up-client-zenoh-cpp
cd up-transport-zenoh-cpp
conan install . --build=missing
cmake --preset conan-release
cd build/Release
Expand Down
4 changes: 2 additions & 2 deletions conanfile.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[requires]
up-cpp/[^1.0.1]
up-cpp/[^1.0.1, include_prerelease]
zenohcpp/1.2.1
zenohc/1.2.1
spdlog/[~1.13]
up-core-api/[~1.6, include_prerelease]
up-core-api/1.6.0-alpha4
protobuf/[~3.21]

[test_requires]
Expand Down
22 changes: 18 additions & 4 deletions include/up-transport-zenoh-cpp/ThreadSafeMap.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache License Version 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: Apache-2.0

#ifndef UP_TRANSPORT_ZENOH_CPP_THREADSAFEMAP_H
#define UP_TRANSPORT_ZENOH_CPP_THREADSAFEMAP_H

#include <algorithm>
#include <map>
#include <mutex>
Expand Down Expand Up @@ -25,9 +39,8 @@ class ThreadSafeMap {
Iterator it = map_.find(key);
if (it != map_.end()) {
return it->second;
} else {
return std::nullopt;
}
return std::nullopt;
}

template <typename Predicate>
Expand All @@ -36,12 +49,13 @@ class ThreadSafeMap {
Iterator it = std::find_if(map_.begin(), map_.end(), pred);
if (it != map_.end()) {
return it->second;
} else {
return std::nullopt;
}
return std::nullopt;
}

private:
MapType map_;
mutable std::mutex mutex_;
};

#endif // UP_TRANSPORT_ZENOH_CPP_THREADSAFEMAP_H
19 changes: 8 additions & 11 deletions include/up-transport-zenoh-cpp/ZenohUTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
#include <up-cpp/transport/UTransport.h>

#include <filesystem>
#include <mutex>
#include <optional>
#include <unordered_map>

#define ZENOHCXX_ZENOHC
#include <zenoh.hxx>
Expand Down Expand Up @@ -46,14 +44,14 @@ namespace uprotocol::transport {
struct ZenohUTransport : public UTransport {
/// @brief Constructor
///
/// @param defaultUri Default Authority and Entity (as a UUri) for
/// @param default_uri Default Authority and Entity (as a UUri) for
/// clients using this transport instance.
/// @param configFile Path to a configuration file containing the Zenoh
/// @param config_file Path to a configuration file containing the Zenoh
/// transport configuration.
ZenohUTransport(const v1::UUri& defaultUri,
const std::filesystem::path& configFile);
ZenohUTransport(const v1::UUri& default_uri,
const std::filesystem::path& config_file);

virtual ~ZenohUTransport() = default;
~ZenohUTransport() override = default;

protected:
/// @brief Send a message.
Expand All @@ -63,8 +61,7 @@ struct ZenohUTransport : public UTransport {
/// @returns * OKSTATUS if the payload has been successfully
/// sent (ACK'ed)
/// * FAILSTATUS with the appropriate failure otherwise.
[[nodiscard]] virtual v1::UStatus sendImpl(
const v1::UMessage& message) override;
[[nodiscard]] v1::UStatus sendImpl(const v1::UMessage& message) override;

/// @brief Represents the callable end of a callback connection.
using CallableConn = typename UTransport::CallableConn;
Expand All @@ -81,7 +78,7 @@ struct ZenohUTransport : public UTransport {
///
/// @returns * OKSTATUS if the listener was registered successfully.
/// * FAILSTATUS with the appropriate failure otherwise.
[[nodiscard]] virtual v1::UStatus registerListenerImpl(
[[nodiscard]] v1::UStatus registerListenerImpl(
CallableConn&& listener, const v1::UUri& source_filter,
std::optional<v1::UUri>&& sink_filter) override;

Expand All @@ -93,7 +90,7 @@ struct ZenohUTransport : public UTransport {
/// @note The default implementation does nothing.
///
/// @param listener shared_ptr of the Connection that has been broken.
virtual void cleanupListener(CallableConn listener) override;
void cleanupListener(const CallableConn& listener) override;

static std::string toZenohKeyString(
const std::string& default_authority_name, const v1::UUri& source,
Expand Down
4 changes: 2 additions & 2 deletions lint/clang-format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pushd -n "$PROJECT_ROOT"
for f in **/*.h **/*.cpp; do
echo
echo "Checking file '$f'"
# NOTE: Using clang-format-12 in CI system, too
clang-format-12 -i "$f"
# NOTE: Using clang-format-13 in CI system, too
clang-format-13 -i "$f"
done
popd -n
41 changes: 20 additions & 21 deletions src/ZenohUTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ std::string ZenohUTransport::toZenohKeyString(
const std::optional<v1::UUri>& sink) {
std::ostringstream zenoh_key;

auto writeUUri = [&](const v1::UUri& uuri) {
auto write_u_uri = [&](const v1::UUri& uuri) {
zenoh_key << "/";

// authority_name
Expand Down Expand Up @@ -74,10 +74,10 @@ std::string ZenohUTransport::toZenohKeyString(

zenoh_key << "up";

writeUUri(source);
write_u_uri(source);

if (sink.has_value()) {
writeUUri(*sink);
write_u_uri(*sink);
} else {
zenoh_key << "/{}/{}/{}/{}";
}
Expand Down Expand Up @@ -105,13 +105,13 @@ v1::UAttributes ZenohUTransport::attachmentToUAttributes(

if (attachment_vec.size() != 2) {
spdlog::error("attachmentToUAttributes: attachment size != 2");
// TODO: error report, exception?
// TODO(unknown) error report, exception?
}

if (attachment_vec[0].second.size() == 1) {
if (attachment_vec[0].second[0] != UATTRIBUTE_VERSION) {
spdlog::error("attachmentToUAttributes: incorrect version");
// TODO: error report, exception?
// TODO(unknown) error report, exception?
}
};
v1::UAttributes res;
Expand Down Expand Up @@ -166,8 +166,7 @@ std::optional<v1::UMessage> ZenohUTransport::sampleToUMessage(
"sampleToUMessage: empty attachment, cannot read uAttributes");
return std::nullopt;
}
std::string payload(
zenoh::ext::deserialize<std::string>(sample.get_payload()));
auto payload(zenoh::ext::deserialize<std::string>(sample.get_payload()));
message.set_payload(payload);

return message;
Expand All @@ -186,20 +185,20 @@ std::optional<v1::UMessage> ZenohUTransport::queryToUMessage(
return std::nullopt;
}
if (query.get_payload().has_value()) {
std::string payload(zenoh::ext::deserialize<std::string>(
auto payload(zenoh::ext::deserialize<std::string>(
query.get_payload().value().get()));
message.set_payload(payload);
}

return message;
}

ZenohUTransport::ZenohUTransport(const v1::UUri& defaultUri,
const std::filesystem::path& configFile)
: UTransport(defaultUri),
ZenohUTransport::ZenohUTransport(const v1::UUri& default_uri,
const std::filesystem::path& config_file)
: UTransport(default_uri),
session_(zenoh::Session::open(
std::move(zenoh::Config::from_file(configFile.string().c_str())))) {
// TODO: add to setup or remove
zenoh::Config::from_file(config_file.string()))) {
// TODO(unknown) add to setup or remove
spdlog::set_level(spdlog::level::debug);

spdlog::info("ZenohUTransport init");
Expand All @@ -211,10 +210,10 @@ v1::UStatus ZenohUTransport::registerPublishNotificationListener_(

// NOTE: listener is captured by copy here so that it does not go out
// of scope when this function returns.
auto on_sample = [this, listener](const zenoh::Sample& sample) mutable {
auto maybeMessage = sampleToUMessage(sample);
if (maybeMessage.has_value()) {
listener(maybeMessage.value());
auto on_sample = [listener](const zenoh::Sample& sample) mutable {
auto maybe_message = sampleToUMessage(sample);
if (maybe_message.has_value()) {
listener(maybe_message.value());
} else {
spdlog::error("on_sample: failed to retrieve uMessage");
}
Expand All @@ -225,7 +224,7 @@ v1::UStatus ZenohUTransport::registerPublishNotificationListener_(
auto subscriber = session_.declare_subscriber(
zenoh_key, std::move(on_sample), std::move(on_drop));
subscriber_map_.emplace(listener, std::move(subscriber));
return v1::UStatus();
return {};
}

v1::UStatus ZenohUTransport::sendPublishNotification_(
Expand All @@ -250,7 +249,7 @@ v1::UStatus ZenohUTransport::sendPublishNotification_(
return uError(v1::UCode::INTERNAL, e.what());
}

return v1::UStatus();
return {};
}

// NOTE: Messages have already been validated by the base class. It does not
Expand Down Expand Up @@ -278,10 +277,10 @@ v1::UStatus ZenohUTransport::registerListenerImpl(
std::string zenoh_key = toZenohKeyString(getEntityUri().authority_name(),
source_filter, sink_filter);

return registerPublishNotificationListener_(zenoh_key, listener);
return registerPublishNotificationListener_(zenoh_key, std::move(listener));
}

void ZenohUTransport::cleanupListener(CallableConn listener) {
void ZenohUTransport::cleanupListener(const CallableConn& listener) {
subscriber_map_.erase(listener);
}

Expand Down
Loading