Obsolete
/
libipc-old
Archived
3
0
Fork 0

Merge branch 'master' of ssh://git.karchnu.fr:2202/Karchnu/perfectos-junk

more_to_read
Luka Vandervelden 2016-05-31 23:07:13 +02:00
commit 6b2e6d6135
7 changed files with 343 additions and 140 deletions

View File

@ -1,4 +1,6 @@
#include "communication.h" #include "communication.h"
#include <stdio.h>
#include <time.h>
int service_path (char *buf, const char *sname) int service_path (char *buf, const char *sname)
{ {
@ -70,17 +72,29 @@ int service_close (const char *fifopath)
return 0; return 0;
} }
// FIXME only works for a single process int service_get_new_process (struct process *proc, const char * spath)
void service_get_new_processes (struct process **proc, int *nproc, int sfifo)
{ {
if (spath == NULL) {
return -1;
}
char buf[BUFSIZ]; char buf[BUFSIZ];
bzero (buf, BUFSIZ); bzero (buf, BUFSIZ);
read (sfifo, buf, BUFSIZ);
//proc[0] = malloc(sizeof(struct process*) * 1); // FIXME // read the pipe, get a process to work on
proc[0] = malloc(sizeof(struct process) * 1); // FIXME struct timespec ts = { 0 };
struct timespec ts2 = { 0 };
// unsigned long long int val = strtoul(s, NULL, 10); FILE * f = fopen (spath, "r");
clock_gettime(CLOCK_REALTIME, &ts);
fgets (buf, BUFSIZ, f);
clock_gettime(CLOCK_REALTIME, &ts2);
fclose (f);
printf("sec: %ld nsec: %ld\n", ts.tv_sec, ts.tv_nsec);
printf("sec: %ld nsec: %ld\n", ts2.tv_sec, ts2.tv_nsec);
printf("diff nsec: %ld\n", ts2.tv_nsec - ts.tv_nsec);
char *token, *saveptr; char *token, *saveptr;
char *str; char *str;
@ -91,27 +105,71 @@ void service_get_new_processes (struct process **proc, int *nproc, int sfifo)
if (token == NULL) if (token == NULL)
break; break;
printf ("token : %s\n", token);
// do something
if (i == 1) { if (i == 1) {
int index = strchr (token, '-') - token; proc->pid = strtoul(token, NULL, 10);
char * buf_pid = strndup (token, index);
proc[0]->pid = strtoul(buf_pid, NULL, 10);
proc[0]->index = strtoul(token + index +1, NULL, 10);
printf ("buf_pid : %s\n", buf_pid);
printf ("pid : %d\n", proc[0]->pid);
printf ("index : %d\n", proc[0]->index);
} }
else if (i == 2) { else if (i == 2) {
// FIXME proc->index = strtoul(token, NULL, 10);
proc[0]->version = strtoul(token, NULL, 10); }
else if (i == 3) {
proc->version = strtoul(token, NULL, 10);
} }
} }
*nproc = 1; return 1;
}
// FIXME only works for a single process
void service_get_new_processes (struct process ***proc, int *nproc, char * spath)
{
if (proc == NULL || spath == NULL) {
return;
}
char buf[BUFSIZ];
bzero (buf, BUFSIZ);
// read the pipe, get a process to work on
FILE * f = fopen (spath, "rb");
fgets (buf, BUFSIZ, f);
fclose (f);
char *token, *line, *saveptr, *saveptr2;
char *str, *str2;
int i, j;
*nproc = 0;
proc[0] = malloc(sizeof(struct process**));
for (str2 = buf, j = 1; ; str2 = NULL, j++) {
line = strtok_r(str2, "\n", &saveptr2);
if (line == NULL)
break;
printf ("line : %s\n", line);
*nproc = *nproc +1;
proc[0] = realloc(proc[0], sizeof(struct process*) * (*nproc));
proc[0][*nproc -1] = malloc(sizeof(struct process));
for (str = line, i = 1; ; str = NULL, i++) {
token = strtok_r(str, " ", &saveptr);
if (token == NULL)
break;
if (i == 1) {
proc[0][*nproc -1]->pid = strtoul(token, NULL, 10);
}
else if (i == 2) {
proc[0][*nproc -1]->index = strtoul(token, NULL, 10);
}
else if (i == 3) {
proc[0][*nproc -1]->version = strtoul(token, NULL, 10);
}
}
}
} }
void struct_process_free (struct process * p) void struct_process_free (struct process * p)
@ -121,7 +179,10 @@ void struct_process_free (struct process * p)
void service_free_processes (struct process **procs, int nproc) void service_free_processes (struct process **procs, int nproc)
{ {
while (--nproc != -1) { printf ("free processes\n");
while (nproc--) {
printf ("free process %d\n", nproc);
struct_process_free (procs[nproc]); struct_process_free (procs[nproc]);
} }
} }
@ -135,7 +196,8 @@ void gen_process_structure (struct process *p
} }
void process_print (struct process *p) { void process_print (struct process *p)
{
printf ("process %d : index %d\n", p->pid, p->index); printf ("process %d : index %d\n", p->pid, p->index);
} }
@ -238,6 +300,44 @@ int process_open_in (struct process *proc)
printf ("opening in %s\n", fifopathin); printf ("opening in %s\n", fifopathin);
proc->in = fopen (fifopathin, "rb"); proc->in = fopen (fifopathin, "rb");
if (proc->in == NULL) {
fprintf (stderr, "\033[31mnot opened\033[00m\n");
return -1;
}
printf ("opened : %d\n", proc->in);
return 0;
}
int service_proc_open_in (struct process *proc)
{
char fifopathin[PATH_MAX];
char fifopathout[PATH_MAX];
process_paths (fifopathin, fifopathout, proc->pid, proc->index);
printf ("opening in %s\n", fifopathin);
proc->in = fopen (fifopathin, "wb");
if (proc->in == NULL) {
fprintf (stderr, "\033[31mnot opened\033[00m\n");
return -1;
}
printf ("opened : %d\n", proc->in);
return 0;
}
int service_proc_open_out (struct process *proc)
{
char fifopathin[PATH_MAX];
char fifopathout[PATH_MAX];
process_paths (fifopathin, fifopathout, proc->pid, proc->index);
printf ("opening out %s\n", fifopathout);
proc->out = fopen (fifopathout, "rb");
if (proc->out == NULL) {
fprintf (stderr, "\033[31mnot opened\033[00m\n");
return -1;
}
printf ("opened\n"); printf ("opened\n");
return 0; return 0;
@ -251,22 +351,10 @@ int process_open_out (struct process *proc)
printf ("opening out %s\n", fifopathout); printf ("opening out %s\n", fifopathout);
proc->out = fopen (fifopathout, "wb"); proc->out = fopen (fifopathout, "wb");
printf ("opened\n"); if (proc->out == NULL) {
fprintf (stderr, "\033[31mnot opened\033[00m\n");
return 0; return -1;
} }
int process_open (struct process *proc)
{
char fifopathin[PATH_MAX];
char fifopathout[PATH_MAX];
process_paths (fifopathin, fifopathout, proc->pid, proc->index);
printf ("opening %s\n", fifopathin);
proc->in = fopen (fifopathin, "rb");
printf ("opening %s\n", fifopathout);
proc->out = fopen (fifopathout, "wb");
printf ("opened\n"); printf ("opened\n");
return 0; return 0;
@ -276,7 +364,9 @@ int process_close_in (struct process *proc)
{ {
printf ("closing in\n"); printf ("closing in\n");
if (proc->in != 0) { if (proc->in != 0) {
printf ("before fclose in\n");
fclose (proc->in); fclose (proc->in);
printf ("after fclose in\n");
proc->in = 0; proc->in = 0;
} }
return 0; return 0;
@ -297,15 +387,14 @@ int process_read (struct process *proc, void * buf, size_t * msize)
{ {
int ret; int ret;
if ((ret = process_open_in (proc))) { if ((ret = process_open_in (proc))) {
fprintf(stdout, "error process_create %d\n", ret); return 1;
exit (1);
} }
*msize = fread (buf, 1, *msize, proc->in); // FIXME check errors *msize = fread (buf, 1, *msize, proc->in); // FIXME check errors
// printf ("DEBUG read, size %ld : %s\n", *msize, buf);
if ((ret = process_close_in (proc))) { if ((ret = process_close_in (proc))) {
fprintf(stdout, "error process_close_in %d\n", ret); return 1;
exit (1);
} }
return 0; return 0;
@ -315,31 +404,45 @@ int process_write (struct process *proc, void * buf, size_t msize)
{ {
int ret; int ret;
if ((ret = process_open_out (proc))) { if ((ret = process_open_out (proc))) {
fprintf(stdout, "error process_create %d\n", ret); return 1;
exit (1);
} }
fwrite (buf, 1, msize, proc->out); // FIXME check errors fwrite (buf, 1, msize, proc->out); // FIXME check errors
if ((ret = process_close_out (proc))) { if ((ret = process_close_out (proc))) {
fprintf(stdout, "error process_close_out %d\n", ret); return 1;
exit (1);
} }
return 0; return 0;
} }
int service_read (struct process *proc, void * buf, size_t * msize) int service_read (struct process *proc, void * buf, size_t * msize)
{ {
if ((*msize = fread (buf, 1, *msize, proc->out))) int ret;
return *msize; if ((ret = service_proc_open_out (proc))) {
return 1;
}
*msize = fread (buf, 1, *msize, proc->out); // FIXME check errors
// printf ("DEBUG read, size %ld : %s\n", *msize, buf);
if ((ret = process_close_out (proc))) {
return 1;
}
return 0; return 0;
} }
int service_write (struct process *proc, void * buf, size_t msize) int service_write (struct process *proc, void * buf, size_t msize)
{ {
if ((msize = fwrite (buf, 1, msize, proc->in))) int ret;
return msize; if ((ret = service_proc_open_in (proc))) {
return 1;
}
fwrite (buf, 1, msize, proc->in); // FIXME check errors
if ((ret = process_close_in (proc))) {
return 1;
}
return 0; return 0;
} }

View File

@ -13,7 +13,7 @@
#include <errno.h> // error numbers #include <errno.h> // error numbers
#define TMPDIR "/tmp/" #define TMPDIR "/tmp/ipc/"
#define COMMUNICATION_VERSION 1 #define COMMUNICATION_VERSION 1
@ -27,6 +27,11 @@ struct process {
FILE *in, *out; FILE *in, *out;
}; };
struct service {
unsigned int version;
unsigned int index;
};
// TODO create the service process structure // TODO create the service process structure
int service_path (char *buf, const char *sname); int service_path (char *buf, const char *sname);
@ -44,17 +49,18 @@ void gen_process_structure (struct process *p
int service_create (const char *sname); int service_create (const char *sname);
int service_close (const char *sname); int service_close (const char *sname);
void service_get_new_processes (struct process **, int *nproc, int sfifo); int service_get_new_process (struct process *proc, const char * spath);
void service_get_new_processes (struct process ***, int *nproc, char *spath);
void service_free_processes (struct process **, int nproc); void service_free_processes (struct process **, int nproc);
void process_print (struct process *); void process_print (struct process *);
int process_create (struct process *, int index); // called by the application int process_create (struct process *, int index); // called by the application
int process_destroy (struct process *); // called by the application int process_destroy (struct process *); // called by the application
int process_open (struct process *); // called by the service & application
int process_close (struct process *); // called by the service & application
int process_read (struct process *, void * buf, size_t *); int process_read (struct process *, void * buf, size_t *);
int process_write (struct process *, void * buf, size_t); int process_write (struct process *, void * buf, size_t);
int service_read (struct process *, void * buf, size_t *);
int service_write (struct process *, void * buf, size_t);
#endif #endif

22
pingpong/Makefile Normal file
View File

@ -0,0 +1,22 @@
CC=gcc
CFLAGS=-Wall -g
LDFLAGS=
CFILES=$(wildcard *.c) # CFILES => recompiles everything on a C file change
EXEC=$(basename $(wildcard *.c))
SOURCES=$(wildcard ../lib/*.c)
OBJECTS=$(SOURCES:.c=.o)
TESTS=$(addsuffix .test, $(EXEC))
all: $(SOURCES) $(EXEC)
$(EXEC): $(OBJECTS) $(CFILES)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@
.c.o:
$(CC) -c $(CFLAGS) $< -o $@
clean:
-rm $(OBJECTS)
mrproper: clean
rm $(EXEC)

31
pingpong/README.markdown Normal file
View File

@ -0,0 +1,31 @@
# Service ping-pong
This service is a brain-dead application. It is only to a pedagogic end.
The purpose is only to communicate with an application once, the application
sends a message and the service answer with the same message.
# How it works
* **S**: service
* **A**: application
1. **S** creates the named pipe /tmp/pingpong, then listens
2. **S** opens the named pipes in & out
3. **A** talks with the test program *pingpong.sh*
4. **S** closes the test program named pipes
5. **S** removes the named pipe /tmp/pingpong after 10 served applications
# pingpong.sh
The script *pingpong.sh* lets you test the service.
Usage :
pingpong.sh [NB]
# NB is the number of exchanged messages
or
pingpong.sh clean
# it is to clean the /tmp/ipc/ directory

88
pingpong/pingpong.c Normal file
View File

@ -0,0 +1,88 @@
#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
ret = service_get_new_process (&proc, spath);
if (ret == -1) {
fprintf (stderr, "error service_get_new_process\n");
continue;
} 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");
if ((ret = service_read (&proc, &buf, &msize))) {
fprintf(stdout, "error service_read %d\n", ret);
continue;
}
// printf ("after read\n");
printf ("read, size %ld : %s\n", msize, buf);
// printf ("before proc write\n");
if ((ret = service_write (&proc, &buf, msize))) {
fprintf(stdout, "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);
}
}
/*
* 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];
service_path (spath, "pingpong");
// creates the service named pipe, that listens to client applications
int ret;
if ((ret = service_create (spath))) {
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
if ((ret = service_close (spath))) {
fprintf(stdout, "error service_close %d\n", ret);
exit (1);
}
return EXIT_SUCCESS;
}

37
pingpong/pingpong.sh Executable file
View File

@ -0,0 +1,37 @@
#!/bin/dash
REP=/tmp/ipc/
SERVICE="pingpong"
NB=3
# CLEAN UP !
if [ $# -ne 0 ] && [ "$1" = clean ]
then
echo "clean rep ${REP}"
rm ${REP}/${SERVICE}
rm ${REP}/*-in
rm ${REP}/*-out
exit 0
fi
if [ $# -ne 0 ]
then
NB=$1
fi
for pid in `seq 1 ${NB}`
do
# we make the application pipes
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}
# the purpose is to send something in the pipe
cat /dev/urandom | base64 | head -n 1 > ${REP}/${pid}-1-out
# the the service will answer with our message
cat ${REP}/${pid}-1-in
done

View File

@ -1,84 +0,0 @@
#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;
int sfifo = open (spath, S_IRUSR); // opens the service named pipe
struct process *proc;
int nproc = 0;
do {
service_get_new_processes (&proc, &nproc, sfifo);
printf ("nb proc : %d\n", nproc);
// for each process : open, read, write, close
for (int i = 0 ; i < nproc ; i++) {
process_print (&proc[i]);
// about the message
size_t msize = BUFSIZ;
char buf[BUFSIZ];
if ((ret = process_read (&proc[i], &buf, &msize))) {
fprintf(stdout, "error process_read %d\n", ret);
exit (1);
}
printf ("read, size %ld : %s\n", msize, buf);
if ((ret = process_write (&proc[i], &buf, msize))) {
fprintf(stdout, "error process_read %d\n", ret);
exit (1);
}
}
service_free_processes (&proc, nproc);
} while (0); // it's a test, we only do it once
close (sfifo); // closes the service named pipe
}
/*
* service test
*
* 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];
service_path (spath, "windows");
// creates the service named pipe, that listens to client applications
int ret;
if ((ret = service_create (spath))) {
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
if ((ret = service_close (spath))) {
fprintf(stdout, "error service_close %d\n", ret);
exit (1);
}
return EXIT_SUCCESS;
}