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/pubsub/app/test-pipe-write.c

58 lines
1.3 KiB
C
Raw Normal View History

2016-09-09 13:29:36 +02:00
#include "../lib/pubsubd.h"
#include <stdlib.h>
#define CHAN "chan1"
#define MESSAGE "coucou"
void
ohshit(int rvalue, const char* str) {
fprintf(stderr, "%s\n", str);
exit(rvalue);
}
void usage (char **argv) {
fprintf (stderr, "usage: %s path times\n", argv[0]);
fprintf (stderr, "ex: %s /tmp/pipe 5 => you will write 5 times\n", argv[0]);
exit (1);
}
int
main(int argc, char **argv)
{
if (argc != 3)
usage (argv);
char *path = argv[1];
int nb = atoi (argv[2]);
printf ("Writing on %s %d times.\n", path, nb);
char *buf = NULL;
size_t msize = 0;
int ret = 0;
while (nb--) {
struct pubsub_msg msg;
memset (&msg, 0, sizeof (struct pubsub_msg));
msg.type = PUBSUB_TYPE_MESSAGE;
msg.chan = malloc (strlen (CHAN) + 1);
strncpy ((char *)msg.chan, CHAN, strlen (CHAN) + 1);
msg.chanlen = strlen (CHAN) + 1;
msg.data = malloc (strlen (MESSAGE) + 1);
strncpy ((char *)msg.data, MESSAGE, strlen (CHAN) + 1);
msg.datalen = strlen (MESSAGE) + 1;
pubsubd_msg_serialize (&msg, &buf, &msize);
pubsubd_msg_print (&msg);
pubsubd_msg_free (&msg);
ret = file_write (path, buf, msize);
if (ret != (int) msize) {
2016-09-09 13:29:36 +02:00
fprintf (stderr, "msg not written\n");
}
}
return EXIT_SUCCESS;
}