Skip to content

Commit

Permalink
added support for z_hello_clone;
Browse files Browse the repository at this point in the history
added support for background declarations;
added support for z_id_t to string;
added support for session_close_options;
  • Loading branch information
DenisBiryukov91 committed Sep 12, 2024
1 parent 663a5f2 commit 3cd6e9e
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 31 deletions.
20 changes: 20 additions & 0 deletions include/zenoh/api/hello.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,25 @@ class Hello : public Owned<::z_owned_hello_t> {
#endif
return locators;
}

/// @brief Copy contructor
Hello(const Hello& other) : Owned(nullptr) { ::z_hello_clone(&this->_0, interop::as_loaned_c_ptr(other)); };

/// @brief Move constructor
Hello(Hello&& other) = default;

/// @name Operators

/// @brief Assignment operator.
Hello& operator=(const Hello& other) {
if (this != &other) {
::z_drop(z_move(this->_0));
::z_hello_clone(&this->_0, interop::as_loaned_c_ptr(other));
}
return *this;
};

/// @brief Move assignment operator.
Hello& operator=(Hello&& other) = default;
};
} // namespace zenoh
8 changes: 5 additions & 3 deletions include/zenoh/api/id.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <array>
#include <iomanip>
#include <iostream>
#include <string_view>

#include "../zenohc.hxx"
#include "base.hxx"
Expand Down Expand Up @@ -44,9 +45,10 @@ class Id : public Copyable<::z_id_t> {
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future release.
/// @brief Print ``Id`` in the hex format.
inline std::ostream& operator<<(std::ostream& os, const Id& id) {
auto id_ptr = reinterpret_cast<const ::z_id_t*>(&id)->id;
for (size_t i = 0; id_ptr[i] != 0 && i < 16; i++)
os << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(id_ptr[i]);
::z_owned_string_t s;
::z_id_to_string(interop::as_copyable_c_ptr(id), &s);
os << std::string_view(::z_string_data(::z_loan(s)), ::z_string_len(::z_loan(s)));
::z_drop(::z_move(s));
return os;
}
} // namespace zenoh
3 changes: 0 additions & 3 deletions include/zenoh/api/publisher.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,10 @@ class Publisher : public Owned<::z_owned_publisher_t> {
"Failed to perform delete_resource operation");
}

#ifdef ZENOHCXX_ZENOHC
/// @brief Get the key expression of the publisher.
/// @note zenoh-c only.
const KeyExpr& get_keyexpr() const {
return interop::as_owned_cpp_ref<KeyExpr>(::z_publisher_keyexpr(interop::as_loaned_c_ptr(*this)));
}
#endif
#if defined(ZENOHCXX_ZENOHC) && defined(UNSTABLE)
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future
/// release.
Expand Down
21 changes: 19 additions & 2 deletions include/zenoh/api/queryable.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ class QueryableBase : public Owned<::z_owned_queryable_t> {
QueryableBase(zenoh::detail::null_object_t) : Owned(nullptr){};
QueryableBase(::z_owned_queryable_t* q) : Owned(q){};

public:
/// @brief Undeclares queryable.
/// @param err if not null, the result code will be written to this location, otherwise ZException exception will be
/// thrown in case of error.
void undeclare(ZResult* err = nullptr) && {
__ZENOH_RESULT_CHECK(::z_undeclare_queryable(interop::as_moved_c_ptr(*this)), err,
"Failed to undeclare queryable");
}

friend class zenoh::Session;
};
} // namespace detail
Expand All @@ -40,13 +49,15 @@ class Queryable<void> : public detail::QueryableBase {

public:
using QueryableBase::QueryableBase;

using QueryableBase::undeclare;
friend class Session;
};

/// A Zenoh queryable. Constructed by ``Session::declare_queryable`` method.
/// @tparam Handler Streaming handler exposing data. If `void`, no handler access is provided and instead data is being
/// processed inside the callback.
/// processed inside the callback. Dropping handler-less queryable does not disable the callback. The corresponding
/// messages are still received and processed unti the corresponding session is destroyed or closed. If callback needs
/// to be disabled undeclare method should be called instead.
template <class Handler>
class Queryable : public detail::QueryableBase {
Handler _handler;
Expand All @@ -67,7 +78,13 @@ class Queryable : public detail::QueryableBase {
/// @brief Return handler to queryable data stream.
const Handler& handler() const { return _handler; };

using QueryableBase::undeclare;
friend class Session;

~Queryable() {
ZResult err;
std::move(*this).undeclare(&err);
}
};

namespace interop {
Expand Down
42 changes: 28 additions & 14 deletions include/zenoh/api/session.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ class Session : public Owned<::z_owned_session_t> {
static SessionOptions create_default() { return {}; }
};

/// @brief Options to be passed when closing a ``Session``.
struct SessionCloseOptions {
/// @name Fields
static SessionCloseOptions create_default() { return {}; }
};

/// @name Constructors

/// @brief Create a new Session.
Expand All @@ -66,7 +72,8 @@ class Session : public Owned<::z_owned_session_t> {
/// thrown in case of error.
Session(Config&& config, SessionOptions&& options = SessionOptions::create_default(), ZResult* err = nullptr)
: Owned(nullptr) {
__ZENOH_RESULT_CHECK(::z_open(&this->_0, interop::as_moved_c_ptr(config)), err, "Failed to open session");
__ZENOH_RESULT_CHECK(::z_open(&this->_0, interop::as_moved_c_ptr(config), nullptr), err,
"Failed to open session");
#ifdef ZENOHCXX_ZENOHPICO
if (err != nullptr && *err != Z_OK) return;
if (options.start_background_tasks) {
Expand All @@ -76,7 +83,7 @@ class Session : public Owned<::z_owned_session_t> {
this->start_lease_task(&err_inner);
}
if (err_inner == Z_OK) return;
::z_close(::z_move(this->_0));
::z_close(::z_move(this->_0), nullptr);
__ZENOH_RESULT_CHECK(err_inner, err, "Failed to start background tasks");
}
#else
Expand All @@ -88,9 +95,13 @@ class Session : public Owned<::z_owned_session_t> {
/// @brief Create a new Session with custom SHM client set.
/// @param config Zenoh session ``Config``.
/// @param shm_storage Storage with custom SHM clients.
/// @param options Options to pass to session creation operation.
/// @param err if not null, the result code will be written to this location, otherwise ZException exception will be
/// thrown in case of error.
Session(Config&& config, const ShmClientStorage& shm_storage, ZResult* err = nullptr) : Owned(nullptr) {
Session(Config&& config, const ShmClientStorage& shm_storage,
SessionOptions&& options = SessionOptions::create_default(), ZResult* err = nullptr)
: Owned(nullptr) {
(void)options;
__ZENOH_RESULT_CHECK(::z_open_with_custom_shm_clients(&this->_0, interop::as_moved_c_ptr(config),
interop::as_loaned_c_ptr(shm_storage)),
err, "Failed to open session");
Expand All @@ -112,24 +123,17 @@ class Session : public Owned<::z_owned_session_t> {
/// @brief A factory method equivalent to a ``Session`` constructor for custom SHM clients list.
/// @param config Zenoh session ``Config``.
/// @param shm_storage Storage with custom SHM clients.
/// @param options Options to pass to session creation operation.
/// @param err if not null, the result code will be written to this location, otherwise ZException exception will be
/// thrown in case of error.
/// @return ``Session`` object. In case of failure it will be return in invalid state.
static Session open(Config&& config, const ShmClientStorage& shm_storage, ZResult* err = nullptr) {
return Session(std::move(config), shm_storage, err);
static Session open(Config&& config, const ShmClientStorage& shm_storage,
SessionOptions&& options = SessionOptions::create_default(), ZResult* err = nullptr) {
return Session(std::move(config), shm_storage, std::move(options), err);
}
#endif

/// @name Methods

/// @brief Create a shallow copy of the session.
/// @return a new ``Session`` instance.
Session clone() const {
Session s(zenoh::detail::null_object);
::z_session_clone(&s._0, interop::as_loaned_c_ptr(*this));
return s;
}

#if defined UNSTABLE
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future
/// release.
Expand Down Expand Up @@ -879,5 +883,15 @@ class Session : public Owned<::z_owned_session_t> {
__ZENOH_RESULT_CHECK(z_timestamp_new(&t, interop::as_loaned_c_ptr(*this)), err, "Failed to create timestamp");
return interop::into_copyable_cpp_obj<Timestamp>(t);
}

/// @brief Close and invalidate the session. This also undeclares all non-undeclared Subscriber and Queryable
/// callbacks.
/// @param options options to pass to close operation.
/// @param err if not null, the result code will be written to this location, otherwise ZException exception will be
/// thrown in case of error.
void close(SessionCloseOptions&& options = SessionCloseOptions::create_default(), ZResult* err = nullptr) && {
(void)options;
__ZENOH_RESULT_CHECK(::z_close(interop::as_moved_c_ptr(*this), nullptr), err, "Failed to close the session");
}
};
} // namespace zenoh
28 changes: 21 additions & 7 deletions include/zenoh/api/subscriber.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ class SubscriberBase : public Owned<::z_owned_subscriber_t> {
SubscriberBase(::z_owned_subscriber_t* s) : Owned(s){};

public:
#ifdef ZENOHCXX_ZENOHC
/// @brief Get the key expression of the subscriber
/// @note zenoh-c only.
const KeyExpr& get_keyexpr() const {
return interop::as_owned_cpp_ref<KeyExpr>(::z_subscriber_keyexpr(interop::as_loaned_c_ptr(*this)));
}
#endif
/// @brief Undeclares subscriber.
/// @param err if not null, the result code will be written to this location, otherwise ZException exception will be
/// thrown in case of error.
void undeclare(ZResult* err = nullptr) && {
__ZENOH_RESULT_CHECK(::z_undeclare_subscriber(interop::as_moved_c_ptr(*this)), err,
"Failed to undeclare subscriber");
}
friend class zenoh::Session;
};

Expand All @@ -48,12 +52,18 @@ class Subscriber<void> : public detail::SubscriberBase {
protected:
using SubscriberBase::SubscriberBase;
friend class Session;

public:
using SubscriberBase::get_keyexpr;
using SubscriberBase::undeclare;
};

/// A Zenoh subscriber. Destroying subscriber cancels the subscription.
/// Constructed by ``Session::declare_subscriber`` method.
/// @tparam Handler Streaming handler exposing data. If `void`, no handler access is provided and instead data is being
/// processed inside the callback.
/// processed inside the callback. Dropping handler-less subscriber does not disable the callback. The corresponding
/// messages are still received and processed unti the corresponding session is destroyed or closed. If callback needs
/// to be disabled undeclare method should be called instead.
template <class Handler>
class Subscriber : public detail::SubscriberBase {
Handler _handler;
Expand All @@ -71,13 +81,17 @@ class Subscriber : public detail::SubscriberBase {
: SubscriberBase(interop::as_owned_c_ptr(s)), _handler(std::move(handler)) {}

/// @name Methods

#ifdef ZENOHCXX_ZENOHC
using SubscriberBase::get_keyexpr;
#endif
using SubscriberBase::undeclare;

/// @brief Return the handler to subscriber data stream.
const Handler& handler() const { return _handler; };
friend class Session;

~Subscriber() {
ZResult err;
std::move(*this).undeclare(&err);
}
};

namespace interop {
Expand Down
6 changes: 6 additions & 0 deletions tests/universal/network/pub_sub.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ void pub_sub(Talloc& alloc) {
assert(received_messages[0].second == "first");
assert(received_messages[1].first == "zenoh/test");
assert(received_messages[1].second == "second");
#ifdef ZENOHCXX_ZENOHC // TODO: remove once pico supports background declarations
/// check that drop does not undeclare
assert(!subscriber_dropped);
std::move(session2).close();
std::this_thread::sleep_for(1s);
#endif
assert(subscriber_dropped);
}

Expand Down
6 changes: 6 additions & 0 deletions tests/universal/network/queryable_get.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ void queryable_get() {
qd = {"zenoh/test/1", "err", 3};
assert(queries[2] == qd);

#ifdef ZENOHCXX_ZENOHC // TODO: remove once pico supports background declarations
/// check that drop does not undeclare
assert(!queryable_dropped);
std::move(session1).close();
std::this_thread::sleep_for(1s);
#endif
assert(queryable_dropped);

assert(replies.size() == 2);
Expand Down
2 changes: 1 addition & 1 deletion zenoh-c
Submodule zenoh-c updated 82 files
+396 −363 Cargo.lock
+77 −341 build-resources/opaque-types/Cargo.lock
+53 −60 build-resources/opaque-types/src/lib.rs
+43 −0 build.rs
+6 −4 docs/api.rst
+2 −2 examples/z_delete.c
+2 −2 examples/z_get.c
+2 −2 examples/z_get_liveliness.c
+2 −2 examples/z_get_shm.c
+2 −2 examples/z_info.c
+2 −2 examples/z_liveliness.c
+2 −2 examples/z_non_blocking_get.c
+2 −2 examples/z_ping.c
+2 −2 examples/z_ping_shm.c
+2 −2 examples/z_pong.c
+2 −2 examples/z_pub.c
+2 −2 examples/z_pub_attachment.c
+2 −2 examples/z_pub_cache.c
+2 −2 examples/z_pub_shm.c
+2 −2 examples/z_pub_shm_thr.c
+2 −2 examples/z_pub_thr.c
+4 −3 examples/z_pull.c
+2 −2 examples/z_put.c
+2 −2 examples/z_query_sub.c
+2 −2 examples/z_queryable.c
+2 −2 examples/z_queryable_shm.c
+2 −2 examples/z_queryable_with_channels.c
+2 −2 examples/z_sub.c
+2 −2 examples/z_sub_attachment.c
+2 −2 examples/z_sub_liveliness.c
+2 −2 examples/z_sub_shm.c
+2 −2 examples/z_sub_thr.c
+414 −308 include/zenoh_commons.h
+54 −0 include/zenoh_macros.h
+8 −8 src/closures/matching_status_closure.rs
+8 −8 src/closures/zenohid_closure.rs
+17 −13 src/collections.rs
+18 −18 src/commons.rs
+2 −53 src/config.rs
+20 −45 src/get.rs
+5 −5 src/info.rs
+10 −3 src/keyexpr.rs
+21 −23 src/liveliness.rs
+5 −5 src/payload.rs
+11 −11 src/publication_cache.rs
+13 −13 src/publisher.rs
+5 −5 src/put.rs
+5 −5 src/queryable.rs
+12 −15 src/querying_subscriber.rs
+9 −2 src/scouting.rs
+36 −28 src/session.rs
+10 −10 src/shm/buffer/zshm.rs
+11 −11 src/shm/buffer/zshmmut.rs
+5 −5 src/shm/client/shm_client.rs
+2 −2 src/shm/client/shm_segment.rs
+15 −15 src/shm/client_storage/mod.rs
+3 −3 src/shm/common/types.rs
+1 −1 src/shm/protocol_implementations/posix/posix_shm_client.rs
+9 −6 src/shm/protocol_implementations/posix/posix_shm_provider.rs
+1 −1 src/shm/protocol_implementations/posix/protocol_id.rs
+1 −1 src/shm/provider/alloc_layout.rs
+1 −1 src/shm/provider/alloc_layout_impl.rs
+2 −2 src/shm/provider/chunk.rs
+24 −21 src/shm/provider/shm_provider.rs
+1 −1 src/shm/provider/shm_provider_backend.rs
+1 −1 src/shm/provider/shm_provider_impl.rs
+19 −19 src/shm/provider/types.rs
+4 −3 src/subscriber.rs
+4 −4 tests/z_api_alignment_test.c
+8 −12 tests/z_api_config_test.c
+9 −9 tests/z_api_double_drop_test.c
+4 −4 tests/z_api_drop_options.c
+4 −4 tests/z_api_keyexpr_drop_test.c
+1 −1 tests/z_api_keyexpr_test.c
+4 −4 tests/z_api_liveliness.c
+4 −4 tests/z_int_pub_cache_query_sub_test.c
+4 −4 tests/z_int_pub_sub_attachment_test.c
+4 −4 tests/z_int_pub_sub_test.c
+4 −4 tests/z_int_queryable_attachment_test.c
+4 −4 tests/z_int_queryable_test.c
+4 −4 tests/z_leak_pub_sub_test.c
+4 −4 tests/z_leak_queryable_get_test.c
2 changes: 1 addition & 1 deletion zenoh-pico
Submodule zenoh-pico updated 102 files

0 comments on commit 3cd6e9e

Please sign in to comment.