// File: /workspace/github_cpp_makefile_star2_O2/bchu7796[P]p2p-file-sharing/peers.cpp
// Function: client::execute()
int client::execute(void){
    client myclient;
    int process_fd;
    int server_fd;
    struct sockaddr_in client_address, peer_address;
    /* initialize random seed: */
    srand (time(NULL));

    //initialize server
    bzero(&server_address, sizeof(server_address));
    server_address.sin_family = PF_INET; 
    server_address.sin_addr.s_addr = inet_addr(SERVER_IP); 
    server_address.sin_port = htons(SERVER_PORT); 

    //create thread for user interface
    std::thread user_thread(&client::user_interface, myclient);
    user_thread.detach();

    //create socket
    if((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
        perror("Failed to create socket"); 
        exit(0);
    }

    //initailize address structure
    bzero(&peer_address, sizeof(peer_address));
    peer_address.sin_family = PF_INET; 
    peer_address.sin_addr.s_addr = INADDR_ANY; 
    peer_address.sin_port = htons(PORT); 

    //bind socket
    if(bind(server_fd, (struct sockaddr*) &peer_address, sizeof(peer_address)) < 0){
        perror("Failed to bind"); 
        exit(0);
    }

    //listen socket
    if(listen(server_fd, 5) < 0){
        perror("Failed to listen"); 
        exit(0);
    }


    while(1){
        int client_address_length = sizeof(client_address);
        if((process_fd = accept(server_fd,(struct sockaddr*) &client_address, (socklen_t *) &client_address_length)) < 0){
            perror("Failed to accept"); 
            exit(0);
        }
        else{
            std::string file_name;
            int chunk_index;
            uint32_t request_type;
            recv(process_fd, &file_name, sizeof(file_name), 0);
            recv(process_fd, &chunk_index, sizeof(chunk_index), 0);
            std::cout<<"upload file:" << file_name <<"," <<chunk_index<<std::endl;
            //use multi-thread to handle requests
            std::thread tid(&client::handle_request, myclient, process_fd, file_name, chunk_index);
            tid.detach();
        }
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
xorl %edi, %edi
callq 0x3280
movl %eax, %edi
callq 0x3290
xorps %xmm0, %xmm0
movups %xmm0, 0x649a(%rip)     # 0xb2b8
movw $0x2, 0x6491(%rip)      # 0xb2b8
leaq 0x22be(%rip), %rdi      # 0x70ec
callq 0x32c0
movl %eax, 0x6483(%rip)      # 0xb2bc
movw $0x901f, 0x6478(%rip)   # imm = 0x901F
leaq -0x1b1(%rip), %rax      # 0x4c98
leaq 0x28(%rsp), %rsi
movq %rax, (%rsi)
andq $0x0, 0x8(%rsi)
leaq 0x20(%rsp), %rbx
leaq 0xb(%rsp), %rdx
movq %rbx, %rdi
callq 0x5346
movq %rbx, %rdi
callq 0x3430
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x3060
testl %eax, %eax
jns 0x4e8a
leaq 0x2193(%rip), %rdi      # 0x701b
jmp 0x4eca
movl %eax, %ebx
leaq 0x58(%rsp), %rsi
andq $0x0, 0x8(%rsi)
movl $0x921f0002, %eax       # imm = 0x921F0002
movq %rax, (%rsi)
pushq $0x10
popq %rdx
movl %ebx, %edi
callq 0x3040
testl %eax, %eax
jns 0x4eb5
leaq 0x2243(%rip), %rdi      # 0x70f6
jmp 0x4eca
pushq $0x5
popq %rsi
movl %ebx, %edi
callq 0x32e0
testl %eax, %eax
jns 0x4ed6
leaq 0x223b(%rip), %rdi      # 0x7105
callq 0x3220
xorl %edi, %edi
callq 0x3300
leaq 0x28(%rsp), %r12
leaq 0x10(%rsp), %rbp
leaq 0x18(%rsp), %r15
leaq 0xb(%rsp), %r14
leaq 0xc(%rsp), %r13
movl $0x10, 0x14(%rsp)
movl %ebx, %edi
leaq 0x68(%rsp), %rsi
leaq 0x14(%rsp), %rdx
callq 0x30c0
movl %eax, 0xc(%rsp)
testl %eax, %eax
js 0x4fd2
leaq 0x38(%rsp), %rcx
movq %rcx, 0x28(%rsp)
andq $0x0, 0x30(%rsp)
movb $0x0, 0x38(%rsp)
movl %eax, %edi
movq %r12, %rsi
pushq $0x20
popq %rdx
xorl %ecx, %ecx
callq 0x30d0
movl 0xc(%rsp), %edi
movq %rbp, %rsi
pushq $0x4
popq %rdx
xorl %ecx, %ecx
callq 0x30d0
movq 0x6080(%rip), %rdi      # 0xafd0
leaq 0x21d0(%rip), %rsi      # 0x7127
callq 0x32a0
movq %rax, %rdi
movq %r12, %rsi
callq 0x3250
movq %rax, %rdi
leaq 0x21c3(%rip), %rsi      # 0x7134
callq 0x32a0
movl 0x10(%rsp), %esi
movq %rax, %rdi
callq 0x3420
movq %rax, %rdi
callq 0x3150
leaq -0x245(%rip), %rax      # 0x4d4c
movq %rax, 0x48(%rsp)
andq $0x0, 0x50(%rsp)
movq %r15, %rdi
leaq 0x48(%rsp), %rsi
movq %r14, %rdx
movq %r13, %rcx
movq %r12, %r8
movq %rbp, %r9
callq 0x53ae
movq %r15, %rdi
callq 0x3430
movq %r15, %rdi
callq 0x5284
movq %r12, %rdi
callq 0x3110
jmp 0x4eef
leaq 0x213d(%rip), %rdi      # 0x7116
jmp 0x4eca
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x5284
jmp 0x4ff4
jmp 0x4ff1
jmp 0x5000
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x3110
jmp 0x5003
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x5284
movq %rbx, %rdi
callq 0x3440
nop