Tfe

Ongi etorri tfe-ren webgunera...

Old stuff/ecole_etude_fac_de_pau/licence_3/projet_commun_se/backup20/client.c

(Deskargatu)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <signal.h>
#include <sys/types.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);
    fd_set sockets;
    int nb =1;
    int tempo=0;
    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;
    
    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)
	{
	    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);
			#endif
			if (!xdrrec_skiprecord(&reception)) { perror("client.c skiprecord"); exit(-1); }
			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);
			table.demi_blonde += ret.demi_blonde;
			table.demi_brune += ret.demi_brune;		
			table.peinte_blonde += ret.peinte_blonde;
			table.peinte_brune += ret.peinte_brune;
		    }
		}
	    }
	    int i=0;
	    for (i=1;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");
			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);
			}
			printf("On a lu %s\n",buffer);
    	    	        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 { 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)
			{
			    if (op==0) 
			    {
				writeindex(clients[i],table);
				printf("Apres write ...\n");
			    }
			    else if (op==1 || op==2)
			    {
				printf("writedemande\n");
				writedemande(buffer,clients[i]);			
			    }
			    else if (op==3)
			    {
			    	printf("writecommander\n");
				writecommander(clients[i]);
			    }
			    else if (op==4)
			    {
				printf("writetourneer\n");			    
				writetourner(clients[i]);
			    }
			    
			    
			}
			else
			{
			    writealarm(clients[i]);
			}
		    }
		}
	    }
	    if(FD_ISSET(clients[0],&sockets))
	    {
		printf("On a trouve un nouveau client ...\n");
		clients[*nb_clients] = accept_tcp(clients[0]);
		*nb_clients = (*nb_clients)+1;
		printf("Client[0]...\n");
	
	    }
	}    
    }
//    return 0;
//}