// File: /workspace/github_cpp_makefile_star2_O2/luisremis[P]SDFS/Membership.cpp
// Function: Membership::detectThread()
void Membership::detectThread()
{
    /*
    WHILE LOOP
        roundId++

        randomly select one node
        send ping message
        sleep(1)
        see msgQueue, search ack
        if(ack)
            cout<<alive
            sleep(4)
            continue
        
        send ping message to random K nodes (call spreadMessage())
        sleep(4)
        see msgQueue, search ack
        if(ack)
            cout<<alive
            continue
        
        if(!ack)
            cout<<fail
            delete node
            send fail message to other nodes
            continue
    */
    bool flagFail = false;
    Node failedNode;
    while(!killDetectingThread)
    {
  
        roundId = (roundId+1)%255;

        if (roundId % 5 == 0)
        {
        	checkLeader();
        }

        logFile<< std::endl << "Detection Thread - Round: "<< roundId << " ------------" << std::endl;
        logFile<<printMember();
        
        if(members.size() < 2)
        {
            usleep(5 * MAX_LATENCY);
            flagFail = false;
            continue;
        }

        int select = rand()%(members.size()-1) + 1;
        Node theNode = members[select];

        if (flagFail == true)
        {
            theNode = failedNode;
        }

        Message msg;
        msg.type = MSG_PING;
        msg.TTL = 0;
        msg.roundId = roundId;
        ipString2Char4(theNode.ip_str, msg.carrierAdd);
        msg.timeStamp = 0;

        logFile<<"detectThread: checking alive or not for "<<theNode.ip_str<<" "<<theNode.timeStamp<<std::endl;
        sendUDP(sockfd, theNode.ip_str, port, (char*)&msg, sizeof(Message));

        bool acked = false;

        if (queueSize() > 10){
            std::cout << "Warning!!! queueSize = " << queueSize() << std::endl;
        }

        usleep(MAX_LATENCY);

        msgQueueLock.lock();
        acked = ackMsgQueue();
        msgQueueLock.unlock();

        static int count = 0;
        if(acked){
            logFile<<"detectThread: node alive: "<<theNode.ip_str<<" "<<theNode.timeStamp<<std::endl;
            flagFail = false;
            usleep(4 * MAX_LATENCY);     
            continue;       
        }

        logFile<<"Hey!!! Node "<<theNode.ip_str<<" did not respond the first time!" << std::endl;

        msg.type = MSG_PIGGY;
        msg.TTL = 0;
        spreadMessage(msg,3);
        usleep(4 * MAX_LATENCY);

        msgQueueLock.lock();
        acked = ackMsgQueue();
        msgQueueLock.unlock();

        if(acked){
            logFile<<"detectThread: second round found node alive: "<<theNode.ip_str<<" "<<theNode.timeStamp<<std::endl;
            flagFail = false;
            continue;
        }
        else{
            logFile<< "detectThread: No ack received: node failed: "<<theNode.ip_str<<" "<<theNode.timeStamp<<std::endl;
            
            if (flagFail == true)
            {
                logFile<< "detectThread: No ack received: node failed: "<<theNode.ip_str<<" "<<theNode.timeStamp<<std::endl;
                failMember(theNode.ip_str, theNode.timeStamp);

                Message failMsg;
                failMsg.type = MSG_FAIL;
                failMsg.roundId = roundId;
                ipString2Char4(theNode.ip_str, failMsg.carrierAdd);
                failMsg.timeStamp = theNode.timeStamp;
                failMsg.TTL = 3;

                spreadMessage(failMsg);
                flagFail = false;
            }
            else
            {
                logFile<< "detectThread: Robust 2: It is alive: "<<theNode.ip_str<<" "<<theNode.timeStamp<<std::endl;
                failedNode = theNode;
                flagFail = true;

            }


            continue;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdi, %rbx
leaq -0xa0(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x310(%rdi), %rax
movq %rax, -0x30(%rbp)
xorl %r13d, %r13d
leaq -0x88(%rbp), %r14
pushq $0x28
popq %r12
cmpb $0x0, 0x351(%rbx)
jne 0x654f
movl 0x2c8(%rbx), %eax
incl %eax
cltd
movl $0xff, %ecx
idivl %ecx
movl %edx, %ecx
movl %ecx, %eax
cwtd
movl %ecx, 0x2c8(%rbx)
movw $0x5, %cx
idivw %cx
testw %dx, %dx
jne 0x60da
movq %rbx, %rdi
callq 0x7b2e
movq %rbx, %rdi
callq 0x41d0
movq %rax, %rdi
leaq 0x9533(%rip), %rsi      # 0xf61f
callq 0x4370
movl 0x2c8(%rbx), %esi
movq %rax, %rdi
callq 0x45a0
movq %rax, %rdi
leaq 0x9531(%rip), %rsi      # 0xf63a
callq 0x4370
movq %rax, %rdi
callq 0x41d0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x7c16
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4300
movq %r14, %rdi
callq 0x41b0
movq 0x260(%rbx), %rax
subq 0x258(%rbx), %rax
cqto
idivq %r12
cmpq $0x1, %rax
ja 0x615f
xorl %r13d, %r13d
movl $0xf4240, %edi          # imm = 0xF4240
callq 0x4470
jmp 0x609d
callq 0x4100
movslq %eax, %rcx
movq 0x258(%rbx), %rdi
movq 0x260(%rbx), %rax
subq %rdi, %rax
cqto
idivq %r12
movq %rax, %rsi
decq %rsi
movq %rcx, %rax
xorl %edx, %edx
divq %rsi
incl %edx
movslq %edx, %rax
imulq $0x28, %rax, %rsi
addq %rdi, %rsi
movq %r14, %rdi
callq 0x8734
testb $0x1, %r13b
je 0x61b4
movq %r14, %rdi
leaq -0xb0(%rbp), %rsi
callq 0x8898
andl $0x0, -0x48(%rbp)
movb $0x0, -0x38(%rbp)
movb 0x2c8(%rbx), %al
movb %al, -0x44(%rbp)
leaq -0xf0(%rbp), %rdi
movq %r14, %rsi
callq 0x4450
leaq -0xf0(%rbp), %r15
movq %r15, %rdi
leaq -0x43(%rbp), %rsi
callq 0xd87c
movq %r15, %rdi
callq 0x41b0
andl $0x0, -0x3c(%rbp)
movq %rbx, %rdi
leaq 0x944b(%rip), %rsi      # 0xf648
callq 0x4370
movq %rax, %rdi
movq %r14, %rsi
callq 0x4300
movq %rax, %rdi
leaq 0x910b(%rip), %rsi      # 0xf322
callq 0x4370
movl -0x68(%rbp), %esi
movq %rax, %rdi
callq 0x45a0
movq %rax, %rdi
callq 0x41d0
movl 0x298(%rbx), %edx
movl 0x29c(%rbx), %edi
movq %r14, %rsi
leaq -0x48(%rbp), %rcx
pushq $0x14
popq %r8
callq 0xdc23
movq 0x300(%rbx), %rax
subq 0x2f8(%rbx), %rax
cqto
pushq $0x14
popq %rcx
idivq %rcx
cmpl $0xb, %eax
jl 0x62a1
movq 0xfd53(%rip), %rdi      # 0x15fc0
leaq 0x93fd(%rip), %rsi      # 0xf671
callq 0x4370
movq %rax, %rdi
movq 0x300(%rbx), %rax
subq 0x2f8(%rbx), %rax
cqto
pushq $0x14
popq %rcx
idivq %rcx
movl %eax, %esi
callq 0x45a0
movq %rax, %rdi
callq 0x41d0
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x4470
movq -0x30(%rbp), %rdi
callq 0x8608
movq %r12, %r15
movq %rbx, %rdi
callq 0x7d88
movl %eax, %r12d
movq -0x30(%rbp), %rdi
callq 0x4290
testb %r12b, %r12b
je 0x631e
movq %rbx, %rdi
leaq 0x93af(%rip), %rsi      # 0xf689
callq 0x4370
movq %r15, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x4300
movq %rax, %rdi
leaq 0x902b(%rip), %rsi      # 0xf322
callq 0x4370
movl -0x68(%rbp), %esi
movq %rax, %rdi
callq 0x45a0
movq %rax, %rdi
callq 0x41d0
movl $0xc3500, %edi          # imm = 0xC3500
callq 0x4470
jmp 0x63df
movq %rbx, %rdi
leaq 0x937c(%rip), %rsi      # 0xf6a4
callq 0x4370
movq %rax, %rdi
movq %r14, %rsi
callq 0x4300
movq %rax, %rdi
leaq 0x936f(%rip), %rsi      # 0xf6b1
callq 0x4370
movq %rax, %rdi
callq 0x41d0
movl $0x2, -0x48(%rbp)
movb $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
movl %eax, 0x10(%rsp)
movups -0x48(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
pushq $0x3
popq %rsi
callq 0x6c28
movl $0xc3500, %edi          # imm = 0xC3500
callq 0x4470
movq -0x30(%rbp), %rdi
callq 0x8608
movq %rbx, %rdi
callq 0x7d88
movl %eax, %r12d
movq -0x30(%rbp), %rdi
callq 0x4290
testb %r12b, %r12b
je 0x63e7
movq %rbx, %rdi
leaq 0x9328(%rip), %rsi      # 0xf6d2
callq 0x4370
movq %r15, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x4300
movq %rax, %rdi
leaq 0x8f5b(%rip), %rsi      # 0xf322
callq 0x4370
movl -0x68(%rbp), %esi
movq %rax, %rdi
callq 0x45a0
movq %rax, %rdi
callq 0x41d0
xorl %r13d, %r13d
jmp 0x6542
movq %rbx, %rdi
leaq 0x930f(%rip), %rsi      # 0xf700
callq 0x4370
movq %r15, %r12
movq %rax, %rdi
movq %r14, %rsi
callq 0x4300
movq %rax, %rdi
leaq 0x8f14(%rip), %rsi      # 0xf322
callq 0x4370
movl -0x68(%rbp), %esi
movq %rax, %rdi
callq 0x45a0
movq %rax, %rdi
callq 0x41d0
testb $0x1, %r13b
je 0x64f3
movq %rbx, %rdi
leaq 0x92c6(%rip), %rsi      # 0xf700
callq 0x4370
movq %rax, %rdi
movq %r14, %rsi
callq 0x4300
movq %rax, %rdi
leaq 0x8ece(%rip), %rsi      # 0xf322
callq 0x4370
movl -0x68(%rbp), %esi
movq %rax, %rdi
callq 0x45a0
movq %rax, %rdi
callq 0x41d0
leaq -0xd0(%rbp), %rdi
movq %r14, %rsi
callq 0x4450
movl -0x68(%rbp), %edx
movq %rbx, %rdi
leaq -0xd0(%rbp), %r15
movq %r15, %rsi
callq 0x6e58
movq %r15, %rdi
callq 0x41b0
movl $0x5, -0x60(%rbp)
movb 0x2c8(%rbx), %al
movb %al, -0x5c(%rbp)
leaq -0x110(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x4450
movq %r15, %rdi
leaq -0x5b(%rbp), %rsi
callq 0xd87c
movq %r15, %rdi
callq 0x41b0
movl -0x68(%rbp), %eax
movl %eax, -0x54(%rbp)
movb $0x3, -0x50(%rbp)
movl -0x50(%rbp), %eax
movl %eax, 0x10(%rsp)
movups -0x60(%rbp), %xmm0
movups %xmm0, (%rsp)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6c28
jmp 0x653e
movq %rbx, %rdi
leaq 0x9230(%rip), %rsi      # 0xf72d
callq 0x4370
movq %rax, %rdi
movq %r14, %rsi
callq 0x4300
movq %rax, %rdi
leaq 0x8e0b(%rip), %rsi      # 0xf322
callq 0x4370
movl -0x68(%rbp), %esi
movq %rax, %rdi
callq 0x45a0
movq %rax, %rdi
callq 0x41d0
leaq -0xb0(%rbp), %rdi
movq %r14, %rsi
callq 0x8898
xorb $0x1, %r13b
movq %r14, %rdi
callq 0x41b0
jmp 0x609d
leaq -0xb0(%rbp), %rdi
callq 0x41b0
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x110(%rbp), %rdi
jmp 0x6595
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
jmp 0x6595
jmp 0x65a7
jmp 0x65a7
jmp 0x65a2
movq %rax, %rbx
leaq -0xf0(%rbp), %rdi
callq 0x41b0
jmp 0x65aa
jmp 0x65a2
jmp 0x65a7
jmp 0x65a7
movq %rax, %rbx
jmp 0x65b6
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x41b0
leaq -0xb0(%rbp), %rdi
callq 0x41b0
movq %rbx, %rdi
callq 0x45d0