// File: /workspace/github_cpp_makefile_star2_O1/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 $0x238, %rsp            # imm = 0x238
cmpl $0x2, %edi
jne 0x1532
movq %rsi, %r14
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1050
cmpl $-0x1, %eax
je 0x1548
movl %eax, %ebx
leaq -0x58(%rbp), %r15
movl $0x2080002, (%r15)      # imm = 0x2080002
movq 0x8(%r14), %rdi
callq 0x10d0
movl %eax, 0x4(%r15)
movl %ebx, %edi
movq %r15, %rsi
movl $0x10, %edx
callq 0x10a0
cmpl $-0x1, %eax
je 0x154d
xorps %xmm0, %xmm0
leaq -0x1e0(%rbp), %rsi
movaps %xmm0, 0x70(%rsi)
movaps %xmm0, 0x60(%rsi)
movaps %xmm0, 0x50(%rsi)
movaps %xmm0, 0x40(%rsi)
movaps %xmm0, 0x30(%rsi)
movaps %xmm0, 0x20(%rsi)
movaps %xmm0, 0x10(%rsi)
leaq -0x260(%rbp), %rax
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x40(%rax)
movaps %xmm0, 0x50(%rax)
movaps %xmm0, 0x60(%rax)
movaps %xmm0, 0x70(%rax)
movl $0x1, %edi
movq %rdi, (%rax)
leal 0x3f(%rbx), %eax
testl %ebx, %ebx
cmovnsl %ebx, %eax
movl %eax, %edx
andl $-0x40, %edx
movl %ebx, %ecx
subl %edx, %ecx
shlq %cl, %rdi
sarl $0x6, %eax
cltq
movq %rdi, -0x48(%rbp)
movq %rax, -0x40(%rbp)
orq %rdi, -0x260(%rbp,%rax,8)
movaps %xmm0, (%rsi)
movl %ebx, %eax
incl %eax
movl %eax, -0x2c(%rbp)
leaq -0x160(%rbp), %r13
movq %rbx, -0x38(%rbp)
movl $0x10, %ecx
leaq -0x1e0(%rbp), %rax
movq %rax, %rdi
leaq -0x260(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movl -0x2c(%rbp), %edi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x10c0
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
testq %rax, -0x1e0(%rbp,%rcx,8)
je 0x14ce
movl $0xfa, %edx
movq %r13, %rdi
xorl %esi, %esi
callq 0x1090
movl $0xfa, %edx
movl %ebx, %edi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x1060
movl $0x26, %edx
movq %r13, %rdi
leaq 0xc62(%rip), %rsi       # 0x203c
callq 0x10e0
testl %eax, %eax
setne %r14b
movq -0x160(%rbp), %rax
movabsq $0x6544202e5252452d, %rcx # imm = 0x6544202E5252452D
xorq %rcx, %rax
movq -0x158(%rbp), %rcx
movabsq $0x6f6978656e6f6373, %rdx # imm = 0x6F6978656E6F6373
xorq %rdx, %rcx
movq -0x154(%rbp), %rdx
movabsq $0x6573206e6f697865, %rsi # imm = 0x6573206E6F697865
xorq %rsi, %rdx
orq %rax, %rdx
movq -0x14c(%rbp), %rax
movabsq $0xa726f64697672, %rsi  # imm = 0xA726F64697672
xorq %rsi, %rax
orq %rcx, %rax
orq %rdx, %rax
setne %bl
movq %r13, %rdi
leaq 0xc33(%rip), %rsi       # 0x207e
callq 0x1040
testq %rax, %rax
sete %r15b
cmpb $0x0, -0x160(%rbp)
je 0x14bc
xorl %r12d, %r12d
movzbl -0x160(%rbp,%r12), %edi
cmpl $0x7c, %edi
jne 0x14a3
movsbl -0x161(%rbp,%r12), %eax
addl $-0x30, %eax
cmpl $0x9, %eax
ja 0x14a7
movsbl -0x15f(%rbp,%r12), %eax
addl $-0x30, %eax
cmpl $0x9, %eax
ja 0x14a7
leaq 0xc00(%rip), %rdi       # 0x209a
xorl %eax, %eax
callq 0x1030
jmp 0x14ac
movsbl %dil, %edi
callq 0x1100
incq %r12
movq %r13, %rdi
callq 0x1070
cmpq %r12, %rax
ja 0x1463
andb %r15b, %r14b
andb %r14b, %bl
movq -0x38(%rbp), %rbx
jne 0x136a
jmp 0x1517
testb $0x1, -0x1e0(%rbp)
je 0x136a
movl $0xfa, %edx
movq %r13, %rdi
xorl %esi, %esi
callq 0x1090
movq 0x2aef(%rip), %rax      # 0x3fe0
movq (%rax), %rdx
movq %r13, %rdi
movl $0xfa, %esi
callq 0x1120
movl $0xfa, %edx
movl %ebx, %edi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x1080
jmp 0x136a
movl %ebx, %edi
callq 0x1130
xorl %eax, %eax
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb64(%rip), %rdi       # 0x209d
callq 0x1110
movl $0x1, %edi
callq 0x10f0
callq 0x1168
callq 0x1150