// File: luisremis[P]SDFS/Membership.cpp
// Function: Membership::forJoinThread()
void Membership::forJoinThread(){ // Will run only in the introducer
    int listenFd = open_socket(port + 1);   //use the port next to UDP as TCP port
    while(true)
    {
        int ret;
        int connFd = listen_socket(listenFd);

        logFile<<"ForJoinThread: one node asking for membership list"<< std::endl;

        Message income;
        //char addr[4];

        read(connFd, &income, sizeof(income));
        
        //if this is normal node: 
        	//if carrier is introducer, send back local list, then add carrier node
        	//else, just add carrier node
        //if this is introducer node:
        	//send back local list. add carrier node. tell everyone carrier node is joining.

        //me is a normal node
        if(!isIntroducer){
        	//if introducer is joining
        	if(income.TTL == 1){
        		//give introducer my local list
        		sendBackLocalList(connFd);
        	}
        	//no matter what is the joining node, join it
        	addMember(char42String(income.carrierAdd), income.timeStamp);
        }
        else{	//this node is an introducer

        	//send back local list
        	sendBackLocalList(connFd);
        	
        	//add carrier node
        	addMember(char42String(income.carrierAdd), income.timeStamp);
        	
        	usleep( 10*1000 );	//wait to make sure last joined member has enough time to open TCP listening port

        	//tell everyone carrier node is joining
        	membersLock.lock();
        	std::thread ** broadThreads = new std::thread*[members.size() ];	//members[0] is introducer itself, members[size-1] is the node just joined
        	for(int i=1; i < members.size() - 1; i++)
        		broadThreads[i] = new std::thread(&Membership::broadcastJoin, this, income, i);
        	for(int i=1; i < members.size() - 1; i++){
        		broadThreads[i]->join();
        		delete broadThreads[i];
        	}
        	delete [] broadThreads;
        	membersLock.unlock();
        }

        close(connFd);
        
        //printMember();
    }
    return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl 0x298(%rax), %edi
addl $0x1, %edi
callq 0x1a970
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %edi
callq 0x1aa80
movq -0xb0(%rbp), %rdi
movl %eax, -0x14(%rbp)
leaq 0x158ff(%rip), %rsi     # 0x1c4a3
callq 0x43f0
movq %rax, %rdi
movq 0x203f5(%rip), %rsi     # 0x26fa8
callq 0x4410
movl -0x14(%rbp), %edi
leaq -0x28(%rbp), %rsi
movl $0x14, %edx
callq 0x4560
movq -0xb0(%rbp), %rax
testb $0x1, 0x2cc(%rax)
jne 0x6c45
movsbl -0x18(%rbp), %eax
cmpl $0x1, %eax
jne 0x6bf1
movq -0xb0(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x9490
leaq -0x23(%rbp), %rsi
leaq -0x48(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
callq 0x1a480
movq -0xb0(%rbp), %rdi
movq -0xb8(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0x91c0
jmp 0x6c1d
leaq -0x48(%rbp), %rdi
callq 0x42a0
jmp 0x6eab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x42a0
jmp 0x6eb8
movq -0xb0(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x9490
leaq -0x23(%rbp), %rsi
leaq -0x78(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
callq 0x1a480
movq -0xb0(%rbp), %rdi
movq -0xc0(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq 0x91c0
jmp 0x6c80
leaq -0x78(%rbp), %rdi
callq 0x42a0
movl $0x2710, %edi           # imm = 0x2710
callq 0x4540
movq -0xb0(%rbp), %rdi
addq $0x230, %rdi            # imm = 0x230
callq 0xb5f0
movq -0xb0(%rbp), %rdi
addq $0x258, %rdi            # imm = 0x258
callq 0xb990
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x4030
movq %rax, -0x80(%rbp)
movl $0x1, -0x84(%rbp)
movq -0xb0(%rbp), %rdi
movslq -0x84(%rbp), %rax
movq %rax, -0xc8(%rbp)
addq $0x258, %rdi            # imm = 0x258
callq 0xb990
movq %rax, %rcx
movq -0xc8(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x6ddf
movl $0x8, %edi
callq 0x4400
movq %rax, %rdi
movq -0xb0(%rbp), %rax
movq %rdi, %rcx
movq %rcx, -0xd0(%rbp)
leaq 0x2a4b(%rip), %rcx      # 0x9790
movq %rcx, -0x98(%rbp)
movq $0x0, -0x90(%rbp)
movq %rax, -0xa0(%rbp)
leaq -0x98(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
leaq -0x28(%rbp), %rcx
leaq -0x84(%rbp), %r8
callq 0xbac0
jmp 0x6d7e
movq -0xd0(%rbp), %rdx
movq -0x80(%rbp), %rax
movslq -0x84(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x6ce8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x42a0
jmp 0x6eb8
movq -0xd0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
callq 0x43c0
jmp 0x6eb8
movl $0x1, -0xa4(%rbp)
movq -0xb0(%rbp), %rdi
movslq -0xa4(%rbp), %rax
movq %rax, -0xd8(%rbp)
addq $0x258, %rdi            # imm = 0x258
callq 0xb990
movq %rax, %rcx
movq -0xd8(%rbp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x6e7b
movq -0x80(%rbp), %rax
movslq -0xa4(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x4650
movq -0x80(%rbp), %rax
movslq -0xa4(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xe0(%rbp)
cmpq $0x0, %rax
je 0x6e65
movq -0xe0(%rbp), %rdi
callq 0x5840
movq -0xe0(%rbp), %rdi
callq 0x43c0
jmp 0x6e67
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x6de9
movq -0x80(%rbp), %rax
movq %rax, -0xe8(%rbp)
cmpq $0x0, %rax
je 0x6e98
movq -0xe8(%rbp), %rdi
callq 0x44b0
movq -0xb0(%rbp), %rdi
addq $0x230, %rdi            # imm = 0x230
callq 0xb620
movl -0x14(%rbp), %edi
callq 0x4740
jmp 0x6b8b
movq -0x50(%rbp), %rdi
callq 0x46a0
nopw %cs:(%rax,%rax)