Skip to content

Commit e0860d4

Browse files
Honor ignore_local_publications in subscription options (#508) (#513)
make linters happy Add comment on ZC_LOCALITY_REMOTE Signed-off-by: Yadunund <[email protected]> (cherry picked from commit f6a4fde) Co-authored-by: Alejandro Hernández Cordero <[email protected]>
1 parent 0323702 commit e0860d4

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

rmw_zenoh_cpp/src/detail/rmw_node_data.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ bool NodeData::create_sub_data(
190190
std::size_t id,
191191
const std::string & topic_name,
192192
const rosidl_message_type_support_t * type_support,
193-
const rmw_qos_profile_t * qos_profile)
193+
const rmw_qos_profile_t * qos_profile,
194+
const rmw_subscription_options_t & sub_options)
194195
{
195196
std::lock_guard<std::recursive_mutex> lock_guard(mutex_);
196197
if (is_shutdown_) {
@@ -216,7 +217,8 @@ bool NodeData::create_sub_data(
216217
std::move(id),
217218
std::move(topic_name),
218219
type_support,
219-
qos_profile);
220+
qos_profile,
221+
sub_options);
220222
if (sub_data == nullptr) {
221223
RMW_ZENOH_LOG_ERROR_NAMED(
222224
"rmw_zenoh_cpp",

rmw_zenoh_cpp/src/detail/rmw_node_data.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ class NodeData final
7575
std::size_t id,
7676
const std::string & topic_name,
7777
const rosidl_message_type_support_t * type_support,
78-
const rmw_qos_profile_t * qos_profile);
78+
const rmw_qos_profile_t * qos_profile,
79+
const rmw_subscription_options_t & sub_options);
7980

8081
// Retrieve the SubscriptionData for a given rmw_subscription_t if present.
8182
SubscriptionDataPtr get_sub_data(const rmw_subscription_t * const subscription);

rmw_zenoh_cpp/src/detail/rmw_subscription_data.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ std::shared_ptr<SubscriptionData> SubscriptionData::make(
5959
std::size_t subscription_id,
6060
const std::string & topic_name,
6161
const rosidl_message_type_support_t * type_support,
62-
const rmw_qos_profile_t * qos_profile)
62+
const rmw_qos_profile_t * qos_profile,
63+
const rmw_subscription_options_t & sub_options)
6364
{
6465
rmw_qos_profile_t adapted_qos_profile = *qos_profile;
6566
rmw_ret_t ret = QoS::get().best_available_qos(
@@ -120,7 +121,8 @@ std::shared_ptr<SubscriptionData> SubscriptionData::make(
120121
std::move(entity),
121122
std::move(session),
122123
type_support->data,
123-
std::move(message_type_support)
124+
std::move(message_type_support),
125+
sub_options
124126
});
125127

126128
if (!sub_data->init()) {
@@ -138,13 +140,15 @@ SubscriptionData::SubscriptionData(
138140
std::shared_ptr<liveliness::Entity> entity,
139141
std::shared_ptr<zenoh::Session> session,
140142
const void * type_support_impl,
141-
std::unique_ptr<MessageTypeSupport> type_support)
143+
std::unique_ptr<MessageTypeSupport> type_support,
144+
rmw_subscription_options_t sub_options)
142145
: rmw_node_(rmw_node),
143146
graph_cache_(std::move(graph_cache)),
144147
entity_(std::move(entity)),
145148
sess_(std::move(session)),
146149
type_support_impl_(type_support_impl),
147150
type_support_(std::move(type_support)),
151+
sub_options_(std::move(sub_options)),
148152
last_known_published_msg_({}),
149153
wait_set_data_(nullptr),
150154
is_shutdown_(false),
@@ -153,6 +157,7 @@ SubscriptionData::SubscriptionData(
153157
events_mgr_ = std::make_shared<EventsManager>();
154158
}
155159

160+
///=============================================================================
156161
// We have to use an "init" function here, rather than do this in the constructor, because we use
157162
// enable_shared_from_this, which is not available in constructors.
158163
bool SubscriptionData::init()
@@ -171,6 +176,14 @@ bool SubscriptionData::init()
171176
using AdvancedSubscriberOptions = zenoh::ext::SessionExt::AdvancedSubscriberOptions;
172177
auto adv_sub_opts = AdvancedSubscriberOptions::create_default();
173178

179+
// By default, this subscription will receive publications from publishers within and outside of
180+
// the same Zenoh session as this subscription.
181+
// If ignore_local_publications is true, we restrict this subscription to only receive samples
182+
// from publishers in remote sessions.
183+
if (sub_options_.ignore_local_publications) {
184+
adv_sub_opts.subscriber_options.allowed_origin = ZC_LOCALITY_REMOTE;
185+
}
186+
174187
// Instantiate the subscription with suitable options depending on the
175188
// adapted_qos_profile.
176189
if (entity_->topic_info()->qos_.durability == RMW_QOS_POLICY_DURABILITY_TRANSIENT_LOCAL) {

rmw_zenoh_cpp/src/detail/rmw_subscription_data.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ class SubscriptionData final : public std::enable_shared_from_this<SubscriptionD
7171
std::size_t Subscription_id,
7272
const std::string & topic_name,
7373
const rosidl_message_type_support_t * type_support,
74-
const rmw_qos_profile_t * qos_profile);
74+
const rmw_qos_profile_t * qos_profile,
75+
const rmw_subscription_options_t & sub_options);
7576

7677
// Get a copy of the keyexpr_hash of this SubscriptionData's liveliness::Entity.
7778
std::size_t keyexpr_hash() const;
@@ -124,7 +125,8 @@ class SubscriptionData final : public std::enable_shared_from_this<SubscriptionD
124125
std::shared_ptr<liveliness::Entity> entity,
125126
std::shared_ptr<zenoh::Session> session,
126127
const void * type_support_impl,
127-
std::unique_ptr<MessageTypeSupport> type_support);
128+
std::unique_ptr<MessageTypeSupport> type_support,
129+
rmw_subscription_options_t sub_options);
128130

129131
bool init();
130132

@@ -145,6 +147,8 @@ class SubscriptionData final : public std::enable_shared_from_this<SubscriptionD
145147
// Type support fields
146148
const void * type_support_impl_;
147149
std::unique_ptr<MessageTypeSupport> type_support_;
150+
// Subscription options.
151+
rmw_subscription_options_t sub_options_;
148152
std::deque<std::unique_ptr<Message>> message_queue_;
149153
// Map GID of a subscription to the sequence number of the message it published.
150154
std::unordered_map<size_t, int64_t> last_known_published_msg_;

rmw_zenoh_cpp/src/rmw_zenoh.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,8 @@ rmw_create_subscription(
985985
context_impl->get_next_entity_id(),
986986
topic_name,
987987
type_support,
988-
qos_profile))
988+
qos_profile,
989+
*subscription_options))
989990
{
990991
// Error already handled.
991992
return nullptr;

0 commit comments

Comments
 (0)