Skip to content

Commit

Permalink
Fix ping-pong examples (#226) (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
cguimaraes committed Jul 5, 2023
1 parent 9b663d5 commit 4df1107
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 24 deletions.
27 changes: 24 additions & 3 deletions examples/unix/c11/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,32 @@ int main(int argc, char** argv) {
_z_condvar_init(&cond);
z_owned_config_t config = z_config_default();
z_owned_session_t session = z_open(z_move(config));
if (!z_check(session)) {
printf("Unable to open session!\n");
return -1;
}

if (zp_start_read_task(z_loan(session), NULL) < 0 || zp_start_lease_task(z_loan(session), NULL) < 0) {
printf("Unable to start read and lease tasks");
return -1;
}

z_keyexpr_t ping = z_keyexpr_unchecked("test/ping");
z_keyexpr_t pong = z_keyexpr_unchecked("test/pong");

z_owned_publisher_t pub = z_declare_publisher(z_loan(session), ping, NULL);
z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)(&pub));
if (!z_check(pub)) {
printf("Unable to declare publisher for key expression!\n");
return -1;
}

z_owned_closure_sample_t respond = z_closure(callback, drop, NULL);
z_owned_subscriber_t sub = z_declare_subscriber(z_loan(session), pong, z_move(respond), NULL);
if (!z_check(sub)) {
printf("Unable to declare subscriber for key expression.\n");
return -1;
}

uint8_t* data = z_malloc(args.size);
for (unsigned int i = 0; i < args.size; i++) {
data[i] = i % 10;
Expand Down Expand Up @@ -79,8 +100,8 @@ int main(int argc, char** argv) {
_z_mutex_unlock(&mutex);
z_free(results);
z_free(data);
z_drop(z_move(sub));
z_drop(z_move(pub));
z_undeclare_publisher(z_move(pub));
z_undeclare_subscriber(z_move(sub));
z_close(z_move(session));
}

Expand Down
25 changes: 23 additions & 2 deletions examples/unix/c11/z_pong.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,34 @@ int main(int argc, char** argv) {
(void)argv;
z_owned_config_t config = z_config_default();
z_owned_session_t session = z_open(z_move(config));
z_keyexpr_t ping = z_keyexpr_unchecked("test/ping");
if (!z_check(session)) {
printf("Unable to open session!\n");
return -1;
}

if (zp_start_read_task(z_loan(session), NULL) < 0 || zp_start_lease_task(z_loan(session), NULL) < 0) {
printf("Unable to start read and lease tasks");
return -1;
}

z_keyexpr_t pong = z_keyexpr_unchecked("test/pong");
z_owned_publisher_t pub = z_declare_publisher(z_loan(session), pong, NULL);
if (!z_check(pub)) {
printf("Unable to declare publisher for key expression!\n");
return -1;
}

z_keyexpr_t ping = z_keyexpr_unchecked("test/ping");
z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)z_move(pub));
z_owned_subscriber_t sub = z_declare_subscriber(z_loan(session), ping, z_move(respond), NULL);
if (!z_check(sub)) {
printf("Unable to declare subscriber for key expression.\n");
return -1;
}

while (getchar() != 'q') {
}
z_drop(z_move(sub));

z_undeclare_subscriber(z_move(sub));
z_close(z_move(session));
}
36 changes: 29 additions & 7 deletions examples/unix/c99/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,34 @@ int main(int argc, char** argv) {
_z_mutex_init(&mutex);
_z_condvar_init(&cond);
z_owned_config_t config = z_config_default();
z_owned_session_t session = z_open(z_move(config));
z_owned_session_t session = z_open(z_config_move(&config));
if (!z_session_check(&session)) {
printf("Unable to open session!\n");
return -1;
}

if (zp_start_read_task(z_session_loan(&session), NULL) < 0 ||
zp_start_lease_task(z_session_loan(&session), NULL) < 0) {
printf("Unable to start read and lease tasks");
return -1;
}

z_keyexpr_t ping = z_keyexpr_unchecked("test/ping");
z_keyexpr_t pong = z_keyexpr_unchecked("test/pong");
z_owned_publisher_t pub = z_declare_publisher(z_session_loan(&session), ping, NULL);
z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)(&pub));
z_owned_subscriber_t sub = z_declare_subscriber(z_session_loan(&session), pong, z_move(respond), NULL);
if (!z_publisher_check(&pub)) {
printf("Unable to declare publisher for key expression!\n");
return -1;
}

z_keyexpr_t pong = z_keyexpr_unchecked("test/pong");
z_owned_closure_sample_t respond = z_closure_sample(callback, drop, NULL);
z_owned_subscriber_t sub =
z_declare_subscriber(z_session_loan(&session), pong, z_closure_sample_move(&respond), NULL);
if (!z_subscriber_check(&sub)) {
printf("Unable to declare subscriber for key expression.\n");
return -1;
}

uint8_t* data = z_malloc(args.size);
for (unsigned int i = 0; i < args.size; i++) {
data[i] = i % 10;
Expand Down Expand Up @@ -78,9 +100,9 @@ int main(int argc, char** argv) {
_z_mutex_unlock(&mutex);
z_free(results);
z_free(data);
z_undeclare_subscriber(z_move(sub));
z_undeclare_publisher(z_move(pub));
z_close(z_move(session));
z_undeclare_subscriber(z_subscriber_move(&sub));
z_undeclare_publisher(z_publisher_move(&pub));
z_close(z_session_move(&session));
}

char* getopt(int argc, char** argv, char option) {
Expand Down
35 changes: 29 additions & 6 deletions examples/unix/c99/z_pong.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,37 @@ int main(int argc, char** argv) {
(void)argc;
(void)argv;
z_owned_config_t config = z_config_default();
z_owned_session_t session = z_open(z_move(config));
z_keyexpr_t ping = z_keyexpr_unchecked("test/ping");
z_owned_session_t session = z_open(z_config_move(&config));
if (!z_session_check(&session)) {
printf("Unable to open session!\n");
return -1;
}

if (zp_start_read_task(z_session_loan(&session), NULL) < 0 ||
zp_start_lease_task(z_session_loan(&session), NULL) < 0) {
printf("Unable to start read and lease tasks");
return -1;
}

z_keyexpr_t pong = z_keyexpr_unchecked("test/pong");
z_owned_publisher_t pub = z_declare_publisher(z_session_loan(&session), pong, NULL);
z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)z_move(pub));
z_owned_subscriber_t sub = z_declare_subscriber(z_session_loan(&session), ping, z_move(respond), NULL);
if (!z_publisher_check(&pub)) {
printf("Unable to declare publisher for key expression!\n");
return -1;
}

z_keyexpr_t ping = z_keyexpr_unchecked("test/ping");
z_owned_closure_sample_t respond = z_closure_sample(callback, drop, (void*)z_publisher_move(&pub));
z_owned_subscriber_t sub =
z_declare_subscriber(z_session_loan(&session), ping, z_closure_sample_move(&respond), NULL);
if (!z_subscriber_check(&sub)) {
printf("Unable to declare subscriber for key expression.\n");
return -1;
}

while (getchar() != 'q') {
}
z_undeclare_subscriber(z_move(sub));
z_close(z_move(session));

z_undeclare_subscriber(z_subscriber_move(&sub));
z_close(z_session_move(&session));
}
28 changes: 24 additions & 4 deletions examples/windows/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,31 @@ int main(int argc, char** argv) {
_z_condvar_init(&cond);
z_owned_config_t config = z_config_default();
z_owned_session_t session = z_open(z_move(config));
if (!z_check(session)) {
printf("Unable to open session!\n");
return -1;
}

if (zp_start_read_task(z_loan(session), NULL) < 0 || zp_start_lease_task(z_loan(session), NULL) < 0) {
printf("Unable to start read and lease tasks");
return -1;
}

z_keyexpr_t ping = z_keyexpr_unchecked("test/ping");
z_keyexpr_t pong = z_keyexpr_unchecked("test/pong");
z_owned_publisher_t pub = z_declare_publisher(z_loan(session), ping, NULL);
z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)(&pub));
if (!z_check(pub)) {
printf("Unable to declare publisher for key expression!\n");
return -1;
}

z_keyexpr_t pong = z_keyexpr_unchecked("test/pong");
z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)z_move(pub));
z_owned_subscriber_t sub = z_declare_subscriber(z_loan(session), pong, z_move(respond), NULL);
if (!z_check(sub)) {
printf("Unable to declare subscriber for key expression.\n");
return -1;
}

uint8_t* data = z_malloc(args.size);
for (unsigned int i = 0; i < args.size; i++) {
data[i] = i % 10;
Expand Down Expand Up @@ -77,8 +97,8 @@ int main(int argc, char** argv) {
_z_mutex_unlock(&mutex);
z_free(results);
z_free(data);
z_drop(z_move(sub));
z_drop(z_move(pub));
z_undeclare_pubscriber(z_move(pub));
z_undeclare_subscriber(z_move(sub));
z_close(z_move(session));
}

Expand Down
25 changes: 23 additions & 2 deletions examples/windows/z_pong.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,34 @@ int main(int argc, char** argv) {
(void)argv;
z_owned_config_t config = z_config_default();
z_owned_session_t session = z_open(z_move(config));
z_keyexpr_t ping = z_keyexpr_unchecked("test/ping");
if (!z_check(session)) {
printf("Unable to open session!\n");
return -1;
}

if (zp_start_read_task(z_loan(session), NULL) < 0 || zp_start_lease_task(z_loan(session), NULL) < 0) {
printf("Unable to start read and lease tasks");
return -1;
}

z_keyexpr_t pong = z_keyexpr_unchecked("test/pong");
z_owned_publisher_t pub = z_declare_publisher(z_loan(session), pong, NULL);
if (!z_check(pub)) {
printf("Unable to declare publisher for key expression!\n");
return -1;
}

z_keyexpr_t ping = z_keyexpr_unchecked("test/ping");
z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)z_move(pub));
z_owned_subscriber_t sub = z_declare_subscriber(z_loan(session), ping, z_move(respond), NULL);
if (!z_check(sub)) {
printf("Unable to declare subscriber for key expression.\n");
return -1;
}

while (getchar() != 'q') {
}
z_drop(z_move(sub));

z_undeclare_subscriber(z_move(sub));
z_close(z_move(session));
}

0 comments on commit 4df1107

Please sign in to comment.