sysdis-tp/RPC/server.c

100 lines
1.7 KiB
C
Raw Normal View History

2017-02-14 08:31:49 +00:00
#include "server.h"
void traiterClient(int socket){
requete req;
int nb_octet;
int fini=0;
2017-02-22 11:35:41 +00:00
char i = 0;
2017-02-14 08:31:49 +00:00
while(!fini){
2017-02-22 11:35:41 +00:00
nb_octet = read(socket,&req,sizeof(requete));
2017-02-14 08:31:49 +00:00
IF_NB_OCTET_BREAK
2017-02-22 11:35:41 +00:00
if(nb_octet == 0){
break;
}
DEBUG&& printf("Request received (operation/size) : (%d/%d)\n",req.type,req.taille);
2017-02-14 08:31:49 +00:00
switch(req.type){
case FIN:
{
fini = 1;
break;
}
case FACTO:
{
int nb;
long res;
2017-02-22 11:35:41 +00:00
DEBUG&& debug("loop","FACTO asked");
nb_octet = read(socket,&nb,sizeof(int));
2017-02-14 08:31:49 +00:00
IF_NB_OCTET_BREAK
2017-02-22 11:35:41 +00:00
DEBUG&& printf("%d\n",nb);
2017-02-14 08:31:49 +00:00
res = factoriel(nb);
2017-02-22 11:35:41 +00:00
DEBUG&& debug("loop","Sending result");
2017-02-14 08:31:49 +00:00
nb_octet = write(socket, &res, sizeof(res));
IF_NB_OCTET_BREAK
2017-02-22 11:35:41 +00:00
DEBUG&& printf("done\n");
2017-02-14 08:31:49 +00:00
break;
}
case POW:
{
int a,b;
long res;
nb_octet = read(socket, &a, sizeof(int));
IF_NB_OCTET_BREAK
nb_octet = read(socket, &b, sizeof(int));
IF_NB_OCTET_BREAK
res = puissance(a,b);
nb_octet = write(socket, &res, sizeof(res));
IF_NB_OCTET_BREAK
break;
}
case ANALYSE:
{
int nb_elem, attendu;
int* donnees;
char message[MAX_BUFFER_LENGTH];
res_analyse_donnees res;
nb_elem = req.taille/sizeof(int);
donnees = (int*) malloc(nb_elem*sizeof(int));
attendu = req.taille;
while(attendu > 0){
nb_octet = read(socket, message, MAX_BUFFER_LENGTH);
IF_NB_OCTET_BREAK
memcpy(donnees+(req.taille - attendu), message,nb_octet);
attendu -= nb_octet;
}
2017-02-22 11:35:41 +00:00
analyser_donnees(donnees,nb_elem,&res);
2017-02-14 08:31:49 +00:00
write(socket, &res, sizeof(res));
free (donnees);
break;
}
}
}
}