// File: /workspace/github_cpp_makefile_star2_O2/SaadBazaz[P]RIP-Protocol-Simulation/ProxyRouter.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:
	{
        vector<string> Command = tokenizeData(vec[3],' ');
        if (Command[0]=="DNS")
        {
            if(Command[1]=="REQ"){
                while (Command[2].back() == '\n')
                {
                    Command[2].pop_back();
                }
                for (size_t i = 0; i < List.size(); i++)
                {
                    if (Command[2]==List[i].DomainName)
                    {
                        string Response = "DNS RES " + List[i].DomainName + " " + List[i].IPAddress;
                        string message = constructNewMessage(MESSAGE, port,stoi(vec[1]),(void*)Response.c_str());
                        cout << "Proxy Intervenes ...\n";

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

                        send(client, message.c_str(), strlen(message.c_str()), 0);
                        return;
                    }
                    
                }
                
            }
            else if (Command[1] == "RES"){
                cout << "Checking Response ... \n";
                int index = SearchDomain(Command[2],List);
                
                if (index < 0)
                {
                    Directory Entry ;
                    Entry.DomainName = Command[2] ;
                    for (size_t i = 0; i < 128; i++)
                    {
                        Entry.IPAddress[i] = Command[3][i];
                    }
                    List.push_back(Entry);
                    cout << "Saved the Entry in Cache ... \n";
                }
                else{
                    for (size_t i = 0; i < 128; i++)
                    {
                        List[index].IPAddress[i] = Command[3][i];
                    }
                }
                
            }
        }
        
		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 $0x1c8, %rsp            # imm = 0x1C8
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x1a8(%rsp), %r15
movq %r15, %rdi
callq 0x22f0
movq %rsp, %rdi
pushq $0x26
popq %rdx
movq %r15, %rsi
callq 0x258c
leaq 0x1a8(%rsp), %rdi
callq 0x2100
movq (%rsp), %rdi
leaq 0x2f24(%rip), %rsi      # 0x60c3
callq 0x498a
testb %al, %al
je 0x31b1
leaq 0x2f17(%rip), %rsi      # 0x60c6
jmp 0x31cc
movq (%rsp), %rdi
leaq 0x2f23(%rip), %rsi      # 0x60df
callq 0x498a
testb %al, %al
je 0x31fa
leaq 0x2f17(%rip), %rsi      # 0x60e3
movq 0x6df5(%rip), %rdi      # 0x9fc8
callq 0x2240
movq %rax, %rdi
callq 0x2110
movq %rsp, %rdi
callq 0x4240
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rsp), %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x4997
cmpl $0x1, %eax
je 0x33cc
cmpl $0x4, %eax
je 0x333c
cmpl $0x2, %eax
jne 0x31e0
movq (%rsp), %rsi
addq $0x60, %rsi
leaq 0x188(%rsp), %rdi
callq 0x22f0
leaq 0x70(%rsp), %rdi
leaq 0x188(%rsp), %rsi
pushq $0x2c
popq %rdx
callq 0x46f2
leaq 0x188(%rsp), %rdi
callq 0x2100
movl (%rbx), %edi
movl 0x70(%rsp), %ebp
movl %ebp, %esi
callq 0x2dbf
testl %eax, %eax
js 0x3581
movl %eax, %r14d
testl %ebp, %ebp
js 0x3283
movq 0x6faf(%rip), %rax      # 0xa228
movq %r14, %rcx
shlq $0x6, %rcx
movl %ebp, (%rax,%rcx)
movzwl 0x78(%rsp), %eax
testw %ax, %ax
js 0x32a0
movq 0x6f94(%rip), %rcx      # 0xa228
movq %r14, %rdx
shlq $0x6, %rdx
movw %ax, 0x8(%rcx,%rdx)
movl 0x7c(%rsp), %eax
testl %eax, %eax
js 0x32ba
movq 0x6f79(%rip), %rcx      # 0xa228
movq %r14, %rdx
shlq $0x6, %rdx
movl %eax, 0xc(%rcx,%rdx)
leaq 0x80(%rsp), %rbx
leaq 0x2dce(%rip), %rsi      # 0x6097
movq %rbx, %rdi
callq 0x49b9
testb %al, %al
je 0x32f3
movq 0x6f4c(%rip), %rax      # 0xa228
movq %r14, %rcx
shlq $0x6, %rcx
leaq (%rax,%rcx), %rdi
addq $0x10, %rdi
movq %rbx, %rsi
callq 0x2140
movb 0xa0(%rsp), %cl
movq 0x6f27(%rip), %rdx      # 0xa228
shlq $0x6, %r14
leaq (%rdx,%r14), %rax
movb %cl, 0x30(%rdx,%r14)
movl 0xa4(%rsp), %ecx
testl %ecx, %ecx
js 0x331c
movl %ecx, 0x34(%rax)
movl 0xa8(%rsp), %ecx
testl %ecx, %ecx
js 0x332a
movl %ecx, 0x38(%rax)
callq 0x2c04
pushq $-0x1
popq %rdi
callq 0x2f01
jmp 0x35ea
movq (%rsp), %rsi
addq $0x60, %rsi
leaq 0x148(%rsp), %rdi
callq 0x22f0
leaq 0x18(%rsp), %rdi
leaq 0x148(%rsp), %rsi
pushq $0x20
popq %rdx
callq 0x258c
leaq 0x148(%rsp), %rdi
callq 0x2100
movq 0x18(%rsp), %rdi
leaq 0x2d82(%rip), %rsi      # 0x6101
callq 0x498a
testb %al, %al
je 0x38d8
movq 0x18(%rsp), %rdi
addq $0x20, %rdi
leaq 0x2d69(%rip), %rsi      # 0x6105
callq 0x498a
testb %al, %al
je 0x35fc
movq 0x18(%rsp), %rdi
movq 0x40(%rdi), %rax
movq 0x48(%rdi), %rcx
cmpb $0xa, -0x1(%rax,%rcx)
jne 0x36c6
addq $0x40, %rdi
callq 0x22b0
jmp 0x33a9
movq (%rsp), %rsi
addq $0x60, %rsi
leaq 0x168(%rsp), %rdi
callq 0x22f0
leaq 0x70(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x2776
leaq 0x168(%rsp), %rdi
callq 0x2100
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %rax
movq %rax, %rcx
subq %rsi, %rcx
sarq $0x6, %rcx
leaq 0x4(%rsi), %rdx
subq $0x1, %rcx
jb 0x3428
movl (%rbx), %edi
movl %edi, (%rdx)
addq $0x40, %rdx
jmp 0x3418
xorl %r15d, %r15d
leaq 0x70(%rsp), %r14
movq 0x6df9(%rip), %rcx      # 0xa230
subq 0x6dea(%rip), %rcx      # 0xa228
sarq $0x6, %rcx
xorl %ebp, %ebp
cmpq %r15, %rcx
jbe 0x34d9
movq %r15, %r12
shlq $0x6, %r12
movslq %ebp, %r13
movq %rax, %rcx
subq %rsi, %rcx
sarq $0x6, %rcx
cmpq %r13, %rcx
jbe 0x34d1
movq 0x6dbb(%rip), %rdi      # 0xa228
addq %r12, %rdi
shlq $0x6, %r13
addq %r13, %rsi
callq 0x264f
movq 0x70(%rsp), %rsi
testb %al, %al
jne 0x34b6
movq 0x6d9c(%rip), %rax      # 0xa228
movl (%rax,%r12), %ecx
movl 0x4(%rax,%r12), %edi
movl (%rsi,%r13), %edx
cmpl (%rbx), %edi
jne 0x34a1
cmpl %edx, %ecx
je 0x34b6
cmpl %edx, %ecx
jne 0x34ae
addq %r12, %rax
cmpl $0x0, 0xc(%rax)
je 0x34b6
cmpl $0xf, 0xc(%rsi,%r13)
jl 0x34c8
addq %r13, %rsi
movq %r14, %rdi
callq 0x49b4
decl %ebp
movq 0x70(%rsp), %rsi
incl %ebp
movq 0x78(%rsp), %rax
jmp 0x3454
incq %r15
jmp 0x3430
leaq 0x70(%rsp), %r14
movslq %ebp, %rcx
movq %rax, %rdx
subq %rsi, %rdx
sarq $0x6, %rdx
cmpq %rcx, %rdx
jbe 0x351f
shlq $0x6, %rcx
movl (%rsi,%rcx), %edx
cmpl $-0x1, %edx
je 0x3504
cmpl 0x6d3e(%rip), %edx      # 0xa240
jne 0x351b
addq %rcx, %rsi
movq %r14, %rdi
callq 0x49b4
decl %ebp
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %rax
incl %ebp
jmp 0x34de
xorl %r15d, %r15d
leaq 0x6cff(%rip), %r14      # 0xa228
xorl %r12d, %r12d
subq %rsi, %rax
sarq $0x6, %rax
cmpq %r12, %rax
jbe 0x3566
incl 0xc(%rsi,%r15)
leaq (%rsi,%r15), %rax
movl (%rbx), %ecx
movl %ecx, 0x4(%rsi,%r15)
movq %r14, %rdi
movq %rax, %rsi
callq 0x4834
incq %r12
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %rax
addq $0x40, %r15
jmp 0x352c
callq 0x2c04
movl (%rbx), %edi
callq 0x2f01
leaq 0x70(%rsp), %rdi
callq 0x4860
jmp 0x31e0
xorl %ebp, %ebp
leaq 0x6c9e(%rip), %r14      # 0xa228
movslq %ebp, %rax
movq 0x6c9c(%rip), %rcx      # 0xa230
movq 0x6c8d(%rip), %rsi      # 0xa228
subq %rsi, %rcx
sarq $0x6, %rcx
cmpq %rax, %rcx
jbe 0x35c2
shlq $0x6, %rax
cmpl $-0x1, (%rsi,%rax)
jne 0x35be
addq %rax, %rsi
movq %r14, %rdi
callq 0x49b4
decl %ebp
incl %ebp
jmp 0x358a
movl (%rbx), %eax
movl %eax, 0x74(%rsp)
cmpl $0x0, 0x7c(%rsp)
jns 0x35d4
andl $0x0, 0x7c(%rsp)
leaq 0x6c4d(%rip), %rdi      # 0xa228
leaq 0x70(%rsp), %rsi
callq 0x4834
callq 0x2c04
leaq 0x80(%rsp), %rdi
callq 0x2100
jmp 0x31e0
movq 0x18(%rsp), %rdi
addq $0x20, %rdi
leaq 0x2b1c(%rip), %rsi      # 0x6128
callq 0x498a
testb %al, %al
je 0x38d8
movq 0x69a8(%rip), %rdi      # 0x9fc8
leaq 0x2b05(%rip), %rsi      # 0x612c
callq 0x2240
movq 0x18(%rsp), %rsi
addq $0x40, %rsi
leaq 0x128(%rsp), %rdi
callq 0x22f0
leaq 0x6bff(%rip), %rsi      # 0xa248
leaq 0x110(%rsp), %rdi
callq 0x4a58
leaq 0x128(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x30a8
movl %eax, %ebx
leaq 0x110(%rsp), %rdi
callq 0x4916
leaq 0x128(%rsp), %rdi
callq 0x2100
testl %ebx, %ebx
js 0x3862
movl %ebx, %eax
imulq $0xa0, %rax, %rax
xorl %ecx, %ecx
cmpq $0x80, %rcx
je 0x38d8
movq 0x18(%rsp), %rdx
movq 0x60(%rdx), %rdx
movb (%rdx,%rcx), %dl
movq 0x6b8e(%rip), %rsi      # 0xa248
addq %rax, %rsi
movb %dl, 0x20(%rcx,%rsi)
incq %rcx
jmp 0x369a
xorl %r15d, %r15d
movl $0xa0, %r12d
xorl %r13d, %r13d
movq 0x6b77(%rip), %rax      # 0xa250
movq 0x6b68(%rip), %rsi      # 0xa248
subq %rsi, %rax
cqto
idivq %r12
cmpq %rax, %r13
jae 0x38d8
movq 0x18(%rsp), %rdi
addq $0x40, %rdi
addq %r15, %rsi
callq 0x4261
testb %al, %al
jne 0x3712
incq %r13
addq $0xa0, %r15
jmp 0x36d2
movq 0x6b2f(%rip), %rdx      # 0xa248
addq %r15, %rdx
leaq 0x29e6(%rip), %rsi      # 0x6109
leaq 0x30(%rsp), %rdi
callq 0x49e2
leaq 0x2afb(%rip), %rdx      # 0x622f
leaq 0x50(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x49c3
movq 0x6afe(%rip), %rax      # 0xa248
leaq (%rax,%r15), %rdx
addq $0x20, %rdx
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x49c3
leaq 0x50(%rsp), %rdi
callq 0x2100
leaq 0x30(%rsp), %rdi
callq 0x2100
movl 0x6ac5(%rip), %ebp      # 0xa240
movq (%rsp), %rdi
addq $0x20, %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x4997
movq 0x70(%rsp), %r8
leaq 0x50(%rsp), %rdi
pushq $0x4
popq %rsi
movl %ebp, %edx
movl %eax, %ecx
callq 0x2880
movq 0x681e(%rip), %rdi      # 0x9fc8
leaq 0x2961(%rip), %rsi      # 0x6112
callq 0x2240
movq 0x8(%rsp), %rax
subq (%rsp), %rax
cmpq $0x80, %rax
jne 0x37f5
leaq 0x50(%rsp), %rdi
pushq $0x26
popq %rsi
callq 0x2200
movl 0x6a66(%rip), %esi      # 0xa240
leaq 0x30(%rsp), %rdi
callq 0x4881
leaq 0x50(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x23e0
jmp 0x3821
leaq 0x50(%rsp), %rdi
pushq $0x2c
popq %rsi
callq 0x2200
movl 0x6a38(%rip), %esi      # 0xa240
leaq 0x30(%rsp), %rdi
callq 0x4881
leaq 0x50(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x23e0
leaq 0x30(%rsp), %rdi
callq 0x2100
movl (%rbx), %ebx
movq 0x50(%rsp), %r14
movq %r14, %rdi
callq 0x20e0
movl %ebx, %edi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x2120
leaq 0x50(%rsp), %rdi
callq 0x2100
leaq 0x70(%rsp), %rdi
callq 0x2100
jmp 0x3993
leaq 0x80(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x18(%rsp), %rsi
addq $0x40, %rsi
leaq 0x70(%rsp), %rdi
callq 0x2140
pushq $0x20
popq %rax
movq 0x18(%rsp), %rcx
cmpq $0xa0, %rax
je 0x38aa
movq 0x60(%rcx), %rdx
movb -0x20(%rdx,%rax), %dl
movb %dl, 0x70(%rsp,%rax)
incq %rax
jmp 0x3891
leaq 0x6997(%rip), %rdi      # 0xa248
leaq 0x70(%rsp), %rsi
callq 0x4ab4
movq 0x6706(%rip), %rdi      # 0x9fc8
leaq 0x287b(%rip), %rsi      # 0x6144
callq 0x2240
leaq 0x70(%rsp), %rdi
callq 0x2100
movq (%rsp), %rdi
addq $0x40, %rdi
pushq $0xa
popq %rdx
xorl %esi, %esi
callq 0x4997
movl %eax, %edi
callq 0x2d4a
testl %eax, %eax
js 0x3993
movl %eax, %ebx
movq 0x8(%rsp), %rax
subq (%rsp), %rax
cmpq $0x80, %rax
jne 0x3936
pushq $0x26
popq %rsi
movq %r14, %rdi
callq 0x2200
movl 0x6923(%rip), %esi      # 0xa240
leaq 0x70(%rsp), %rdi
callq 0x4881
leaq 0x70(%rsp), %rsi
movq %r14, %rdi
callq 0x23e0
jmp 0x395e
pushq $0x2c
popq %rsi
movq %r14, %rdi
callq 0x2200
movl 0x68f9(%rip), %esi      # 0xa240
leaq 0x70(%rsp), %rdi
callq 0x4881
leaq 0x70(%rsp), %rsi
movq %r14, %rdi
callq 0x23e0
leaq 0x70(%rsp), %rdi
callq 0x2100
movl %ebx, %eax
movq 0x68b7(%rip), %rcx      # 0xa228
shlq $0x6, %rax
movl 0x4(%rcx,%rax), %ebx
movq (%r14), %r14
movq %r14, %rdi
callq 0x20e0
movl %ebx, %edi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x2120
leaq 0x18(%rsp), %rdi
callq 0x4240
jmp 0x31e0
jmp 0x39a4
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x2100
jmp 0x3a07
jmp 0x3a04
jmp 0x3a04
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x2100
jmp 0x39c9
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
jmp 0x3a16
jmp 0x3a4a
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x4916
jmp 0x39e7
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
jmp 0x3a16
jmp 0x3a4a
jmp 0x3a4a
jmp 0x39fb
jmp 0x39fb
jmp 0x39fb
movq %rax, %rbx
jmp 0x3a11
jmp 0x3a4a
jmp 0x3a4a
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x2100
leaq 0x70(%rsp), %rdi
callq 0x2100
jmp 0x3a4d
movq %rax, %rbx
leaq 0x168(%rsp), %rdi
jmp 0x3a68
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
jmp 0x3a68
movq %rax, %rbx
leaq 0x188(%rsp), %rdi
jmp 0x3a68
jmp 0x3a83
jmp 0x3a83
jmp 0x3a83
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x4240
jmp 0x3a97
jmp 0x3a8a
jmp 0x3a5d
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x2100
jmp 0x3a97
jmp 0x3a8a
movq %rax, %rbx
leaq 0x1a8(%rsp), %rdi
callq 0x2100
jmp 0x3a9f
movq %rax, %rbx
jmp 0x3a97
jmp 0x3a8a
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x4860
movq %rsp, %rdi
callq 0x4240
movq %rbx, %rdi
callq 0x23a0