-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathsession.hxx
More file actions
1179 lines (1079 loc) · 63.1 KB
/
session.hxx
File metadata and controls
1179 lines (1079 loc) · 63.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 2024 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
#pragma once
#include <optional>
#include "../detail/closures_concrete.hxx"
#include "base.hxx"
#include "closures.hxx"
#include "config.hxx"
#include "enums.hxx"
#include "id.hxx"
#include "interop.hxx"
#include "keyexpr.hxx"
#include "liveliness.hxx"
#include "publisher.hxx"
#include "query_consolidation.hxx"
#include "queryable.hxx"
#include "subscriber.hxx"
#include "timestamp.hxx"
#if (defined(ZENOHCXX_ZENOHC) || Z_FEATURE_QUERY == 1) && defined(Z_FEATURE_UNSTABLE_API)
#include "querier.hxx"
#endif
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_SHARED_MEMORY) && defined(Z_FEATURE_UNSTABLE_API)
#include "shm/client_storage/client_storage.hxx"
#endif
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
#include "close.hxx"
#endif
namespace zenoh {
namespace ext {
class SessionExt;
}
/// A Zenoh session.
class Session : public Owned<::z_owned_session_t> {
Session(zenoh::detail::null_object_t) : Owned(nullptr){};
public:
/// @brief Options to be passed when opening a ``Session``.
struct SessionOptions {
/// @name Fields
#ifdef ZENOHCXX_ZENOHPICO
/// @brief If ``true``, start background threads which handle the network
/// traffic. If false, the threads should be called manually with ``Session::start_read_task`` and
/// ``Session::start_lease_task`` or methods ``Session::read``, ``Session::send_keep_alive`` and
/// ``Session::send_join`` should be called in loop.
/// @note Zenoh-pico only.
bool start_background_tasks = true;
#endif
static SessionOptions create_default() { return {}; }
};
/// @brief Options to be passed when closing a ``Session``.
struct SessionCloseOptions {
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
/// The timeout for close operation in milliseconds. 0 means default close timeout which is 10 seconds.
uint32_t timeout_ms = 10000;
/// A function to receive close handle. If set, the close operation will be executed concurrently
/// in separate task, and this function will receive a handle to be used for controlling
/// close execution.
std::function<void(CloseHandle&&)> out_concurrent = nullptr;
#endif
/// @name Fields
static SessionCloseOptions create_default() { return {}; }
};
/// @name Constructors
/// @brief Create a new Session.
/// @param config Zenoh session ``Config``.
/// @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, SessionOptions&& options = SessionOptions::create_default(), ZResult* err = nullptr)
: Owned(nullptr) {
__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) {
ZResult err_inner;
this->start_read_task(&err_inner);
if (err_inner == Z_OK) {
this->start_lease_task(&err_inner);
}
if (err_inner == Z_OK) return;
::z_drop(::z_move(this->_0));
__ZENOH_RESULT_CHECK(err_inner, err, "Failed to start background tasks");
}
#else
(void)options;
#endif
}
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_SHARED_MEMORY) && defined(Z_FEATURE_UNSTABLE_API)
/// @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,
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");
}
#endif
/// @brief A factory method equivalent to a ``Session`` constructor.
/// @param config Zenoh session ``Config``.
/// @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, SessionOptions&& options = SessionOptions::create_default(),
ZResult* err = nullptr) {
return Session(std::move(config), std::move(options), err);
}
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_SHARED_MEMORY) && defined(Z_FEATURE_UNSTABLE_API)
/// @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,
SessionOptions&& options = SessionOptions::create_default(), ZResult* err = nullptr) {
return Session(std::move(config), shm_storage, std::move(options), err);
}
#endif
/// @name Methods
/// @brief Get the unique identifier of the zenoh node associated to this ``Session``.
/// @return the unique identifier ``Id``.
Id get_zid() const { return interop::into_copyable_cpp_obj<Id>(::z_info_zid(interop::as_loaned_c_ptr(*this))); }
/// @brief Create ``KeyExpr`` instance with numeric id registered in ``Session`` routing tables (to reduce bandwith
/// usage).
/// @param key_expr ``KeyExpr`` to declare.
/// @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 declared ``KeyExpr`` instance.
KeyExpr declare_keyexpr(const KeyExpr& key_expr, ZResult* err = nullptr) const {
KeyExpr k = interop::detail::null<KeyExpr>();
__ZENOH_RESULT_CHECK(::z_declare_keyexpr(interop::as_loaned_c_ptr(*this), interop::as_owned_c_ptr(k),
interop::as_loaned_c_ptr(key_expr)),
err, std::string("Failed to declare key expression: ").append(k.as_string_view()));
return k;
}
/// @brief Remove ``KeyExpr`` instance from ``Session`` routing table and drop ``KeyExpr`` instance.
/// @param err if not null, the result code will be written to this location, otherwise ZException exception will be
/// thrown in case of error.
/// @param key_expr ``KeyExpr`` instance to undeclare, that was previously returned by ``Session::declare_keyexpr``.
void undeclare_keyexpr(KeyExpr&& key_expr, ZResult* err = nullptr) const {
__ZENOH_RESULT_CHECK(::z_undeclare_keyexpr(interop::as_loaned_c_ptr(*this), interop::as_moved_c_ptr(key_expr)),
err, "Failed to undeclare key expression");
}
#if defined(ZENOHCXX_ZENOHC) || Z_FEATURE_QUERY == 1
/// @brief Options passed to the ``Session::get`` operation.
struct GetOptions {
/// @name Fields
/// @brief The Queryables that should be target of the query.
QueryTarget target = QueryTarget::Z_QUERY_TARGET_BEST_MATCHING;
/// @brief The replies consolidation strategy to apply on replies to the query.
QueryConsolidation consolidation = QueryConsolidation();
/// @brief The priority of the get message.
Priority priority = Z_PRIORITY_DEFAULT;
/// @brief The congestion control to apply when routing get message.
CongestionControl congestion_control = ::z_internal_congestion_control_default_request();
/// @brief Whether Zenoh will NOT wait to batch get message with others to reduce the bandwith.
bool is_express = false;
/// @brief An optional payload of the query.
std::optional<Bytes> payload = {};
/// @brief An optional encoding of the query payload and/or attachment.
std::optional<Encoding> encoding = {};
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future
/// release.
/// @brief The source info for the query.
std::optional<SourceInfo> source_info = {};
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future
/// release.
///
/// @brief The accepted replies for the query.
/// @note Zenoh-c only.
ReplyKeyExpr accept_replies = ::zc_reply_keyexpr_default();
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future
/// release.
/// @brief Allowed destination.
/// @note Zenoh-c only.
Locality allowed_destination = ::zc_locality_default();
#endif
/// @brief An optional attachment to the query.
std::optional<Bytes> attachment = {};
/// @brief The timeout for the query in milliseconds. 0 means default query timeout from zenoh configuration.
uint64_t timeout_ms = 0;
/// @name Methods
/// @brief Create default option settings.
static GetOptions create_default() { return {}; }
};
/// @brief Query data from the matching queryables in the system. Replies are provided through a callback function.
/// @param key_expr ``KeyExpr`` the key expression matching resources to query.
/// @param parameters the parameters string in URL format.
/// @param on_reply callable that will be called once for each received reply.
/// @param on_drop callable that will be called once all replies are received.
/// @param options ``GetOptions`` query options.
/// @param err if not null, the result code will be written to this location, otherwise ZException exception will be
/// thrown in case of error.
template <class C, class D>
void get(const KeyExpr& key_expr, const std::string& parameters, C&& on_reply, D&& on_drop,
GetOptions&& options = GetOptions::create_default(), ZResult* err = nullptr) const {
static_assert(std::is_invocable_r<void, C, Reply&>::value,
"on_reply should be callable with the following signature: void on_reply(zenoh::Reply& reply)");
static_assert(std::is_invocable_r<void, D>::value,
"on_drop should be callable with the following signature: void on_drop()");
::z_owned_closure_reply_t c_closure;
using Cval = std::remove_reference_t<C>;
using Dval = std::remove_reference_t<D>;
using ClosureType = typename detail::closures::Closure<Cval, Dval, void, Reply&>;
auto closure = ClosureType::into_context(std::forward<C>(on_reply), std::forward<D>(on_drop));
::z_closure(&c_closure, detail::closures::_zenoh_on_reply_call, detail::closures::_zenoh_on_drop, closure);
::z_get_options_t opts;
z_get_options_default(&opts);
opts.target = options.target;
opts.consolidation = *interop::as_copyable_c_ptr(options.consolidation);
opts.congestion_control = options.congestion_control;
opts.priority = options.priority;
opts.is_express = options.is_express;
opts.payload = interop::as_moved_c_ptr(options.payload);
opts.encoding = interop::as_moved_c_ptr(options.encoding);
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
opts.source_info = interop::as_moved_c_ptr(options.source_info);
opts.accept_replies = options.accept_replies;
opts.allowed_destination = options.allowed_destination;
#endif
opts.attachment = interop::as_moved_c_ptr(options.attachment);
opts.timeout_ms = options.timeout_ms;
__ZENOH_RESULT_CHECK(::z_get(interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr),
parameters.c_str(), ::z_move(c_closure), &opts),
err, "Failed to perform get operation");
}
/// @brief Query data from the matching queryables in the system. Replies are provided through a channel.
/// @tparam Channel the type of channel used to create stream of data (see ``zenoh::channels::FifoChannel`` or
/// ``zenoh::channels::RingChannel``).
/// @param key_expr the key expression matching resources to query.
/// @param parameters the parameters string in URL format.
/// @param channel channel instance.
/// @param options query options.
/// @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 reply handler.
template <class Channel>
typename Channel::template HandlerType<Reply> get(const KeyExpr& key_expr, const std::string& parameters,
Channel channel,
GetOptions&& options = GetOptions::create_default(),
ZResult* err = nullptr) const {
auto cb_handler_pair = channel.template into_cb_handler_pair<Reply>();
::z_get_options_t opts;
z_get_options_default(&opts);
opts.target = options.target;
opts.consolidation = *interop::as_copyable_c_ptr(options.consolidation);
opts.congestion_control = options.congestion_control;
opts.priority = options.priority;
opts.is_express = options.is_express;
opts.payload = interop::as_moved_c_ptr(options.payload);
opts.encoding = interop::as_moved_c_ptr(options.encoding);
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
opts.source_info = interop::as_moved_c_ptr(options.source_info);
opts.accept_replies = options.accept_replies;
opts.allowed_destination = options.allowed_destination;
#endif
opts.attachment = interop::as_moved_c_ptr(options.attachment);
opts.timeout_ms = options.timeout_ms;
ZResult res = ::z_get(interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), parameters.c_str(),
::z_move(cb_handler_pair.first), &opts);
__ZENOH_RESULT_CHECK(res, err, "Failed to perform get operation");
if (res != Z_OK) ::z_drop(interop::as_moved_c_ptr(cb_handler_pair.second));
return std::move(cb_handler_pair.second);
}
#endif
#if defined(ZENOHCXX_ZENOHC) || Z_FEATURE_QUERYABLE == 1
/// @brief Options to be passed when declaring a ``Queryable``
struct QueryableOptions {
/// @name Fields
/// @brief The completeness of the Queryable.
bool complete = false;
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future
/// release.
/// Restrict the matching requests that will be received by this Queryable to the ones
/// that have the compatible allowed_destination.
/// @note Zenoh-c only.
Locality allowed_origin = ::zc_locality_default();
#endif
/// @name Methods
/// @brief Create default option settings.
static QueryableOptions create_default() { return {}; }
private:
friend struct interop::detail::Converter;
::z_queryable_options_t to_c_opts() {
::z_queryable_options_t opts;
z_queryable_options_default(&opts);
opts.complete = this->complete;
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
opts.allowed_origin = this->allowed_origin;
#endif
return opts;
}
};
/// @brief Create a ``Queryable`` object to answer to ``Session::get`` requests.
/// @param key_expr the key expression to match the ``Session::get`` requests.
/// @param on_query the callable to handle ``Query`` requests. Will be called once for each query.
/// @param on_drop the drop callable. Will be called once, when ``Queryable`` is destroyed or undeclared.
/// @param options options passed to queryable declaration.
/// @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 a ``Queryable`` object.
template <class C, class D>
[[nodiscard]] Queryable<void> declare_queryable(const KeyExpr& key_expr, C&& on_query, D&& on_drop,
QueryableOptions&& options = QueryableOptions::create_default(),
ZResult* err = nullptr) const {
static_assert(std::is_invocable_r<void, C, Query&>::value,
"on_query should be callable with the following signature: void on_query(zenoh::Query& query)");
static_assert(std::is_invocable_r<void, D>::value,
"on_drop should be callable with the following signature: void on_drop()");
::z_owned_closure_query_t c_closure;
using Cval = std::remove_reference_t<C>;
using Dval = std::remove_reference_t<D>;
using ClosureType = typename detail::closures::Closure<Cval, Dval, void, Query&>;
auto closure = ClosureType::into_context(std::forward<C>(on_query), std::forward<D>(on_drop));
::z_closure(&c_closure, detail::closures::_zenoh_on_query_call, detail::closures::_zenoh_on_drop, closure);
::z_queryable_options_t opts = interop::detail::Converter::to_c_opts(options);
Queryable<void> q(zenoh::detail::null_object);
ZResult res = ::z_declare_queryable(interop::as_loaned_c_ptr(*this), interop::as_owned_c_ptr(q),
interop::as_loaned_c_ptr(key_expr), ::z_move(c_closure), &opts);
__ZENOH_RESULT_CHECK(res, err, "Failed to declare Queryable");
return q;
}
/// @brief Declare a background queryable. It will answer to ``Session::get`` requests, until the corresponding
/// session is closed or destroyed.
/// @param key_expr the key expression to match the ``Session::get`` requests.
/// @param on_query the callable to handle ``Query`` requests. Will be called once for each query.
/// @param on_drop the drop callable. Will be called once, when ``Queryable`` is destroyed or undeclared.
/// @param options options passed to queryable declaration.
/// @param err if not null, the result code will be written to this location, otherwise ZException exception will be
/// thrown in case of error.
template <class C, class D>
void declare_background_queryable(const KeyExpr& key_expr, C&& on_query, D&& on_drop,
QueryableOptions&& options = QueryableOptions::create_default(),
ZResult* err = nullptr) const {
static_assert(std::is_invocable_r<void, C, Query&>::value,
"on_query should be callable with the following signature: void on_query(zenoh::Query& query)");
static_assert(std::is_invocable_r<void, D>::value,
"on_drop should be callable with the following signature: void on_drop()");
::z_owned_closure_query_t c_closure;
using Cval = std::remove_reference_t<C>;
using Dval = std::remove_reference_t<D>;
using ClosureType = typename detail::closures::Closure<Cval, Dval, void, Query&>;
auto closure = ClosureType::into_context(std::forward<C>(on_query), std::forward<D>(on_drop));
::z_closure(&c_closure, detail::closures::_zenoh_on_query_call, detail::closures::_zenoh_on_drop, closure);
::z_queryable_options_t opts = interop::detail::Converter::to_c_opts(options);
ZResult res = ::z_declare_background_queryable(interop::as_loaned_c_ptr(*this),
interop::as_loaned_c_ptr(key_expr), ::z_move(c_closure), &opts);
__ZENOH_RESULT_CHECK(res, err, "Failed to declare Background Queryable");
}
/// @brief Create a ``Queryable`` object to answer to ``Session::get`` requests.
/// @tparam Channel the type of channel used to create stream of data (see ``zenoh::channels::FifoChannel`` or
/// ``zenoh::channels::RingChannel``).
/// @param key_expr the key expression to match the ``Session::get`` requests.
/// @param channel an instance of channel.
/// @param options options passed to queryable declaration.
/// @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 a ``Queryable`` object.
template <class Channel>
[[nodiscard]] Queryable<typename Channel::template HandlerType<Query>> declare_queryable(
const KeyExpr& key_expr, Channel channel, QueryableOptions&& options = QueryableOptions::create_default(),
ZResult* err = nullptr) const {
auto cb_handler_pair = channel.template into_cb_handler_pair<Query>();
::z_queryable_options_t opts = interop::detail::Converter::to_c_opts(options);
Queryable<void> q(zenoh::detail::null_object);
ZResult res = ::z_declare_queryable(interop::as_loaned_c_ptr(*this), interop::as_owned_c_ptr(q),
interop::as_loaned_c_ptr(key_expr), ::z_move(cb_handler_pair.first), &opts);
__ZENOH_RESULT_CHECK(res, err, "Failed to declare Queryable");
if (res != Z_OK) ::z_drop(interop::as_moved_c_ptr(cb_handler_pair.second));
return Queryable<typename Channel::template HandlerType<Query>>(std::move(q),
std::move(cb_handler_pair.second));
}
#endif
#if defined(ZENOHCXX_ZENOHC) || Z_FEATURE_SUBSCRIPTION == 1
/// @brief Options to be passed when declaring a ``Subscriber``.
struct SubscriberOptions {
/// @name Fields
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future
/// release.
/// Restrict the matching publications that will be received by this Subscribers to the ones
/// that have the compatible allowed_destination.
/// @note Zenoh-c only.
Locality allowed_origin = ::zc_locality_default();
#endif
/// @name Methods
/// @brief Create default option settings.
static SubscriberOptions create_default() { return {}; }
private:
friend struct interop::detail::Converter;
::z_subscriber_options_t to_c_opts() {
::z_subscriber_options_t opts;
z_subscriber_options_default(&opts);
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
opts.allowed_origin = this->allowed_origin;
#endif
return opts;
}
};
/// @brief Create a ``Subscriber`` object to receive data from matching ``Publisher`` objects or from
/// ``Session::put`` and ``Session::delete_resource`` requests.
/// @param key_expr the key expression to match the publishers.
/// @param on_sample the callback that will be called for each received sample.
/// @param on_drop the callback that will be called once subscriber is destroyed or undeclared.
/// @param options options to pass to subscriber declaration.
/// @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 a ``Subscriber`` object.
template <class C, class D>
[[nodiscard]] Subscriber<void> declare_subscriber(const KeyExpr& key_expr, C&& on_sample, D&& on_drop,
SubscriberOptions&& options = SubscriberOptions::create_default(),
ZResult* err = nullptr) const {
static_assert(
std::is_invocable_r<void, C, Sample&>::value,
"on_sample should be callable with the following signature: void on_sample(zenoh::Sample& sample)");
static_assert(std::is_invocable_r<void, D>::value,
"on_drop should be callable with the following signature: void on_drop()");
::z_owned_closure_sample_t c_closure;
using Cval = std::remove_reference_t<C>;
using Dval = std::remove_reference_t<D>;
using ClosureType = typename detail::closures::Closure<Cval, Dval, void, Sample&>;
auto closure = ClosureType::into_context(std::forward<C>(on_sample), std::forward<D>(on_drop));
::z_closure(&c_closure, detail::closures::_zenoh_on_sample_call, detail::closures::_zenoh_on_drop, closure);
::z_subscriber_options_t opts = interop::detail::Converter::to_c_opts(options);
Subscriber<void> s = interop::detail::null<Subscriber<void>>();
ZResult res = ::z_declare_subscriber(interop::as_loaned_c_ptr(*this), interop::as_owned_c_ptr(s),
interop::as_loaned_c_ptr(key_expr), ::z_move(c_closure), &opts);
__ZENOH_RESULT_CHECK(res, err, "Failed to declare Subscriber");
return s;
}
/// @brief Declare a a background subscriber. It will receive data from matching ``Publisher`` objects or from
/// ``Session::put`` and ``Session::delete_resource`` requests, until the corresponding session is closed or
/// destroyed.
/// @param key_expr the key expression to match the publishers.
/// @param on_sample the callback that will be called for each received sample.
/// @param on_drop the callback that will be called once subscriber is destroyed or undeclared.
/// @param options options to pass to subscriber declaration.
/// @param err if not null, the result code will be written to this location, otherwise ZException exception will be
/// thrown in case of error.
template <class C, class D>
void declare_background_subscriber(const KeyExpr& key_expr, C&& on_sample, D&& on_drop,
SubscriberOptions&& options = SubscriberOptions::create_default(),
ZResult* err = nullptr) const {
static_assert(
std::is_invocable_r<void, C, Sample&>::value,
"on_sample should be callable with the following signature: void on_sample(zenoh::Sample& sample)");
static_assert(std::is_invocable_r<void, D>::value,
"on_drop should be callable with the following signature: void on_drop()");
::z_owned_closure_sample_t c_closure;
using Cval = std::remove_reference_t<C>;
using Dval = std::remove_reference_t<D>;
using ClosureType = typename detail::closures::Closure<Cval, Dval, void, Sample&>;
auto closure = ClosureType::into_context(std::forward<C>(on_sample), std::forward<D>(on_drop));
::z_closure(&c_closure, detail::closures::_zenoh_on_sample_call, detail::closures::_zenoh_on_drop, closure);
::z_subscriber_options_t opts = interop::detail::Converter::to_c_opts(options);
ZResult res = ::z_declare_background_subscriber(interop::as_loaned_c_ptr(*this),
interop::as_loaned_c_ptr(key_expr), ::z_move(c_closure), &opts);
__ZENOH_RESULT_CHECK(res, err, "Failed to declare Background Subscriber");
}
/// @brief Create a ``Subscriber`` object to receive data from matching ``Publisher`` objects or from.
/// ``Session::put`` and ``Session::delete_resource`` requests.
/// @tparam Channel the type of channel used to create stream of data (see ``zenoh::channels::FifoChannel`` or
/// ``zenoh::channels::RingChannel``).
/// @param key_expr the key expression to match the publishers.
/// @param channel an instance of channel.
/// @param options options to pass to subscriber declaration.
/// @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 a ``Subscriber`` object.
template <class Channel>
[[nodiscard]] Subscriber<typename Channel::template HandlerType<Sample>> declare_subscriber(
const KeyExpr& key_expr, Channel channel, SubscriberOptions&& options = SubscriberOptions::create_default(),
ZResult* err = nullptr) const {
auto cb_handler_pair = channel.template into_cb_handler_pair<Sample>();
::z_subscriber_options_t opts = interop::detail::Converter::to_c_opts(options);
Subscriber<void> s = interop::detail::null<Subscriber<void>>();
ZResult res =
::z_declare_subscriber(interop::as_loaned_c_ptr(*this), interop::as_owned_c_ptr(s),
interop::as_loaned_c_ptr(key_expr), ::z_move(cb_handler_pair.first), &opts);
__ZENOH_RESULT_CHECK(res, err, "Failed to declare Subscriber");
if (res != Z_OK) ::z_drop(interop::as_moved_c_ptr(cb_handler_pair.second));
return Subscriber<typename Channel::template HandlerType<Sample>>(std::move(s),
std::move(cb_handler_pair.second));
}
#endif
#if defined(ZENOHCXX_ZENOHC) || Z_FEATURE_PUBLICATION == 1
/// @brief Options to be passed to ``delete_resource`` operation
struct DeleteOptions {
/// @name Fields
/// @brief The priority of the delete message.
Priority priority = Z_PRIORITY_DEFAULT;
/// @brief The congestion control to apply when routing delete message.
CongestionControl congestion_control = ::z_internal_congestion_control_default_push();
/// @brief Whether Zenoh will NOT wait to batch delete message with others to reduce the bandwith.
bool is_express = false;
#if defined(Z_FEATURE_UNSTABLE_API)
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future
/// release.
/// @brief The delete operation reliability.
Reliability reliability = z_reliability_default();
#endif
/// @brief the timestamp of this message.
std::optional<Timestamp> timestamp = {};
/// @name Methods
/// @brief Create default option settings.
static DeleteOptions create_default() { return {}; }
};
/// @brief Undeclare a resource. Equivalent to ``Publisher::delete_resource``.
/// @param key_expr the key expression to delete the resource.
/// @param options options to pass to delete 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 delete_resource(const KeyExpr& key_expr, DeleteOptions&& options = DeleteOptions::create_default(),
ZResult* err = nullptr) const {
::z_delete_options_t opts;
z_delete_options_default(&opts);
opts.congestion_control = options.congestion_control;
opts.priority = options.priority;
opts.is_express = options.is_express;
#if defined(Z_FEATURE_UNSTABLE_API)
opts.reliability = options.reliability;
#endif
__ZENOH_RESULT_CHECK(::z_delete(interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), &opts),
err, "Failed to perform delete operation");
}
/// @brief Options passed to the ``Session::put`` operation.
struct PutOptions {
/// @name Fields
/// @brief The priority of this message.
Priority priority = Z_PRIORITY_DEFAULT;
/// @brief The congestion control to apply when routing this message.
CongestionControl congestion_control = ::z_internal_congestion_control_default_push();
/// @brief Whether Zenoh will NOT wait to batch this message with others to reduce the bandwith.
bool is_express = false;
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future
/// release.
/// @brief Allowed destination.
/// @note Zenoh-c only.
Locality allowed_destination = ::zc_locality_default();
#endif
/// @brief the timestamp of this message.
std::optional<Timestamp> timestamp = {};
/// @brief An optional encoding of the message payload and/or attachment.
std::optional<Encoding> encoding = {};
#if defined(Z_FEATURE_UNSTABLE_API)
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future
/// release.
/// @brief The put operation reliability.
Reliability reliability = z_reliability_default();
#endif
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future
/// release.
/// @brief The source info of this message.
/// @note Zenoh-c only.
std::optional<SourceInfo> source_info = {};
#endif
/// @brief An optional attachment to the message.
std::optional<Bytes> attachment = {};
/// @name Methods
/// @brief Create default option settings.
static PutOptions create_default() { return {}; }
};
/// @brief Publish data to the matching subscribers in the system. Equivalent to ``Publisher::put``.
/// @param key_expr the key expression to put the data.
/// @param payload the data to publish.
/// @param options options to pass to put 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 put(const KeyExpr& key_expr, Bytes&& payload, PutOptions&& options = PutOptions::create_default(),
ZResult* err = nullptr) const {
::z_put_options_t opts;
z_put_options_default(&opts);
opts.encoding = interop::as_moved_c_ptr(options.encoding);
opts.congestion_control = options.congestion_control;
opts.priority = options.priority;
opts.is_express = options.is_express;
#if defined(Z_FEATURE_UNSTABLE_API)
opts.reliability = options.reliability;
#endif
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
opts.allowed_destination = options.allowed_destination;
opts.source_info = interop::as_moved_c_ptr(options.source_info);
#endif
opts.attachment = interop::as_moved_c_ptr(options.attachment);
opts.timestamp = interop::as_copyable_c_ptr(options.timestamp);
auto payload_ptr = interop::as_moved_c_ptr(payload);
__ZENOH_RESULT_CHECK(
::z_put(interop::as_loaned_c_ptr(*this), interop::as_loaned_c_ptr(key_expr), payload_ptr, &opts), err,
"Failed to perform put operation");
}
/// @brief Options to be passed when declaring a ``Publisher``.
struct PublisherOptions {
/// @name Fields
/// @brief The congestion control to apply when routing messages from this publisher.
CongestionControl congestion_control = ::z_internal_congestion_control_default_push();
/// @brief The priority of messages from this publisher.
Priority priority = Z_PRIORITY_DEFAULT;
/// @brief If ``true``, Zenoh will not wait to batch this message with others to reduce the bandwith.
bool is_express = false;
#if defined(Z_FEATURE_UNSTABLE_API)
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future
/// release.
/// @brief The publisher reliability.
Reliability reliability = z_reliability_default();
#endif
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future
/// release.
/// @brief Allowed destination.
/// @note Zenoh-c only.
Locality allowed_destination = ::zc_locality_default();
#endif
/// @brief Default encoding to use for Publisher::put.
std::optional<Encoding> encoding = {};
/// @name Methods
/// @brief Create default option settings.
static PublisherOptions create_default() { return {}; }
private:
friend struct interop::detail::Converter;
::z_publisher_options_t to_c_opts() {
::z_publisher_options_t opts;
z_publisher_options_default(&opts);
opts.congestion_control = this->congestion_control;
opts.priority = this->priority;
opts.is_express = this->is_express;
#if defined(Z_FEATURE_UNSTABLE_API)
opts.reliability = this->reliability;
#endif
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
opts.allowed_destination = this->allowed_destination;
#endif
opts.encoding = interop::as_moved_c_ptr(this->encoding);
return opts;
}
};
/// @brief Create a ``Publisher`` object to publish data to matching ``Subscriber`` objects.
/// @param key_expr the key expression to match the subscribers.
/// @param options options passed to publisher declaration.
/// @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 a ``Publisher`` object.
Publisher declare_publisher(const KeyExpr& key_expr,
PublisherOptions&& options = PublisherOptions::create_default(),
ZResult* err = nullptr) const {
Publisher p = interop::detail::null<Publisher>();
::z_publisher_options_t opts = interop::detail::Converter::to_c_opts(options);
ZResult res = ::z_declare_publisher(interop::as_loaned_c_ptr(*this), interop::as_owned_c_ptr(p),
interop::as_loaned_c_ptr(key_expr), &opts);
__ZENOH_RESULT_CHECK(res, err, "Failed to declare Publisher");
return p;
}
#endif
#if (defined(ZENOHCXX_ZENOHC) || Z_FEATURE_QUERY == 1) && defined(Z_FEATURE_UNSTABLE_API)
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future
/// release.
/// @brief Options to be passed when declaring a ``Querier``.
struct QuerierOptions {
/// @name Fields
/// @brief The Queryables that should be target of the querier queries.
QueryTarget target = QueryTarget::Z_QUERY_TARGET_BEST_MATCHING;
/// @brief The replies consolidation strategy to apply on replies to the querier queries.
QueryConsolidation consolidation = QueryConsolidation();
/// @brief The priority of the querier queries.
Priority priority = Z_PRIORITY_DEFAULT;
/// @brief The congestion control to apply when routing querier queries.
CongestionControl congestion_control = ::z_internal_congestion_control_default_request();
/// @brief Whether Zenoh will NOT wait to batch querier queries with other messages to reduce the bandwith.
bool is_express = false;
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future
/// release.
///
/// @brief The accepted replies for the querier queries.
/// @note Zenoh-c only.
ReplyKeyExpr accept_replies = ::zc_reply_keyexpr_default();
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future
/// release.
/// @brief Allowed destination for querier queries.
/// @note Zenoh-c only.
Locality allowed_destination = ::zc_locality_default();
#endif
/// @brief The timeout for the querier queries in milliseconds. 0 means default query timeout from zenoh
/// configuration.
uint64_t timeout_ms = 0;
/// @name Methods
/// @brief Create default option settings.
static QuerierOptions create_default() { return {}; }
};
/// @warning This API has been marked as unstable: it works as advertised, but it may be changed in a future
/// release.
/// @brief Create a ``Querier`` object to send queries to matching ``Queryable`` objects.
/// @param key_expr the key expression to match the queryables.
/// @param options options passed to querier declaration.
/// @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 a ``Querier`` object.
Querier declare_querier(const KeyExpr& key_expr, QuerierOptions&& options = QuerierOptions::create_default(),
ZResult* err = nullptr) const {
::z_querier_options_t opts;
z_querier_options_default(&opts);
opts.target = options.target;
opts.consolidation = *interop::as_copyable_c_ptr(options.consolidation);
opts.congestion_control = options.congestion_control;
opts.priority = options.priority;
opts.is_express = options.is_express;
;
#if defined(ZENOHCXX_ZENOHC) && defined(Z_FEATURE_UNSTABLE_API)
opts.accept_replies = options.accept_replies;
opts.allowed_destination = options.allowed_destination;
#endif
opts.timeout_ms = options.timeout_ms;
Querier q = interop::detail::null<Querier>();
ZResult res = ::z_declare_querier(interop::as_loaned_c_ptr(*this), interop::as_owned_c_ptr(q),
interop::as_loaned_c_ptr(key_expr), &opts);
__ZENOH_RESULT_CHECK(res, err, "Failed to declare Querier");
return q;
}
#endif
/// @brief Fetches the Zenoh IDs of all connected routers.
/// @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 a vector of all connected router Id.
std::vector<Id> get_routers_z_id(ZResult* err = nullptr) const {
std::vector<Id> out;
auto f = [&out](const Id& z_id) { out.push_back(z_id); };
typedef decltype(f) F;
::z_owned_closure_zid_t c_closure;
using ClosureType = typename detail::closures::Closure<F, closures::None, void, const Id&>;
auto closure = ClosureType::into_context(std::forward<F>(f), closures::none);
::z_closure(&c_closure, detail::closures::_zenoh_on_id_call, detail::closures::_zenoh_on_drop, closure);
__ZENOH_RESULT_CHECK(::z_info_routers_zid(interop::as_loaned_c_ptr(*this), ::z_move(c_closure)), err,
"Failed to fetch router Ids");
return out;
}
/// @brief Fetches the Zenoh IDs of all connected peers.
/// @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 a vector of all connected peer Id.
std::vector<Id> get_peers_z_id(ZResult* err = nullptr) const {
std::vector<Id> out;
auto f = [&out](const Id& z_id) { out.push_back(z_id); };
typedef decltype(f) F;
::z_owned_closure_zid_t c_closure;
auto closure = detail::closures::Closure<F, closures::None, void, const Id&>::into_context(std::forward<F>(f),
closures::none);
::z_closure(&c_closure, detail::closures::_zenoh_on_id_call, detail::closures::_zenoh_on_drop, closure);
__ZENOH_RESULT_CHECK(::z_info_peers_zid(interop::as_loaned_c_ptr(*this), ::z_move(c_closure)), err,
"Failed to fetch peer Ids");
return out;
}
#ifdef ZENOHCXX_ZENOHPICO
/// @brief Start a separate task to read from the network and process the messages as soon as they are received.
/// @param err if not null, the result code will be written to this location, otherwise ZException exception will be
/// thrown in case of error.
/// @note Zenoh-pico only.
void start_read_task(ZResult* err = nullptr) {
__ZENOH_RESULT_CHECK(zp_start_read_task(interop::as_loaned_c_ptr(*this), nullptr), err,
"Failed to start read task");
}
/// @brief Stop the read task.
/// @param err if not null, the result code will be written to this location, otherwise ZException exception will be
/// thrown in case of error.
/// @note Zenoh-pico only.
void stop_read_task(ZResult* err = nullptr) {
__ZENOH_RESULT_CHECK(zp_stop_read_task(interop::as_loaned_c_ptr(*this)), err, "Failed to stop read task");
}
/// @brief Start a separate task to handle the session lease. This task will send KeepAlive messages when needed
/// and will close the session when the lease is expired. When operating over a multicast transport, it also
/// periodically sends the Join messages.
/// @param err if not null, the result code will be written to this location, otherwise ZException exception will be
/// thrown in case of error.
/// @note Zenoh-pico only.
void start_lease_task(ZResult* err = nullptr) {
__ZENOH_RESULT_CHECK(zp_start_lease_task(interop::as_loaned_c_ptr(*this), NULL), err,
"Failed to start lease task");
}
/// @brief Stop the lease task.
/// @param err if not null, the result code will be written to this location, otherwise ZException exception will be
/// thrown in case of error.
/// @note Zenoh-pico only.
void stop_lease_task(ZResult* err = nullptr) {
__ZENOH_RESULT_CHECK(zp_stop_lease_task(interop::as_loaned_c_ptr(*this)), err, "Failed to stop lease task");
}
/// @brief Triggers a single execution of reading procedure from the network and processes of any received the
/// message.
/// @param err if not null, the result code will be written to this location, otherwise ZException exception will be
/// thrown in case of error.
/// @note Zenoh-pico only.
void read(ZResult* err = nullptr) {
__ZENOH_RESULT_CHECK(zp_read(interop::as_loaned_c_ptr(*this), nullptr), err, "Failed to perform read");
}
/// @brief Triggers a single execution of keep alive procedure. It will send KeepAlive messages when needed and
/// will close the session when the lease is expired.
/// @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 send_keep_alive(ZResult* err = nullptr) {
__ZENOH_RESULT_CHECK(zp_send_keep_alive(interop::as_loaned_c_ptr(*this), nullptr), err,
"Failed to perform send_keep_alive");
}
/// @brief Triggers a single execution of join procedure: send the Join message.
/// @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 send_join(ZResult* err = nullptr) {
__ZENOH_RESULT_CHECK(zp_send_join(interop::as_loaned_c_ptr(*this), nullptr), err,
"Failed to perform send_join");
}
#endif
#if defined(ZENOHCXX_ZENOHC) || Z_FEATURE_LIVELINESS == 1
/// @brief Options to pass to ``Session::liveliness_declare_token``.
struct LivelinessDeclarationOptions {
protected:
uint8_t _dummy = 0;
public:
/// @name Methods
/// @brief Create default option settings.
static LivelinessDeclarationOptions create_default() { return {}; }
};
/// @brief Declares a liveliness token on the network.
///
/// Liveliness token subscribers on an intersecting key expression will receive a PUT sample when connectivity
/// is achieved, and a DELETE sample if it's lost.
///
/// @param key_expr: a keyexpr to declare a liveliess token for.
/// @param options: liveliness token declaration properties.
/// @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 a ``LivelinessToken``.
LivelinessToken liveliness_declare_token(
const KeyExpr& key_expr,
LivelinessDeclarationOptions&& options = LivelinessDeclarationOptions::create_default(),
ZResult* err = nullptr) {
LivelinessToken t = interop::detail::null<LivelinessToken>();
::z_liveliness_token_options_t opts;
z_liveliness_token_options_default(&opts);
(void)options;
__ZENOH_RESULT_CHECK(::z_liveliness_declare_token(interop::as_loaned_c_ptr(*this), interop::as_owned_c_ptr(t),
interop::as_loaned_c_ptr(key_expr), &opts),
err, "Failed to perform liveliness_declare_token operation");
return t;
}
/// @brief Options to pass to ``Session::liveliness_declare_subscriber``.
struct LivelinessSubscriberOptions {
public:
/// @name Fields
/// If true, subscriber will receive the state change notifications for liveliness tokens that were declared
/// before its declaration.
bool history = false;
/// @name Methods
/// @brief Create default option settings.
static LivelinessSubscriberOptions create_default() { return {}; }
private:
friend struct interop::detail::Converter;
::z_liveliness_subscriber_options_t to_c_opts() {
::z_liveliness_subscriber_options_t opts;
::z_liveliness_subscriber_options_default(&opts);
opts.history = this->history;
return opts;
}
};
/// @brief Declares a subscriber on liveliness tokens that intersect `key_expr`.
/// @param key_expr the key expression to subscribe to.
/// @param on_sample the callable that will be called each time a liveliness token status is changed.
/// @param on_drop the callable that will be called once subscriber is destroyed or undeclared.
/// @param options options to pass to subscriber declaration.
/// @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 a ``Subscriber`` object.
template <class C, class D>
[[nodiscard]] Subscriber<void> liveliness_declare_subscriber(
const KeyExpr& key_expr, C&& on_sample, D&& on_drop,
LivelinessSubscriberOptions&& options = LivelinessSubscriberOptions::create_default(),
ZResult* err = nullptr) const {
static_assert(
std::is_invocable_r<void, C, Sample&>::value,
"on_sample should be callable with the following signature: void on_sample(zenoh::Sample& sample)");
static_assert(std::is_invocable_r<void, D>::value,
"on_drop should be callable with the following signature: void on_drop()");
::z_owned_closure_sample_t c_closure;
using Cval = std::remove_reference_t<C>;
using Dval = std::remove_reference_t<D>;
using ClosureType = typename detail::closures::Closure<Cval, Dval, void, Sample&>;
auto closure = ClosureType::into_context(std::forward<C>(on_sample), std::forward<D>(on_drop));
::z_closure(&c_closure, detail::closures::_zenoh_on_sample_call, detail::closures::_zenoh_on_drop, closure);
::z_liveliness_subscriber_options_t opts = interop::detail::Converter::to_c_opts(options);
Subscriber<void> s = interop::detail::null<Subscriber<void>>();
ZResult res = ::z_liveliness_declare_subscriber(interop::as_loaned_c_ptr(*this), interop::as_owned_c_ptr(s),
interop::as_loaned_c_ptr(key_expr), ::z_move(c_closure), &opts);
__ZENOH_RESULT_CHECK(res, err, "Failed to declare Liveliness Token Subscriber");
return s;
}
/// @brief Declares a background subscriber on liveliness tokens that intersect `key_expr`. The subscriber callback
/// will be run in the background until the corresponding session is closed or destroyed.