// File: /workspace/github_cpp_makefile_star2_O3/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 0x114f5
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 0x115c0
movl %eax, %r14d
movl $0x32, %edx
movq %rbx, %rdi
leaq 0xd4a8(%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 0x61f1
movq %rbx, %rdi
movl %r14d, %r15d
movl %r14d, %esi
callq 0x8422
leaq -0x88(%rbp), %r14
movq %r14, %rdi
leaq -0x47(%rbp), %rsi
callq 0x11124
movl -0x40(%rbp), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x81aa
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x60b1
callq 0x4360
movl $0x2710, %edi           # imm = 0x2710
callq 0x4490
movq -0x38(%rbp), %rdi
callq 0x44d0
testl %eax, %eax
jne 0x6253
movq 0x260(%rbx), %r14
subq 0x258(%rbx), %r14
sarq $0x3, %r14
imulq %r12, %r14
movq %r14, %rax
shrq $0x3d, %rax
leaq (,%r14,8), %rcx
negq %rax
movl $0x0, %edi
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x4030
movq %rax, %r13
movl $0x1, -0x2c(%rbp)
addq $-0x3, %r14
cmpq $-0x3, %r14
ja 0x61da
movl $0x8, %edi
callq 0x43a0
movq %rax, %r14
leaq 0x256c(%rip), %rax      # 0x869c
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 0xa3ec
movslq -0x2c(%rbp), %rcx
movq %r14, (%r13,%rcx,8)
incq %rcx
movl %ecx, -0x2c(%rbp)
movq 0x260(%rbx), %rax
subq 0x258(%rbx), %rax
sarq $0x3, %rax
imulq %r12, %rax
leaq -0x1(%rax), %rdx
cmpq %rcx, %rdx
ja 0x611c
addq $-0x3, %rax
cmpq $-0x3, %rax
ja 0x61da
movl $0x1, %r14d
movq (%r13,%r14,8), %rdi
callq 0x45e0
movq (%r13,%r14,8), %rdi
testq %rdi, %rdi
je 0x61b4
cmpq $0x0, (%rdi)
jne 0x624e
callq 0x4360
incq %r14
movq 0x260(%rbx), %rax
subq 0x258(%rbx), %rax
sarq $0x3, %rax
imulq %r12, %rax
decq %rax
cmpq %r14, %rax
ja 0x6196
movq %r13, %rdi
callq 0x4450
movq -0x38(%rbp), %rdi
callq 0x42a0
movl -0x30(%rbp), %r14d
jmp 0x6241
cmpb $0x1, -0x3c(%rbp)
jne 0x6202
movq %rbx, %rdi
movl %r14d, %esi
callq 0x8422
movl %r14d, %r15d
leaq -0xa8(%rbp), %r14
movq %r14, %rdi
leaq -0x47(%rbp), %rsi
callq 0x11124
movl -0x40(%rbp), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x81aa
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
movl %r13d, %r14d
je 0x6241
callq 0x4360
movl %r15d, %edi
callq 0x46b0
jmp 0x6006
callq 0x4110
movl %eax, %edi
callq 0x4240
movq %rax, %rbx
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
jmp 0x627b
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
jne 0x6288
jmp 0x628d
movq %rax, %rbx
movq %r14, %rdi
callq 0x4360
movq %rbx, %rdi
callq 0x4630
nop