diff --git a/core-test/app/array_proc.c b/core-test/app/array_proc.c index 3e1e51e..0d64685 100644 --- a/core-test/app/array_proc.c +++ b/core-test/app/array_proc.c @@ -2,30 +2,33 @@ #include /* memset */ #include -int main() { - int ret; - struct array_proc tab_proc; - memset(&tab_proc, 0, sizeof(struct array_proc)); +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); + 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"); - } - } + 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); + 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); + ret = del_proc(&tab_proc, &process_tab[2]); + if(ret < 0) { + printf("erreur %d\n", ret ); + } + array_proc_print(&tab_proc); - return 0; -} \ No newline at end of file + array_proc_free (&tab_proc); + + return 0; +} diff --git a/core/communication.c b/core/communication.c index 7f384b1..b754b1f 100644 --- a/core/communication.c +++ b/core/communication.c @@ -151,12 +151,22 @@ int app_write (struct service *srv, const struct msg *m) } -/*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) { +/* + * srv_select prend en parametre + * * un tableau de process qu'on écoute + * * le service qui attend de nouvelles connexions + * * un tableau de process qui souhaitent parler + * + * la fonction trouve le processus/service actif et renvoie + * un entier correspondant à quel descripteur de fichier il faut lire + * * celui du serveur = nouvelle connexion entrante (CONNECTION) + * * celui d'un ou plusieurs processus = ils nous parlent (APPLICATION) + * * les deux à la fois (CON_APP) + */ + +int srv_select (struct array_proc *ap, struct service *srv + , struct process **proc) +{ int i, j; /* master file descriptor list */ fd_set master; @@ -182,10 +192,9 @@ int srv_select(struct array_proc *ap, struct service *srv, struct process **proc while (1) { readf = master; - if(select(fdmax+1, &readf, NULL, NULL, NULL) == -1) - { - perror("Server-select() error lol!"); - exit(1); + if(select(fdmax+1, &readf, NULL, NULL, NULL) == -1) { + perror("select"); + return -1; } /*run through the existing connections looking for data to be read*/ @@ -193,7 +202,7 @@ int srv_select(struct array_proc *ap, struct service *srv, struct process **proc if (FD_ISSET(i, &readf)) { if (i == listener) { return CONNECTION; - }else { + } else { for(j = 0; j < ap->size; j++) { if(i == ap->tab_proc[j]->proc_fd ) { *proc = ap->tab_proc[j]; @@ -207,9 +216,12 @@ int srv_select(struct array_proc *ap, struct service *srv, struct process **proc } /*calculer le max filedescriptor*/ -int getMaxFd(struct array_proc *ap) { +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; diff --git a/core/msg.c b/core/msg.c index 5d82d01..f5d1586 100644 --- a/core/msg.c +++ b/core/msg.c @@ -86,9 +86,6 @@ int msg_read (int fd, struct msg *m) } free (buf); - // printf ("msg received: "); - // print_msg (m); - return 0; } @@ -96,14 +93,10 @@ int msg_write (int fd, const struct msg *m) { assert (m != NULL); - // printf ("msg to write: "); - // print_msg (m); - char *buf = NULL; size_t msize = 0; msg_format_write (m, &buf, &msize); - // printf ("msg to send, real size %ld\n", msize); int ret = usock_send (fd, buf, msize); if (ret < 0) { handle_err ("msg_write", "usock_send"); diff --git a/core/msg.h b/core/msg.h index da809e0..c2c904b 100644 --- a/core/msg.h +++ b/core/msg.h @@ -13,7 +13,7 @@ struct msg { char type; - short valsize; + unsigned short valsize; char *val; }; diff --git a/core/process.c b/core/process.c index 68c8843..507a3ac 100644 --- a/core/process.c +++ b/core/process.c @@ -33,11 +33,14 @@ void srv_process_gen (struct process *p #endif -int add_proc(struct array_proc *aproc, struct process *p) { +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); + aproc->tab_proc = realloc(aproc->tab_proc + , sizeof(struct process) * aproc->size); + if (aproc->tab_proc == NULL) { return -1; } @@ -46,16 +49,20 @@ int add_proc(struct array_proc *aproc, struct process *p) { return 0; } -int del_proc(struct array_proc *aproc, struct process *p) { +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); + aproc->tab_proc = realloc(aproc->tab_proc + , sizeof(struct process) * aproc->size); + if (aproc->tab_proc == NULL) { return -1; } @@ -69,10 +76,12 @@ int del_proc(struct array_proc *aproc, struct process *p) { void process_print (struct process *p) { if (p != NULL) - printf ("process %d : index %d, version %d\n", p->proc_fd, p->index, p->version); + printf ("process %d : index %d, version %d\n" + , p->proc_fd, p->index, p->version); } -void array_proc_print( struct array_proc *ap) { +void array_proc_print (struct array_proc *ap) +{ int i; for (i = 0; i < ap->size; i++) { printf("%d : ", i); @@ -80,4 +89,8 @@ void array_proc_print( struct array_proc *ap) { } } - +void array_proc_free (struct array_proc *ap) +{ + if (ap->tab_proc != NULL) + free (ap->tab_proc), ap->tab_proc = NULL; +} diff --git a/core/process.h b/core/process.h index 7c36bbe..f6ed0fe 100644 --- a/core/process.h +++ b/core/process.h @@ -18,7 +18,8 @@ int add_proc(struct array_proc *, struct process *); int del_proc(struct array_proc *, struct process *); -void array_proc_print(struct array_proc *); +void array_proc_print (struct array_proc *); +void array_proc_free (struct array_proc *); #if 0