// File: tonymuu[P]distributed-kv-store/MP1Node.cpp
// Function: MP1Node::recvCallBack(void*, char*, int)
bool MP1Node::recvCallBack(void *env, char *data, int size ) {
    // Get msg header from msg
    MessageHdr header;
    memcpy(&header, data, sizeof(MessageHdr));

    // Perform different functions based on different msg type
    if (header.msgType == JOINREQ) {
        // deserialize msg from memory
        Address addr;
        addr.init();
        long heartbeat = 0;
        memcpy(&addr.addr, data + sizeof(MessageHdr), sizeof(addr.addr));
        memcpy(&heartbeat, data + sizeof(MessageHdr) + sizeof(addr.addr), sizeof(long));

        // add to my memberlist
        int id = 0;
        short port;
        setIdAndPortFromAddress(addr, &id, &port);
        MemberListEntry entry = MemberListEntry(id, port, heartbeat, par->getcurrtime());
        memberNode->memberList.push_back(entry);

        // log to debug log about new node join
        log->logNodeAdd(&memberNode->addr, &addr);

        // send joinrep msg that includes my memberlist
        sendJoinRepMsg(&addr);
    } else if (header.msgType == JOINREP) {
        // Since I received JOINREP, I've made myself known so I'm in the group
        memberNode->inGroup = true;
        // We just joined, so we should use introducer's membership list and clear anything previously stored.
        memberNode->memberList.clear();

        // Now we deserialize the msg and update my member list
        deserializeAndUpdateMemberList(data, size);
    } else if (header.msgType == UPDATEREQ) {
        // update membership list, add any new nodes, log
        deserializeAndUpdateMemberList(data, size);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x65c2
leaq -0x26(%rbp), %rdi
callq 0x71f0
leaq -0x26(%rbp), %rdi
callq 0x7300
movq $0x0, -0x30(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %ecx
movl %ecx, -0x26(%rbp)
movw 0x8(%rax), %ax
movw %ax, -0x22(%rbp)
movq -0x18(%rbp), %rax
movq 0xa(%rax), %rax
movq %rax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
leaq -0x3c(%rbp), %rdi
leaq -0x26(%rbp), %rsi
callq 0x18e40
movq -0x60(%rbp), %rdi
leaq -0x3c(%rbp), %rsi
leaq -0x34(%rbp), %rdx
leaq -0x36(%rbp), %rcx
callq 0x5ff0
movq -0x60(%rbp), %rax
movl -0x34(%rbp), %ecx
movl %ecx, -0x74(%rbp)
movw -0x36(%rbp), %cx
movw %cx, -0x62(%rbp)
movq -0x30(%rbp), %rcx
movq %rcx, -0x70(%rbp)
movq 0x18(%rax), %rdi
callq 0x18c60
movl -0x74(%rbp), %esi
movq -0x70(%rbp), %rcx
movl %eax, %edx
movw -0x62(%rbp), %ax
movslq %edx, %r8
leaq -0x58(%rbp), %rdi
movswl %ax, %edx
callq 0x18f20
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rdi
addq $0x28, %rdi
leaq -0x58(%rbp), %rsi
callq 0x7210
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rdi
movq 0x20(%rax), %rsi
addq $0x8, %rsi
leaq -0x26(%rbp), %rdx
callq 0x17ea0
movq -0x60(%rbp), %rdi
leaq -0x26(%rbp), %rsi
callq 0x6610
jmp 0x660d
cmpl $0x1, -0x20(%rbp)
jne 0x65f3
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rcx
movb $0x1, 0xf(%rcx)
movq 0x20(%rax), %rdi
addq $0x28, %rdi
callq 0x7280
movq -0x60(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0x66b0
jmp 0x660b
cmpl $0x2, -0x20(%rbp)
jne 0x6609
movq -0x60(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0x66b0
jmp 0x660b
jmp 0x660d
ud2
nop