Skip to content

Commit

Permalink
Improve pull example
Browse files Browse the repository at this point in the history
  • Loading branch information
Mallets committed Mar 22, 2024
1 parent 25b0661 commit 0d76f92
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions examples/unix/c11/z_pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,26 @@
int main(int argc, char **argv) {
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:")) != -1) {
while ((opt = getopt(argc, argv, "k:e:i:s:")) != -1) {

Check warning

Code scanning / Cppcheck (reported by Codacy)

getopt is MT-unsafe Warning

getopt is MT-unsafe

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 13.4 rule Note

MISRA 13.4 rule
switch (opt) {
case 'k':
keyexpr = optarg;
break;
case 'e':
locator = optarg;
break;
case 'i':
interval = (size_t)atoi(optarg);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 21.7 rule Note

MISRA 21.7 rule
break;
case 's':
size = (size_t)atoi(optarg);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 21.7 rule Note

MISRA 21.7 rule
break;
case '?':
if (optopt == 'k' || optopt == 'e') {
if (optopt == 'k' || optopt == 'e' || optopt == 'i' || optopt == 's') {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 12.1 rule Note

MISRA 12.1 rule
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
} else {
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
Expand Down Expand Up @@ -64,29 +72,25 @@ int main(int argc, char **argv) {
}

printf("Declaring Subscriber on '%s'...\n", keyexpr);
z_owned_sample_channel_t channel = z_sample_channel_ring_new(3);
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");

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
return -1;

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 15.5 rule Note

MISRA 15.5 rule
}

printf("Enter any key to pull data or 'q' to quit...\n");
char c = '\0';
for (int ret = scanf("%c", &c); c != 'q'; ret = scanf("%c", &c)) {
// Try to receive one sample from the ring channel
z_owned_sample_t sample = z_sample_null();
z_call(channel.recv, &sample);
// Check if we actually received something
if (z_check(sample)) {
printf("Pulling data every %zu ms... Ring size: %zd\n", interval, size);

Check notice

Code scanning / Cppcheck (reported by Codacy)

%zd in format string (no. 2) requires 'ssize_t' but the argument type is 'size_t {aka unsigned long}'. Note

%zd in format string (no. 2) requires 'ssize_t' but the argument type is 'size_t {aka unsigned long}'.

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
z_owned_sample_t sample = z_sample_null();
while (true) {
for (z_call(channel.recv, &sample); z_check(sample); z_call(channel.recv, &sample)) {

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 14.2 rule Note

MISRA 14.2 rule
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,

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
sample.payload.start);
z_drop(z_move(keystr));
} else {
printf(">> [Subscriber] Nothing to pull...\n");
z_drop(z_move(sample));
}
z_drop(z_move(sample));
printf(">> [Subscriber] Nothing to pull... sleep for %zu ms\n", interval);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
zp_sleep_ms(interval);
}

z_undeclare_subscriber(z_move(sub));
Expand Down

0 comments on commit 0d76f92

Please sign in to comment.