// File: /workspace/github_cpp_makefile_star2_O1/SaadBazaz[P]RIP-Protocol-Simulation/Router.cpp
// Function: handlePacket(int const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void handlePacket(const int &client, string packet)
{
	auto vec = tokenizeData(packet, '&');
	// for (int i=0; i<vec.size(); i++){
	// 		std::cout<<vec[i]<<std::endl;
	// }


	if (vec[0] == "OK"){
		std::cout<<"Something good happened!"<<std::endl;
		return;
	}
	else if (vec[0] == "BAD"){
		std::cout<<"Oops, something went wrong..."<<std::endl;
		return;
	}


	switch (stoi(vec[0]))
	{
	case UPDATE:
	{
		// cout<<"In UPDATE..."<<endl;
		TableRow table_row(vec[3]);
		// cout<<"Made table_row..."<<endl;
		int index = findRowByFDAndID(client, table_row.identifier);

		if (index < 0)
		{

			// if match not found
			// remove all id = -1 values from table and add new table_row
			for (int i=0; i<table.size(); i++){
				if (table[i].identifier == -1){
					table.erase (table.begin() + i);
					i--;
				}
			} 

			table_row.fd = client;
			if (table_row.hop_count < 0)
			{
				table_row.hop_count = 0;
			}
			
			table.push_back(table_row);
			printTable();
			break;
		}


		if (table_row.identifier >= 0)
		{
			table[index].identifier = table_row.identifier;
		}

		// if (table_row.client_name >= 0){
		// 	table[index].client_name = table_row.client_name;
		// }

		if (table_row.status >= 0)
		{
			table[index].status = table_row.status;
		}

		if (table_row.hop_count >= 0)
		{
			table[index].hop_count = table_row.hop_count;
		}

		if (table_row.type != "")
		{
			table[index].type = table_row.type;
		}

		// if (table_row.in_use >= 0){
		table[index].in_use = table_row.in_use;
		// }

		if (table_row.send_port >= 0)
		{
			table[index].send_port = table_row.send_port;
		}

		if (table_row.receive_port >= 0)
		{
			table[index].receive_port = table_row.receive_port;
		}

		printTable();
		sendTableToAllRouters();

		break;
	}
	case ADD:
		break;

	case DELETE:
		break;

	case TABLE:{

		// auto index_vec = findAllRowsByFD(client);

		auto new_rows = deserialize(vec[3]);		


		// cout<<"New Rows - - - - - "<<endl;
		for (int i=0; i<new_rows.size(); i++){


			new_rows[i].fd = client;

			// cout<<new_rows[i].identifier<<endl;

		}
		// cout<<"- - - - - - - - - - "<<endl;



		// Clean useless data out of new_rows vector
		for (int i=0; i<table.size(); i++)
		{
			for (int j=0; j<new_rows.size(); j++){
				if (table[i] == new_rows[j] 
				or (table[i].fd == client and table[i].identifier == new_rows[j].identifier)
				or (table[i].identifier == new_rows[j].identifier and table[i].hop_count == 0)
				or (new_rows[j].hop_count >= 15)
				//  or new_rows[j].identifier == -1 or new_rows[j].identifier == port
				)

				{
					new_rows.erase(new_rows.begin() + j);
					j--;	
				}
			}
		}

		for (int j=0; j<new_rows.size(); j++){
			if (new_rows[j].identifier == -1 or new_rows[j].identifier == port){
				new_rows.erase(new_rows.begin() + j);
				j--;
			}
		}


		// cout<<"New Rows [CLEANED] - - - - - "<<endl;
		// for (int i=0; i<new_rows.size(); i++){
		// 	cout<<new_rows[i].identifier<<endl;
		// }
		// cout<<"- - - - - - - - - - "<<endl;



		// Add data from new_rows into table
		for (int i=0; i<new_rows.size(); i++){
				new_rows[i].hop_count ++;
				new_rows[i].fd = client;
				table.push_back(new_rows[i]);
		}

		// for (int i=0; i<new_rows.size(); i++){
		// 	if (new_rows[i].identifier != port)
		// 		table.push_back(new_rows[i]);
		// }

		printTable();
		sendTableToAllRouters(client);  //ignore this client when sending table to routers
		break;
	}

	case MESSAGE:
	{
		int index = findRowByID_MinHopCount(stoi(vec[2]));

		if (index < 0)
		{
			break;
		}

		if (vec.size() == 4){
			packet += '&';
			packet += to_string(port);
		}
		else {
			packet += ',';
			packet += to_string(port);			
		}

		// Forward packet to appropriate socket
		send(table[index].fd, packet.c_str(), strlen(packet.c_str()), 0);
		break;
	}
	default:
		break;
	}
	// if (vec[0] == UPDATE){
	// 	if ()

	// }
	// send(client, "OK", 2, 0); //Reply message to client
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0xb8(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rsi), %rsi
movq 0x8(%r14), %rdx
addq %rsi, %rdx
leaq 0xa8(%rsp), %r15
movq %r15, %rdi
callq 0x65c4
leaq 0x50(%rsp), %rdi
movq %r15, %rsi
movl $0x26, %edx
callq 0x256c
movq 0xa8(%rsp), %rdi
cmpq %r12, %rdi
je 0x3b02
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0x50(%rsp), %rdi
leaq 0x35a2(%rip), %rsi      # 0x70b0
callq 0x2090
testl %eax, %eax
je 0x3c63
movq 0x50(%rsp), %rdi
leaq 0x35a5(%rip), %rsi      # 0x70cc
callq 0x2090
testl %eax, %eax
je 0x3c71
movq %rbx, 0x48(%rsp)
movq 0x50(%rsp), %rax
movq (%rax), %r12
callq 0x2040
movq %rax, %r15
movl (%rax), %ebp
movl $0x0, (%rax)
leaq 0x8(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
movl $0xa, %edx
callq 0x22e0
cmpq %r12, (%r13)
je 0x416a
movslq %eax, %rcx
cmpq %rax, %rcx
jne 0x4176
movl (%r15), %ecx
cmpl $0x22, %ecx
je 0x4176
testl %ecx, %ecx
jne 0x3b8f
movl %ebp, (%r15)
cmpl $0x1, %eax
je 0x3d8d
cmpl $0x4, %eax
movq 0x48(%rsp), %r12
je 0x3cd3
cmpl $0x2, %eax
jne 0x3cb7
movq 0x50(%rsp), %rax
leaq 0x98(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x60(%rax), %rsi
movq 0x68(%rax), %rdx
addq %rsi, %rdx
leaq 0x88(%rsp), %rdi
callq 0x65c4
leaq 0x8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
movl $0x2c, %edx
callq 0x5f94
movq 0x88(%rsp), %rdi
cmpq %r14, %rdi
je 0x3c0c
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x2260
movl 0x8(%rsp), %ecx
movq 0x6601(%rip), %rsi      # 0xa218
movq 0x65f2(%rip), %rax      # 0xa210
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
subq %rax, %rsi
je 0x4011
movl (%r12), %edi
sarq $0x6, %rsi
cmpq $0x1, %rsi
adcq $0x0, %rsi
leaq 0x4(%rax), %r9
xorl %r8d, %r8d
cmpl %edi, (%r9)
jne 0x3c52
cmpl %ecx, -0x4(%r9)
je 0x400e
incq %r8
addq $0x40, %r9
cmpq %r8, %rsi
jne 0x3c43
jmp 0x4011
movl $0x18, %edx
leaq 0x3444(%rip), %rsi      # 0x70b3
jmp 0x3c7d
movl $0x1d, %edx
leaq 0x3453(%rip), %rsi      # 0x70d0
movq 0x634c(%rip), %rbx      # 0x9fd0
movq %rbx, %rdi
callq 0x22a0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2230
movsbl %al, %esi
movq 0x6326(%rip), %rdi      # 0x9fd0
callq 0x2030
movq %rax, %rdi
callq 0x2180
leaq 0x50(%rsp), %rdi
callq 0x4d7c
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x50(%rsp), %rax
movq 0x40(%rax), %rbx
movl (%r15), %ebp
movl $0x0, (%r15)
leaq 0x8(%rsp), %r12
movq %rbx, %rdi
movq %r12, %rsi
movl $0xa, %edx
callq 0x22e0
cmpq %rbx, (%r12)
je 0x4182
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
leaq -0x80000000(%rax), %rdx
cmpq %rcx, %rdx
jb 0x418e
movl (%r15), %ecx
cmpl $0x22, %ecx
je 0x418e
testl %ecx, %ecx
jne 0x3d32
movl %ebp, (%r15)
movl %eax, %edi
callq 0x3558
testl %eax, %eax
js 0x3cb7
movl %eax, %ebx
movq 0x58(%rsp), %rax
subq 0x50(%rsp), %rax
cmpq $0x80, %rax
jne 0x3f94
movq %r14, %rdi
movl $0x26, %esi
callq 0x2200
movl 0x64bc(%rip), %esi      # 0xa228
leaq 0x8(%rsp), %rdi
callq 0x64a9
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r14, %rdi
callq 0x20a0
jmp 0x3fc3
movq 0x50(%rsp), %rax
leaq 0x78(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x60(%rax), %rsi
movq 0x68(%rax), %rdx
addq %rsi, %rdx
leaq 0x68(%rsp), %rdi
callq 0x65c4
movq 0x48(%rsp), %rbx
leaq 0x8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x2809
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x3ddb
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x2260
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
subq %rax, %rcx
je 0x3e07
sarq $0x6, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
addq $0x4, %rax
movl (%rbx), %edx
movl %edx, (%rax)
addq $0x40, %rax
decq %rcx
jne 0x3dfa
movq 0x640a(%rip), %rax      # 0xa218
cmpq 0x63fb(%rip), %rax      # 0xa210
je 0x3ed4
xorl %r13d, %r13d
leaq 0x8(%rsp), %r14
movq 0x8(%rsp), %rax
cmpq %rax, 0x10(%rsp)
je 0x3eb6
xorl %r15d, %r15d
movq %r13, %rbp
shlq $0x6, %rbp
xorl %ebx, %ebx
movq 0x63ca(%rip), %r12      # 0xa210
addq %rbp, %r12
shlq $0x6, %r15
addq %rax, %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x268c
testb %al, %al
jne 0x3e8e
movl 0x4(%r12), %eax
movq 0x48(%rsp), %rcx
cmpl (%rcx), %eax
jne 0x3e76
movl (%r12), %eax
cmpl (%r15), %eax
je 0x3e8e
movl (%r12), %eax
cmpl (%r15), %eax
jne 0x3e87
cmpl $0x0, 0xc(%r12)
je 0x3e8e
cmpl $0xf, 0xc(%r15)
jl 0x3e9b
movq %r14, %rdi
movq %r15, %rsi
callq 0x6d12
decl %ebx
incl %ebx
movslq %ebx, %r15
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
subq %rax, %rcx
sarq $0x6, %rcx
cmpq %r15, %rcx
ja 0x3e3f
incq %r13
movq 0x6358(%rip), %rax      # 0xa218
subq 0x6349(%rip), %rax      # 0xa210
sarq $0x6, %rax
cmpq %r13, %rax
ja 0x3e23
movq 0x8(%rsp), %rsi
cmpq %rsi, 0x10(%rsp)
movq 0x48(%rsp), %r12
je 0x3f2a
xorl %eax, %eax
leaq 0x8(%rsp), %r14
xorl %ebx, %ebx
shlq $0x6, %rax
movl (%rsi,%rax), %ecx
cmpl $-0x1, %ecx
je 0x3f02
cmpl 0x6328(%rip), %ecx      # 0xa228
jne 0x3f0f
addq %rax, %rsi
movq %r14, %rdi
callq 0x6d12
decl %ebx
incl %ebx
movslq %ebx, %rax
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rcx
subq %rsi, %rcx
sarq $0x6, %rcx
cmpq %rax, %rcx
ja 0x3eee
movq 0x8(%rsp), %rax
cmpq %rax, 0x10(%rsp)
je 0x3f77
xorl %ebx, %ebx
leaq 0x62d1(%rip), %r14      # 0xa210
xorl %r15d, %r15d
incl 0xc(%rax,%rbx)
leaq (%rax,%rbx), %rsi
movl (%r12), %ecx
movl %ecx, 0x4(%rax,%rbx)
movq %r14, %rdi
callq 0x63e8
incq %r15
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
subq %rax, %rcx
sarq $0x6, %rcx
addq $0x40, %rbx
cmpq %r15, %rcx
ja 0x3f42
callq 0x3373
movl (%r12), %edi
callq 0x37c3
leaq 0x8(%rsp), %rdi
callq 0x644e
jmp 0x3cb7
movq %r14, %rdi
movl $0x2c, %esi
callq 0x2200
movl 0x6281(%rip), %esi      # 0xa228
leaq 0x8(%rsp), %rdi
callq 0x64a9
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %r14, %rdi
callq 0x20a0
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3fde
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x2260
movl %ebx, %eax
movq 0x6229(%rip), %rcx      # 0xa210
shlq $0x6, %rax
movl 0x4(%rcx,%rax), %ebx
movq (%r14), %r14
movq %r14, %rdi
callq 0x2100
movl %ebx, %edi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x2110
jmp 0x3cb7
movl %r8d, %edx
testl %edx, %edx
js 0x40c8
movl %edx, %r14d
testl %ecx, %ecx
js 0x402a
movq %r14, %rdx
shlq $0x6, %rdx
movl %ecx, (%rax,%rdx)
movzwl 0x10(%rsp), %ecx
testw %cx, %cx
js 0x4040
movq %r14, %rdx
shlq $0x6, %rdx
movw %cx, 0x8(%rax,%rdx)
movl 0x14(%rsp), %ecx
testl %ecx, %ecx
js 0x4053
movq %r14, %rdx
shlq $0x6, %rdx
movl %ecx, 0xc(%rax,%rdx)
leaq 0x18(%rsp), %rbx
leaq 0x3038(%rip), %rsi      # 0x7097
movq %rbx, %rdi
callq 0x2090
testl %eax, %eax
je 0x4089
movq 0x619e(%rip), %rax      # 0xa210
movq %r14, %rcx
shlq $0x6, %rcx
leaq (%rax,%rcx), %rdi
addq $0x10, %rdi
movq %rbx, %rsi
callq 0x2130
movb 0x38(%rsp), %cl
movq 0x617c(%rip), %rdx      # 0xa210
shlq $0x6, %r14
leaq (%rdx,%r14), %rax
movb %cl, 0x30(%rdx,%r14)
movl 0x3c(%rsp), %ecx
testl %ecx, %ecx
js 0x40ac
movl %ecx, 0x34(%rax)
movl 0x40(%rsp), %ecx
testl %ecx, %ecx
js 0x40b7
movl %ecx, 0x38(%rax)
callq 0x3373
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x37c3
jmp 0x4146
movq 0x6141(%rip), %rsi      # 0xa210
cmpq %rsi, 0x6142(%rip)      # 0xa218
je 0x4119
xorl %eax, %eax
leaq 0x612f(%rip), %r14      # 0xa210
xorl %ebx, %ebx
shlq $0x6, %rax
cmpl $-0x1, (%rsi,%rax)
jne 0x40fa
addq %rax, %rsi
movq %r14, %rdi
callq 0x6d12
decl %ebx
incl %ebx
movslq %ebx, %rax
movq 0x6112(%rip), %rcx      # 0xa218
movq 0x6103(%rip), %rsi      # 0xa210
subq %rsi, %rcx
sarq $0x6, %rcx
cmpq %rax, %rcx
ja 0x40e3
movl (%r12), %eax
movl %eax, 0xc(%rsp)
cmpl $0x0, 0x14(%rsp)
jns 0x4130
movl $0x0, 0x14(%rsp)
leaq 0x60d9(%rip), %rdi      # 0xa210
leaq 0x8(%rsp), %rsi
callq 0x63e8
callq 0x3373
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3cb7
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x3cb7
leaq 0x30c9(%rip), %rdi      # 0x723a
callq 0x2150
leaq 0x30bd(%rip), %rdi      # 0x723a
callq 0x22f0
leaq 0x30b1(%rip), %rdi      # 0x723a
callq 0x2150
leaq 0x30a5(%rip), %rdi      # 0x723a
callq 0x22f0
jmp 0x419c
movq %rax, %rbx
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4266
movq 0x18(%rsp), %rsi
jmp 0x4216
jmp 0x4254
jmp 0x4254
movq %rax, %rbx
movq 0x68(%rsp), %rdi
cmpq %r14, %rdi
je 0x4266
movq 0x78(%rsp), %rsi
jmp 0x4216
movq %rax, %rbx
movq 0x88(%rsp), %rdi
cmpq %r14, %rdi
je 0x4266
movq 0x98(%rsp), %rsi
jmp 0x4216
jmp 0x4254
jmp 0x4254
jmp 0x4254
jmp 0x4259
jmp 0x4220
jmp 0x4200
movq %rax, %rbx
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4266
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x4266
movq %rax, %rbx
cmpl $0x0, (%r15)
jne 0x4266
movl %ebp, (%r15)
jmp 0x4266
jmp 0x4259
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
cmpq %r12, %rdi
je 0x4270
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x2260
jmp 0x4270
jmp 0x4259
movq %rax, %rbx
jmp 0x4266
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x644e
leaq 0x50(%rsp), %rdi
callq 0x4d7c
movq %rbx, %rdi
callq 0x2370