Tfe

Ongi etorri tfe-ren webgunera...

Old stuff/ecole_etude_fac_de_pau/licence_3/projet_commun_se/backup19/oldclient.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 <rpc/xdr.h>

#include "include/tcp.h"
#include "include/global.h"
#include "include/demandes.h"
#include "include/sockets.h"
#include "include/gui.h"


int main(int argc, char** argv)
{
/*   clearscreen();

    setpos(1,1);
    printred();
    setbold();    
    printf("barman client ...\n");
    
    printnormal();
    setnormal();
    printf("Loading tools ...\n");
*/    
    sock = con_tcp(4000,"localhost");
//    printf("Client %d\n",sock);
    
    
    
    fd_set sockets;
    
    

    
    pid_t pid;
    clearscreen();
    
    if((pid = fork()) == -1) { perror("client fork error"); exit(-1); }
    if (pid == 0)
    {
	while(1) 
	{
	printf("Check de data...\n");
	select_client(&sockets);
	if (FD_ISSET(sock,&sockets))
	{
	    XDR reception;
	    retour_item ret;
	    reception.x_op=XDR_DECODE;
	    #ifdef DEBUG
	    printf("Socket de lecture %d\n",sock);
	    #endif
	    if (check_fermeture_client(sock))
	    {
	        exit(-1);
	    }
	    xdrrec_create(&reception, 0, 0, (void*)sock, (void*)read_tcp, (void*)write_tcp);
	    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);
	}
	}    
    }
    else 
    {
    demande_item dem;
    
    XDR data;
    data.x_op=XDR_ENCODE;
    xdrrec_create(&data, 0, 0, (void*)sock, (void*)read_tcp, (void*)write_tcp);
    int op=1;
    
    while (op != 0)
    {
        bzero(&dem,sizeof(demande_item));
//	setpos(10,1);
	printf(" ***********\n\t1- Demande de bieres\n\t2- Tournee generale\n\t0- Quitter\n*************\n\n");
	fflush(stdout);	
	scanf("%d",&op);
//	upline();
//	clearline();
	if(op == 1)
	{
	        printf("\
    1- Ajout d une demi brune\n\
    2- Ajout d une demi blonde\n\
    3- Ajout d une peinte brune\n\
    4- Ajout d une peinte blonde\n\
    0- fin\n");
	
	    int ajout=1;
	    while(ajout != 0)
	    {
//	    setpos(22,1);
	    printf("Demande de   \n\
    %d demi brune  \n\
    %d demi brlone  \n\
    %d peinte brune  \n\
    %d peinte blonde  \n\n",dem.demi_brune,dem.demi_blonde,dem.peinte_brune,dem.peinte_blonde);	    
	    
	    printf("Votre choix ?");
		fflush(stdout);			
    		scanf("%d",&ajout);		
//	    upline();
//	    clearline();

		
		switch(ajout)
		{
		    case 0: break;		
		    case 1: dem.demi_brune++; break;
		    case 2: dem.demi_blonde++; break;		    
		    case 3: dem.peinte_brune++; break;		    		    
		    case 4: dem.peinte_blonde++; break;		    		    		    
		    default: break;
		}
	    }
	}	
	if (op != 0)
	{
	    printf("Demande de %d %d %d %d\n",dem.demi_brune,dem.demi_blonde,dem.peinte_brune,dem.peinte_blonde);
    	    if (!xdr_int(&data,&op)) { perror("clients.c xdr_int"); exit(-1); }
	    if (!xdr_demande(&data,&dem)) { perror("clients.c xdr_demande"); exit(-1); }
	    if (!xdrrec_endofrecord(&data, TRUE)) { perror("send eof"); exit(-1); }    
	    printf("ENVOIE de la  demande: OK\n");
	}
    }
    xdr_destroy(&data);    
    
    
    }
    

    
    close(sock);    
    
    
    return 0;
}