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/pingpong/pingpong.c

93 lines
2.4 KiB
C
Raw Normal View History

#include "../lib/communication.h"
2016-06-05 20:48:13 +02:00
#define PONGD_SERVICE_NAME "pongd"
/*
* main loop
*
* opens the application pipes,
* reads then writes the same message,
* then closes the pipes
*/
2016-06-05 20:48:13 +02:00
void main_loop (const struct service *srv)
{
int ret;
struct process proc;
int cnt = 10;
while (cnt--) {
2016-09-10 16:23:30 +02:00
// -1 : error, 0 = no new process, 1 = new process
2016-06-07 11:45:21 +02:00
ret = srv_get_new_process (srv, &proc);
2016-09-10 16:23:30 +02:00
//printf("ret = %d\n", ret);
if (ret == -1) {
2016-09-10 16:23:30 +02:00
fprintf (stderr, "MAIN_LOOP: error service_get_new_process\n");
continue;
} else if (ret == 0) { // that should not happen
continue;
}
2016-06-07 13:49:23 +02:00
srv_process_print (&proc);
2016-09-10 16:23:30 +02:00
//printf ("after print\n");
// about the message
size_t msize = BUFSIZ;
char *buf = NULL;
// printf ("before read\n");
2016-09-10 17:16:26 +02:00
if ((ret = srv_read (&proc, &buf, &msize)) == -1) {
2016-09-10 16:23:30 +02:00
fprintf(stdout, "MAIN_LOOP: error service_read %d\n", ret);
continue;
}
// printf ("after read\n");
printf ("read, size %ld : %s\n", msize, buf);
// printf ("before proc write\n");
2016-09-10 17:16:26 +02:00
if ((ret = srv_write (&proc, buf, msize)) == -1) {
2016-09-10 16:23:30 +02:00
fprintf(stdout, "MAIN_LOOP: error service_write %d\n", ret);
continue;
}
// printf ("after proc write\n");
2016-09-10 16:23:30 +02:00
printf ("%d applications to serve\n",cnt);
}
}
/*
* service ping-pong
*
* 1. creates the named pipe /tmp/<service>, then listens
* 2. opens the named pipes in & out
* 3. talks with the (test) program
* 4. closes the test program named pipes
* 5. removes the named pipe /tmp/<service>
*/
2016-06-12 14:41:25 +02:00
int main(int argc, char * argv[], char **env)
{
2016-06-05 20:48:13 +02:00
struct service srv;
2016-06-13 09:47:19 +02:00
srv_init (argc, argv, env, &srv, PONGD_SERVICE_NAME, NULL);
2016-06-05 20:48:13 +02:00
printf ("Listening on %s.\n", srv.spath);
// creates the service named pipe, that listens to client applications
int ret;
2016-06-05 20:48:13 +02:00
if ((ret = srv_create (&srv))) {
fprintf(stdout, "error service_create %d\n", ret);
exit (1);
}
printf("MAIN: server created\n" );
// the service will loop until the end of time, a specific message, a signal
2016-06-05 20:48:13 +02:00
main_loop (&srv);
// the application will shut down, and remove the service named pipe
2016-06-05 20:48:13 +02:00
if ((ret = srv_close (&srv))) {
fprintf(stdout, "error service_close %d\n", ret);
exit (1);
}
return EXIT_SUCCESS;
}