Obsolete
/
libipc-old
Archived
3
0
Fork 0

PingPong is now working correctly

more_to_read
lapupe 2016-09-10 16:23:30 +02:00
parent 18c763eed3
commit 0a4988c024
3 changed files with 34 additions and 17 deletions

View File

@ -24,16 +24,17 @@ int file_open (FILE **f, const char *path, const char *mode)
int file_close (FILE *f)
{
if (f != 0) {
printf ("before fclosing\n");
if (fclose (f)) {
return ER_FILE_CLOSE;
}
printf ("after fclosing\n");
printf ("FILE_CLOSE: closed file\n");
}
return 0;
}
int file_read (FILE *f, char **buf, size_t *msize) {
size_t n =0;
if (*msize == 0) {
*msize = BUFSIZ; // default value
}
@ -41,18 +42,28 @@ int file_read (FILE *f, char **buf, size_t *msize) {
if (*buf == NULL) {
*buf = malloc (*msize);
if (*buf == NULL) {
fprintf (stderr, "err can't allocate enough memory (%ld)\n", *msize);
fprintf (stderr, "FILE_READ: err can't allocate enough memory (%ld)\n", *msize);
int ret = file_close (f);
if (ret != 0)
return ret;
}
}
*msize = fread (*buf, *msize, 1, f);
//*msize = fread (*buf, *msize, 1, f);
// if (*msize == 0) {
// fprintf (stderr, "FILE_READ: err can't read a file\n");
// if (ER_FILE_CLOSE == file_close (f)) {
// fprintf (stderr, "FILE_READ: err closing the file\n");
// return ER_FILE_CLOSE;
// }
// return ER_FILE_READ;
// }
*msize = getline (buf, &n, f);
if (*msize == 0) {
fprintf (stderr, "err can't read a file\n");
fprintf (stderr, "FILE_READ: err can't read a file\n");
if (ER_FILE_CLOSE == file_close (f)) {
fprintf (stderr, "err closing the file\n");
fprintf (stderr, "FILE_READ: err closing the file\n");
return ER_FILE_CLOSE;
}
return ER_FILE_READ;
@ -243,9 +254,10 @@ int srv_get_new_process (const struct service *srv, struct process *p)
}
}
// printf("pid = %d, index = %d, version = %d \n",pid, index, version );
srv_process_gen (p, pid, index, version);
return 0;
return 1;
}
int srv_read_cb (struct process *p, char ** buf, size_t * msize

View File

@ -18,18 +18,20 @@ void main_loop (const struct service *srv)
int cnt = 10;
while (cnt--) {
// -1 : error, 0 = no new process, 1 = new process
ret = srv_get_new_process (srv, &proc);
//printf("ret = %d\n", ret);
if (ret == -1) {
fprintf (stderr, "error service_get_new_process\n");
fprintf (stderr, "MAIN_LOOP: error service_get_new_process\n");
continue;
} else if (ret == 0) { // that should not happen
continue;
}
// printf ("before print\n");
srv_process_print (&proc);
// printf ("after print\n");
//printf ("after print\n");
// about the message
size_t msize = BUFSIZ;
@ -37,7 +39,7 @@ void main_loop (const struct service *srv)
// printf ("before read\n");
if ((ret = srv_read (&proc, &buf, &msize))) {
fprintf(stdout, "error service_read %d\n", ret);
fprintf(stdout, "MAIN_LOOP: error service_read %d\n", ret);
continue;
}
// printf ("after read\n");
@ -45,11 +47,11 @@ void main_loop (const struct service *srv)
// printf ("before proc write\n");
if ((ret = srv_write (&proc, buf, msize))) {
fprintf(stdout, "error service_write %d\n", ret);
fprintf(stdout, "MAIN_LOOP: error service_write %d\n", ret);
continue;
}
// printf ("after proc write\n");
printf ("\033[32mStill \033[31m%d\033[32m applications to serve\n",cnt);
printf ("%d applications to serve\n",cnt);
}
}

View File

@ -22,15 +22,18 @@ fi
for pid in `seq 1 ${NB}`
do
echo "${pid}"
# we make the application pipes
mkfifo ${REP}/${pid}-1-in 2>/dev/null
mkfifo ${REP}/${pid}-1-out 2>/dev/null
mkfifo ${REP}${pid}-1-in 2>/dev/null
mkfifo ${REP}${pid}-1-out 2>/dev/null
# pid index version
echo "${pid} 1 1" > ${REP}/${SERVICE}
echo "${pid} 1 5" > ${REP}${SERVICE}
# the purpose is to send something in the pipe
cat /dev/urandom | base64 | head -n 1 > ${REP}/${pid}-1-out
cat /dev/urandom | base64 | head -n 1 > ${REP}${pid}-1-out
# echo "hello world" > ${REP}${pid}-1-out
# the the service will answer with our message
cat ${REP}/${pid}-1-in