diff --git a/RPC/client.c b/RPC/client.c new file mode 100644 index 0000000..e69de29 diff --git a/RPC/server.c b/RPC/server.c new file mode 100644 index 0000000..6d8350d --- /dev/null +++ b/RPC/server.c @@ -0,0 +1,84 @@ +#include "server.h" + +void traiterClient(int socket){ + + requete req; + int nb_octet; + int fini=0; + + while(!fini){ + + nb_octet = read(socket,&req,sizeof(req)); + IF_NB_OCTET_BREAK + + switch(req.type){ + + case FIN: + { + fini = 1; + break; + } + + case FACTO: + { + + int nb; + long res; + + nb_octet = read(socket, &nb, sizeof(int)); + IF_NB_OCTET_BREAK + + res = factoriel(nb); + nb_octet = write(socket, &res, sizeof(res)); + IF_NB_OCTET_BREAK + + 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; + } + + analyse(donnees,nb_elem,&res); + write(socket, &res, sizeof(res)); + free (donnees); + + break; + } + } + + } + +} diff --git a/RPC/server.h b/RPC/server.h new file mode 100644 index 0000000..abadb6f --- /dev/null +++ b/RPC/server.h @@ -0,0 +1,16 @@ +#ifndef _RPCSERVER_H_ + + #define _RPCSERVER_H_ + + #include + #include + #include + #include "../calculs.h" + #include "../lib.h" + + #define IF_NB_OCTET_BREAK if(nb_octet == -1){fini = 1; break;} + #define MAX_BUFFER_LENGTH 200 + + void traiterClient(int socket); + +#endif diff --git a/calculs.c b/calculs.c new file mode 100644 index 0000000..b5f17f8 --- /dev/null +++ b/calculs.c @@ -0,0 +1,41 @@ +#include "calculs.h" + +long factoriel(int nb) +{ + int i; + long res; + res = 1; + for (i=nb;i>1;i--) + res = res * i; + return res; +} + + +void analyser_donnees(int donnees[], int taille, res_analyse_donnees *res) +{ + int i; + long somme; + + res -> min = donnees[0]; + res -> max = donnees[0]; + somme = donnees[0]; + + for (i=1; i < taille; i++) + { + if (donnees[i] > res -> max) res -> max = donnees[i]; + if (donnees[i] < res -> min) res -> min = donnees[i]; + somme += donnees[i]; + } + res -> moy = ((float)somme) / taille; +} + +long puissance(int nb, int puiss) +{ + long res = nb; + int i; + for (i=1; i < puiss; i++) + res = res * nb; + return res; +} + + diff --git a/calculs.h b/calculs.h new file mode 100644 index 0000000..28e0ba8 --- /dev/null +++ b/calculs.h @@ -0,0 +1,15 @@ +#ifndef _CALCULS_H_ + + typedef struct { + float moy; + int max; + int min; + }res_analyse_donnees; + + long factoriel(int nb); + + void analyser_donnees(int donnees[], int taille, res_analyse_donnees *res); + + long puissance(int nb, int puiss); + +#endif diff --git a/lib.h b/lib.h index ff7423c..e31a55b 100644 --- a/lib.h +++ b/lib.h @@ -37,4 +37,23 @@ int xread(const int xsocket, char* buffer, int bufsize); int xwrite(const int xsocket, char* buffer); + + + /************************************************ + **** Structs **** + ************************************************/ + + typedef enum + { + FIN=0, + FACTO=1, + POW=2, + ANALYSE=3 + }requete_t; + + typedef struct { + requete_t type; + int taille; + }requete; + #endif diff --git a/makefile b/makefile index 7ca2720..cf86cc3 100644 --- a/makefile +++ b/makefile @@ -10,12 +10,17 @@ server.o: lib.h server.h server.c lib.o: lib.h lib.c gcc $(CC) -c -o lib.o lib.c -server: lib.o server.o - gcc $(CC) -o server lib.o server.o +server: lib.o server.o serverRPC.o + gcc $(CC) -o server lib.o server.o serverRPC.o client: lib.o client.o gcc $(CC) -o client lib.o client.o +clientRPC.o: calculs.h RPC/client.c + gcc $(CC) -c -o clientRPC.o RPC/client.c + +serverRPC.o: RPC/server.c RPC/server.h calculs.h + gcc $(CC) -c -o serverRPC.o RPC/server.c