// File: /workspace/github_cpp_makefile_star2_O2/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 0xdce2
movl %eax, %r13d
leaq -0x43(%rbp), %r14
leaq 0x230(%rbx), %r15
leaq 0x9b9a(%rip), %r12      # 0xf4a3
movl %eax, -0x2c(%rbp)
movq %r15, -0x50(%rbp)
movl %r13d, %edi
callq 0xdda9
movl %eax, %r13d
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4370
movq %rax, %rdi
callq 0x41d0
movl %r13d, %edi
leaq -0x48(%rbp), %rsi
pushq $0x14
popq %rdx
callq 0x4490
cmpb $0x0, 0x2cc(%rbx)
movl %r13d, -0x34(%rbp)
je 0x5a45
movq %rbx, %rdi
movl %r13d, %esi
callq 0x7186
leaq -0x88(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0xd999
movl -0x3c(%rbp), %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x6fb4
movq %r12, %rdi
callq 0x41b0
movl $0x2710, %edi           # imm = 0x2710
callq 0x4470
movq %r15, %rdi
callq 0x8608
movq 0x258(%rbx), %r15
movq 0x260(%rbx), %r12
movq %r12, %rax
subq %r15, %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
leaq (,%rax,8), %rcx
shrq $0x3d, %rax
negq %rax
movl $0x0, %edi
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x4030
movq %rax, %r14
pushq $0x1
popq %rax
movl %eax, -0x30(%rbp)
movslq %eax, %rcx
movq %r12, %rax
subq %r15, %rax
cqto
pushq $0x28
popq %rsi
idivq %rsi
decq %rax
cmpq %rcx, %rax
jbe 0x5a84
pushq $0x8
popq %rdi
callq 0x4380
movq %rax, %r12
leaq 0x1978(%rip), %rax      # 0x737e
movq %rax, -0x68(%rbp)
andq $0x0, -0x60(%rbp)
movq %rbx, -0x58(%rbp)
movq %r12, %rdi
leaq -0x68(%rbp), %rsi
leaq -0x58(%rbp), %rdx
leaq -0x48(%rbp), %rcx
leaq -0x30(%rbp), %r8
callq 0x87f8
movslq -0x30(%rbp), %rax
movq %r12, (%r14,%rax,8)
incl %eax
movq 0x258(%rbx), %r15
movq 0x260(%rbx), %r12
jmp 0x59d4
cmpb $0x1, -0x38(%rbp)
jne 0x5a56
movq %rbx, %rdi
movl %r13d, %esi
callq 0x7186
leaq -0xa8(%rbp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0xd999
movl -0x3c(%rbp), %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x6fb4
movq %r13, %rdi
callq 0x41b0
movl -0x2c(%rbp), %r13d
jmp 0x5af6
pushq $0x1
popq %r13
subq %r15, %r12
movq %r12, %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
decq %rax
cmpq %r13, %rax
jbe 0x5ad3
movq (%r14,%r13,8), %rdi
callq 0x4590
movq (%r14,%r13,8), %r12
testq %r12, %r12
je 0x5ab8
movq %r12, %rdi
callq 0x5204
movq %r12, %rdi
callq 0x4350
incq %r13
movq 0x258(%rbx), %r15
movq 0x260(%rbx), %r12
jmp 0x5a88
movq %r14, %rdi
callq 0x4410
movq -0x50(%rbp), %r15
movq %r15, %rdi
callq 0x4290
movl -0x2c(%rbp), %r13d
leaq -0x43(%rbp), %r14
leaq 0x99ad(%rip), %r12      # 0xf4a3
movl -0x34(%rbp), %edi
callq 0x4650
jmp 0x5910
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
jmp 0x5b19
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x41b0
jmp 0x5b2b
movq %rax, %rbx
movq %r12, %rdi
callq 0x4350
movq %rbx, %rdi
callq 0x45d0
nop