2023-01-17 04:33:09 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "../libipc.h"
|
|
|
|
|
|
|
|
#define SERVICE "pong"
|
|
|
|
#define SERVICE_LEN 4
|
|
|
|
|
|
|
|
|
2023-01-17 07:51:41 +01:00
|
|
|
int main(int argc, char**argv) {
|
2023-01-17 04:33:09 +01:00
|
|
|
int ret = 0;
|
|
|
|
int servicefd = 0;
|
|
|
|
char message[10000];
|
2023-01-17 05:47:14 +01:00
|
|
|
size_t size = 10000;
|
2023-01-17 04:33:09 +01:00
|
|
|
char event_type;
|
2023-01-17 05:47:14 +01:00
|
|
|
size_t index = 0;
|
2023-01-17 04:33:09 +01:00
|
|
|
int originfd = 0;
|
|
|
|
void *ctx = NULL;
|
|
|
|
|
2023-01-17 07:51:41 +01:00
|
|
|
int max_count = 0;
|
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
max_count = atoi(argv[1]);
|
|
|
|
printf ("Wait for %d timer events.\n", max_count);
|
|
|
|
}
|
|
|
|
|
2023-01-17 04:33:09 +01:00
|
|
|
printf ("Init context.\n");
|
|
|
|
ret = ipc_context_init (&ctx);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
printf ("Cannot init context.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("Create a 'pong' service.\n");
|
|
|
|
ret = ipc_service_init (ctx, &servicefd, SERVICE, SERVICE_LEN);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
printf ("Cannot create a service.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-01-18 01:34:30 +01:00
|
|
|
printf ("Set the timer to two seconds.\n");
|
|
|
|
ipc_context_timer (ctx, 2000);
|
2023-01-17 04:33:09 +01:00
|
|
|
|
|
|
|
printf ("Loop over events.\n");
|
|
|
|
char should_continue = 1;
|
2023-01-17 05:47:14 +01:00
|
|
|
size_t count = 0;
|
|
|
|
size_t count_timer = 0;
|
2023-01-17 04:33:09 +01:00
|
|
|
while(should_continue) {
|
|
|
|
size = 10000;
|
|
|
|
ret = ipc_wait_event (ctx, &event_type, &index, &originfd, message, &size);
|
|
|
|
if (ret != 0) {
|
|
|
|
printf ("Error while waiting for an event.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((enum event_types) event_type != TIMER) {
|
2023-01-17 05:47:14 +01:00
|
|
|
printf ("EVENT %lu\t", count++);
|
2023-01-17 04:33:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch ((enum event_types) event_type) {
|
|
|
|
case ERROR: {
|
|
|
|
printf ("Error.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
case EXTERNAL: {
|
|
|
|
printf ("External (shouldn't happen).\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
case SWITCH_RX: {
|
|
|
|
printf ("Switch RX (shouldn't happen).\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
case SWITCH_TX: {
|
|
|
|
printf ("Switch TX (shouldn't happen).\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
case CONNECTION: {
|
|
|
|
printf ("New connection.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DISCONNECTION: {
|
|
|
|
printf ("User disconnected.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TIMER: {
|
2023-01-17 05:47:14 +01:00
|
|
|
printf ("\rTIMER (%lu).", count_timer++);
|
2023-01-17 04:33:09 +01:00
|
|
|
fflush(stdout);
|
2023-01-17 07:51:41 +01:00
|
|
|
if (max_count && count_timer >= (size_t) max_count) {
|
|
|
|
printf ("waited for %lu timer events: quitting\n", count_timer);
|
|
|
|
should_continue = 0;
|
|
|
|
}
|
|
|
|
|
2023-01-17 04:33:09 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TX: {
|
|
|
|
printf ("Message sent.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MESSAGE: {
|
|
|
|
if (size == 0) {
|
|
|
|
printf ("Error: no message returned.\n");
|
|
|
|
should_continue = 0;
|
|
|
|
}
|
|
|
|
else {
|
2023-01-17 07:51:41 +01:00
|
|
|
message[size+1] = '\0';
|
|
|
|
printf ("Message received (size %lu): %s.\n", size, message);
|
2023-01-17 04:33:09 +01:00
|
|
|
printf ("Scheduling this message.\n");
|
|
|
|
|
|
|
|
ret = ipc_schedule (ctx, originfd, message, size);
|
|
|
|
if (ret != 0) {
|
|
|
|
printf ("Cannot schedule a message.\n");
|
|
|
|
should_continue = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("Deinit context\n");
|
|
|
|
ipc_context_deinit (ctx);
|
|
|
|
|
|
|
|
free(ctx);
|
|
|
|
|
|
|
|
printf ("Context freed.\n");
|
|
|
|
return 0;
|
|
|
|
}
|