Obsolete
/
libipc-old
Archived
3
0
Fork 0
This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues/pull-requests.
libipc-old/tests/func_05_read-write-loop.c

80 lines
2.1 KiB
C
Raw Normal View History

2020-07-04 11:01:24 +02:00
// #define _BSD_SOURCE
#define _DEFAULT_SOURCE
2020-01-28 13:41:09 +01:00
#include "../src/ipc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#define SERVICE_NAME "pong"
#define SECURE_MALLOC(p, s, wat) p = malloc (s); if (p == NULL) { wat; }
#define DEFAULT_MSG "coucou"
2020-07-04 11:01:24 +02:00
int main(int argc, char * argv[])
2020-01-28 13:41:09 +01:00
{
2020-07-04 11:01:24 +02:00
SECURE_DECLARATION(struct ipc_ctx, ctx);
2020-01-28 13:41:09 +01:00
SECURE_DECLARATION(struct ipc_event, event);
SECURE_DECLARATION (struct ipc_message, write_m);
SECURE_DECLARATION (struct ipc_message, read_m);
// timing the exchange duration
struct timeval tval_before, tval_after, tval_result;
size_t nb_rounds = 1000;
char *message_str = NULL;
if (argc > 1 && strncmp(argv[1], "-h", 2) == 0) {
fprintf (stderr, "usage: %s [nb-rounds [message]]\n", argv[0]);
fprintf (stderr, "default: nb-rounds = 1000, message='coucou'\n");
exit(0);
}
if (argc > 1) {
nb_rounds = (size_t) atoi(argv[1]);
}
if (argc > 2) {
message_str = argv[2];
}
if (message_str == NULL) {
// message par defaut
SECURE_MALLOC (message_str, strlen(DEFAULT_MSG) +1, exit(EXIT_FAILURE));
memcpy(message_str, DEFAULT_MSG, strlen(DEFAULT_MSG));
}
2020-07-04 11:01:24 +02:00
TEST_IPC_Q (ipc_connection (&ctx, SERVICE_NAME), EXIT_FAILURE);
2020-01-28 13:41:09 +01:00
SECURE_MALLOC (write_m.payload, strlen(message_str), exit(EXIT_FAILURE));
memcpy (write_m.payload, message_str, strlen(message_str));
write_m.type = MSG_TYPE_DATA;
write_m.user_type = 42;
2020-07-04 11:01:24 +02:00
write_m.fd = ctx.pollfd[0].fd;
2020-01-28 13:41:09 +01:00
write_m.length = strlen(message_str);
gettimeofday(&tval_before, NULL);
for (size_t i = 0 ; i < nb_rounds ; i++) {
2020-07-04 11:01:24 +02:00
ipc_write_fd (ctx.pollfd[0].fd, &write_m);
2020-01-28 13:41:09 +01:00
// reading
2020-07-04 11:01:24 +02:00
TEST_IPC_Q(ipc_read (&ctx, 0 /* only valid index */, &read_m), EXIT_FAILURE);
2020-01-28 13:41:09 +01:00
// printf ("received message (%d bytes): %*s\n", read_m.length, read_m.length, read_m.payload);
// ipc_message_empty (&read_m);
}
gettimeofday(&tval_after, NULL);
timersub(&tval_after, &tval_before, &tval_result);
printf("Time elapsed: %ld.%06ld\n", (long int)tval_result.tv_sec, (long int)tval_result.tv_usec);
// disconnection
2020-07-04 11:01:24 +02:00
TEST_IPC_Q (ipc_close_all (&ctx), EXIT_FAILURE);
2020-01-28 13:41:09 +01:00
return EXIT_SUCCESS;
}