// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %rbx
movl 0x298(%rdi), %edi
incl %edi
callq 0x11677
movl %eax, %r14d
leaq 0x230(%rbx), %rax
movq %rax, -0x38(%rbp)
movabsq $-0x3333333333333333, %r12 # imm = 0xCCCCCCCCCCCCCCCD
movl %r14d, -0x30(%rbp)
movl %r14d, %r13d
movl %r14d, %edi
callq 0x11740
movl %eax, %r14d
movl $0x32, %edx
movq %rbx, %rdi
leaq 0xd49c(%rip), %rsi      # 0x134cb
callq 0x43f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x4380
movsbl %al, %esi
movq %rbx, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x4260
movl $0x14, %edx
movl %r14d, %edi
leaq -0x4c(%rbp), %rsi
callq 0x44b0
cmpb $0x0, 0x2cc(%rbx)
je 0x6220
movq %rbx, %rdi
movl %r14d, %r15d
movl %r14d, %esi
callq 0x84ac
leaq -0x88(%rbp), %r14
movq %r14, %rdi
leaq -0x47(%rbp), %rsi
callq 0x112b0
movl -0x40(%rbp), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8232
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x60bd
callq 0x4360
movl $0x2710, %edi           # imm = 0x2710
callq 0x4490
movq -0x38(%rbp), %rdi
callq 0x44d0
testl %eax, %eax
jne 0x6282
movq 0x260(%rbx), %rax
subq 0x258(%rbx), %rax
sarq $0x3, %rax
imulq %r12, %rax
movq %rax, %rcx
shrq $0x3d, %rcx
shlq $0x3, %rax
negq %rcx
movl $0x0, %edi
sbbq %rdi, %rdi
orq %rax, %rdi
callq 0x4030
movq %rax, %r13
movl $0x1, -0x2c(%rbp)
movq 0x260(%rbx), %rax
subq 0x258(%rbx), %rax
sarq $0x3, %rax
imulq %r12, %rax
addq $-0x3, %rax
cmpq $-0x3, %rax
ja 0x619f
movl $0x8, %edi
callq 0x43a0
movq %rax, %r14
leaq 0x25bc(%rip), %rax      # 0x8706
movq %rax, -0x68(%rbp)
movq $0x0, -0x60(%rbp)
movq %rbx, -0x58(%rbp)
movq %r14, %rdi
leaq -0x68(%rbp), %rsi
leaq -0x58(%rbp), %rdx
leaq -0x4c(%rbp), %rcx
leaq -0x2c(%rbp), %r8
callq 0xa4d0
movslq -0x2c(%rbp), %rax
movq %r14, (%r13,%rax,8)
incq %rax
movl %eax, -0x2c(%rbp)
movq 0x260(%rbx), %rcx
subq 0x258(%rbx), %rcx
sarq $0x3, %rcx
imulq %r12, %rcx
decq %rcx
cmpq %rax, %rcx
ja 0x6136
movq 0x260(%rbx), %rax
subq 0x258(%rbx), %rax
sarq $0x3, %rax
imulq %r12, %rax
addq $-0x3, %rax
cmpq $-0x3, %rax
ja 0x6209
movl $0x1, %r14d
movq (%r13,%r14,8), %rdi
callq 0x45e0
movq (%r13,%r14,8), %rdi
testq %rdi, %rdi
je 0x61e8
cmpq $0x0, (%rdi)
jne 0x627d
callq 0x4360
incq %r14
movq 0x260(%rbx), %rax
subq 0x258(%rbx), %rax
sarq $0x3, %rax
imulq %r12, %rax
decq %rax
cmpq %r14, %rax
ja 0x61c5
movq %r13, %rdi
callq 0x4450
movq -0x38(%rbp), %rdi
callq 0x42a0
movl -0x30(%rbp), %r14d
jmp 0x6270
cmpb $0x1, -0x3c(%rbp)
jne 0x6231
movq %rbx, %rdi
movl %r14d, %esi
callq 0x84ac
movl %r14d, %r15d
leaq -0xa8(%rbp), %r14
movq %r14, %rdi
leaq -0x47(%rbp), %rsi
callq 0x112b0
movl -0x40(%rbp), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8232
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
movl %r13d, %r14d
je 0x6270
callq 0x4360
movl %r15d, %edi
callq 0x46b0
jmp 0x6012
callq 0x4110
movl %eax, %edi
callq 0x4240
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
jmp 0x62aa
movq %rax, %rbx
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
jne 0x62b7
jmp 0x62bc
movq %rax, %rbx
movq %r14, %rdi
callq 0x4360
movq %rbx, %rdi
callq 0x4630