Skip to content

Commit

Permalink
API rework
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed May 15, 2024
1 parent 382e3ab commit f4d4438
Show file tree
Hide file tree
Showing 112 changed files with 2,208 additions and 1,925 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ jobs:
run: |
sudo apt install -y ninja-build
CMAKE_GENERATOR=Ninja make
python3 ./build/tests/modularity.py --pub $Z_FEATURE_PUBLICATION --sub $Z_FEATURE_SUBSCRIPTION --queryable $Z_FEATURE_QUERYABLE --query $Z_FEATURE_QUERY --attachment 1
python3 ./build/tests/modularity.py --pub $Z_FEATURE_PUBLICATION --sub $Z_FEATURE_SUBSCRIPTION --queryable $Z_FEATURE_QUERYABLE --query $Z_FEATURE_QUERY --attachment 0
timeout-minutes: 5
env:
Z_FEATURE_PUBLICATION: ${{ matrix.feature_publication }}
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ set(Z_FEATURE_SUBSCRIPTION 1 CACHE STRING "Toggle subscription feature")
set(Z_FEATURE_QUERY 1 CACHE STRING "Toggle query feature")
set(Z_FEATURE_QUERYABLE 1 CACHE STRING "Toggle queryable feature")
set(Z_FEATURE_RAWETH_TRANSPORT 0 CACHE STRING "Toggle raw ethernet transport feature")
set(Z_FEATURE_ATTACHMENT 1 CACHE STRING "Toggle attachment feature")
set(Z_FEATURE_INTEREST 1 CACHE STRING "Toggle interest feature")
set(Z_FEATURE_ATTACHMENT 0 CACHE STRING "Toggle attachment feature")
set(Z_FEATURE_INTEREST 0 CACHE STRING "Toggle interest feature") # Toggle to 1 when protocol changes are merged
add_definition(Z_FEATURE_MULTI_THREAD=${Z_FEATURE_MULTI_THREAD})
add_definition(Z_FEATURE_PUBLICATION=${Z_FEATURE_PUBLICATION})
add_definition(Z_FEATURE_SUBSCRIPTION=${Z_FEATURE_SUBSCRIPTION})
Expand Down
27 changes: 15 additions & 12 deletions examples/arduino/z_get.ino
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ void reply_dropper(void *ctx) {
void reply_handler(z_owned_reply_t *oreply, void *ctx) {
(void)(ctx);
if (z_reply_is_ok(oreply)) {
z_loaned_sample_t sample = z_reply_ok(oreply);
z_owned_str_t keystr = z_keyexpr_to_string(sample.keyexpr);
std::string val((const char *)sample.payload.start, sample.payload.len);
const z_loaned_sample_t* sample = z_reply_ok(oreply);
z_owned_str_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
std::string val((const char *)z_sample_payload(sample)->start, z_sample_payload(sample)->len);

Serial.print(" >> [Get listener] Received (");
Serial.print(z_str_loan(&keystr));
Expand Down Expand Up @@ -78,16 +79,16 @@ void setup() {
Serial.println("OK");

// Initialize Zenoh Session and other parameters
z_owned_config_t config = z_config_default();
zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(MODE));
z_owned_config_t config;
z_config_default(&config);
zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, MODE);
if (strcmp(CONNECT, "") != 0) {
zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT));
zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, CONNECT);
}

// Open Zenoh session
Serial.print("Opening Zenoh Session...");
s = z_open(z_config_move(&config));
if (!z_session_check(&s)) {
if (z_open(&s, z_config_move(&config)) < 0) {
Serial.println("Unable to open session!");
while (1) {
;
Expand All @@ -96,8 +97,8 @@ void setup() {
Serial.println("OK");

// Start the receive and the session lease loop for zenoh-pico
zp_start_read_task(z_session_loan(&s), NULL);
zp_start_lease_task(z_session_loan(&s), NULL);
zp_start_read_task(z_session_loan_mut(&s), NULL);
zp_start_lease_task(z_session_loan_mut(&s), NULL);

Serial.println("Zenoh setup finished!");

Expand All @@ -110,11 +111,13 @@ void loop() {
Serial.print("Sending Query ");
Serial.print(KEYEXPR);
Serial.println(" ...");
z_get_options_t opts = z_get_options_default();
z_get_options_t opts;
z_get_options_default(&opts);
if (strcmp(VALUE, "") != 0) {
opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE));
}
z_owned_closure_reply_t callback = z_closure_reply(reply_handler, reply_dropper, NULL);
z_owned_closure_reply_t callback;
z_closure_reply(&callback, reply_handler, reply_dropper, NULL);
if (z_get(z_session_loan(&s), z_keyexpr(KEYEXPR), "", z_closure_reply_move(&callback), &opts) < 0) {
Serial.println("Unable to send query.");
}
Expand Down
18 changes: 9 additions & 9 deletions examples/arduino/z_pub.ino
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,17 @@ void setup() {
Serial.println("OK");

// Initialize Zenoh Session and other parameters
z_owned_config_t config = z_config_default();
zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(MODE));
z_owned_config_t config;
z_config_default(&config);
zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, MODE);
if (strcmp(CONNECT, "") != 0) {
zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT));
zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, CONNECT);
}

// Open Zenoh session
Serial.print("Opening Zenoh Session...");
z_owned_session_t s = z_open(z_config_move(&config));
if (!z_session_check(&s)) {
z_owned_session_t s;
if (z_open(&s, z_config_move(&config)) < 0) {
Serial.println("Unable to open session!");
while (1) {
;
Expand All @@ -73,15 +74,14 @@ void setup() {
Serial.println("OK");

// Start the receive and the session lease loop for zenoh-pico
zp_start_read_task(z_session_loan(&s), NULL);
zp_start_lease_task(z_session_loan(&s), NULL);
zp_start_read_task(z_session_loan_mut(&s), NULL);
zp_start_lease_task(z_session_loan_mut(&s), NULL);

// Declare Zenoh publisher
Serial.print("Declaring publisher for ");
Serial.print(KEYEXPR);
Serial.println("...");
pub = z_declare_publisher(z_session_loan(&s), z_keyexpr(KEYEXPR), NULL);
if (!z_publisher_check(&pub)) {
if (z_declare_publisher(&pub, z_session_loan(&s), z_keyexpr(KEYEXPR), NULL) < 0) {
Serial.println("Unable to declare publisher for key expression!");
while (1) {
;
Expand Down
23 changes: 13 additions & 10 deletions examples/arduino/z_pull.ino
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
// z_owned_pull_subscriber_t sub;

// @TODO
// void data_handler(const z_sample_t *sample, void *arg) {
// z_owned_str_t keystr = z_keyexpr_to_string(sample->keyexpr);
// void data_handler(const z_loaned_sample_t *sample, void *arg) {
// z_owned_str_t keystr;
// z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
// std::string val((const char *)sample->payload.start, sample->payload.len);

// Serial.print(" >> [Subscription listener] Received (");
Expand Down Expand Up @@ -68,16 +69,17 @@ void setup() {
Serial.println("OK");

// Initialize Zenoh Session and other parameters
z_owned_config_t config = z_config_default();
zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(MODE));
z_owned_config_t config;
z_config_default(&config);
zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, MODE);
if (strcmp(CONNECT, "") != 0) {
zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT));
zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, CONNECT);
}

// Open Zenoh session
Serial.print("Opening Zenoh Session...");
z_owned_session_t s = z_open(z_config_move(&config));
if (!z_session_check(&s)) {
z_owned_session_t s;
if (z_open(&s, z_config_move(&config)) < 0) {
Serial.println("Unable to open session!");
while (1) {
;
Expand All @@ -86,15 +88,16 @@ void setup() {
Serial.println("OK");

// Start the receive and the session lease loop for zenoh-pico
zp_start_read_task(z_session_loan(&s), NULL);
zp_start_lease_task(z_session_loan(&s), NULL);
zp_start_read_task(z_session_loan_mut(&s), NULL);
zp_start_lease_task(z_session_loan_mut(&s), NULL);

// Declare Zenoh subscriber
Serial.print("Declaring Subscriber on ");
Serial.print(KEYEXPR);
Serial.println(" ...");
// @TODO
// z_owned_closure_sample_t callback = z_closure_sample(data_handler, NULL, NULL);
// z_owned_closure_sample_t callback;
// z_closure_sample(&callback, data_handler, NULL, NULL);
// @TODO
// sub = z_declare_pull_subscriber(z_session_loan(&s), z_keyexpr(KEYEXPR), z_closure_sample_move(&callback), NULL);
// if (!z_pull_subscriber_check(&sub)) {
Expand Down
29 changes: 16 additions & 13 deletions examples/arduino/z_queryable.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
#define KEYEXPR "demo/example/zenoh-pico-queryable"
#define VALUE "[ARDUINO]{ESP32} Queryable from Zenoh-Pico!"

void query_handler(const z_query_t *query, void *arg) {
z_owned_str_t keystr = z_keyexpr_to_string(z_query_keyexpr(query));
void query_handler(const z_loaned_query_t *query, void *arg) {
z_owned_str_t keystr;
z_keyexpr_to_string(z_query_keyexpr(query), &keystr);

Serial.print(" >> [Queryable handler] Replying Data ('");
Serial.print(z_str_loan(&keystr));
Expand Down Expand Up @@ -66,16 +67,17 @@ void setup() {
Serial.println("OK");

// Initialize Zenoh Session and other parameters
z_owned_config_t config = z_config_default();
zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(MODE));
z_owned_config_t config;
z_config_default(&config);
zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, MODE);
if (strcmp(CONNECT, "") != 0) {
zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT));
zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, CONNECT);
}

// Open Zenoh session
Serial.print("Opening Zenoh Session...");
z_owned_session_t s = z_open(z_config_move(&config));
if (!z_session_check(&s)) {
z_owned_session_t s;
if (z_open(&s, z_config_move(&config)) < 0) {
Serial.println("Unable to open session!");
while (1) {
;
Expand All @@ -84,17 +86,18 @@ void setup() {
Serial.println("OK");

// Start the receive and the session lease loop for zenoh-pico
zp_start_read_task(z_session_loan(&s), NULL);
zp_start_lease_task(z_session_loan(&s), NULL);
zp_start_read_task(z_session_loan_mut(&s), NULL);
zp_start_lease_task(z_session_loan_mut(&s), NULL);

// Declare Zenoh queryable
Serial.print("Declaring Queryable on ");
Serial.print(KEYEXPR);
Serial.println(" ...");
z_owned_closure_query_t callback = z_closure_query(query_handler, NULL, NULL);
z_owned_queryable_t qable =
z_declare_queryable(z_session_loan(&s), z_keyexpr(KEYEXPR), z_closure_query_move(&callback), NULL);
if (!z_queryable_check(&qable)) {
z_owned_closure_query_t callback;
z_closure_query(&callback, query_handler, NULL, NULL);
z_owned_queryable_t qable;
if (z_declare_queryable(&qable, z_session_loan(&s), z_keyexpr(KEYEXPR), z_closure_query_move(&callback), NULL) <
0) {
Serial.println("Unable to declare queryable.");
while (1) {
;
Expand Down
6 changes: 4 additions & 2 deletions examples/arduino/z_scout.ino
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,10 @@ void setup() {
void loop() {
int *context = (int *)malloc(sizeof(int));
*context = 0;
z_owned_scouting_config_t config = z_scouting_config_default();
z_owned_closure_hello_t closure = z_closure_hello(callback, drop, context);
z_owned_scouting_config_t config;
z_scouting_config_default(&config);
z_owned_closure_hello_t closure;
z_closure_hello(&closure, callback, drop, context);
printf("Scouting...\n");
z_scout(z_scouting_config_move(&config), z_closure_hello_move(&closure));
}
33 changes: 17 additions & 16 deletions examples/arduino/z_sub.ino
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@

#define KEYEXPR "demo/example/**"

void data_handler(const z_sample_t *sample, void *arg) {
z_keyexpr_t keyexpr = z_sample_keyexpr(sample);
z_bytes_t payload = z_sample_payload(sample);
z_owned_str_t keystr = z_keyexpr_to_string(keyexpr);
std::string val((const char *)payload.start, payload.len);
void data_handler(const z_loaned_sample_t *sample, void *arg) {
z_owned_str_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
std::string val((const char *)z_sample_payload(sample)->start, z_sample_payload(sample)->len);

Serial.print(" >> [Subscription listener] Received (");
Serial.print(z_str_loan(&keystr));
Expand Down Expand Up @@ -66,16 +65,17 @@ void setup() {
Serial.println("OK");

// Initialize Zenoh Session and other parameters
z_owned_config_t config = z_config_default();
zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(MODE));
z_owned_config_t config;
z_config_default(&config);
zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_MODE_KEY, MODE);
if (strcmp(CONNECT, "") != 0) {
zp_config_insert(z_config_loan(&config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT));
zp_config_insert(z_config_loan_mut(&config), Z_CONFIG_CONNECT_KEY, CONNECT);
}

// Open Zenoh session
Serial.print("Opening Zenoh Session...");
z_owned_session_t s = z_open(z_config_move(&config));
if (!z_session_check(&s)) {
z_owned_session_t s;
if (z_open(&s, z_config_move(&config)) < 0) {
Serial.println("Unable to open session!");
while (1) {
;
Expand All @@ -84,17 +84,18 @@ void setup() {
Serial.println("OK");

// Start the receive and the session lease loop for zenoh-pico
zp_start_read_task(z_session_loan(&s), NULL);
zp_start_lease_task(z_session_loan(&s), NULL);
zp_start_read_task(z_session_loan_mut(&s), NULL);
zp_start_lease_task(z_session_loan_mut(&s), NULL);

// Declare Zenoh subscriber
Serial.print("Declaring Subscriber on ");
Serial.print(KEYEXPR);
Serial.println(" ...");
z_owned_closure_sample_t callback = z_closure_sample(data_handler, NULL, NULL);
z_owned_subscriber_t sub =
z_declare_subscriber(z_session_loan(&s), z_keyexpr(KEYEXPR), z_closure_sample_move(&callback), NULL);
if (!z_subscriber_check(&sub)) {
z_owned_closure_sample_t callback;
z_closure_sample(&callback, data_handler, NULL, NULL);
z_owned_subscriber_t sub;
if (z_declare_subscriber(&sub, z_session_loan(&s), z_keyexpr(KEYEXPR), z_closure_sample_move(&callback), NULL) <
0) {
Serial.println("Unable to declare subscriber.");
while (1) {
;
Expand Down
34 changes: 20 additions & 14 deletions examples/espidf/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ void reply_dropper(void *ctx) { printf(" >> Received query final notification\n"

void reply_handler(z_owned_reply_t *oreply, void *ctx) {
if (z_reply_is_ok(oreply)) {
z_loaned_sample_t sample = z_reply_ok(oreply);
z_owned_str_t keystr = z_keyexpr_to_string(sample.keyexpr);
printf(" >> Received ('%s': '%.*s')\n", z_loan(keystr), (int)sample.payload.len, sample.payload.start);
const z_loaned_sample_t *sample = z_reply_ok(oreply);
z_owned_str_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);

printf(" >> Received ('%s': '%.*s')\n", z_str_data(z_loan(keystr)), (int)z_sample_payload(sample)->len,
z_sample_payload(sample)->start);
z_drop(z_move(keystr));
} else {
printf(" >> Received an error\n");
Expand All @@ -132,33 +135,36 @@ void app_main() {
printf("OK!\n");

// Initialize Zenoh Session and other parameters
z_owned_config_t config = z_config_default();
zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(MODE));
z_owned_config_t config;
z_config_default(&config);
zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, MODE);
if (strcmp(CONNECT, "") != 0) {
zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT));
zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, CONNECT);
}

// Open Zenoh session
printf("Opening Zenoh Session...");
z_owned_session_t s = z_open(z_move(config));
if (!z_check(s)) {
z_owned_session_t s;
if (z_open(&s, z_move(config)) < 0) {
printf("Unable to open session!\n");
exit(-1);
}
printf("OK\n");

// Start the receive and the session lease loop for zenoh-pico
zp_start_read_task(z_loan(s), NULL);
zp_start_lease_task(z_loan(s), NULL);
zp_start_read_task(z_loan_mut(s), NULL);
zp_start_lease_task(z_loan_mut(s), NULL);

while (1) {
sleep(5);
printf("Sending Query '%s'...\n", KEYEXPR);
z_get_options_t opts = z_get_options_default();
z_get_options_t opts;
z_get_options_default(&opts);
if (strcmp(VALUE, "") != 0) {
opts.value.payload = _z_bytes_wrap((const uint8_t *)VALUE, strlen(VALUE));
}
z_owned_closure_reply_t callback = z_closure(reply_handler, reply_dropper);
z_owned_closure_reply_t callback;
z_closure(&callback, reply_handler, reply_dropper);
if (z_get(z_loan(s), z_keyexpr(KEYEXPR), "", z_move(callback), &opts) < 0) {
printf("Unable to send query.\n");
exit(-1);
Expand All @@ -167,8 +173,8 @@ void app_main() {

printf("Closing Zenoh Session...");
// Stop the receive and the session lease loop for zenoh-pico
zp_stop_read_task(z_loan(s));
zp_stop_lease_task(z_loan(s));
zp_stop_read_task(z_loan_mut(s));
zp_stop_lease_task(z_loan_mut(s));

z_close(z_move(s));
printf("OK!\n");
Expand Down
Loading

0 comments on commit f4d4438

Please sign in to comment.