// File: /workspace/github_cpp_makefile_star2_O2/janthonyo[P]Redes-Practicas/cliente.cc
// Function: main
int main (int argc, char const *argv[])
{
    if(argc != 2)
    {
        printf("-ERR. No ha introducido la IP del servidor.\n");
        exit(1);
    }

	/*----------------------------------------------------
		Descriptor del socket y buffer de datos
	-----------------------------------------------------*/
	int sd;
	struct sockaddr_in sockname;
	char buffer[250];
	socklen_t len_sockname;
    fd_set readfds, auxfds;
    int salida;
    int fin = 0;

    //Variables añadidas
    int numbers[100];
    int numbers_len = 0;


	/* --------------------------------------------------
		Se abre el socket
	---------------------------------------------------*/
  	sd = socket (AF_INET, SOCK_STREAM, 0);
	if (sd == -1)
	{
		perror("No se puede abrir el socket cliente\n");
    		exit (1);
	}



	/* ------------------------------------------------------------------
		Se rellenan los campos de la estructura con la IP del
		servidor y el puerto del servicio que solicitamos
	-------------------------------------------------------------------*/
	sockname.sin_family = AF_INET;
	sockname.sin_port = htons(2050);
	sockname.sin_addr.s_addr =  inet_addr(argv[1]);

	/* ------------------------------------------------------------------
		Se solicita la conexión con el servidor
	-------------------------------------------------------------------*/
	len_sockname = sizeof(sockname);

	if (connect(sd, (struct sockaddr *)&sockname, len_sockname) == -1)
	{
		perror ("Error de conexión");
		exit(1);
	}

    //Inicializamos las estructuras
    FD_ZERO(&auxfds);
    FD_ZERO(&readfds);

    FD_SET(0,&readfds);
    FD_SET(sd,&readfds);


	/* ------------------------------------------------------------------
		Se transmite la información (Parte a modificar)
	-------------------------------------------------------------------*/
	do
	{
        auxfds = readfds;
        salida = select(sd+1,&auxfds,NULL,NULL,NULL);

        //Tengo mensaje desde el servidor
        if(FD_ISSET(sd, &auxfds)){

            bzero(buffer,sizeof(buffer));
            recv(sd,buffer,sizeof(buffer),0);

            if(strcmp(buffer,"-ERR. Demasiados clientes conectados\n") == 0)
                fin =1;

            if(strcmp(buffer,"-ERR. Desconexion servidor\n") == 0)
                fin =1;

            if(strstr(buffer, "+Ok. Desconexion procesada\n") != NULL)
            {
                fin =1;
            }

            //Formatea de forma bonita las fichas
            numbers_len = 0;
            for (int i = 0; i < strlen(buffer); i++) {
                if (buffer[i]=='|') {
                    if( isdigit(buffer[i-1]) && isdigit(buffer[i+1]) ){
                        printf("·");
                    }
                    else{
                        printf("%c", buffer[i]);
                    }
                }
                else{
                    printf("%c", buffer[i]);
                }
            }

            for (int i = 0; i < numbers_len; i++) {
                printf("|%d·", numbers[i]);
                i++;
                printf("%d|\n", numbers[i]);
            }


        }
        else
        {

            //He introducido información por teclado
            /*(No hay que cambiarlo. El cliente escribe el numero de la opción
            que quiere escoger, este se manda al servidor y él lo interpreta.)*/
            if(FD_ISSET(0,&auxfds)){
                bzero(buffer,sizeof(buffer));
                fgets(buffer,sizeof(buffer),stdin);
                send(sd,buffer,sizeof(buffer),0);
            }


        }



    }while(fin == 0);

    close(sd);

    return 0;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
cmpl $0x2, %edi
jne 0x1497
movq %rsi, %rbx
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x1050
cmpl $-0x1, %eax
je 0x14a5
movl %eax, %r15d
leaq -0x50(%rbp), %r14
movl $0x2080002, (%r14)      # imm = 0x2080002
movq 0x8(%rbx), %rdi
callq 0x10d0
movl %eax, 0x4(%r14)
pushq $0x10
popq %rdx
movl %r15d, %edi
movq %r14, %rsi
callq 0x10a0
cmpl $-0x1, %eax
je 0x14ae
xorl %eax, %eax
cmpq $0x10, %rax
je 0x12ba
andq $0x0, -0xd0(%rbp,%rax,8)
incq %rax
jmp 0x12a6
xorl %eax, %eax
cmpq $0x10, %rax
je 0x12d0
andq $0x0, -0x150(%rbp,%rax,8)
incq %rax
jmp 0x12bc
leaq -0x150(%rbp), %rax
orb $0x1, (%rax)
pushq $0x40
popq %rcx
movl %r15d, %eax
cltd
idivl %ecx
movslq %eax, %rdx
shll $0x6, %eax
movl %r15d, %ecx
subl %eax, %ecx
pushq $0x1
popq %rax
shlq %cl, %rax
movq %rdx, -0x40(%rbp)
movq %rax, -0x38(%rbp)
orq %rax, -0x150(%rbp,%rdx,8)
movl %r15d, %eax
incl %eax
movl %eax, -0x30(%rbp)
leaq -0x250(%rbp), %r14
pushq $0x7c
popq %rbx
movl %r15d, -0x2c(%rbp)
pushq $0x10
popq %rcx
leaq -0xd0(%rbp), %rax
movq %rax, %rdi
leaq -0x150(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movl -0x30(%rbp), %edi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x10c0
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rcx
testq %rcx, -0xd0(%rbp,%rax,8)
je 0x1431
movl $0xfa, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x1090
movl $0xfa, %edx
movl %r15d, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x1060
movq %r14, %rdi
leaq 0xcb8(%rip), %rsi       # 0x203c
pushq $0x26
popq %rdx
callq 0x10e0
movl %eax, %r13d
movq %r14, %rdi
leaq 0xcc9(%rip), %rsi       # 0x2062
pushq $0x1c
popq %rdx
callq 0x10e0
testl %eax, %eax
setne %r12b
movq %r14, %rdi
leaq 0xccd(%rip), %rsi       # 0x207e
callq 0x1040
testq %rax, %rax
sete %r15b
andb %r12b, %r15b
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x1070
cmpq %r12, %rax
jbe 0x141c
movsbl -0x250(%rbp,%r12), %edi
cmpl $0x7c, %edi
jne 0x1412
movsbl -0x251(%rbp,%r12), %eax
addl $-0x30, %eax
cmpl $0x9, %eax
ja 0x1410
movsbl -0x24f(%rbp,%r12), %eax
addl $-0x30, %eax
cmpl $0x9, %eax
ja 0x1410
leaq 0xc93(%rip), %rdi       # 0x209a
xorl %eax, %eax
callq 0x1030
jmp 0x1417
movl %ebx, %edi
callq 0x1100
incq %r12
jmp 0x13c3
testl %r13d, %r13d
setne %al
testb %al, %r15b
movl -0x2c(%rbp), %r15d
jne 0x131a
jmp 0x147b
testb $0x1, -0xd0(%rbp)
je 0x131a
movl $0xfa, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x1090
movq 0x2b8c(%rip), %rax      # 0x3fe0
movq (%rax), %rdx
movq %r14, %rdi
movl $0xfa, %esi
callq 0x1120
movl $0xfa, %edx
movl %r15d, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x1080
jmp 0x131a
movl %r15d, %edi
callq 0x1130
xorl %eax, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xbff(%rip), %rdi       # 0x209d
callq 0x1110
jmp 0x14ba
leaq 0xb58(%rip), %rdi       # 0x2004
jmp 0x14b5
leaq 0xb74(%rip), %rdi       # 0x2029
callq 0x10b0
pushq $0x1
popq %rdi
callq 0x10f0