// File: /workspace/github_cpp_makefile_star2_O2/luisremis[P]SDFS/Membership.cpp
// Function: Membership::firstJoin()
bool Membership::firstJoin()
{
    logFile<<"calling firstJoin"<< std::endl;

    //set my own addr, ip, timeStamp, roundID
    myTimeStamp = time(NULL);
    my_ip_str = getOwnIPAddr();
    
    //now I have my self as member
    addMember( my_ip_str, myTimeStamp );

    bool joined = false;

    Message msg;
    msg.type = MSG_JOIN;
    msg.roundId = -1;

    ipString2Char4(my_ip_str, msg.carrierAdd);

    msg.timeStamp = myTimeStamp;
    msg.TTL = isIntroducer;	//used to distinguish joining node is Introducer or not

    for(int i=0; (i < nodes.size()) /*&& !joined*/ ; i++){
        int connectionToServer;
        //TCP connect to introducer/other nodes
        logFile <<"Join: Connecting to "<< nodes[i].ip_str << "..." << std::endl;

        int ret = connect_to_server(nodes[i].ip_str.c_str(), port + 1, &connectionToServer);
        if(ret!=0)
        {

            logFile <<"ERROR Join: Cannot connect to "<<nodes[i].ip_str<< std::endl;
            if(!isIntroducer){
            	i--;
            	usleep(200*1000);
            }
            continue;
        }
        else{
            ret = write(connectionToServer, &msg, sizeof(Message) );
            
            Message newMsg;
            read(connectionToServer, &newMsg, sizeof(Message));

            int size = newMsg.timeStamp;
            
            Message * msgs = new Message[size];

            int j=0;
            for(j=0; j < size; j++){
                read(connectionToServer, msgs + j, sizeof(Message));
                logFile<<"FirstJoin: received "<<char42String(msgs[j].carrierAdd)<<" "<<msgs[j].timeStamp<< std::endl;
            }

            if(j == size){
                joined = true;
                for(j=0; j < size; j++)
                	addMember(char42String(msgs[j].carrierAdd), msgs[j].timeStamp);
            }
            else{
                std::cout << "Join: Failed downloading nodes list"<< std::endl;
            	logFile   <<"ERROR Join: Failed downloading nodes list"<< std::endl;
            }

            delete [] msgs;
            close(connectionToServer);
        }    
    }

    //printMember();

    return joined;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rdi, %rbx
leaq 0x8ce0(%rip), %rsi      # 0xf41e
callq 0x4370
movq %rax, %rdi
callq 0x41d0
xorl %edi, %edi
callq 0x4330
movl %eax, 0x2d0(%rbx)
leaq -0xd0(%rbp), %r15
movq %r15, %rdi
callq 0xda65
leaq 0x2d8(%rbx), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x43d0
movq %r15, %rdi
callq 0x41b0
leaq -0xb0(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x4450
movl 0x2d0(%rbx), %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6fb4
leaq -0xb0(%rbp), %rdi
callq 0x41b0
leaq -0x57(%rbp), %r15
movl $0x6, -0x5(%r15)
movb $-0x1, -0x1(%r15)
leaq -0x90(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x4450
movq %r12, %rdi
movq %r15, %rsi
callq 0xd87c
leaq -0x90(%rbp), %rdi
callq 0x41b0
movl 0x2d0(%rbx), %eax
leaq -0x5c(%rbp), %rcx
movl %eax, 0xc(%rcx)
movb 0x2cc(%rbx), %al
movb %al, 0x10(%rcx)
movq $0x0, -0x38(%rbp)
pushq $0x28
popq %r15
leaq 0x8c1d(%rip), %r12      # 0xf430
leaq -0xd0(%rbp), %r13
xorl %ecx, %ecx
movslq %ecx, %r14
movq 0x220(%rbx), %rax
subq 0x218(%rbx), %rax
cqto
idivq %r15
cmpq %r14, %rax
jbe 0x6a6c
movl %ecx, -0x30(%rbp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4370
imulq $0x28, %r14, %r14
movq 0x218(%rbx), %rsi
addq %r14, %rsi
movq %rax, %rdi
callq 0x4300
movq %rax, %rdi
leaq 0x8f4f(%rip), %rsi      # 0xf7b8
callq 0x4370
movq %rax, %rdi
callq 0x41d0
movq 0x218(%rbx), %rax
movq (%rax,%r14), %rdi
movl 0x298(%rbx), %esi
incl %esi
leaq -0x2c(%rbp), %rdx
callq 0xddff
testl %eax, %eax
je 0x68e5
movq %rbx, %rdi
leaq 0x8ba5(%rip), %rsi      # 0xf445
callq 0x4370
addq 0x218(%rbx), %r14
movq %rax, %rdi
movq %r14, %rsi
callq 0x4300
movq %rax, %rdi
callq 0x41d0
cmpb $0x0, 0x2cc(%rbx)
jne 0x6a62
movl -0x30(%rbp), %r14d
decl %r14d
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x4470
movl %r14d, %ecx
jmp 0x6a65
movl -0x2c(%rbp), %edi
leaq -0x5c(%rbp), %rsi
pushq $0x14
popq %r14
movq %r14, %rdx
callq 0x4150
movl -0x2c(%rbp), %edi
leaq -0x70(%rbp), %rsi
movq %r14, %rdx
callq 0x4490
movslq -0x64(%rbp), %r15
movq %r15, %rax
mulq %r14
pushq $-0x1
popq %rcx
cmovoq %rcx, %rax
movq %rax, %rdi
callq 0x4030
movq %rax, %r12
testl %r15d, %r15d
movl $0x0, %r14d
movq %r15, -0x40(%rbp)
cmovgl %r15d, %r14d
movq %rax, -0x48(%rbp)
subq $0x1, %r14
jb 0x69a8
movl -0x2c(%rbp), %edi
movq %r12, %rsi
pushq $0x14
popq %rdx
callq 0x4490
movq %rbx, %rdi
leaq 0x8b0e(%rip), %rsi      # 0xf464
callq 0x4370
movq %rax, %r15
leaq 0x5(%r12), %rsi
movq %r13, %rdi
callq 0xd999
movq %r15, %rdi
movq %r13, %rsi
callq 0x4300
movq %rax, %rdi
leaq 0x89a2(%rip), %rsi      # 0xf322
callq 0x4370
movl 0xc(%r12), %esi
movq %rax, %rdi
callq 0x45a0
movq %rax, %rdi
callq 0x41d0
movq %r13, %rdi
callq 0x41b0
addq $0x14, %r12
jmp 0x6938
movq -0x40(%rbp), %rax
testl %eax, %eax
js 0x6a14
imulq $0x14, %rax, %rax
movq %rax, -0x40(%rbp)
movq -0x48(%rbp), %rax
addq $0x5, %rax
movq %rax, -0x38(%rbp)
xorl %r12d, %r12d
cmpq %r12, -0x40(%rbp)
je 0x6a0c
movq %r13, %r14
movq -0x38(%rbp), %rax
leaq (%rax,%r12), %rsi
leaq -0xf0(%rbp), %r13
movq %r13, %rdi
callq 0xd999
movq -0x48(%rbp), %rax
movl 0xc(%rax,%r12), %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x6fb4
movq %r13, %rdi
callq 0x41b0
addq $0x14, %r12
movq %r14, %r13
jmp 0x69c7
movb $0x1, %al
movq %rax, -0x38(%rbp)
jmp 0x6a46
movq 0xf5a5(%rip), %rdi      # 0x15fc0
leaq 0x8a5d(%rip), %rsi      # 0xf47f
callq 0x4370
movq %rax, %rdi
callq 0x41d0
movq %rbx, %rdi
leaq 0x8a40(%rip), %rsi      # 0xf479
callq 0x4370
movq %rax, %rdi
callq 0x41d0
movq -0x48(%rbp), %rdi
callq 0x4410
movl -0x2c(%rbp), %edi
callq 0x4650
pushq $0x28
popq %r15
leaq 0x89ce(%rip), %r12      # 0xf430
movl -0x30(%rbp), %ecx
incl %ecx
jmp 0x681c
movq -0x38(%rbp), %rax
andb $0x1, %al
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
jmp 0x6ab2
movq %rax, %rbx
leaq -0xb0(%rbp), %rdi
jmp 0x6ab2
movq %rax, %rbx
leaq -0xf0(%rbp), %rdi
jmp 0x6ab2
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
callq 0x41b0
movq %rbx, %rdi
callq 0x45d0
nop