// File: /workspace/github_cpp_makefile_star2_O2/luisremis[P]SDFS/FileSystem.cpp
// Function: FileSystem::listeningThread()
void FileSystem::listeningThread()
{ 
    int listenFd = open_socket(port); 

    while(true)
    {
        size_t ret;
        int connFd = listen_socket(listenFd);

        logFile<<"listeningThread: someone is contacting me... "<< std::endl;

        Message_fs msg;

        ret = read(connFd, &msg, sizeof(Message_fs));
        std::string filename = msg.filename;
        //cout<<"listening filename: "<<filename<<endl;
        char* buffer;

        if(msg.type == MSG_PUT)
        {
            buffer = new char[msg.size];
            ret = read(connFd, buffer, msg.size);
            saveFile(filename, buffer, msg.size);
            delete buffer;
        }
        if(msg.type == MSG_GET)
        {
            msg.size = readFile(filename, &buffer);
            write(connFd, &msg, sizeof(Message_fs));

            if (msg.size == 0)
            {
                std::cout << "File does not exist" << std::endl;
            }
            else
            {
                ret = splitWrite( connFd, buffer, msg.size );
                delete buffer;    
            }
        }
        if(msg.type == MSG_DELETE)
        {
            filesLock.lock();
            for( auto &file : files )
            {
                if( file.filename.compare(filename) == 0)
                {
                    files.remove(file);
                    break;
                }

            }
            filesLock.unlock();
            deleteFile(filename);
        }
        if(msg.type == MSG_PULL_CP || msg.type == MSG_PULL_MV){
            sendFileByRange(connFd, msg);
        }

        close(connFd);
    }
    return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x268, %rsp            # imm = 0x268
movq %rdi, %rbx
movl 0x200(%rdi), %edi
callq 0xdce2
movl %eax, -0x3c(%rbp)
leaq 0x228(%rbx), %rax
movq %rax, -0x48(%rbp)
movq %rbx, -0x30(%rbp)
leaq 0x210(%rbx), %r13
leaq -0xc8(%rbp), %r14
movl -0x3c(%rbp), %edi
callq 0xdda9
movl %eax, %r12d
movq -0x30(%rbp), %rdi
leaq 0x6177(%rip), %rsi      # 0xfb0d
callq 0x4370
movq %rax, %rdi
callq 0x41d0
movl $0xe0, %edx
movl %r12d, %edi
leaq -0x1a8(%rbp), %rsi
callq 0x4490
movq %r14, %rdi
leaq -0x1a4(%rbp), %rsi
leaq -0x38(%rbp), %rdx
callq 0x4170
movl -0x1a8(%rbp), %eax
testl %eax, %eax
jne 0x9a3a
movq -0xd8(%rbp), %rbx
movq %rbx, %rdi
callq 0x4030
movq %rax, %r15
movq %rax, -0x38(%rbp)
movl %r12d, %edi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x4490
leaq -0xa8(%rbp), %rdi
movq %r14, %rsi
callq 0x4450
movq -0xd8(%rbp), %rcx
movq -0x30(%rbp), %rdi
leaq -0xa8(%rbp), %rbx
movq %rbx, %rsi
movq %r15, %rdx
callq 0xafda
movq %rbx, %rdi
callq 0x41b0
movq %r15, %rdi
callq 0x4350
movl -0x1a8(%rbp), %eax
cmpl $0x1, %eax
jne 0x9acc
leaq -0x88(%rbp), %rdi
movq %r14, %rsi
callq 0x4450
movq -0x30(%rbp), %rdi
leaq -0x88(%rbp), %rbx
movq %rbx, %rsi
leaq -0x38(%rbp), %rdx
callq 0xb1da
movq %rax, -0xd8(%rbp)
movq %rbx, %rdi
callq 0x41b0
movl $0xe0, %edx
movl %r12d, %edi
leaq -0x1a8(%rbp), %rsi
callq 0x4150
movq -0xd8(%rbp), %rdx
testq %rdx, %rdx
je 0x9ab1
movq -0x38(%rbp), %rbx
movl %r12d, %edi
movq %rbx, %rsi
callq 0xdfc0
movq %rbx, %rdi
callq 0x4350
jmp 0x9acc
movq 0xc508(%rip), %rdi      # 0x15fc0
leaq 0x607c(%rip), %rsi      # 0xfb3b
callq 0x4370
movq %rax, %rdi
callq 0x41d0
movl -0x1a8(%rbp), %eax
cmpl $0x2, %eax
jne 0x9b38
movq -0x48(%rbp), %rdi
callq 0x8608
movq %r13, %rbx
movq (%rbx), %rbx
cmpq %r13, %rbx
je 0x9b09
leaq 0x10(%rbx), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x4040
testl %eax, %eax
jne 0x9ae3
movq %r13, %rdi
movq %r15, %rsi
callq 0xc9d6
movq -0x48(%rbp), %rdi
callq 0x4290
leaq -0x68(%rbp), %rdi
movq %r14, %rsi
callq 0x4450
leaq -0x68(%rbp), %rbx
movq %rbx, %rsi
callq 0xab14
movq %rbx, %rdi
callq 0x41b0
movl -0x1a8(%rbp), %eax
addl $-0x3, %eax
cmpl $0x1, %eax
ja 0x9b5c
pushq $0x1c
popq %rcx
movq %rsp, %rdi
leaq -0x1a8(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq -0x30(%rbp), %rdi
movl %r12d, %esi
callq 0xb3b6
movl %r12d, %edi
callq 0x4650
movq %r14, %rdi
callq 0x41b0
jmp 0x9980
jmp 0x9b9b
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
jmp 0x9b92
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
jmp 0x9b92
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x41b0
jmp 0x9b9e
jmp 0x9b9b
movq %rax, %rbx
leaq -0xc8(%rbp), %rdi
callq 0x41b0
movq %rbx, %rdi
callq 0x45d0