// File: luisremis[P]SDFS/Membership.cpp
// Function: Membership::listeningThread()
void Membership::listeningThread()
{
    //char buffer[BUFFER_MAX];
    struct Message msg;
    std::string sender;
    srand (time(NULL));
    logFile<< std::endl <<"listeningThread: thread begins"<<std::endl; 
    while (!killListeningThread)
    {
        if (members.empty())
        {
            //In case the node decided to leave
            continue;
        }

        int byte_count = receiveUDP(sockfd, (char*)&msg, sizeof(msg), sender);

        logFile << "listeningThread: Receive message from: " << sender.c_str() << " ";
        logFile << "type: " << msg.type << "ttl: " << (int)msg.TTL << std::endl;

        if (byte_count != sizeof(msg))
        {
            logFile << "ERROR listeningThread: size receiving incorrect: Message dropped" << std::endl;
            continue;
        }
        
        switch (msg.type)
        {
            case MSG_PING:  
                pingMsg(msg, sender);
                break;
            case MSG_ACK:
                ackMsg(msg, sender);
                break;
            case MSG_PIGGY:         
                piggyMsg(msg, sender);
                break;
            case MSG_PIGGY_PING:    
                piggyPingMsg(msg, sender);
                break;
            case MSG_PIGGY_ACK:     
                piggyAckMsg(msg, sender);
                break;
            case MSG_FAIL:          
                failMsg(msg, sender);
                break;
            case MSG_JOIN:          
                joinMsg(msg, sender);
                break;
            case MSG_LEAVE:         
                leaveMsg(msg, sender);
                break;
            case MSG_ELECTION:         
                electionMsg(msg, sender);
                break;
            case MSG_BULLY:         
                bullyMsg(msg, sender);
                break;
            case MSG_NEWLEADER:         
                newLeaderMsg(msg, sender);
                break;
            
            default:
        	   logFile<<"ERROR: listeningThread: received msg does not belong to a type"<<std::endl;
        }

    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x390, %rsp            # imm = 0x390
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x320(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x4520
xorl %eax, %eax
movl %eax, %edi
callq 0x43a0
movl %eax, %edi
callq 0x43d0
movq -0x320(%rbp), %rdi
movq 0x20097(%rip), %rsi     # 0x26fa8
callq 0x4410
movq %rax, -0x318(%rbp)
jmp 0x6f1f
movq -0x318(%rbp), %rdi
leaq 0x1561e(%rip), %rsi     # 0x1c54b
callq 0x43f0
movq %rax, -0x328(%rbp)
jmp 0x6f3b
movq -0x328(%rbp), %rdi
movq 0x2005f(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x6f50
jmp 0x6f52
movq -0x320(%rbp), %rax
movb 0x350(%rax), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x6f6a
jmp 0x769c
movq -0x320(%rbp), %rdi
addq $0x258, %rdi            # imm = 0x258
callq 0xbb90
testb $0x1, %al
jne 0x6f83
jmp 0x6f96
jmp 0x6f52
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0x76ae
movq -0x320(%rbp), %rax
movl 0x29c(%rax), %edi
leaq -0x1c(%rbp), %rsi
movl $0x14, %edx
leaq -0x40(%rbp), %rcx
callq 0x1a7d0
movl %eax, -0x32c(%rbp)
jmp 0x6fbd
movq -0x320(%rbp), %rdi
movl -0x32c(%rbp), %eax
movl %eax, -0x50(%rbp)
leaq 0x15596(%rip), %rsi     # 0x1c56a
callq 0x43f0
movq %rax, -0x338(%rbp)
jmp 0x6fe2
leaq -0x40(%rbp), %rdi
callq 0x41d0
movq -0x338(%rbp), %rdi
movq %rax, %rsi
callq 0x43f0
movq %rax, -0x340(%rbp)
jmp 0x7003
movq -0x340(%rbp), %rdi
leaq 0x15311(%rip), %rsi     # 0x1c322
callq 0x43f0
jmp 0x7018
movq -0x320(%rbp), %rdi
leaq 0x1556c(%rip), %rsi     # 0x1c592
callq 0x43f0
movq %rax, -0x348(%rbp)
jmp 0x7034
movq -0x348(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x4670
movq %rax, -0x350(%rbp)
jmp 0x704c
movq -0x350(%rbp), %rdi
leaq 0x1553f(%rip), %rsi     # 0x1c599
callq 0x43f0
movq %rax, -0x358(%rbp)
jmp 0x7068
movq -0x358(%rbp), %rdi
movsbl -0xc(%rbp), %esi
callq 0x4670
movq %rax, -0x360(%rbp)
jmp 0x7081
movq -0x360(%rbp), %rdi
movq 0x1ff19(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x7096
movslq -0x50(%rbp), %rax
cmpq $0x14, %rax
je 0x70d6
movq -0x320(%rbp), %rdi
leaq 0x154f1(%rip), %rsi     # 0x1c59f
callq 0x43f0
movq %rax, -0x368(%rbp)
jmp 0x70bc
movq -0x368(%rbp), %rdi
movq 0x1fede(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x70d1
jmp 0x6f52
movl -0x1c(%rbp), %eax
movq %rax, -0x370(%rbp)
subq $0xb, %rax
ja 0x7664
movq -0x370(%rbp), %rax
leaq 0x15288(%rip), %rcx     # 0x1c380
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl -0xc(%rbp), %eax
movl %eax, -0x60(%rbp)
movups -0x1c(%rbp), %xmm0
movaps %xmm0, -0x70(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x41e0
jmp 0x7121
movq -0x320(%rbp), %rdi
movl -0x60(%rbp), %ecx
movq %rsp, %rax
movl %ecx, 0x10(%rax)
movaps -0x70(%rbp), %xmm0
movups %xmm0, (%rax)
leaq -0x90(%rbp), %rsi
callq 0x98b0
jmp 0x7146
leaq -0x90(%rbp), %rdi
callq 0x42a0
jmp 0x7697
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x42a0
jmp 0x76ae
movl -0xc(%rbp), %eax
movl %eax, -0xa0(%rbp)
movups -0x1c(%rbp), %xmm0
movaps %xmm0, -0xb0(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x41e0
jmp 0x719a
movq -0x320(%rbp), %rdi
movl -0xa0(%rbp), %ecx
movq %rsp, %rax
movl %ecx, 0x10(%rax)
movaps -0xb0(%rbp), %xmm0
movups %xmm0, (%rax)
leaq -0xd0(%rbp), %rsi
callq 0x98f0
jmp 0x71c5
leaq -0xd0(%rbp), %rdi
callq 0x42a0
jmp 0x7697
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x42a0
jmp 0x76ae
movl -0xc(%rbp), %eax
movl %eax, -0xe0(%rbp)
movups -0x1c(%rbp), %xmm0
movaps %xmm0, -0xf0(%rbp)
leaq -0x110(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x41e0
jmp 0x7219
movq -0x320(%rbp), %rdi
movl -0xe0(%rbp), %ecx
movq %rsp, %rax
movl %ecx, 0x10(%rax)
movaps -0xf0(%rbp), %xmm0
movups %xmm0, (%rax)
leaq -0x110(%rbp), %rsi
callq 0x9970
jmp 0x7244
leaq -0x110(%rbp), %rdi
callq 0x42a0
jmp 0x7697
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x42a0
jmp 0x76ae
movl -0xc(%rbp), %eax
movl %eax, -0x120(%rbp)
movups -0x1c(%rbp), %xmm0
movaps %xmm0, -0x130(%rbp)
leaq -0x150(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x41e0
jmp 0x7298
movq -0x320(%rbp), %rdi
movl -0x120(%rbp), %ecx
movq %rsp, %rax
movl %ecx, 0x10(%rax)
movaps -0x130(%rbp), %xmm0
movups %xmm0, (%rax)
leaq -0x150(%rbp), %rsi
callq 0x9ae0
jmp 0x72c3
leaq -0x150(%rbp), %rdi
callq 0x42a0
jmp 0x7697
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x42a0
jmp 0x76ae
movl -0xc(%rbp), %eax
movl %eax, -0x160(%rbp)
movups -0x1c(%rbp), %xmm0
movaps %xmm0, -0x170(%rbp)
leaq -0x190(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x41e0
jmp 0x7317
movq -0x320(%rbp), %rdi
movl -0x160(%rbp), %ecx
movq %rsp, %rax
movl %ecx, 0x10(%rax)
movaps -0x170(%rbp), %xmm0
movups %xmm0, (%rax)
leaq -0x190(%rbp), %rsi
callq 0x9b90
jmp 0x7342
leaq -0x190(%rbp), %rdi
callq 0x42a0
jmp 0x7697
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x190(%rbp), %rdi
callq 0x42a0
jmp 0x76ae
movl -0xc(%rbp), %eax
movl %eax, -0x1a0(%rbp)
movups -0x1c(%rbp), %xmm0
movaps %xmm0, -0x1b0(%rbp)
leaq -0x1d0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x41e0
jmp 0x7396
movq -0x320(%rbp), %rdi
movl -0x1a0(%rbp), %ecx
movq %rsp, %rax
movl %ecx, 0x10(%rax)
movaps -0x1b0(%rbp), %xmm0
movups %xmm0, (%rax)
leaq -0x1d0(%rbp), %rsi
callq 0x9c70
jmp 0x73c1
leaq -0x1d0(%rbp), %rdi
callq 0x42a0
jmp 0x7697
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x1d0(%rbp), %rdi
callq 0x42a0
jmp 0x76ae
movl -0xc(%rbp), %eax
movl %eax, -0x1e0(%rbp)
movups -0x1c(%rbp), %xmm0
movaps %xmm0, -0x1f0(%rbp)
leaq -0x210(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x41e0
jmp 0x7415
movq -0x320(%rbp), %rdi
movl -0x1e0(%rbp), %ecx
movq %rsp, %rax
movl %ecx, 0x10(%rax)
movaps -0x1f0(%rbp), %xmm0
movups %xmm0, (%rax)
leaq -0x210(%rbp), %rsi
callq 0x9e20
jmp 0x7440
leaq -0x210(%rbp), %rdi
callq 0x42a0
jmp 0x7697
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x210(%rbp), %rdi
callq 0x42a0
jmp 0x76ae
movl -0xc(%rbp), %eax
movl %eax, -0x220(%rbp)
movups -0x1c(%rbp), %xmm0
movaps %xmm0, -0x230(%rbp)
leaq -0x250(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x41e0
jmp 0x7494
movq -0x320(%rbp), %rdi
movl -0x220(%rbp), %ecx
movq %rsp, %rax
movl %ecx, 0x10(%rax)
movaps -0x230(%rbp), %xmm0
movups %xmm0, (%rax)
leaq -0x250(%rbp), %rsi
callq 0x9e30
jmp 0x74bf
leaq -0x250(%rbp), %rdi
callq 0x42a0
jmp 0x7697
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x250(%rbp), %rdi
callq 0x42a0
jmp 0x76ae
movl -0xc(%rbp), %eax
movl %eax, -0x260(%rbp)
movups -0x1c(%rbp), %xmm0
movaps %xmm0, -0x270(%rbp)
leaq -0x290(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x41e0
jmp 0x7513
movq -0x320(%rbp), %rdi
movl -0x260(%rbp), %ecx
movq %rsp, %rax
movl %ecx, 0x10(%rax)
movaps -0x270(%rbp), %xmm0
movups %xmm0, (%rax)
leaq -0x290(%rbp), %rsi
callq 0xa040
jmp 0x753e
leaq -0x290(%rbp), %rdi
callq 0x42a0
jmp 0x7697
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x290(%rbp), %rdi
callq 0x42a0
jmp 0x76ae
movl -0xc(%rbp), %eax
movl %eax, -0x2a0(%rbp)
movups -0x1c(%rbp), %xmm0
movaps %xmm0, -0x2b0(%rbp)
leaq -0x2d0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x41e0
jmp 0x7592
movq -0x320(%rbp), %rdi
movl -0x2a0(%rbp), %ecx
movq %rsp, %rax
movl %ecx, 0x10(%rax)
movaps -0x2b0(%rbp), %xmm0
movups %xmm0, (%rax)
leaq -0x2d0(%rbp), %rsi
callq 0xa110
jmp 0x75bd
leaq -0x2d0(%rbp), %rdi
callq 0x42a0
jmp 0x7697
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x2d0(%rbp), %rdi
callq 0x42a0
jmp 0x76ae
movl -0xc(%rbp), %eax
movl %eax, -0x2e0(%rbp)
movups -0x1c(%rbp), %xmm0
movaps %xmm0, -0x2f0(%rbp)
leaq -0x310(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x41e0
jmp 0x7611
movq -0x320(%rbp), %rdi
movl -0x2e0(%rbp), %ecx
movq %rsp, %rax
movl %ecx, 0x10(%rax)
movaps -0x2f0(%rbp), %xmm0
movups %xmm0, (%rax)
leaq -0x310(%rbp), %rsi
callq 0xa160
jmp 0x763c
leaq -0x310(%rbp), %rdi
callq 0x42a0
jmp 0x7697
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x310(%rbp), %rdi
callq 0x42a0
jmp 0x76ae
movq -0x320(%rbp), %rdi
leaq 0x14f6e(%rip), %rsi     # 0x1c5e0
callq 0x43f0
movq %rax, -0x378(%rbp)
jmp 0x7680
movq -0x378(%rbp), %rdi
movq 0x1f91a(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x7695
jmp 0x7697
jmp 0x6f52
leaq -0x40(%rbp), %rdi
callq 0x42a0
addq $0x390, %rsp            # imm = 0x390
popq %rbp
retq
leaq -0x40(%rbp), %rdi
callq 0x42a0
movq -0x48(%rbp), %rdi
callq 0x46a0