2016-05-30 16:30:05 +02:00
|
|
|
#include "../lib/communication.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* main loop
|
|
|
|
*
|
|
|
|
* opens the application pipes,
|
|
|
|
* reads then writes the same message,
|
|
|
|
* then closes the pipes
|
|
|
|
*/
|
|
|
|
|
|
|
|
void main_loop (const char *spath)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct process proc;
|
|
|
|
|
|
|
|
int cnt = 10;
|
|
|
|
|
|
|
|
while (cnt--) {
|
|
|
|
// -1 : error, 0 = no new process, 1 = new process
|
2016-06-05 03:19:36 +02:00
|
|
|
ret = srv_get_new_process (&proc, spath);
|
2016-05-30 16:30:05 +02:00
|
|
|
if (ret == -1) {
|
2016-05-31 16:14:50 +02:00
|
|
|
fprintf (stderr, "error service_get_new_process\n");
|
|
|
|
continue;
|
2016-05-30 16:30:05 +02:00
|
|
|
} else if (ret == 0) { // that should not happen
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// printf ("before print\n");
|
|
|
|
process_print (&proc);
|
|
|
|
// printf ("after print\n");
|
|
|
|
|
|
|
|
// about the message
|
|
|
|
size_t msize = BUFSIZ;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
bzero(buf, BUFSIZ);
|
|
|
|
|
|
|
|
// printf ("before read\n");
|
2016-06-05 03:19:36 +02:00
|
|
|
if ((ret = srv_read (&proc, &buf, &msize))) {
|
2016-05-30 16:30:05 +02:00
|
|
|
fprintf(stdout, "error service_read %d\n", ret);
|
2016-05-31 16:14:50 +02:00
|
|
|
continue;
|
2016-05-30 16:30:05 +02:00
|
|
|
}
|
|
|
|
// printf ("after read\n");
|
|
|
|
printf ("read, size %ld : %s\n", msize, buf);
|
|
|
|
|
|
|
|
// printf ("before proc write\n");
|
2016-06-05 03:19:36 +02:00
|
|
|
if ((ret = srv_write (&proc, &buf, msize))) {
|
2016-05-30 16:30:05 +02:00
|
|
|
fprintf(stdout, "error service_write %d\n", ret);
|
2016-05-31 16:14:50 +02:00
|
|
|
continue;
|
2016-05-30 16:30:05 +02:00
|
|
|
}
|
|
|
|
// printf ("after proc write\n");
|
|
|
|
printf ("\033[32mStill \033[31m%d\033[32m 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>
|
|
|
|
*/
|
|
|
|
|
|
|
|
int main(int argc, char * argv[])
|
|
|
|
{
|
|
|
|
// gets the service path, such as /tmp/<service>
|
|
|
|
char spath[PATH_MAX];
|
2016-06-05 03:19:36 +02:00
|
|
|
srv_path (spath, "pingpong");
|
2016-05-30 16:30:05 +02:00
|
|
|
|
|
|
|
// creates the service named pipe, that listens to client applications
|
|
|
|
int ret;
|
2016-06-05 03:19:36 +02:00
|
|
|
if ((ret = srv_create (spath))) {
|
2016-05-30 16:30:05 +02:00
|
|
|
fprintf(stdout, "error service_create %d\n", ret);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// the service will loop until the end of time, a specific message, a signal
|
|
|
|
main_loop (spath);
|
|
|
|
|
|
|
|
// the application will shut down, and remove the service named pipe
|
2016-06-05 03:19:36 +02:00
|
|
|
if ((ret = srv_close (spath))) {
|
2016-05-30 16:30:05 +02:00
|
|
|
fprintf(stdout, "error service_close %d\n", ret);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|