// File: 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
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x118(%rbp)
leaq 0x141ad(%rip), %rsi     # 0x1c41e
callq 0x43f0
movq %rax, %rdi
movq 0x1ed28(%rip), %rsi     # 0x26fa8
callq 0x4410
xorl %eax, %eax
movl %eax, %edi
callq 0x43a0
movq %rax, %rcx
movq -0x118(%rbp), %rax
movl %ecx, 0x2d0(%rax)
leaq -0x28(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0x1a5c0
movq -0x128(%rbp), %rsi
movq -0x118(%rbp), %rdi
addq $0x2d8, %rdi            # imm = 0x2D8
movq %rdi, -0x120(%rbp)
callq 0x4470
movq -0x128(%rbp), %rdi
callq 0x42a0
movq -0x120(%rbp), %rsi
leaq -0x48(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x41e0
movq -0x118(%rbp), %rdi
movq -0x110(%rbp), %rsi
movl 0x2d0(%rdi), %edx
callq 0x91c0
jmp 0x830d
leaq -0x48(%rbp), %rdi
callq 0x42a0
movq -0x118(%rbp), %rsi
movb $0x0, -0x55(%rbp)
movl $0x6, -0x6c(%rbp)
movb $-0x1, -0x68(%rbp)
addq $0x2d8, %rsi            # imm = 0x2D8
leaq -0x90(%rbp), %rdi
movq %rdi, -0x130(%rbp)
callq 0x41e0
movq -0x130(%rbp), %rdi
leaq -0x67(%rbp), %rsi
callq 0x1a2b0
jmp 0x8358
leaq -0x90(%rbp), %rdi
callq 0x42a0
movq -0x118(%rbp), %rax
movl 0x2d0(%rax), %ecx
movl %ecx, -0x60(%rbp)
movb 0x2cc(%rax), %al
andb $0x1, %al
movb %al, -0x5c(%rbp)
movl $0x0, -0x94(%rbp)
movq -0x118(%rbp), %rdi
movslq -0x94(%rbp), %rax
movq %rax, -0x138(%rbp)
addq $0x218, %rdi            # imm = 0x218
callq 0xb990
movq %rax, %rcx
movq -0x138(%rbp), %rax
cmpq %rcx, %rax
jae 0x8827
movq -0x118(%rbp), %rdi
leaq 0x14065(%rip), %rsi     # 0x1c430
callq 0x43f0
movq -0x118(%rbp), %rdi
movq %rax, -0x140(%rbp)
addq $0x218, %rdi            # imm = 0x218
movslq -0x94(%rbp), %rsi
callq 0xb8d0
movq -0x140(%rbp), %rdi
movq %rax, %rsi
callq 0x4370
movq %rax, %rdi
leaq 0x143ae(%rip), %rsi     # 0x1c7b8
callq 0x43f0
movq %rax, %rdi
movq 0x1eb8f(%rip), %rsi     # 0x26fa8
callq 0x4410
movq -0x118(%rbp), %rdi
addq $0x218, %rdi            # imm = 0x218
movslq -0x94(%rbp), %rsi
callq 0xb8d0
movq %rax, %rdi
callq 0x41d0
movq %rax, %rdi
movq -0x118(%rbp), %rax
movl 0x298(%rax), %esi
addl $0x1, %esi
leaq -0x98(%rbp), %rdx
callq 0x1aaf0
movl %eax, -0x9c(%rbp)
cmpl $0x0, -0x9c(%rbp)
je 0x852b
movq -0x118(%rbp), %rdi
leaq 0x13fc5(%rip), %rsi     # 0x1c445
callq 0x43f0
movq -0x118(%rbp), %rdi
movq %rax, -0x148(%rbp)
addq $0x218, %rdi            # imm = 0x218
movslq -0x94(%rbp), %rsi
callq 0xb8d0
movq -0x148(%rbp), %rdi
movq %rax, %rsi
callq 0x4370
movq %rax, %rdi
movq 0x1eae9(%rip), %rsi     # 0x26fa8
callq 0x4410
movq -0x118(%rbp), %rax
testb $0x1, 0x2cc(%rax)
jne 0x8526
movl -0x94(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x94(%rbp)
movl $0x30d40, %edi          # imm = 0x30D40
callq 0x4540
jmp 0x8526
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x42a0
jmp 0x8835
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x42a0
jmp 0x8835
jmp 0x8813
movl -0x98(%rbp), %edi
leaq -0x6c(%rbp), %rsi
movl $0x14, %edx
callq 0x4190
movl %eax, -0x9c(%rbp)
movl -0x98(%rbp), %edi
leaq -0xb0(%rbp), %rsi
movl $0x14, %edx
callq 0x4560
movl -0xa4(%rbp), %eax
movl %eax, -0xb4(%rbp)
movslq -0xb4(%rbp), %rax
movl $0x14, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x4030
movq %rax, -0xc0(%rbp)
movl $0x0, -0xc4(%rbp)
movl $0x0, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
cmpl -0xb4(%rbp), %eax
jge 0x86dc
movl -0x98(%rbp), %edi
movq -0xc0(%rbp), %rax
movslq -0xc4(%rbp), %rcx
leaq (%rcx,%rcx,4), %rcx
leaq (%rax,%rcx,4), %rsi
movl $0x14, %edx
callq 0x4560
movq -0x118(%rbp), %rdi
leaq 0x13e73(%rip), %rsi     # 0x1c464
callq 0x43f0
movq %rax, -0x160(%rbp)
movq -0xc0(%rbp), %rax
movslq -0xc4(%rbp), %rcx
leaq (%rcx,%rcx,4), %rcx
leaq 0x5(%rax,%rcx,4), %rsi
leaq -0xe8(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x1a480
movq -0x160(%rbp), %rdi
movq -0x158(%rbp), %rsi
callq 0x4370
movq %rax, -0x150(%rbp)
jmp 0x8643
movq -0x150(%rbp), %rdi
leaq 0x13cd1(%rip), %rsi     # 0x1c322
callq 0x43f0
movq %rax, -0x168(%rbp)
jmp 0x865f
movq -0x168(%rbp), %rdi
movq -0xc0(%rbp), %rax
movslq -0xc4(%rbp), %rcx
leaq (%rcx,%rcx,4), %rcx
movl 0xc(%rax,%rcx,4), %esi
callq 0x4670
movq %rax, -0x170(%rbp)
jmp 0x868a
movq -0x170(%rbp), %rdi
movq 0x1e910(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x869f
leaq -0xe8(%rbp), %rdi
callq 0x42a0
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x85ab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x42a0
jmp 0x8835
movl -0xc4(%rbp), %eax
cmpl -0xb4(%rbp), %eax
jne 0x87a2
movb $0x1, -0x55(%rbp)
movl $0x0, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
cmpl -0xb4(%rbp), %eax
jge 0x87a0
movq -0xc0(%rbp), %rax
movslq -0xc4(%rbp), %rcx
leaq (%rcx,%rcx,4), %rcx
leaq 0x5(%rax,%rcx,4), %rsi
leaq -0x108(%rbp), %rdi
movq %rdi, -0x178(%rbp)
callq 0x1a480
movq -0x118(%rbp), %rdi
movq -0x178(%rbp), %rsi
movq -0xc0(%rbp), %rax
movslq -0xc4(%rbp), %rcx
leaq (%rcx,%rcx,4), %rcx
movl 0xc(%rax,%rcx,4), %edx
callq 0x91c0
jmp 0x8763
leaq -0x108(%rbp), %rdi
callq 0x42a0
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x86fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x42a0
jmp 0x8835
jmp 0x87e6
movq 0x1e817(%rip), %rdi     # 0x26fc0
leaq 0x13ccf(%rip), %rsi     # 0x1c47f
callq 0x43f0
movq %rax, %rdi
movq 0x1e7e9(%rip), %rsi     # 0x26fa8
callq 0x4410
movq -0x118(%rbp), %rdi
leaq 0x13ca7(%rip), %rsi     # 0x1c479
callq 0x43f0
movq %rax, %rdi
movq 0x1e7c7(%rip), %rsi     # 0x26fa8
callq 0x4410
movq -0xc0(%rbp), %rax
movq %rax, -0x180(%rbp)
cmpq $0x0, %rax
je 0x8806
movq -0x180(%rbp), %rdi
callq 0x44b0
movl -0x98(%rbp), %edi
callq 0x4740
jmp 0x8813
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0x8389
movb -0x55(%rbp), %al
andb $0x1, %al
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x46a0
nop