// File: /workspace/github_cpp_makefile_star2_O1/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 0x32b0
movl %eax, %edi
callq 0x32c0
xorps %xmm0, %xmm0
movups %xmm0, 0x58b8(%rip)     # 0xb2d8
movw $0x2, 0x58af(%rip)      # 0xb2d8
leaq 0x26d1(%rip), %rdi      # 0x8101
callq 0x32f0
movl %eax, 0x58a1(%rip)      # 0xb2dc
movw $0x901f, 0x5896(%rip)   # imm = 0x901F
leaq -0x231(%rip), %rax      # 0x581a
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
movq $0x0, 0x8(%rsi)
leaq 0x38(%rsp), %rbx
leaq 0x3(%rsp), %rdx
movq %rbx, %rdi
callq 0x60c4
movq %rbx, %rdi
callq 0x3470
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x3070
testl %eax, %eax
jns 0x5a93
leaq 0x259f(%rip), %rdi      # 0x8030
jmp 0x5ad8
movl %eax, %ebx
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %rsi
movaps %xmm0, (%rsi)
movl $0x921f0002, %eax       # imm = 0x921F0002
movq %rax, (%rsi)
movl %ebx, %edi
movl $0x10, %edx
callq 0x3050
testl %eax, %eax
jns 0x5ac1
leaq 0x264c(%rip), %rdi      # 0x810b
jmp 0x5ad8
movl %ebx, %edi
movl $0x5, %esi
callq 0x3310
testl %eax, %eax
jns 0x5ae4
leaq 0x2642(%rip), %rdi      # 0x811a
callq 0x3250
xorl %edi, %edi
callq 0x3340
leaq 0x10(%rsp), %r12
leaq 0x8(%rsp), %r13
movq 0x54db(%rip), %rbp      # 0xafd0
leaq 0x30(%rsp), %r14
movl $0x10, 0xc(%rsp)
movl %ebx, %edi
leaq 0x68(%rsp), %rsi
leaq 0xc(%rsp), %rdx
callq 0x30d0
movl %eax, 0x4(%rsp)
testl %eax, %eax
js 0x5c35
leaq 0x20(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movb $0x0, 0x20(%rsp)
movl $0x20, %edx
movl %eax, %edi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x30e0
movl 0x4(%rsp), %edi
movl $0x4, %edx
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x30e0
movl $0xc, %edx
movq %rbp, %rdi
leaq 0x25d2(%rip), %rsi      # 0x813c
callq 0x3330
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %rbp, %rdi
callq 0x3330
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
leaq 0x25b6(%rip), %rsi      # 0x8149
callq 0x3330
movl 0x8(%rsp), %esi
movq %r15, %rdi
callq 0x3460
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x32d0
movsbl %al, %esi
movq %r15, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31d0
leaq -0x289(%rip), %rax      # 0x594c
movq %rax, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
movq %r14, %rdi
leaq 0x40(%rsp), %rsi
leaq 0x3(%rsp), %rdx
leaq 0x4(%rsp), %rcx
movq %r12, %r8
movq %r13, %r9
callq 0x6142
movq %r14, %rdi
callq 0x3470
cmpq $0x0, 0x30(%rsp)
jne 0x5c83
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x5afa
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3300
jmp 0x5afa
leaq 0x24ef(%rip), %rdi      # 0x812b
jmp 0x5ad8
cmpq $0x0, 0x30(%rsp)
je 0x5c4f
jmp 0x5c83
jmp 0x5c4f
jmp 0x5c70
movq %rax, %r14
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x5c73
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3300
jmp 0x5c73
movq %rax, %r14
cmpq $0x0, 0x38(%rsp)
jne 0x5c83
movq %r14, %rdi
callq 0x3480
callq 0x30a0