// File: 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
subq $0x3e0, %rsp            # imm = 0x3E0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x2a8(%rbp)
movl 0x200(%rax), %edi
callq 0x1a970
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %edi
callq 0x1aa80
movq -0x2a8(%rbp), %rdi
movl %eax, -0x1c(%rbp)
leaq 0xc01d(%rip), %rsi      # 0x1cb1e
callq 0x43f0
movq %rax, %rdi
movq 0x16498(%rip), %rsi     # 0x26fa8
callq 0x4410
movl -0x1c(%rbp), %edi
leaq -0x100(%rbp), %rsi
movl $0xe0, %edx
callq 0x4560
movq %rax, -0x18(%rbp)
leaq -0xfc(%rbp), %rax
movq %rax, -0x2b8(%rbp)
leaq -0x121(%rbp), %rdi
movq %rdi, -0x2b0(%rbp)
callq 0x46b0
movq -0x2b8(%rbp), %rsi
movq -0x2b0(%rbp), %rdx
leaq -0x120(%rbp), %rdi
callq 0x4510
jmp 0x10b6a
leaq -0x121(%rbp), %rdi
callq 0x4430
cmpl $0x0, -0x100(%rbp)
jne 0x10c8a
movq -0x30(%rbp), %rdi
callq 0x4030
movq %rax, -0x2c0(%rbp)
jmp 0x10b95
movq -0x2c0(%rbp), %rax
movq %rax, -0x140(%rbp)
movl -0x1c(%rbp), %edi
movq -0x140(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x4560
movq %rax, -0x2c8(%rbp)
jmp 0x10bbf
movq -0x2c8(%rbp), %rax
movq %rax, -0x18(%rbp)
leaq -0x160(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x41e0
jmp 0x10bdf
movq -0x2a8(%rbp), %rdi
movq -0x140(%rbp), %rdx
movq -0x30(%rbp), %rcx
leaq -0x160(%rbp), %rsi
callq 0x13310
jmp 0x10bff
leaq -0x160(%rbp), %rdi
callq 0x42a0
movq -0x140(%rbp), %rax
movq %rax, -0x2d0(%rbp)
cmpq $0x0, %rax
je 0x10c2b
movq -0x2d0(%rbp), %rdi
callq 0x43c0
jmp 0x10c8a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x130(%rbp)
movl %eax, -0x134(%rbp)
leaq -0x121(%rbp), %rdi
callq 0x4430
jmp 0x10f76
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x130(%rbp)
movl %eax, -0x134(%rbp)
jmp 0x10f6a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x130(%rbp)
movl %eax, -0x134(%rbp)
leaq -0x160(%rbp), %rdi
callq 0x42a0
jmp 0x10f6a
cmpl $0x1, -0x100(%rbp)
jne 0x10da4
leaq -0x180(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x41e0
jmp 0x10cac
movq -0x2a8(%rbp), %rdi
leaq -0x180(%rbp), %rsi
leaq -0x140(%rbp), %rdx
callq 0x13690
movq %rax, -0x2d8(%rbp)
jmp 0x10ccf
movq -0x2d8(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq -0x180(%rbp), %rdi
callq 0x42a0
movl -0x1c(%rbp), %edi
leaq -0x100(%rbp), %rsi
movl $0xe0, %edx
callq 0x4190
jmp 0x10cfc
cmpq $0x0, -0x30(%rbp)
jne 0x10d59
movq 0x162b6(%rip), %rdi     # 0x26fc0
leaq 0xbe3b(%rip), %rsi      # 0x1cb4c
callq 0x43f0
movq %rax, -0x2e0(%rbp)
jmp 0x10d1f
movq -0x2e0(%rbp), %rdi
movq 0x1627b(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x10d34
jmp 0x10da2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x130(%rbp)
movl %eax, -0x134(%rbp)
leaq -0x180(%rbp), %rdi
callq 0x42a0
jmp 0x10f6a
movl -0x1c(%rbp), %edi
movq -0x140(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x1ad90
movq %rax, -0x2e8(%rbp)
jmp 0x10d75
movq -0x2e8(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x140(%rbp), %rax
movq %rax, -0x2f0(%rbp)
cmpq $0x0, %rax
je 0x10da0
movq -0x2f0(%rbp), %rdi
callq 0x43c0
jmp 0x10da2
jmp 0x10da4
cmpl $0x2, -0x100(%rbp)
jne 0x10eed
movq -0x2a8(%rbp), %rdi
addq $0x228, %rdi            # imm = 0x228
callq 0xb5f0
jmp 0x10dc6
movq -0x2a8(%rbp), %rax
addq $0x210, %rax            # imm = 0x210
movq %rax, -0x188(%rbp)
movq -0x188(%rbp), %rdi
callq 0x164c0
movq %rax, -0x190(%rbp)
movq -0x188(%rbp), %rdi
callq 0x164f0
movq %rax, -0x198(%rbp)
leaq -0x190(%rbp), %rdi
leaq -0x198(%rbp), %rsi
callq 0x16520
testb $0x1, %al
jne 0x10e19
jmp 0x10e80
leaq -0x190(%rbp), %rdi
callq 0x16550
movq %rax, -0x1a0(%rbp)
movq -0x1a0(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x4040
movl %eax, -0x2f4(%rbp)
jmp 0x10e47
movl -0x2f4(%rbp), %eax
cmpl $0x0, %eax
jne 0x10e70
movq -0x2a8(%rbp), %rdi
addq $0x210, %rdi            # imm = 0x210
movq -0x1a0(%rbp), %rsi
callq 0x165d0
jmp 0x10e6e
jmp 0x10e80
jmp 0x10e72
leaq -0x190(%rbp), %rdi
callq 0x16570
jmp 0x10e00
movq -0x2a8(%rbp), %rdi
addq $0x228, %rdi            # imm = 0x228
callq 0xb620
jmp 0x10e95
leaq -0x1c0(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x41e0
jmp 0x10eaa
movq -0x2a8(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
callq 0x12a20
jmp 0x10ebf
leaq -0x1c0(%rbp), %rdi
callq 0x42a0
jmp 0x10eed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x130(%rbp)
movl %eax, -0x134(%rbp)
leaq -0x1c0(%rbp), %rdi
callq 0x42a0
jmp 0x10f6a
cmpl $0x3, -0x100(%rbp)
je 0x10eff
cmpl $0x4, -0x100(%rbp)
jne 0x10f4f
movl -0x1c(%rbp), %eax
movl %eax, -0x2f8(%rbp)
leaq -0x2a0(%rbp), %rdi
movq %rdi, -0x300(%rbp)
leaq -0x100(%rbp), %rsi
movl $0xe0, %edx
callq 0x4330
movq -0x300(%rbp), %rsi
movq %rsp, %rdi
movl $0x1c, %ecx
rep  movsq (%rsi), %es:(%rdi)
movl -0x2f8(%rbp), %esi
movq -0x2a8(%rbp), %rdi
callq 0x13a40
jmp 0x10f4d
jmp 0x10f4f
movl -0x1c(%rbp), %edi
callq 0x4740
jmp 0x10f59
leaq -0x120(%rbp), %rdi
callq 0x42a0
jmp 0x10ae8
leaq -0x120(%rbp), %rdi
callq 0x42a0
movq -0x130(%rbp), %rdi
callq 0x46a0
nopw %cs:(%rax,%rax)