// File: vianna274[P]dropbox/src/Server.cpp
// Function: Dropbox::Server::run()
void Server::run(){

    thread listenToServersThread(&Server::listenToServers, this, &(this->listenToServersSocket));
    listenToServersThread.detach();

    if(!this->isMain) {
        this->makeConnection();
    }
    while(true){
        if(this->isMain){
            connectNewClient();
        }
        else {
            this->electionMutex.lock();
            if (this->status == STATUS_NORMAL) {
                this->electionMutex.unlock();
                MessageData packet = make_packet(TYPE_PING, 1, 1, -1, "");
                talkToPrimaryMtx.lock();
                bool isPrimaryAlive = this->talkToPrimary->send(&packet, 100);
                talkToPrimaryMtx.unlock();
                this->electionMutex.lock();
                if(!isPrimaryAlive && this->status == STATUS_NORMAL){
                    this->status = BEGIN_ELECTION;
                    this->electionMutex.unlock();
                    backupList.lock();
                    this->removeFromBackup(this->backups, this->ipMain);
                    backupList.unlock();
                } else {
                    this->electionMutex.unlock();
                }
                this_thread::sleep_for(chrono::milliseconds(3000));
            } else {
                this->electionMutex.unlock();
            }
        }
        
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1080, %rsp           # imm = 0x1080
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x1070(%rbp)
leaq 0x2ef(%rip), %rcx       # 0xcd90
movq %rcx, -0x20(%rbp)
movq $0x0, -0x18(%rbp)
movq %rax, -0x28(%rbp)
addq $0x130, %rax            # imm = 0x130
movq %rax, -0x30(%rbp)
leaq -0x10(%rbp), %rdi
movq %rdi, -0x1068(%rbp)
leaq -0x20(%rbp), %rsi
leaq -0x28(%rbp), %rdx
leaq -0x30(%rbp), %rcx
callq 0x11d10
movq -0x1068(%rbp), %rdi
callq 0x3600
jmp 0xcae5
movq -0x1070(%rbp), %rax
testb $0x1, 0x9f8(%rax)
jne 0xcb16
movq -0x1070(%rbp), %rdi
callq 0xdb60
jmp 0xcb03
jmp 0xcb16
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xcd74
jmp 0xcb18
jmp 0xcb1a
movq -0x1070(%rbp), %rax
testb $0x1, 0x9f8(%rax)
je 0xcb3d
movq -0x1070(%rbp), %rdi
callq 0xdbf0
jmp 0xcb38
jmp 0xcd6f
movq -0x1070(%rbp), %rdi
addq $0x20, %rdi
callq 0x6a60
jmp 0xcb4f
movq -0x1070(%rbp), %rax
cmpl $0x18, 0x180(%rax)
jne 0xcd59
movq -0x1070(%rbp), %rdi
addq $0x20, %rdi
callq 0x6a90
jmp 0xcb75
leaq 0xcad6(%rip), %r9       # 0x19652
leaq -0x1010(%rbp), %rdi
movl $0x11, %esi
movl $0x1, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl %ecx, %edx
callq 0x97a0
jmp 0xcb9c
movq -0x1070(%rbp), %rdi
addq $0xc0, %rdi
callq 0x6a60
jmp 0xcbb1
movq -0x1070(%rbp), %rax
movq 0x178(%rax), %rdi
leaq -0x1010(%rbp), %rsi
movl $0x64, %edx
callq 0x178c0
movb %al, -0x1071(%rbp)
jmp 0xcbd8
movq -0x1070(%rbp), %rdi
movb -0x1071(%rbp), %al
andb $0x1, %al
movb %al, -0x1011(%rbp)
addq $0xc0, %rdi
callq 0x6a90
jmp 0xcbfb
movq -0x1070(%rbp), %rdi
addq $0x20, %rdi
callq 0x6a60
jmp 0xcc0d
testb $0x1, -0x1011(%rbp)
jne 0xcd16
movq -0x1070(%rbp), %rax
cmpl $0x18, 0x180(%rax)
jne 0xcd16
movq -0x1070(%rbp), %rdi
movl $0x1c, 0x180(%rdi)
addq $0x20, %rdi
callq 0x6a90
jmp 0xcc4a
movq -0x1070(%rbp), %rdi
addq $0x98, %rdi
callq 0x6a60
jmp 0xcc5f
movq -0x1070(%rbp), %rsi
addq $0x970, %rsi            # imm = 0x970
leaq -0x1030(%rbp), %rdi
callq 0x11dd0
jmp 0xcc7b
movq -0x1070(%rbp), %rsi
addq $0x9d8, %rsi            # imm = 0x9D8
leaq -0x1050(%rbp), %rdi
callq 0x3190
jmp 0xcc97
movq -0x1070(%rbp), %rdi
leaq -0x1030(%rbp), %rsi
leaq -0x1050(%rbp), %rdx
callq 0xe190
jmp 0xccb3
leaq -0x1050(%rbp), %rdi
callq 0x3280
leaq -0x1030(%rbp), %rdi
callq 0x11a00
movq -0x1070(%rbp), %rdi
addq $0x98, %rdi
callq 0x6a90
jmp 0xcce0
jmp 0xcd2a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0xcd08
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x1050(%rbp), %rdi
callq 0x3280
leaq -0x1030(%rbp), %rdi
callq 0x11a00
jmp 0xcd74
movq -0x1070(%rbp), %rdi
addq $0x20, %rdi
callq 0x6a90
jmp 0xcd28
jmp 0xcd2a
movl $0xbb8, -0x105c(%rbp)   # imm = 0xBB8
leaq -0x1058(%rbp), %rdi
leaq -0x105c(%rbp), %rsi
callq 0x6ba0
jmp 0xcd49
leaq -0x1058(%rbp), %rdi
callq 0x6af0
jmp 0xcd57
jmp 0xcd6d
movq -0x1070(%rbp), %rdi
addq $0x20, %rdi
callq 0x6a90
jmp 0xcd6b
jmp 0xcd6d
jmp 0xcd6f
jmp 0xcb18
leaq -0x10(%rbp), %rdi
callq 0x69d0
movq -0x38(%rbp), %rdi
callq 0x3610
nopw %cs:(%rax,%rax)