-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove pull subscriber. Todo: add ring handler.
- Loading branch information
Showing
16 changed files
with
239 additions
and
562 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,73 +11,85 @@ | |
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
#include <ctype.h> | ||
#include <stddef.h> | ||
#include <stdio.h> | ||
#include "zenoh.h" | ||
|
||
const char *kind_to_str(z_sample_kind_t kind); | ||
|
||
void data_handler(const z_sample_t *sample, void *arg) { | ||
z_owned_str_t keystr = z_keyexpr_to_string(sample->keyexpr); | ||
printf(">> [Subscriber] Received %s ('%s': '%.*s')\n", kind_to_str(sample->kind), z_loan(keystr), | ||
(int)sample->payload.len, sample->payload.start); | ||
z_drop(z_move(keystr)); | ||
} | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <zenoh.h> | ||
|
||
int main(int argc, char **argv) { | ||
char *expr = "demo/example/**"; | ||
if (argc > 1) { | ||
expr = argv[1]; | ||
const char *keyexpr = "demo/example/**"; | ||
char *locator = NULL; | ||
size_t interval = 5000; | ||
size_t size = 3; | ||
|
||
int opt; | ||
while ((opt = getopt(argc, argv, "k:e:i:s:")) != -1) { | ||
switch (opt) { | ||
case 'k': | ||
keyexpr = optarg; | ||
break; | ||
case 'e': | ||
locator = optarg; | ||
break; | ||
case 'i': | ||
interval = (size_t)atoi(optarg); | ||
break; | ||
case 's': | ||
size = (size_t)atoi(optarg); | ||
break; | ||
case '?': | ||
if (optopt == 'k' || optopt == 'e' || optopt == 'i' || optopt == 's') { | ||
fprintf(stderr, "Option -%c requires an argument.\n", optopt); | ||
} else { | ||
fprintf(stderr, "Unknown option `-%c'.\n", optopt); | ||
} | ||
return 1; | ||
default: | ||
return -1; | ||
} | ||
} | ||
|
||
z_owned_config_t config = z_config_default(); | ||
if (argc > 2) { | ||
if (zc_config_insert_json(z_loan(config), Z_CONFIG_LISTEN_KEY, argv[2]) < 0) { | ||
printf( | ||
"Couldn't insert value `%s` in configuration at `%s`. This is likely because `%s` expects a " | ||
"JSON-serialized list of strings\n", | ||
argv[2], Z_CONFIG_LISTEN_KEY, Z_CONFIG_LISTEN_KEY); | ||
exit(-1); | ||
} | ||
if (locator != NULL) { | ||
zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); | ||
} | ||
|
||
printf("Opening session...\n"); | ||
z_owned_session_t s = z_open(z_move(config)); | ||
if (!z_check(s)) { | ||
printf("Unable to open session!\n"); | ||
exit(-1); | ||
return -1; | ||
} | ||
|
||
z_owned_closure_sample_t callback = z_closure(data_handler); | ||
printf("Declaring Subscriber on '%s'...\n", expr); | ||
z_owned_pull_subscriber_t sub = z_declare_pull_subscriber(z_loan(s), z_keyexpr(expr), z_move(callback), NULL); | ||
if (!z_check(sub)) { | ||
printf("Unable to declare subscriber.\n"); | ||
exit(-1); | ||
} | ||
printf("Pull functionality not implemented!\n"); | ||
// @TODO: implement z_owned_sample_channel_t and z_sample_channel_ring_new | ||
// printf("Declaring Subscriber on '%s'...\n", keyexpr); | ||
// z_owned_sample_channel_t channel = z_sample_channel_ring_new(size); | ||
// z_owned_subscriber_t sub = z_declare_subscriber(z_loan(s), z_keyexpr(keyexpr), z_move(channel.send), NULL); | ||
// if (!z_check(sub)) { | ||
// printf("Unable to declare subscriber.\n"); | ||
// return -1; | ||
// } | ||
|
||
printf("Press <enter> to pull data...\n"); | ||
char c = 0; | ||
while (c != 'q') { | ||
c = getchar(); | ||
if (c == -1) { | ||
z_sleep_s(1); | ||
} else { | ||
z_subscriber_pull(z_loan(sub)); | ||
} | ||
} | ||
// printf("Pulling data every %zu ms... Ring size: %zd\n", interval, size); | ||
// z_owned_sample_t sample = z_sample_null(); | ||
// while (true) { | ||
// for (z_call(channel.recv, &sample); z_check(sample); z_call(channel.recv, &sample)) { | ||
// z_owned_str_t keystr = z_keyexpr_to_string(z_loan(sample.keyexpr)); | ||
// printf(">> [Subscriber] Pulled ('%s': '%.*s')\n", z_loan(keystr), (int)sample.payload.len, | ||
// sample.payload.start); | ||
// z_drop(z_move(keystr)); | ||
// z_drop(z_move(sample)); | ||
// } | ||
// printf(">> [Subscriber] Nothing to pull... sleep for %zu ms\n", interval); | ||
// zp_sleep_ms(interval); | ||
// } | ||
|
||
// z_undeclare_subscriber(z_move(sub)); | ||
|
||
z_undeclare_pull_subscriber(z_move(sub)); | ||
z_close(z_move(s)); | ||
|
||
return 0; | ||
} | ||
|
||
const char *kind_to_str(z_sample_kind_t kind) { | ||
switch (kind) { | ||
case Z_SAMPLE_KIND_PUT: | ||
return "PUT"; | ||
case Z_SAMPLE_KIND_DELETE: | ||
return "DELETE"; | ||
default: | ||
return "UNKNOWN"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.