Implémentation du RPC coté serveur

This commit is contained in:
MASCARO LUCAS 2017-02-14 09:31:49 +01:00
parent fa6265f3d0
commit 4b8ae83e1f
7 changed files with 182 additions and 2 deletions

0
RPC/client.c Normal file
View File

84
RPC/server.c Normal file
View File

@ -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;
}
}
}
}

16
RPC/server.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef _RPCSERVER_H_
#define _RPCSERVER_H_
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#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

41
calculs.c Normal file
View File

@ -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;
}

15
calculs.h Normal file
View File

@ -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

19
lib.h
View File

@ -37,4 +37,23 @@
int xread(const int xsocket, char* buffer, int bufsize); int xread(const int xsocket, char* buffer, int bufsize);
int xwrite(const int xsocket, char* buffer); 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 #endif

View File

@ -10,12 +10,17 @@ server.o: lib.h server.h server.c
lib.o: lib.h lib.c lib.o: lib.h lib.c
gcc $(CC) -c -o lib.o lib.c gcc $(CC) -c -o lib.o lib.c
server: lib.o server.o server: lib.o server.o serverRPC.o
gcc $(CC) -o server lib.o server.o gcc $(CC) -o server lib.o server.o serverRPC.o
client: lib.o client.o client: lib.o client.o
gcc $(CC) -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