2016-05-30 16:30:05 +02:00
|
|
|
#include "../lib/communication.h"
|
2016-09-22 18:37:24 +02:00
|
|
|
#include <pthread.h>
|
2016-05-30 16:30:05 +02:00
|
|
|
|
2016-06-05 20:48:13 +02:00
|
|
|
#define PONGD_SERVICE_NAME "pongd"
|
|
|
|
|
2016-09-22 18:37:24 +02:00
|
|
|
|
|
|
|
/* control the file descriptor*/
|
|
|
|
void * pongd_thread(void * pdata) {
|
|
|
|
struct process *proc = (struct process*) pdata;
|
|
|
|
|
|
|
|
// about the message
|
2016-10-03 17:30:52 +02:00
|
|
|
size_t msize = 0;
|
2016-09-22 18:37:24 +02:00
|
|
|
char *buf = NULL;
|
2016-10-03 17:30:52 +02:00
|
|
|
int ret = 0;
|
2016-09-22 18:37:24 +02:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
// printf ("before read\n");
|
2016-10-03 17:30:52 +02:00
|
|
|
while (ret <= 0 || msize == 0) {
|
|
|
|
if ((ret = srv_read (proc, &buf, &msize)) == -1) {
|
|
|
|
fprintf(stdout, "MAIN_LOOP: error service_read %d\n", ret);
|
|
|
|
}
|
2016-09-22 18:37:24 +02:00
|
|
|
}
|
|
|
|
// printf ("after read\n");
|
|
|
|
printf ("read, size %ld : %s\n", msize, buf);
|
|
|
|
|
2016-10-03 17:30:52 +02:00
|
|
|
if (strncmp ("exit", buf, 4) != 0) {
|
2016-09-23 20:27:26 +02:00
|
|
|
//printf ("before proc write\n");
|
2016-09-22 18:37:24 +02:00
|
|
|
if ((ret = srv_write (proc, buf, msize)) == -1) {
|
|
|
|
fprintf(stdout, "MAIN_LOOP: error service_write %d\n", ret);
|
|
|
|
}
|
|
|
|
}else {
|
2016-09-28 19:30:53 +02:00
|
|
|
printf("------thread shutdown------------\n");
|
2016-09-29 17:30:24 +02:00
|
|
|
free(buf);
|
2016-09-22 18:37:24 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-10-03 17:30:52 +02:00
|
|
|
ret = 0;
|
|
|
|
msize = 0;
|
2016-09-22 18:37:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-05-30 16:30:05 +02:00
|
|
|
/*
|
|
|
|
* 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)
|
2016-05-30 16:30:05 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2016-09-22 18:37:24 +02:00
|
|
|
struct process tab_proc[10];
|
|
|
|
//thread
|
|
|
|
pthread_t tab_thread[10];
|
|
|
|
int i;
|
2016-05-30 16:30:05 +02:00
|
|
|
|
2016-09-22 18:37:24 +02:00
|
|
|
int cnt = 0;
|
2016-05-30 16:30:05 +02:00
|
|
|
|
2016-09-22 18:37:24 +02:00
|
|
|
while (cnt < 10) {
|
2016-09-10 16:23:30 +02:00
|
|
|
|
2016-05-30 16:30:05 +02:00
|
|
|
// -1 : error, 0 = no new process, 1 = new process
|
2016-09-22 18:37:24 +02:00
|
|
|
ret = srv_get_new_process (srv, &tab_proc[cnt]);
|
2016-09-10 16:23:30 +02:00
|
|
|
|
2016-05-30 16:30:05 +02:00
|
|
|
if (ret == -1) {
|
2016-09-10 16:23:30 +02:00
|
|
|
fprintf (stderr, "MAIN_LOOP: error service_get_new_process\n");
|
2016-05-31 16:14:50 +02:00
|
|
|
continue;
|
2016-05-30 16:30:05 +02:00
|
|
|
} else if (ret == 0) { // that should not happen
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-09-22 18:37:24 +02:00
|
|
|
srv_process_print (&tab_proc[cnt]);
|
2016-09-28 19:30:53 +02:00
|
|
|
|
|
|
|
printf ("\n-------New thread created---------\n");
|
2016-09-22 18:37:24 +02:00
|
|
|
int ret = pthread_create( &tab_thread[cnt], NULL, &pongd_thread, (void *) &tab_proc[cnt]);
|
|
|
|
if (ret) {
|
|
|
|
perror("pthread_create()");
|
|
|
|
exit(errno);
|
|
|
|
} else {
|
|
|
|
printf("Creation of listen thread \n");
|
2016-05-30 16:30:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// printf ("after proc write\n");
|
2016-09-10 16:23:30 +02:00
|
|
|
printf ("%d applications to serve\n",cnt);
|
2016-09-22 18:37:24 +02:00
|
|
|
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < cnt; i++) {
|
|
|
|
pthread_join(tab_thread[cnt], NULL);
|
2016-05-30 16:30:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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-05-30 16:30:05 +02:00
|
|
|
{
|
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);
|
2016-05-30 16:30:05 +02:00
|
|
|
|
|
|
|
// 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))) {
|
2016-05-30 16:30:05 +02:00
|
|
|
fprintf(stdout, "error service_create %d\n", ret);
|
|
|
|
exit (1);
|
|
|
|
}
|
2016-09-10 18:34:02 +02:00
|
|
|
printf("MAIN: server created\n" );
|
2016-05-30 16:30:05 +02:00
|
|
|
|
|
|
|
// 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);
|
2016-05-30 16:30:05 +02:00
|
|
|
|
|
|
|
// the application will shut down, and remove the service named pipe
|
2016-06-05 20:48:13 +02:00
|
|
|
if ((ret = srv_close (&srv))) {
|
2016-05-30 16:30:05 +02:00
|
|
|
fprintf(stdout, "error service_close %d\n", ret);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|