Skip to content

Commit

Permalink
Methods reordering / modular build
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Nov 5, 2024
1 parent b648506 commit 009fb00
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 146 deletions.
111 changes: 69 additions & 42 deletions include/zenoh-pico/api/liveliness.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "zenoh-pico/api/types.h"
#include "zenoh-pico/protocol/core.h"

#if Z_FEATURE_LIVELINESS == 1

typedef struct {
uint32_t _id;
_z_keyexpr_t _key;
Expand All @@ -30,13 +32,53 @@ typedef struct {
_Z_OWNED_TYPE_VALUE(_z_liveliness_token_t, liveliness_token)
_Z_OWNED_FUNCTIONS_DEF(liveliness_token)

/**************** Liveliness Token ****************/

/**
* The options for `z_liveliness_declare_token()`.
*/
typedef struct z_liveliness_declaration_options_t {
uint8_t __dummy;
} z_liveliness_declaration_options_t;

/**
* Constructs default value for `z_liveliness_declaration_options_t`.
*/
z_result_t z_liveliness_declaration_options_default(z_liveliness_declaration_options_t *options);

/**
* Constructs and 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.
*
* Parameters:
* token: An uninitialized memory location where liveliness token will be constructed.
* zs: A Zenos session to declare the liveliness token.
* keyexpr: A keyexpr to declare a liveliess token for.
* options: Liveliness token declaration properties.
*
* Return:
* ``0`` if put operation is successful, ``negative value`` otherwise.
*/
z_result_t z_liveliness_declare_token(const z_loaned_session_t *zs, z_owned_liveliness_token_t *token,
const z_loaned_keyexpr_t *keyexpr,
const z_liveliness_declaration_options_t *options);

/**
* Undeclare a liveliness token, notifying subscribers of its destruction.
*
* Parameters:
* token: Moved :c:type:`z_owned_liveliness_token_t` to undeclare.
*
* Return:
* ``0`` if put operation is successful, ``negative value`` otherwise.
*/
z_result_t z_liveliness_undeclare_token(z_moved_liveliness_token_t *token);

/**************** Liveliness Subscriber ****************/

#if Z_FEATURE_SUBSCRIPTION == 1
/**
* The options for `z_liveliness_declare_subscriber()`
*/
Expand All @@ -45,50 +87,37 @@ typedef struct z_liveliness_subscriber_options_t {
} z_liveliness_subscriber_options_t;

/**
* The options for `z_liveliness_get()`
*/
typedef struct z_liveliness_get_options_t {
uint32_t timeout_ms;
} z_liveliness_get_options_t;

/**
* Constucts default value for `z_liveliness_declare_subscriber_options_t`.
* Constucts default value for `z_liveliness_subscriber_options_t`.
*/
z_result_t z_liveliness_subscriber_options_default(z_liveliness_subscriber_options_t *options);

/**
* Declares a subscriber on liveliness tokens that intersect `keyexpr`.
*
* @param token: An uninitialized memory location where subscriber will be constructed.
* @param zs: The Zenoh session.
* @param keyexpr: The key expression to subscribe to.
* @param callback: The callback function that will be called each time a liveliness token status is changed.
* @param _options: The options to be passed to the liveliness subscriber declaration.
* Parameters:
* token: An uninitialized memory location where subscriber will be constructed.
* zs: The Zenoh session.
* keyexpr: The key expression to subscribe to.
* callback: The callback function that will be called each time a liveliness token status is changed.
* options: The options to be passed to the liveliness subscriber declaration.
*
* @return 0 in case of success, negative error values otherwise.
* Return:
* ``0`` if put operation is successful, ``negative value`` otherwise.
*/
z_result_t z_liveliness_declare_subscriber(const z_loaned_session_t *zs, z_owned_subscriber_t *sub,
const z_loaned_keyexpr_t *keyexpr, z_moved_closure_sample_t *callback,
z_liveliness_subscriber_options_t *options);
/**
* Constructs default value for `z_liveliness_declaration_options_t`.
*/
z_result_t z_liveliness_declaration_options_default(z_liveliness_declaration_options_t *options);
#endif // Z_FEATURE_SUBSCRIPTION == 1

/**************** Liveliness Query ****************/

#if Z_FEATURE_QUERY == 1
/**
* Constructs and 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 token: An uninitialized memory location where liveliness token will be constructed.
* @param zs: A Zenos session to declare the liveliness token.
* @param keyexpr: A keyexpr to declare a liveliess token for.
* @param _options: Liveliness token declaration properties.
* The options for `z_liveliness_get()`
*/
z_result_t z_liveliness_declare_token(const z_loaned_session_t *zs, z_owned_liveliness_token_t *token,
const z_loaned_keyexpr_t *keyexpr,
const z_liveliness_declaration_options_t *options);
typedef struct z_liveliness_get_options_t {
uint32_t timeout_ms;
} z_liveliness_get_options_t;

/**
* Constructs default value `z_liveliness_get_options_t`.
Expand All @@ -98,22 +127,20 @@ z_result_t z_liveliness_get_options_default(z_liveliness_get_options_t *options)
/**
* Queries liveliness tokens currently on the network with a key expression intersecting with `keyexpr`.
*
* @param zs: The Zenoh session.
* @param keyexpr: The key expression to query liveliness tokens for.
* @param callback: The callback function that will be called for each received reply.
* @param options: Additional options for the liveliness get operation.
* Parameters:
* zs: The Zenoh session.
* keyexpr: The key expression to query liveliness tokens for.
* callback: The callback function that will be called for each received reply.
* options: Additional options for the liveliness get operation.
*
* Return:
* ``0`` if put operation is successful, ``negative value`` otherwise.
*/
z_result_t z_liveliness_get(const z_loaned_session_t *zs, const z_loaned_keyexpr_t *keyexpr,
z_moved_closure_reply_t *callback, z_liveliness_get_options_t *options);

/**
* Destroys a liveliness token, notifying subscribers of its destruction.
*/
#endif // Z_FEATURE_QUERY == 1

z_result_t z_liveliness_undeclare_token(z_moved_liveliness_token_t *token);
/**
* Borrows token.
*/
const z_loaned_liveliness_token_t *z_liveliness_token_loan(const z_owned_liveliness_token_t *token);
#endif // Z_FEATURE_LIVELINESS == 1

#endif // INCLUDE_ZENOH_PICO_API_LIVELINESS_H
16 changes: 12 additions & 4 deletions include/zenoh-pico/session/liveliness.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,27 @@
#ifndef ZENOH_PICO_SESSION_LIVELINESS_H
#define ZENOH_PICO_SESSION_LIVELINESS_H

#if Z_FEATURE_QUERYABLE == 1
#if Z_FEATURE_LIVELINESS == 1
uint32_t _z_liveliness_get_query_id(_z_session_t *zn);

z_result_t _z_liveliness_register_token(_z_session_t *zn, uint32_t id, const _z_keyexpr_t keyexpr);
void _z_liveliness_unregister_token(_z_session_t *zn, uint32_t id);

#if Z_FEATURE_SUBSCRIPTION == 1
z_result_t _z_liveliness_subscription_declare(_z_session_t *zn, uint32_t id, const _z_keyexpr_t keyexpr,
const _z_timestamp_t *timestamp);
z_result_t _z_liveliness_subscription_undeclare(_z_session_t *zn, uint32_t id, const _z_timestamp_t *timestamp);
#endif

#if Z_FEATURE_QUERY == 1
z_result_t _z_liveliness_register_pending_query(_z_session_t *zn, uint32_t id, _z_liveliness_pending_query_t *pen_qry);
void _z_liveliness_unregister_pending_query(_z_session_t *zn, uint32_t id);
#endif

z_result_t _z_liveliness_process_token_declare(_z_session_t *zn, const _z_n_msg_declare_t *decl);
z_result_t _z_liveliness_process_token_undeclare(_z_session_t *zn, const _z_n_msg_declare_t *decl);
z_result_t _z_liveliness_process_declare_final(_z_session_t *zn, const _z_n_msg_declare_t *decl);

z_result_t _z_liveliness_register_token(_z_session_t *zn, uint32_t id, const _z_keyexpr_t keyexpr);
void _z_liveliness_unregister_token(_z_session_t *zn, uint32_t id);

void _z_liveliness_init(_z_session_t *zn);
void _z_liveliness_clear(_z_session_t *zn);
#endif
Expand Down
74 changes: 44 additions & 30 deletions src/api/liveliness.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#include "zenoh-pico/session/resource.h"
#include "zenoh-pico/utils/result.h"

#if Z_FEATURE_LIVELINESS == 1

/**************** Liveliness Token ****************/

_Bool _z_liveliness_token_check(const _z_liveliness_token_t *token) {
_z_keyexpr_check(&token->_key);
return true;
Expand All @@ -44,6 +48,40 @@ void _z_liveliness_token_clear(_z_liveliness_token_t *token) {
_Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL(_z_liveliness_token_t, liveliness_token, _z_liveliness_token_check,
_z_liveliness_token_null, _z_liveliness_token_clear)

z_result_t z_liveliness_declaration_options_default(z_liveliness_declaration_options_t *options) {
options->__dummy = 0;
return _Z_RES_OK;
}

z_result_t z_liveliness_declare_token(const z_loaned_session_t *zs, z_owned_liveliness_token_t *token,
const z_loaned_keyexpr_t *keyexpr,
const z_liveliness_declaration_options_t *options) {
(void)options;

_z_keyexpr_t keyexpr_aliased = _z_keyexpr_alias_from_user_defined(*keyexpr, true);
_z_keyexpr_t key = keyexpr_aliased;

// TODO: Currently, if resource declarations are done over multicast transports, the current protocol definition
// lacks a way to convey them to later-joining nodes. Thus, in the current version automatic
// resource declarations are only performed on unicast transports.
if (_Z_RC_IN_VAL(zs)->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) {
_z_resource_t *r = _z_get_resource_by_key(_Z_RC_IN_VAL(zs), &keyexpr_aliased);
if (r == NULL) {
uint16_t id = _z_declare_resource(_Z_RC_IN_VAL(zs), keyexpr_aliased);
key = _z_rid_with_suffix(id, NULL);
}
}

return _z_declare_liveliness_token(zs, &token->_val, key);
}

z_result_t z_liveliness_undeclare_token(z_moved_liveliness_token_t *token) {
return _z_undeclare_liveliness_token(&token->_this._val);
}

/**************** Liveliness Subscriber ****************/

#if Z_FEATURE_SUBSCRIPTION == 1
z_result_t z_liveliness_subscriber_options_default(z_liveliness_subscriber_options_t *options) {
options->__dummy = 0;
return _Z_RES_OK;
Expand Down Expand Up @@ -71,7 +109,11 @@ z_result_t z_liveliness_declare_subscriber(const z_loaned_session_t *zs, z_owned
return _Z_RES_OK;
}
}
#endif // Z_FEATURE_SUBSCRIPTION == 1

/**************** Liveliness Query ****************/

#if Z_FEATURE_QUERY == 1
z_result_t z_liveliness_get_options_default(z_liveliness_get_options_t *options) {
options->timeout_ms = Z_GET_TIMEOUT_DEFAULT;
return _Z_RES_OK;
Expand Down Expand Up @@ -113,34 +155,6 @@ z_result_t z_liveliness_get(const z_loaned_session_t *zs, const z_loaned_keyexpr
&callback->_this); // call and drop passed to _z_liveliness_query, so we nullify the closure here
return ret;
}
#endif // Z_FEATURE_QUERY == 1

z_result_t z_liveliness_declaration_options_default(z_liveliness_declaration_options_t *options) {
options->__dummy = 0;
return _Z_RES_OK;
}

z_result_t z_liveliness_declare_token(const z_loaned_session_t *zs, z_owned_liveliness_token_t *token,
const z_loaned_keyexpr_t *keyexpr,
const z_liveliness_declaration_options_t *options) {
(void)options;

_z_keyexpr_t keyexpr_aliased = _z_keyexpr_alias_from_user_defined(*keyexpr, true);
_z_keyexpr_t key = keyexpr_aliased;

// TODO: Currently, if resource declarations are done over multicast transports, the current protocol definition
// lacks a way to convey them to later-joining nodes. Thus, in the current version automatic
// resource declarations are only performed on unicast transports.
if (_Z_RC_IN_VAL(zs)->_tp._type == _Z_TRANSPORT_UNICAST_TYPE) {
_z_resource_t *r = _z_get_resource_by_key(_Z_RC_IN_VAL(zs), &keyexpr_aliased);
if (r == NULL) {
uint16_t id = _z_declare_resource(_Z_RC_IN_VAL(zs), keyexpr_aliased);
key = _z_rid_with_suffix(id, NULL);
}
}

return _z_declare_liveliness_token(zs, &token->_val, key);
}

z_result_t z_liveliness_undeclare_token(z_moved_liveliness_token_t *token) {
return _z_undeclare_liveliness_token(&token->_this._val);
}
#endif // Z_FEATURE_LIVELINESS == 1
Loading

0 comments on commit 009fb00

Please sign in to comment.