Obsolete
/
libipc-old
Archived
3
0
Fork 0

struct array_proc adapted pong

more_to_read
lapupe 2016-12-20 23:36:00 +01:00
parent 0419ab4645
commit 341382a2e5
7 changed files with 223 additions and 211 deletions

View File

@ -0,0 +1,31 @@
#include "../../core/process.h"
#include <string.h> /* memset */
#include <stdio.h>
int main() {
int ret;
struct array_proc tab_proc;
memset(&tab_proc, 0, sizeof(struct array_proc));
struct process process_tab[5];
memset(&process_tab, 0, sizeof(struct process) * 5);
int i;
for (i = 0; i < 5; i++) {
process_tab[i].proc_fd = i;
ret = add_proc(&tab_proc, &process_tab[i]);
if (ret == -1) {
printf("erreur realloc\n");
}
}
array_proc_print(&tab_proc);
ret = del_proc(&tab_proc, &process_tab[2]);
if(ret < 0) {
printf("erreur %d\n", ret );
}
array_proc_print(&tab_proc);
return 0;
}

View File

@ -7,6 +7,7 @@
#include <stdio.h>
#include <errno.h>
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
@ -140,3 +141,72 @@ int app_write (struct service *srv, char * buf, size_t msize)
{
return usock_send (srv->service_fd, buf, msize);
}
/*prendre en parametre un tableau de process.
* trouver le processus/service actif et renvoyer CONNECTION/APPLICATION
* si un processus il va etre placer dans proc
* si un service il va etre placer dans service.
*/
int srv_select(struct array_proc *ap, struct service *srv, struct process **proc) {
int i, j;
/* master file descriptor list */
fd_set master;
fd_set readf;
/* maximum file descriptor number */
int fdmax;
/* listening socket descriptor */
int listener = srv->service_fd;
/* clear the master and temp sets */
FD_ZERO(&master);
FD_ZERO(&readf);
/* add the listener to the master set */
FD_SET(listener, &master);
for(i=0; i < ap->size; i++) {
FD_SET(ap->tab_proc[i]->proc_fd, &master);
}
/* keep track of the biggest file descriptor */
fdmax = getMaxFd(ap) > srv->service_fd ? getMaxFd(ap) : srv->service_fd;
while (1) {
readf = master;
if(select(fdmax+1, &readf, NULL, NULL, NULL) == -1)
{
perror("Server-select() error lol!");
exit(1);
}
/*run through the existing connections looking for data to be read*/
for (i = 0; i <= fdmax; i++) {
if (FD_ISSET(i, &readf)) {
if (i == listener) {
return CONNECTION;
}else {
for(j = 0; j < ap->size; j++) {
if(i == ap->tab_proc[j]->proc_fd ) {
*proc = ap->tab_proc[j];
return APPLICATION;
}
}
}
}
}
}
}
/*calculer le max filedescriptor*/
int getMaxFd(struct array_proc *ap) {
int i;
int max = 0;
for (i = 0; i < ap->size; i++ ) {
if (ap->tab_proc[i]->proc_fd > max) {
max = ap->tab_proc[i]->proc_fd;
}
}
return max;
}

View File

@ -23,6 +23,9 @@
#define PATH_MAX BUFSIZ
#define CONNECTION 0
#define APPLICATION 1
struct service {
unsigned int version;
unsigned int index;
@ -44,6 +47,10 @@ int srv_accept (struct service *srv, struct process *p);
int srv_read (const struct process *, char **buf, size_t *msize);
int srv_write (const struct process *, const char * buf, size_t);
int srv_select(struct array_proc *, struct service *, struct process **);
int getMaxFd(struct array_proc *);
// APPLICATION
// Initialize connection with unix socket
@ -56,4 +63,6 @@ int app_close (struct service *);
int app_read (struct service *srv, char ** buf, size_t *msize);
int app_write (struct service *, char * buf, size_t msize);
#endif

View File

@ -1,4 +1,7 @@
#include "process.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
// TODO
// tout revoir ici
@ -27,9 +30,54 @@ void srv_process_gen (struct process *p
p->index = index;
}
void srv_process_print (struct process *p)
#endif
int add_proc(struct array_proc *aproc, struct process *p) {
assert(aproc != NULL);
assert(p != NULL);
aproc->size++;
aproc->tab_proc = realloc(aproc->tab_proc, sizeof(struct process) * aproc->size);
if (aproc->tab_proc == NULL) {
return -1;
}
aproc->tab_proc[aproc->size - 1] = p;
return 0;
}
int del_proc(struct array_proc *aproc, struct process *p) {
assert(aproc != NULL);
assert(p != NULL);
int i;
for (i = 0; i < aproc->size; i++) {
if (aproc->tab_proc[i] == p) {
aproc->tab_proc[i] = aproc->tab_proc[aproc->size-1];
aproc->size--;
aproc->tab_proc = realloc(aproc->tab_proc, sizeof(struct process) * aproc->size);
if (aproc->tab_proc == NULL) {
return -1;
}
return 0;
}
}
return -2;
}
void process_print (struct process *p)
{
if (p != NULL)
printf ("process %d : index %d, version %d\n", p->index, p->version);
printf ("process %d : index %d, version %d\n", p->proc_fd, p->index, p->version);
}
#endif
void array_proc_print( struct array_proc *ap) {
int i;
for (i = 0; i < ap->size; i++) {
printf("%d : ", i);
process_print(ap->tab_proc[i]);
}
}

View File

@ -7,8 +7,18 @@ struct process {
int proc_fd;
};
struct array_proc {
struct process **tab_proc;
int size;
};
// TODO
// tout revoir
int add_proc(struct array_proc *, struct process *);
int del_proc(struct array_proc *, struct process *);
void array_proc_print(struct array_proc *);
#if 0

View File

@ -1,17 +1,16 @@
CC=gcc
CFLAGS=-Wall -g
CFLAGS=-Wall -g -Wextra
LDFLAGS= -pthread
CFILES=$(wildcard *.c) # CFILES => recompiles everything on a C file change
EXEC=$(basename $(wildcard *.c))
SOURCES=$(wildcard ../lib/communication.c ../lib/process.c)
SOURCES=$(wildcard ../lib/*.c ../../core/*.c)
OBJECTS=$(SOURCES:.c=.o)
TESTS=$(addsuffix .test, $(EXEC))
LCBOR=-lbor
all: $(SOURCES) $(EXEC)
$(EXEC): $(OBJECTS) $(CFILES)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@.bin
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -lcbor -o $@.bin
.c.o:
$(CC) -c $(CFLAGS) $< -o $@

View File

@ -1,51 +1,14 @@
#include "../../lib/communication.h"
#include <pthread.h>
#include "../../core/communication.h"
#include <sys/socket.h>
#include <sys/un.h>
#include "../../core/process.h"
#include <unistd.h>
#define PONGD_SERVICE_NAME "pongd"
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
/* control the file descriptor*/
void * pongd_thread(void * pdata) {
//struct process *proc = (struct process*) pdata;
int *sockclient = (int*) pdata;
// about the message
char *buf = malloc(BUFSIZ);
if (buf == NULL)
{
handle_error("malloc");
}
memset(buf, 0, BUFSIZ);
int nbytes;
// init unix socket
while (1) {
if ((nbytes = file_read (*sockclient, &buf)) == -1) {
fprintf(stdout, "MAIN_LOOP: error service_read %d\n", nbytes);
}
if (nbytes == 0 || strncmp ("exit", buf, 4) == 0){
printf("------thread shutdown------------\n");
//close(cfd);
close(*sockclient);
free(buf);
break;
}else {
printf ("read, size %d : %s\n", nbytes, buf);
if ((nbytes = file_write (*sockclient, buf, nbytes)) == -1) {
fprintf(stdout, "MAIN_LOOP: error service_write %d\n", nbytes);
}
}
}
return NULL;
}
/*
* main loop
@ -57,169 +20,53 @@ void * pongd_thread(void * pdata) {
void main_loop (struct service *srv)
{
int ret;
struct process tab_proc[10];
//thread
pthread_t tab_thread[10];
int cnt = 0;
//init socket unix for server
int sfd;
struct sockaddr_un peer_addr;
socklen_t peer_addr_size;
sfd = set_listen_socket(srv->spath);
if (sfd == -1){
handle_error("set_listen_socket");
size_t msize = BUFSIZ;
char *buf = NULL;
if ( (buf = malloc (BUFSIZ)) == NULL) {
handle_error ("malloc");
}
memset (buf, 0, BUFSIZ);
/* master file descriptor list */
fd_set master;
/* temp file descriptor list for select() */
fd_set read_fds;
int i,ret, cpt = 0;
/* maximum file descriptor number */
int fdmax;
/* listening socket descriptor */
int listener = sfd;
/* newly accept()ed socket descriptor */
int newfd;
/* buffer for client data */
char *buf = malloc(BUFSIZ);
if (buf == NULL)
{
handle_error("malloc");
}
memset(buf, 0, BUFSIZ);
struct array_proc ap;
memset(&ap, 0, sizeof(struct array_proc));
int nbytes;
struct process *p2 = malloc(sizeof(struct process));
int i;
/* clear the master and temp sets */
FD_ZERO(&master);
FD_ZERO(&read_fds);
/* add the listener to the master set */
FD_SET(listener, &master);
//FD_SET(sfd, &master);
/* keep track of the biggest file descriptor */
fdmax = sfd; /* so far, it's this one*/
for(;;) {
/* copy it */
read_fds = master;
if(select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1)
{
perror("Server-select() error lol!");
exit(1);
}
//printf("Server-select...OK\n");
/*run through the existing connections looking for data to be read*/
for(i = 0; i <= fdmax; i++) {
if(FD_ISSET(i, &read_fds)) {
/* we got one... */
if(i == listener) {
/* handle new connections */
peer_addr_size = sizeof(struct sockaddr_un);
newfd = accept(sfd, (struct sockaddr *) &peer_addr, &peer_addr_size);
if (newfd == -1) {
handle_error("accept");
}
else
{
printf("Server-accept() is OK...\n");
//FD_SET(newfd, &master); /* add to master set */
//if(newfd > fdmax)
//{ /* keep track of the maximum */
//fdmax = newfd;
//}
nbytes = file_read (newfd, &buf);
if ( nbytes == -1) {
handle_error("file_read");
} else {
buf[BUFSIZ - 1] = '\0';
printf ("msg received (%d) : %s\n", nbytes, buf);
if (strncmp ("exit", buf, 4) == 0) {
break;
}
// -1 : error, 0 = no new process, 1 = new process
ret = srv_get_new_process (buf, &tab_proc[cnt]);
if (ret == -1) {
fprintf (stderr, "MAIN_LOOP: error service_get_new_process\n");
continue;
}
srv_process_print (&tab_proc[cnt]);
int ret = pthread_create( &tab_thread[cnt], NULL, &pongd_thread, (void *) &newfd);
if (ret) {
perror("pthread_create()");
exit(errno);
} else {
printf ("\n-------New thread created---------\n");
}
printf ("%d applications to serve\n",cnt);
cnt++;
}
}
/*else {
nbytes = file_read (i, &buf);
if ( nbytes == -1) {
handle_error("file_read");
} else if( nbytes == 0) {*/
/* close it... */
//close(i);
/* remove from master set */
/*FD_CLR(i, &master);
}else {
buf[BUFSIZ - 1] = '\0';
printf ("msg received (%d) : %s\n", nbytes, buf);
if (strncmp ("exit", buf, 4) == 0) {
break;
}
// -1 : error, 0 = no new process, 1 = new process
ret = srv_get_new_process (buf, &tab_proc[cnt]);
if (ret == -1) {
fprintf (stderr, "MAIN_LOOP: error service_get_new_process\n");
continue;
}
srv_process_print (&tab_proc[cnt]);
int ret = pthread_create( &tab_thread[cnt], NULL, &pongd_thread, (void *) &i);
if (ret) {
perror("pthread_create()");
exit(errno);
} else {
printf ("\n-------New thread created---------\n");
}
printf ("%d applications to serve\n",cnt);
cnt++;
}*/
}
while(cpt < 5) {
ret = srv_select(&ap, srv, &p2);
if (ret == CONNECTION) {
struct process *p = malloc(sizeof(struct process));
memset(p, 0, sizeof(struct process));
if (srv_accept (srv, p) < 0) {
handle_error("srv_accept < 0");
}else {
printf("new connection\n");
}
if (add_proc(&ap, p) < 0) {
handle_error("add_proc < 0");
}
cpt++;
} else {
if (srv_read(p2, &buf, &msize) < 0) {
handle_error("srv_read < 0");
}
if (srv_write (p2, buf, msize) < 0) {
handle_error("srv_write < 0");
}
}
if (strncmp ("exit", buf, 4) == 0) {
break;
}
}
for (i = 0; i < ap.size; i++) {
if (srv_close_proc (ap.tab_proc[i]) < 0) {
handle_error( "srv_close_proc < 0");
}
}
for (i = 0; i < cnt; i++) {
pthread_join(tab_thread[i], NULL);
}
free(buf);
close(sfd);
}
@ -237,26 +84,24 @@ int main(int argc, char * argv[], char **env)
{
struct service srv;
memset (&srv, 0, sizeof (struct service));
srv->index = 0;
srv->version = 0;
srv_init (argc, argv, env, &srv, PONGD_SERVICE_NAME, NULL);
srv.index = 0;
srv.version = 0;
unlink("/tmp/ipc/pongd-0-0");
if (srv_init (argc, argv, env, &srv, PONGD_SERVICE_NAME) < 0) {
handle_error("srv_init < 0");
return EXIT_FAILURE;
}
printf ("Listening on %s.\n", srv.spath);
// creates the service named pipe, that listens to client applications
int ret;
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
main_loop (&srv);
// the application will shut down, and remove the service named pipe
if ((ret = srv_close (&srv))) {
fprintf(stdout, "error service_close %d\n", ret);
exit (1);
if (srv_close (&srv) < 0) {
handle_error("srv_close < 0");
}
return EXIT_SUCCESS;