Old stuff/ecole_etude_fac_de_pau/licence_3/projet_commun_se/rpc/client.c
(Deskargatu)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <string.h>
#include <poll.h>
#include <rpc/xdr.h>
#include "include/tcp.h"
#include "include/global.h"
#include "include/demandes.h"
#include "include/sockets.h"
#include "include/gui.h"
#include "include/html.h"
int main(int argc, char** argv)
{
clearscreen();
setpos(1,1);
int connecte=1;
printred();
setbold();
printf("barman client ...\n");
printnormal();
setnormal();
printf("Loading tools ...\n");
// printf("Client %d\n",sock);
sock = con_tcp(4000,"localhost");
clients = malloc(10*sizeof(int));
clients[0] = gen_tcp(8080);
clients[1] = gen_udp(4002,"127.0.0.1");
fd_set sockets;
int nb =2;
int tempo=0;
char buffer[500];
char* barman_msg = (char*)malloc(500*sizeof(char));
nb_clients= &nb;
demande_item table;
table.demi_blonde=0;
table.demi_brune=0;
table.peinte_blonde=0;
table.peinte_brune=0;
int lu=0;
int restant =0;
clearscreen();
XDR reception;
retour_item ret;
reception.x_op=XDR_DECODE;
xdrrec_create(&reception, 0, 0, (void*)sock, (void*)read_tcp, (void*)write_tcp);
// while(kill(getppid(),0) != -1)
while(1)
{
// write_udp(clients[1],4002,"127.0.0.1","test",4*sizeof(char));
select_client(&sockets);
if (FD_ISSET(sock,&sockets))
{
struct pollfd polld;
polld.revents=0;
polld.fd=sock;
if (poll(&polld,1,1) == -1)
{
perror("error check socket close (poll)");
}
if ((polld.revents & POLLHUP) == POLLHUP ||
(polld.revents & POLLNVAL) == POLLNVAL ||
(polld.revents & POLLERR) == POLLERR
)
{
connecte=0;
}
else
{
if ((lu =recv(sock, &tempo, 1, MSG_PEEK)) == -1)
{
perror("client.c: recv error");
}
if ( lu == 0) {
connecte=0;
}
else
{
#ifdef DEBUG
printf("Socket de lecture %d\n",sock);
int op=0;
#endif
if (!xdrrec_skiprecord(&reception)) { perror("client.c skiprecord"); exit(-1); }
if (!xdr_int(&reception,&op)) { perror("clients.c xdr_demande"); exit(-1); }
switch(op)
{
case 11:
if (!xdr_retour(&reception,&ret)) { perror("clients.c xdr_demande"); exit(-1); }
setpos(2,1);
printf("Recu le retour %d %d %d %d\n",ret.demi_blonde, ret.demi_brune, ret.peinte_blonde, ret.peinte_brune);
strcpy(barman_msg,"Recu les/la biere(s)");
printf("Recu %s\n",barman_msg);
table.demi_blonde += ret.demi_blonde;
table.demi_brune += ret.demi_brune;
table.peinte_blonde += ret.peinte_blonde;
table.peinte_brune += ret.peinte_brune;
break;
case 12:
printf("Operation 12\n");
// if(!xdr_vector(&reception,barman_msg,500*sizeof(char),sizeof(char),0)) { perror("client.c: xdr_string"); exit(-1); }
// printf("Recu %s\n",barman_msg);
default: break;
}
}
}
}
if (FD_ISSET(clients[1],&sockets))
{
bzero(buffer, 500*sizeof(char));
int tempo=1;
if ((lu=recv(clients[1],buffer,500*sizeof(char),0)) == -1)
{
perror("client.c: read socket error");
exit(-1);
}
printf("Trouve qqch sur l udp: %s\n",buffer);
if (sscanf(buffer,"tournee %d clients",&restant)== 0)
{
if(strncmp(buffer,"je leve le verre",16*sizeof(char))==0){ restant -= tempo; }
}
else
{
write_udp(clients[1], 4002, "127.0.0.1", "je leve le verre",16*sizeof(char));
}
if (restant ==0)
{
clearscreen();
setpos(5,5);
printf(" Tchiiiiiiiiiiiiiin ! (ensemble)\n\n\n\n");
}
printf("RESTANT %d\n",restant);
}
int i=0;
for (i=2;i<*nb_clients;i++)
{
char nulle[500];
lu=0;
if(FD_ISSET(clients[i],&sockets))
{
if ((lu =recv(clients[i], &tempo, 1, MSG_PEEK)) == -1)
{
perror("client.c: recv error");
}
if ( lu == 0) {
printf("Fin du message ...\n");
close(clients[i]);
clients[i] = clients[*nb_clients-1];
*nb_clients = (*nb_clients) - 1;
}
else
{
int op=0;
char buffer[500];
bzero(buffer,500*sizeof(char));
if ((lu=recv(clients[i],buffer,500*sizeof(char),0)) == -1)
{
perror("client.c: read socket error");
exit(-1);
}
if (strncmp("GET /1?",buffer,7*sizeof(char))==0)
{
printf("On a lue une demande ...\n");
op=1;
}
else if (strncmp("GET /2?",buffer,7*sizeof(char))==0)
{
printf("On a lue une tournee...\n");
op=2;
}
else if (strncmp("GET /commander.html",buffer,19*sizeof(char))==0)
{
printf("commander \n");
op=3;
}
else if (strncmp("GET /tournee.html",buffer,17*sizeof(char))==0)
{
printf("commander \n");
op=4;
}
else if (strncmp("GET /toast.html",buffer,15*sizeof(char))==0)
{
printf("commander \n");
op=5;
}
else { op = 0; }
while(lu==500)
{
if ((lu=read(clients[i],nulle,500*sizeof(char))) == -1)
{
perror("client.c: read socket error");
exit(-1);
}
}
if (connecte==1)
{
switch(op)
{
case 0:
writeindex(clients[i],table);
printf("Apres write ...\n");
break;
case 1:
case 2:
printf("writedemande\n");
writedemande(buffer,clients[i]);
break;
case 3:
printf("writecommander\n");
writecommander(clients[i]);
break;
case 4:
printf("writetourneer\n");
writetourner(clients[i]);
break;
case 5:
printf("Toast html");
writetoast(clients[i]);
}
}
else
{
writealarm(clients[i]);
}
}
}
}
if(FD_ISSET(clients[0],&sockets))
{
printf("On a trouve un nouveau client ... (%d nbclients)\n",*nb_clients);
clients[*nb_clients] = accept_tcp(clients[0]);
*nb_clients = (*nb_clients)+1;
printf("Client[0]...\n");
}
}
}
// return 0;
//}