// File: luisremis[P]SDFS/Membership.cpp
// Function: Membership::runLeaderElection()
void Membership::runLeaderElection()
{
	leader.active = false;
	bullied = false;

	if (members.size() < 2)
	{
		logFile << "No members, canceling leader election" << std::endl;
		return;
	}

	membersLock.lock();

	for (int i = 1; i < members.size(); ++i)
	{
		std::string other = members.at(i).ip_str;
		if ( isBiggerThanMine( other, my_ip_str) )
		{
			Message msg;
			msg.type = MSG_ELECTION;
			sendUDP( sockfd,  other, port, (char*)&msg, sizeof(Message) );
		}
	}

	membersLock.unlock();

	usleep(500); //Sleep one second

	if ( !bullied ) // Send all other that I am the leader
	{
		membersLock.lock();

		for (int i = 1; i < members.size(); ++i)
		{
			std::string other = members.at(i).ip_str;
			Message msg;
			msg.type = MSG_NEWLEADER;
			sendUDP( sockfd,  other, port, (char*)&msg, sizeof(Message) );
		}

		membersLock.unlock();

		leader = members.at(0);
		leader.active = true;
	}

/*
	Sleep
	check the queue

	if queue not empty, return
	else send NEWLEADER MSG  (I AM THE LEADER)
*/

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
movl $0x0, 0x294(%rdi)
movb $0x0, 0x352(%rdi)
addq $0x258, %rdi            # imm = 0x258
callq 0xb990
cmpq $0x2, %rax
jae 0xac14
movq -0xd8(%rbp), %rdi
leaq 0x11c14(%rip), %rsi     # 0x1c80f
callq 0x43f0
movq %rax, %rdi
movq 0x1c39e(%rip), %rsi     # 0x26fa8
callq 0x4410
jmp 0xaefb
movq -0xd8(%rbp), %rdi
addq $0x230, %rdi            # imm = 0x230
callq 0xb5f0
movl $0x1, -0xc(%rbp)
movq -0xd8(%rbp), %rdi
movslq -0xc(%rbp), %rax
movq %rax, -0xe0(%rbp)
addq $0x258, %rdi            # imm = 0x258
callq 0xb990
movq %rax, %rcx
movq -0xe0(%rbp), %rax
cmpq %rcx, %rax
jae 0xad86
movq -0xd8(%rbp), %rdi
addq $0x258, %rdi            # imm = 0x258
movslq -0xc(%rbp), %rsi
callq 0xb950
movq %rax, %rsi
leaq -0x30(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
callq 0x41e0
movq -0xe8(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x41e0
jmp 0xac9b
movq -0xd8(%rbp), %rsi
addq $0x2d8, %rsi            # imm = 0x2D8
leaq -0x80(%rbp), %rdi
callq 0x41e0
jmp 0xacb4
movq -0xd8(%rbp), %rdi
leaq -0x50(%rbp), %rsi
leaq -0x80(%rbp), %rdx
callq 0xa860
movb %al, -0xe9(%rbp)
jmp 0xacd0
leaq -0x80(%rbp), %rdi
callq 0x42a0
leaq -0x50(%rbp), %rdi
callq 0x42a0
movb -0xe9(%rbp), %al
testb $0x1, %al
jne 0xacee
jmp 0xad61
movq -0xd8(%rbp), %rax
movl $0x9, -0x94(%rbp)
movl 0x298(%rax), %edx
movl 0x29c(%rax), %edi
leaq -0x30(%rbp), %rsi
leaq -0x94(%rbp), %rcx
movl $0x14, %r8d
callq 0x1a870
jmp 0xad23
jmp 0xad61
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0xad78
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0xad56
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x42a0
leaq -0x50(%rbp), %rdi
callq 0x42a0
jmp 0xad78
leaq -0x30(%rbp), %rdi
callq 0x42a0
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0xac2e
leaq -0x30(%rbp), %rdi
callq 0x42a0
jmp 0xaf04
movq -0xd8(%rbp), %rdi
addq $0x230, %rdi            # imm = 0x230
callq 0xb620
movl $0x1f4, %edi            # imm = 0x1F4
callq 0x4540
movq -0xd8(%rbp), %rax
testb $0x1, 0x352(%rax)
jne 0xaefb
movq -0xd8(%rbp), %rdi
addq $0x230, %rdi            # imm = 0x230
callq 0xb5f0
movl $0x1, -0x98(%rbp)
movq -0xd8(%rbp), %rdi
movslq -0x98(%rbp), %rax
movq %rax, -0xf8(%rbp)
addq $0x258, %rdi            # imm = 0x258
callq 0xb990
movq %rax, %rcx
movq -0xf8(%rbp), %rax
cmpq %rcx, %rax
jae 0xaeaa
movq -0xd8(%rbp), %rdi
addq $0x258, %rdi            # imm = 0x258
movslq -0x98(%rbp), %rsi
callq 0xb950
movq %rax, %rsi
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x41e0
movq -0xd8(%rbp), %rax
movq -0x100(%rbp), %rsi
movl $0xb, -0xcc(%rbp)
movl 0x298(%rax), %edx
movl 0x29c(%rax), %edi
leaq -0xcc(%rbp), %rcx
movl $0x14, %r8d
callq 0x1a870
jmp 0xae70
leaq -0xb8(%rbp), %rdi
callq 0x42a0
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0xadd4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x42a0
jmp 0xaf04
movq -0xd8(%rbp), %rdi
addq $0x230, %rdi            # imm = 0x230
callq 0xb620
movq -0xd8(%rbp), %rdi
addq $0x258, %rdi            # imm = 0x258
xorl %eax, %eax
movl %eax, %esi
callq 0xb950
movq -0xd8(%rbp), %rdi
movq %rax, %rsi
addq $0x270, %rdi            # imm = 0x270
callq 0xbbd0
movq -0xd8(%rbp), %rax
movl $0x1, 0x294(%rax)
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x46a0
nopl (%rax)