Skip to content

Commit

Permalink
Fix history option processing and add it to the example
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Nov 7, 2024
1 parent 7ca92f5 commit 88b9800
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
12 changes: 10 additions & 2 deletions examples/unix/c11/z_sub_liveliness.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ int main(int argc, char **argv) {
const char *mode = "client";
char *clocator = NULL;
char *llocator = NULL;
bool history = false;

int opt;
while ((opt = getopt(argc, argv, "k:e:m:l:n:")) != -1) {
while ((opt = getopt(argc, argv, "k:e:m:l:n:h")) != -1) {
switch (opt) {
case 'k':
keyexpr = optarg;
Expand All @@ -58,6 +59,9 @@ int main(int argc, char **argv) {
case 'l':
llocator = optarg;
break;
case 'h':
history = true;
break;
case '?':
if (optopt == 'k' || optopt == 'e' || optopt == 'm' || optopt == 'l') {
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
Expand Down Expand Up @@ -99,9 +103,13 @@ int main(int argc, char **argv) {
z_closure(&callback, data_handler, NULL, NULL);
z_owned_subscriber_t sub;

z_liveliness_subscriber_options_t sub_opt;
z_liveliness_subscriber_options_default(&sub_opt);
sub_opt.history = history;

z_view_keyexpr_t ke;
z_view_keyexpr_from_str(&ke, keyexpr);
if (z_liveliness_declare_subscriber(z_loan(s), &sub, z_loan(ke), z_move(callback), NULL) < 0) {
if (z_liveliness_declare_subscriber(z_loan(s), &sub, z_loan(ke), z_move(callback), &sub_opt) < 0) {
printf("Unable to declare liveliness subscriber.\n");
exit(-1);
}
Expand Down
3 changes: 2 additions & 1 deletion include/zenoh-pico/net/liveliness.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ z_result_t _z_undeclare_liveliness_token(_z_liveliness_token_t *token);
* zn: The zenoh-net session. The caller keeps its ownership.
* keyexpr: The resource key to subscribe. The callee gets the ownership of any allocated value.
* callback: The callback function that will be called each time a matching liveliness token changed.
* history: Enable current interest to return history tokens.
* arg: A pointer that will be passed to the **callback** on each call.
*
* Returns:
* The created :c:type:`_z_subscriber_t` (in null state if the declaration failed).
*/
_z_subscriber_t _z_declare_liveliness_subscriber(const _z_session_rc_t *zn, _z_keyexpr_t keyexpr,
_z_closure_sample_callback_t callback, _z_drop_handler_t dropper,
void *arg);
bool history, void *arg);

/**
* Undeclare a liveliness :c:type:`_z_subscriber_t`.
Expand Down
4 changes: 2 additions & 2 deletions src/api/liveliness.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ z_result_t z_liveliness_declare_subscriber(const z_loaned_session_t *zs, z_owned

_z_keyexpr_t key = _z_update_keyexpr_to_declared(_Z_RC_IN_VAL(zs), *keyexpr);

_z_subscriber_t int_sub =
_z_declare_liveliness_subscriber(zs, key, callback->_this._val.call, callback->_this._val.drop, ctx);
_z_subscriber_t int_sub = _z_declare_liveliness_subscriber(zs, key, callback->_this._val.call,
callback->_this._val.drop, opt.history, ctx);

z_internal_closure_sample_null(&callback->_this);
sub->_val = int_sub;
Expand Down
9 changes: 4 additions & 5 deletions src/net/liveliness.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ z_result_t _z_undeclare_liveliness_token(_z_liveliness_token_t *token) {
#if Z_FEATURE_SUBSCRIPTION == 1
_z_subscriber_t _z_declare_liveliness_subscriber(const _z_session_rc_t *zn, _z_keyexpr_t keyexpr,
_z_closure_sample_callback_t callback, _z_drop_handler_t dropper,
void *arg) {
bool history, void *arg) {
_z_subscription_t s;
s._id = _z_get_entity_id(_Z_RC_IN_VAL(zn));
s._key_id = keyexpr._id;
Expand All @@ -84,10 +84,9 @@ _z_subscriber_t _z_declare_liveliness_subscriber(const _z_session_rc_t *zn, _z_k
return ret;
}
// Build the declare message to send on the wire
_z_interest_t interest =
_z_make_interest(&keyexpr, s._id,
_Z_INTEREST_FLAG_KEYEXPRS | _Z_INTEREST_FLAG_TOKENS | _Z_INTEREST_FLAG_RESTRICTED |
_Z_INTEREST_FLAG_CURRENT | _Z_INTEREST_FLAG_FUTURE);
int mode = history ? (_Z_INTEREST_FLAG_CURRENT | _Z_INTEREST_FLAG_FUTURE) : _Z_INTEREST_FLAG_FUTURE;
_z_interest_t interest = _z_make_interest(
&keyexpr, s._id, _Z_INTEREST_FLAG_KEYEXPRS | _Z_INTEREST_FLAG_TOKENS | _Z_INTEREST_FLAG_RESTRICTED | mode);

_z_network_message_t n_msg = _z_n_msg_make_interest(interest);
if (_z_send_n_msg(_Z_RC_IN_VAL(zn), &n_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) {
Expand Down

0 comments on commit 88b9800

Please sign in to comment.