// File: luisremis[P]SDFS/Membership.cpp
// Function: Membership::Membership(bool, int)
Membership::Membership(bool introducer, int port)
{
	logFile.open("logMembership.log");

	isIntroducer = introducer;
	this->port = port;
	leader.active = false;

	sockfd = bindSocket(port);

    if(isIntroducer){
        logFile << "I am the introducer! " << std::endl;
        getAdress("Address.add");
    }
    else{
        getAdress("AddrIntro.add");
    }

    roundLock.lock();
    roundId = 0;
    roundLock.unlock();

    join();

    std::thread th3(&Membership::forJoinThread, this);
    std::thread th1(&Membership::listeningThread, this);
    std::thread th2(&Membership::detectThread, this);

    listening.swap(th1);
    detecting.swap(th2);
    joinThread.swap(th3);

    killListeningThread = false;
	killDetectingThread = false;

	//runLeaderElection();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movb %sil, %al
movq %rdi, -0x8(%rbp)
andb $0x1, %al
movb %al, -0x9(%rbp)
movl %edx, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
movq %rdi, %rax
movq %rax, -0x138(%rbp)
callq 0x4630
movq -0xd8(%rbp), %rdi
addq $0x200, %rdi            # imm = 0x200
movq %rdi, -0x130(%rbp)
callq 0xb510
movq -0xd8(%rbp), %rdi
addq $0x218, %rdi            # imm = 0x218
movq %rdi, -0x128(%rbp)
callq 0xb530
movq -0xd8(%rbp), %rdi
addq $0x230, %rdi            # imm = 0x230
callq 0xb550
movq -0xd8(%rbp), %rdi
addq $0x258, %rdi            # imm = 0x258
movq %rdi, -0x120(%rbp)
callq 0xb530
movq -0xd8(%rbp), %rdi
addq $0x270, %rdi            # imm = 0x270
movq %rdi, -0x118(%rbp)
callq 0xb570
movq -0xd8(%rbp), %rdi
addq $0x2a0, %rdi            # imm = 0x2A0
callq 0xb550
movq -0xd8(%rbp), %rdi
addq $0x2d8, %rdi            # imm = 0x2D8
movq %rdi, -0x110(%rbp)
callq 0x4520
movq -0xd8(%rbp), %rdi
addq $0x2f8, %rdi            # imm = 0x2F8
movq %rdi, -0x108(%rbp)
callq 0xb590
movq -0xd8(%rbp), %rdi
addq $0x310, %rdi            # imm = 0x310
callq 0xb550
movq -0xd8(%rbp), %rdi
addq $0x338, %rdi            # imm = 0x338
movq %rdi, -0x100(%rbp)
callq 0xb5b0
movq -0xd8(%rbp), %rdi
addq $0x340, %rdi            # imm = 0x340
movq %rdi, -0xf8(%rbp)
callq 0xb5b0
movq -0xd8(%rbp), %rdi
addq $0x348, %rdi            # imm = 0x348
movq %rdi, -0xf0(%rbp)
callq 0xb5b0
movq -0xd8(%rbp), %rdi
addq $0x358, %rdi            # imm = 0x358
movq %rdi, -0xe8(%rbp)
callq 0xb5d0
movq -0xd8(%rbp), %rdi
addq $0x370, %rdi            # imm = 0x370
callq 0xb550
movq -0xd8(%rbp), %rdi
addq $0x398, %rdi            # imm = 0x398
movq %rdi, -0xe0(%rbp)
callq 0x4530
movq -0xd8(%rbp), %rdi
leaq 0x16104(%rip), %rsi     # 0x1c3b0
movl $0x10, %edx
callq 0x4070
jmp 0x62b8
movq -0x138(%rbp), %rax
movb -0x9(%rbp), %cl
andb $0x1, %cl
movb %cl, 0x2cc(%rax)
movl -0x10(%rbp), %ecx
movl %ecx, 0x298(%rax)
movl $0x0, 0x294(%rax)
movl -0x10(%rbp), %edi
callq 0x1a700
movl %eax, -0x13c(%rbp)
jmp 0x62ee
movq -0x138(%rbp), %rax
movl -0x13c(%rbp), %ecx
movl %ecx, 0x29c(%rax)
testb $0x1, 0x2cc(%rax)
je 0x63d3
movq -0x138(%rbp), %rdi
leaq 0x160a6(%rip), %rsi     # 0x1c3c2
callq 0x43f0
movq %rax, -0x148(%rbp)
jmp 0x632a
movq -0x148(%rbp), %rdi
movq 0x20c70(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0x633f
leaq -0x41(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0x46b0
movq -0x150(%rbp), %rdx
leaq 0x1607b(%rip), %rsi     # 0x1c3d8
leaq -0x40(%rbp), %rdi
callq 0x4510
jmp 0x6368
movq -0x138(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x66b0
jmp 0x637a
leaq -0x40(%rbp), %rdi
callq 0x42a0
leaq -0x41(%rbp), %rdi
callq 0x4430
jmp 0x6453
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x6611
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x63c5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x42a0
leaq -0x41(%rbp), %rdi
callq 0x4430
jmp 0x6611
leaq -0x69(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x46b0
movq -0x158(%rbp), %rdx
leaq 0x15ff3(%rip), %rsi     # 0x1c3e4
leaq -0x68(%rbp), %rdi
callq 0x4510
jmp 0x63fc
movq -0x138(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x66b0
jmp 0x640e
leaq -0x68(%rbp), %rdi
callq 0x42a0
leaq -0x69(%rbp), %rdi
callq 0x4430
jmp 0x6453
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x6445
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x42a0
leaq -0x69(%rbp), %rdi
callq 0x4430
jmp 0x6611
movq -0x138(%rbp), %rdi
addq $0x2a0, %rdi            # imm = 0x2A0
callq 0xb5f0
jmp 0x6468
movq -0x138(%rbp), %rdi
movl $0x0, 0x2c8(%rdi)
addq $0x2a0, %rdi            # imm = 0x2A0
callq 0xb620
jmp 0x6487
movq -0x138(%rbp), %rdi
callq 0x6a10
jmp 0x6495
movq -0x138(%rbp), %rax
leaq 0x6bd(%rip), %rcx       # 0x6b60
movq %rcx, -0x88(%rbp)
movq $0x0, -0x80(%rbp)
movq %rax, -0x90(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x88(%rbp), %rsi
leaq -0x90(%rbp), %rdx
callq 0xb640
jmp 0x64d2
movq -0x138(%rbp), %rax
leaq 0x9f0(%rip), %rcx       # 0x6ed0
movq %rcx, -0xa8(%rbp)
movq $0x0, -0xa0(%rbp)
movq %rax, -0xb0(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
callq 0xb640
jmp 0x6515
movq -0x138(%rbp), %rax
leaq 0x119d(%rip), %rcx      # 0x76c0
movq %rcx, -0xc8(%rbp)
movq $0x0, -0xc0(%rbp)
movq %rax, -0xd0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
leaq -0xd0(%rbp), %rdx
callq 0xb640
jmp 0x6558
movq -0x138(%rbp), %rdi
addq $0x338, %rdi            # imm = 0x338
leaq -0x98(%rbp), %rsi
callq 0xb700
movq -0x138(%rbp), %rdi
addq $0x340, %rdi            # imm = 0x340
leaq -0xb8(%rbp), %rsi
callq 0xb700
movq -0x138(%rbp), %rdi
addq $0x348, %rdi            # imm = 0x348
leaq -0x78(%rbp), %rsi
callq 0xb700
movq -0x138(%rbp), %rax
movb $0x0, 0x350(%rax)
movb $0x0, 0x351(%rax)
leaq -0xb8(%rbp), %rdi
callq 0x5840
leaq -0x98(%rbp), %rdi
callq 0x5840
leaq -0x78(%rbp), %rdi
callq 0x5840
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x6608
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x5840
leaq -0x78(%rbp), %rdi
callq 0x5840
movq -0xe0(%rbp), %rdi
callq 0x46e0
movq -0xe8(%rbp), %rdi
callq 0xb730
movq -0xf0(%rbp), %rdi
callq 0x5840
movq -0xf8(%rbp), %rdi
callq 0x5840
movq -0x100(%rbp), %rdi
callq 0x5840
movq -0x108(%rbp), %rdi
callq 0xb790
movq -0x110(%rbp), %rdi
callq 0x42a0
movq -0x118(%rbp), %rdi
callq 0xb7f0
movq -0x120(%rbp), %rdi
callq 0xb810
movq -0x128(%rbp), %rdi
callq 0xb810
movq -0x130(%rbp), %rdi
callq 0xb870
movq -0xd8(%rbp), %rdi
callq 0x4768
movq -0x18(%rbp), %rdi
callq 0x46a0
nopw (%rax,%rax)