// File: /workspace/github_cpp_makefile_star2_O2/vianna274[P]dropbox/src/Server.cpp
// Function: Dropbox::Server::listenToServers(Dropbox::WrapperSocket*)
void Server::listenToServers(WrapperSocket * socket){
    string ipDoBackup;
    WrapperSocket *talkToBackup;
    MessageData * data = NULL;
    while(true){
        this->electionMutex.lock();
        if (this->status == BEGIN_ELECTION) {
            this->status = STATUS_ELECTION;
            this->electionMutex.unlock();
            if (this->getHighers().size() > 0) {
                this->startElection(this->getHighers());
            }
            else {
                this->becomeMain();
            }
        }
        else {
            this->electionMutex.unlock();
            data = socket->receive(TIMEOUT_ON, 5);
        }
        if (data == NULL)
            continue;
        string username = string(data->username);
        User * user = getUser(username);
        string ip;
        FileRecord fileRecord;
        switch (data->type)
        {
            case TYPE_MAKE_CONNECTION:
                talkToPrimaryMtx.lock();
                this->talkToPrimary = new WrapperSocket(ipMain, stoi(data->payload));
                synchronize();
                talkToPrimaryMtx.unlock();
                this->electionMutex.lock();
                this->status = STATUS_NORMAL;
                this->electionMutex.unlock();
                break;
            case TYPE_COORDINATOR:
                handleReceiveCoordinator(data);
                break;
            case TYPE_ANSWER:
                if(!this->waitForCoordinator() && this->status == STATUS_ELECTION) {
                    this->becomeMain();
                }
                break;
            case TYPE_ELECTION:
                ip = string(data->payload);
                this->answer(ip);
                this->electionMutex.lock();
                if (this->status == STATUS_NORMAL) {
                    this->status = STATUS_ELECTION;
                    this->electionMutex.unlock();
                    if (this->getHighers().size() > 0) {
                        this->startElection(this->getHighers());
                    }
                    else {
                        this->becomeMain();
                    }
                }
                this->electionMutex.unlock(); 
                break;
            case TYPE_MAKE_BACKUP:
                ipDoBackup = string(data->payload);
                talkToBackup = new WrapperSocket(ipDoBackup, BACKUPS_PORT);
                this->backupsSockets.push_back(talkToBackup);
                this->createNewPortBackup(talkToBackup);
                break;
            case TYPE_PING:
                //cout << "PINGING" << endl;
                break;
            case TYPE_CREATE_USER:
                this->usersIp.push_back(string(data->payload));
                if(user == nullptr) {
                    user = new User(username, rootDir+username+"/");
                    users.push_back(user);
                }
                break;
            case TYPE_DELETE: 
                deleteFile(user->getDirPath() + string(data->payload));
                user->removeFileRecord(string(data->payload));
                break;
            case TYPE_SEND_FILE:
                fileRecord = *((FileRecord *)data->payload);
                receiveFile(&listenToServersSocket, string(data->payload), user->getDirPath());
                user->updateFileRecord(fileRecord);
                break;
            //case de propagar coisas 
            default:
                cout << "PACOTE INCORRETO! " << data->type << endl;
                break;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x448, %rsp            # imm = 0x448
movq %rsi, -0xa8(%rbp)
movq %rdi, %r14
leaq -0x148(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x20(%rdi), %r15
leaq 0x130(%rdi), %rax
movq %rax, -0x98(%rbp)
leaq 0x988(%rdi), %rax
movq %rax, -0xa0(%rbp)
leaq 0x958(%rdi), %rax
movq %rax, -0x80(%rbp)
leaq 0x9a0(%rdi), %rax
movq %rax, -0x88(%rbp)
leaq 0xc0(%rdi), %rax
movq %rax, -0x70(%rbp)
leaq 0x9d8(%rdi), %rax
movq %rax, -0x90(%rbp)
movq $0x0, -0x30(%rbp)
movq %r15, %rdi
callq 0x50d8
cmpl $0x1c, 0x180(%r14)
jne 0x76b7
movl $0x19, 0x180(%r14)
movq %r14, %rbx
movq %r15, %r14
movq %r15, %rdi
callq 0x3250
leaq -0x380(%rbp), %r15
movq %r15, %rdi
movq %rbx, %r12
movq %rbx, %rsi
callq 0x85be
movq -0x378(%rbp), %rbx
movq -0x380(%rbp), %r13
movq %r15, %rdi
callq 0xa056
cmpq %r13, %rbx
je 0x76d7
leaq -0x138(%rbp), %rdi
movq %r12, %rsi
callq 0x85be
movq %r14, %r15
movq %r12, %r14
movq %r12, %rdi
leaq -0x138(%rbp), %rbx
movq %rbx, %rsi
callq 0x87da
movq %rbx, %rdi
callq 0xa056
jmp 0x76e5
movq %r15, %rdi
callq 0x3250
movq -0xa8(%rbp), %rdi
pushq $0x1
popq %rsi
pushq $0x5
popq %rdx
callq 0xb734
movq %rax, -0x30(%rbp)
jmp 0x76e5
movq %r12, %rdi
callq 0x8470
movq %r14, %r15
movq %r12, %r14
cmpq $0x0, -0x30(%rbp)
je 0x762b
movq -0x30(%rbp), %rax
leaq 0xfb4(%rax), %rsi
leaq -0x120(%rbp), %rbx
movq %rbx, %rdi
leaq -0x380(%rbp), %rdx
callq 0x3130
leaq -0x298(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x33d0
movq %r14, %rdi
movq %r12, %rsi
callq 0x8c56
movq %rax, %rbx
movq %r12, %rdi
callq 0x3170
movq %rbx, -0x40(%rbp)
leaq -0xd8(%rbp), %rax
movq %rax, -0xe8(%rbp)
andq $0x0, -0xe0(%rbp)
movb $0x0, -0xd8(%rbp)
movq -0x30(%rbp), %rax
movl (%rax), %eax
addl $-0x2, %eax
cmpl $0x14, %eax
ja 0x7baf
leaq 0x4bb5(%rip), %rcx      # 0xc328
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x70(%rbp), %rdi
callq 0x50d8
pushq $0x48
popq %rdi
callq 0x3320
movq %rax, %rbx
leaq -0x1d8(%rbp), %rdi
movq -0x90(%rbp), %rsi
callq 0x33d0
movq -0x30(%rbp), %rax
leaq 0x14(%rax), %rsi
leaq -0x60(%rbp), %r12
movq %r12, %rdi
leaq -0xc8(%rbp), %rdx
callq 0x3130
movq %r12, %rdi
xorl %esi, %esi
pushq $0xa
popq %rdx
callq 0x5029
leaq -0x1d8(%rbp), %r12
movq %rbx, %rdi
movq %r12, %rsi
movl %eax, %edx
callq 0xb422
movq %rbx, 0x178(%r14)
leaq -0x60(%rbp), %rdi
callq 0x3170
movq %r12, %rdi
callq 0x3170
callq 0x8c38
movq -0x70(%rbp), %rdi
callq 0x3250
movq %r15, %rdi
callq 0x50d8
movl $0x18, 0x180(%r14)
jmp 0x7be2
movq -0x30(%rbp), %rax
leaq 0x14(%rax), %rbx
movl $0xe8, %edx
leaq -0x380(%rbp), %rdi
movq %rbx, %rsi
callq 0x3280
leaq -0x178(%rbp), %rdi
movq %rbx, %rsi
leaq -0x60(%rbp), %rdx
callq 0x3130
movq -0x40(%rbp), %rsi
leaq -0x1f8(%rbp), %r12
movq %r12, %rdi
callq 0xb362
movq %r14, %rdi
movq -0x98(%rbp), %rsi
leaq -0x178(%rbp), %rbx
movq %rbx, %rdx
movq %r12, %rcx
callq 0x5a7a
movq %r12, %rdi
callq 0x3170
movq %rbx, %rdi
callq 0x3170
movq -0x40(%rbp), %rax
pushq $0x1d
popq %rcx
movq %rsp, %rdi
leaq -0x380(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
callq 0xb28c
jmp 0x7bea
movq -0x30(%rbp), %rax
leaq 0x14(%rax), %rsi
leaq -0x60(%rbp), %rbx
movq %rbx, %rdi
leaq -0xc8(%rbp), %rdx
callq 0x3130
movq -0xa0(%rbp), %rdi
movq %rbx, %rsi
callq 0xadd4
movq %rbx, %rdi
callq 0x3170
cmpq $0x0, -0x40(%rbp)
jne 0x7bea
movl $0x98, %edi
callq 0x3320
movq %rax, -0x68(%rbp)
leaq -0x198(%rbp), %rdi
leaq -0x120(%rbp), %rbx
movq %rbx, %rsi
callq 0x33d0
leaq -0x60(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x5352
leaq -0x258(%rbp), %r13
movq %r13, %rdi
movq %r12, %rsi
leaq 0x4a88(%rip), %rdx      # 0xc3bb
callq 0x4f8a
movq -0x68(%rbp), %r12
movq %r12, %rdi
leaq -0x198(%rbp), %rbx
movq %rbx, %rsi
movq %r13, %rdx
callq 0xb084
movq %r12, -0x40(%rbp)
movq %r13, %rdi
callq 0x3170
leaq -0x60(%rbp), %rdi
callq 0x3170
movq %rbx, %rdi
callq 0x3170
movq -0x80(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0xa374
jmp 0x7bea
movq -0x30(%rbp), %rax
leaq 0x14(%rax), %rsi
leaq -0x60(%rbp), %r12
movq %r12, %rdi
leaq -0xc8(%rbp), %rdx
callq 0x3130
leaq -0xe8(%rbp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3380
movq %r12, %rdi
callq 0x3170
leaq -0x1b8(%rbp), %rdi
movq %rbx, %rsi
callq 0x33d0
leaq -0x1b8(%rbp), %rbx
movq %rbx, %rsi
callq 0x83d4
movq %rbx, %rdi
callq 0x3170
movq %r15, %rdi
callq 0x50d8
cmpl $0x18, 0x180(%r14)
jne 0x7be2
movl $0x19, 0x180(%r14)
movq %r15, %rdi
callq 0x3250
movq %r12, %rdi
movq %r14, %rsi
callq 0x85be
movq -0x58(%rbp), %rbx
movq -0x60(%rbp), %r13
movq %r12, %rdi
callq 0xa056
cmpq %r13, %rbx
je 0x7bda
leaq -0x100(%rbp), %rdi
movq %r14, %rsi
callq 0x85be
movq %r14, %rdi
leaq -0x100(%rbp), %rbx
movq %rbx, %rsi
callq 0x87da
movq %rbx, %rdi
callq 0xa056
jmp 0x7be2
movq %r14, %rdi
movq -0x30(%rbp), %rsi
callq 0x8aa4
jmp 0x7bea
leaq -0x60(%rbp), %rdi
movq %rbx, %rsi
callq 0xb362
movq -0x30(%rbp), %rax
leaq 0x14(%rax), %rbx
leaq -0xc8(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
leaq -0x31(%rbp), %rdx
callq 0x3130
leaq -0x238(%rbp), %r13
movq %r13, %rdi
leaq -0x60(%rbp), %rsi
movq %r12, %rdx
callq 0x51aa
movq %r14, %rdi
movq %r13, %rsi
callq 0x6b96
movq %r13, %rdi
callq 0x3170
leaq -0xc8(%rbp), %rdi
callq 0x3170
leaq -0x60(%rbp), %rdi
callq 0x3170
movq -0x40(%rbp), %r13
leaq -0x218(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
leaq -0x60(%rbp), %rdx
callq 0x3130
movq %r13, %rdi
movq %r12, %rsi
callq 0xb212
movq %r12, %rdi
callq 0x3170
jmp 0x7bea
movq -0x30(%rbp), %rax
leaq 0x14(%rax), %rsi
leaq -0x60(%rbp), %rbx
movq %rbx, %rdi
leaq -0xc8(%rbp), %rdx
callq 0x3130
leaq -0x158(%rbp), %rdi
movq %rbx, %rsi
callq 0x3380
movq %rbx, %rdi
callq 0x3170
pushq $0x48
popq %rdi
callq 0x3320
movq %rax, %rbx
leaq -0x278(%rbp), %r12
movq %r12, %rdi
leaq -0x158(%rbp), %rsi
callq 0x33d0
movq %rbx, %rdi
movq %r12, %rsi
movl $0x2328, %edx           # imm = 0x2328
callq 0xb422
movq %rbx, -0x78(%rbp)
movq %r12, %rdi
callq 0x3170
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xa350
movq -0x78(%rbp), %rsi
movq %r14, %rdi
callq 0x894e
jmp 0x7bea
movq %r14, %rdi
callq 0x88fc
cmpl $0x19, 0x180(%r14)
setne %cl
orb %cl, %al
jne 0x7bea
movq %r14, %rdi
callq 0x8470
jmp 0x7bea
movq 0x9412(%rip), %rdi      # 0x10fc8
leaq 0x4800(%rip), %rsi      # 0xc3bd
callq 0x3310
movq -0x30(%rbp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x3500
movq %rax, %rdi
callq 0x31c0
jmp 0x7bea
movq %r14, %rdi
callq 0x8470
movq %r15, %rdi
callq 0x3250
leaq -0xe8(%rbp), %rdi
callq 0x3170
leaq -0x120(%rbp), %rdi
callq 0x3170
jmp 0x762b
movq %rax, %r14
leaq -0x100(%rbp), %rdi
callq 0xa056
jmp 0x7d35
movq %rax, %r14
leaq -0x258(%rbp), %rdi
callq 0x3170
jmp 0x7c2f
movq %rax, %r14
leaq -0x60(%rbp), %rdi
callq 0x3170
jmp 0x7c3d
movq %rax, %r14
leaq -0x198(%rbp), %rdi
callq 0x3170
jmp 0x7c4e
movq %rax, %r14
movq -0x68(%rbp), %rdi
jmp 0x7d05
jmp 0x7d32
movq %rax, %r14
leaq -0x218(%rbp), %rdi
jmp 0x7cd9
jmp 0x7d32
movq %rax, %r14
leaq -0x238(%rbp), %rdi
callq 0x3170
jmp 0x7c94
movq %rax, %r14
jmp 0x7cf6
movq %rax, %r14
leaq -0x278(%rbp), %rdi
jmp 0x7cfd
jmp 0x7cb3
movq %rax, %r14
leaq -0xc8(%rbp), %rdi
callq 0x3170
jmp 0x7cd5
movq %rax, %r14
leaq -0x1f8(%rbp), %rdi
callq 0x3170
jmp 0x7cc9
movq %rax, %r14
jmp 0x7d02
movq %rax, %r14
leaq -0x1b8(%rbp), %rdi
jmp 0x7cd9
jmp 0x7cd2
movq %rax, %r14
leaq -0x178(%rbp), %rdi
jmp 0x7cd9
movq %rax, %r14
leaq -0x60(%rbp), %rdi
callq 0x3170
jmp 0x7d35
jmp 0x7d32
jmp 0x7d32
jmp 0x7d32
jmp 0x7d32
jmp 0x7d32
movq %rax, %r14
leaq -0x60(%rbp), %rdi
callq 0x3170
leaq -0x1d8(%rbp), %rdi
callq 0x3170
movq %rbx, %rdi
callq 0x32f0
jmp 0x7d35
movq %rax, %r14
leaq -0x138(%rbp), %rdi
callq 0xa056
jmp 0x7d52
jmp 0x7d4f
movq %rax, %r14
leaq -0x298(%rbp), %rdi
jmp 0x7d3c
movq %rax, %r14
jmp 0x7d41
jmp 0x7d4f
movq %rax, %r14
leaq -0xe8(%rbp), %rdi
callq 0x3170
leaq -0x120(%rbp), %rdi
callq 0x3170
jmp 0x7d52
movq %rax, %r14
leaq -0x158(%rbp), %rdi
callq 0x3170
movq %r14, %rdi
callq 0x3540