// File: /workspace/github_cpp_makefile_star2_O2/SaadBazaz[P]RIP-Protocol-Simulation/ProxyRouter.cpp
// Function: main
int main()
{
	cout << "Which Unique Port do you want to use?\n";
	cin >> port;

	int sockfd = socket(AF_INET, SOCK_STREAM, 0);
	assert(sockfd != -1);

	printf("sockfd=%d\n", sockfd);

	struct sockaddr_in saddr, caddr;
	memset(&saddr, 0, sizeof(saddr));
	saddr.sin_family = AF_INET;
	saddr.sin_port = htons(port);
	saddr.sin_addr.s_addr = inet_addr("127.0.0.1");

	int res = bind(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
	assert(res != -1);

	//Create listening queue
	listen(sockfd, 5);

	//Define fdset collection
	fd_set fdset;

	//Define fds array
	int fds[MAXFD];
	int i = 0;
	for (; i < MAXFD; ++i)
	{
		fds[i] = -1;
	}


	//Add a file descriptor to the fds array
	fds_add(fds, sockfd);





{
	char option;
	
	do {

		cout << "Would you like to connect to any other routers? (y/n)\n";
		cin >> option;
		if (option == 'n' or option == 'N'){
			break;
		} 

		int router_port;
		cout<<"Enter the partner router's Unique Port: ";
		cin >> router_port;

		int sockfd_router = socket(AF_INET, SOCK_STREAM, 0);
		assert(sockfd_router != -1);

		//Set Address Information
		struct sockaddr_in saddr;
		memset(&saddr, 0, sizeof(saddr));
		saddr.sin_family = AF_INET;
		saddr.sin_port = htons(router_port);
		saddr.sin_addr.s_addr = inet_addr("127.0.0.1");

		//Link to router
		int res = connect(sockfd_router, (struct sockaddr *)&saddr, sizeof(saddr));
		assert(res != -1);

		TableRow myself(port, -1, "ROUTER");

		cout << "Sending this router's unique port to partner router..." << endl;
		string init_msg = constructNewMessage(UPDATE, port, router_port, (void *)&myself);
		send(sockfd_router, init_msg.c_str(), strlen(init_msg.c_str()), 0);

		cout << endl;


		TableRow table_row (router_port, sockfd_router, "ROUTER");
		table.push_back(table_row);

		fds_add(fds, sockfd_router);		

		printTable();

	}
	while(option != 'n' and option != 'N');


}


	while (1)
	{
		FD_ZERO(&fdset); //Clear the fdset array to 0

		int maxfd = -1;

		int i = 0;

		//For loop finds the maximum subscript for the ready event in the fds array
		for (; i < MAXFD; i++)
		{
			if (fds[i] == -1)
			{
				continue;
			}

			FD_SET(fds[i], &fdset);

			if (fds[i] > maxfd)
			{
				maxfd = fds[i];
			}
		}

		struct timeval tv = {5, 0}; //Set timeout of 5 seconds

		int n = select(maxfd + 1, &fdset, NULL, NULL, &tv); //Selectect system call, where we only focus on read events
		if (n == -1)										//fail
		{
			perror("select error");
		}
		else if (n == 0) //Timeout, meaning no file descriptor returned
		{
			printf(".\n");
			sendTableToAllRouters();
		}
		else //Ready event generation
		{
			//Because we only know the number of ready events by the return value of select, we don't know which events are ready.
			//Therefore, each file descriptor needs to be traversed for judgment
			for (i = 0; i < MAXFD; ++i)
			{
				if (fds[i] == -1) //If fds[i]==-1, the event is not ready
				{
					continue;
				}
				if (FD_ISSET(fds[i], &fdset)) //Determine if the event corresponding to the file descriptor is ready
				{

					//There are two kinds of cases for judging file descriptors
					try
					{
						if (fds[i] == sockfd) //A file descriptor is a socket, meaning accept if a new client requests a connection
						{
							//accept
							struct sockaddr_in caddr;
							int len = sizeof(caddr);

							int c = accept(sockfd, (struct sockaddr *)&caddr, (socklen_t *)&len); //Accept new client connections
							if (c < 0)
							{
								continue;
							}

							printf("accept c=%d\n", c);
							fds_add(fds, c); //Add the connection socket to the array where the file descriptor is stored
							TableRow newClient(-1, c, "UNKNOWN");
							table.push_back(newClient);
							printTable();

							sendTableToAllRouters();

						}
						else //Receive data recv when an existing client sends data
						{
							char buff[500] = {0};
							int res = recv(fds[i], buff, 499, 0);
							if (res <= 0)
							{
								close(fds[i]);
								auto index_vec = findAllRowsByFD(fds[i]);

								fds[i] = -1;
								printf("one client over\n");

								for (int i=0; i<index_vec.size(); i++)
								{
									table.erase(table.begin() + index_vec[i]);
									if (i+1!=index_vec.size())
										index_vec[i+1]--;
								}

								printTable();
								sendTableToAllRouters();
							}
							else
							{
								printf("recv(%d)=%s\n", fds[i], buff); //Output Client Sent Information
								handlePacket(fds[i], buff);
							}
						}
					}
					catch (...)
					{
						cout<<"Crashed!"<<endl;
						// send(fds[i], "BAD", 3, 0);
						sleep(5);
					}
				}
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x378, %rsp            # imm = 0x378
movq 0x64ef(%rip), %rdi      # 0x9fc8
leaq 0x2683(%rip), %rsi      # 0x6163
callq 0x2240
movq 0x64f4(%rip), %rdi      # 0x9fe0
leaq 0x674d(%rip), %rsi      # 0xa240
callq 0x2170
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x2070
cmpl $-0x1, %eax
jne 0x3b29
leaq 0x2679(%rip), %rdi      # 0x618a
leaq 0x267f(%rip), %rsi      # 0x6197
leaq 0x2688(%rip), %rcx      # 0x61a7
movl $0x1ae, %edx            # imm = 0x1AE
callq 0x2180
movl %eax, %ebx
leaq 0x2680(%rip), %rdi      # 0x61b2
movl %eax, %esi
xorl %eax, %eax
callq 0x2040
leaq 0x130(%rsp), %r14
andq $0x0, 0x8(%r14)
movw $0x2, (%r14)
movzwl 0x66eb(%rip), %eax      # 0xa240
rolw $0x8, %ax
movw %ax, 0x2(%r14)
leaq 0x2658(%rip), %rdi      # 0x61bd
callq 0x2260
movl %eax, 0x4(%r14)
pushq $0x10
popq %rdx
movl %ebx, %edi
movq %r14, %rsi
callq 0x2060
cmpl $-0x1, %eax
jne 0x3b9f
leaq 0x2640(%rip), %rdi      # 0x61c7
leaq 0x2609(%rip), %rsi      # 0x6197
leaq 0x2612(%rip), %rcx      # 0x61a7
movl $0x1b9, %edx            # imm = 0x1B9
callq 0x2180
pushq $0x5
popq %rsi
movl %ebx, 0x4(%rsp)
movl %ebx, %edi
callq 0x2290
xorl %eax, %eax
cmpq $0xa, %rax
je 0x3bbf
orl $-0x1, 0x40(%rsp,%rax,4)
incq %rax
jmp 0x3baf
leaq 0x40(%rsp), %rdi
movl 0x4(%rsp), %esi
callq 0x3aa7
movq 0x63f4(%rip), %r13      # 0x9fc8
movq 0x6405(%rip), %rbx      # 0x9fe0
leaq 0x20(%rsp), %r14
leaq 0xf0(%rsp), %r12
movq %r13, %rdi
leaq 0x25df(%rip), %rsi      # 0x61d1
callq 0x2240
movq %rbx, %rdi
leaq 0x3(%rsp), %rsi
callq 0x22e0
movb 0x3(%rsp), %al
andb $-0x21, %al
cmpb $0x4e, %al
je 0x3e15
movq %r13, %rdi
leaq 0x25ec(%rip), %rsi      # 0x6208
callq 0x2240
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
callq 0x2170
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x2070
cmpl $-0x1, %eax
je 0x416c
movl %eax, %r15d
leaq 0x14(%rsp), %rax
andl $0x0, 0x8(%rax)
andq $0x0, (%rax)
movw $0x2, 0x10(%rsp)
movzwl 0x8(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0x12(%rsp)
leaq 0x254d(%rip), %rdi      # 0x61bd
callq 0x2260
movl %eax, 0x14(%rsp)
movl %r15d, %edi
leaq 0x10(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x21e0
cmpl $-0x1, %eax
je 0x418b
movq %r12, %rbx
movq %r13, %r12
movq %r14, %r13
movl 0x659f(%rip), %ebp      # 0xa240
leaq 0x110(%rsp), %r14
movq %r14, %rdi
leaq 0x23ec(%rip), %rsi      # 0x609f
leaq 0x70(%rsp), %rdx
callq 0x48da
movl %ebp, 0x140(%rsp)
orl $-0x1, 0x144(%rsp)
andw $0x0, 0x148(%rsp)
andl $0x0, 0x14c(%rsp)
leaq 0x150(%rsp), %rdi
movq %r14, %rsi
callq 0x22f0
movb $0x0, 0x170(%rsp)
orq $-0x1, 0x174(%rsp)
movq %r14, %rdi
callq 0x2100
movq %r12, %rdi
leaq 0x2535(%rip), %rsi      # 0x6245
callq 0x2240
movq %rax, %rdi
callq 0x2110
movl 0x651d(%rip), %edx      # 0xa240
movl 0x8(%rsp), %ecx
movq %r13, %r14
movq %r13, %rdi
pushq $0x2
popq %rsi
leaq 0x140(%rsp), %r8
callq 0x2880
movq 0x20(%rsp), %rbp
movq %rbp, %rdi
callq 0x20e0
movl %r15d, %edi
movq %rbp, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x2120
movq %r12, %r13
movq %r12, %rdi
callq 0x2110
movl 0x8(%rsp), %ebp
movq %rbx, %r12
movq %rbx, %rdi
leaq 0x2329(%rip), %rsi      # 0x609f
leaq 0xf(%rsp), %rdx
callq 0x48da
movl %ebp, 0x70(%rsp)
movl %r15d, 0x74(%rsp)
andw $0x0, 0x78(%rsp)
andl $0x0, 0x7c(%rsp)
leaq 0x80(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x22f0
movb $0x0, 0xa0(%rsp)
orq $-0x1, 0xa4(%rsp)
movq %r12, %rdi
callq 0x2100
leaq 0x6461(%rip), %rdi      # 0xa228
leaq 0x70(%rsp), %rsi
callq 0x4834
leaq 0x40(%rsp), %rdi
movl %r15d, %esi
callq 0x3aa7
callq 0x2c04
movq %rbp, %rdi
callq 0x2100
movq %r14, %rdi
callq 0x2100
leaq 0x150(%rsp), %rdi
callq 0x2100
movb 0x3(%rsp), %al
andb $-0x21, %al
cmpb $0x4e, %al
movq 0x61d1(%rip), %rbx      # 0x9fe0
jne 0x3be8
pushq $0x40
popq %r13
leaq 0x6408(%rip), %r15      # 0xa228
pushq $0x1
popq %r9
xorl %eax, %eax
cmpq $0x10, %rax
jne 0x3e67
xorl %r8d, %r8d
pushq $-0x1
popq %rdi
cmpq $0xa, %r8
je 0x3e72
movl 0x40(%rsp,%r8,4), %esi
cmpl $-0x1, %esi
je 0x3e62
movl %esi, %eax
cltd
idivl %r13d
movslq %eax, %rdx
shll $0x6, %eax
movl %esi, %ecx
subl %eax, %ecx
movq %r9, %rax
shlq %cl, %rax
orq %rax, 0x70(%rsp,%rdx,8)
cmpl %edi, %esi
cmovgl %esi, %edi
incq %r8
jmp 0x3e32
andq $0x0, 0x70(%rsp,%rax,8)
incq %rax
jmp 0x3e26
movups 0x21b7(%rip), %xmm0     # 0x6030
movaps %xmm0, 0x10(%rsp)
incl %edi
leaq 0x70(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq 0x10(%rsp), %r8
callq 0x2230
testl %eax, %eax
je 0x413f
cmpl $-0x1, %eax
je 0x415e
xorl %r14d, %r14d
movl 0x4(%rsp), %edi
cmpq $0xa, %r14
je 0x4153
movl 0x40(%rsp,%r14,4), %r12d
cmpl $-0x1, %r12d
je 0x40c7
movl %r12d, %eax
cltd
idivl %r13d
cltq
movq 0x70(%rsp,%rax,8), %rax
btq %rdx, %rax
jae 0x40c7
cmpl %edi, %r12d
jne 0x3fae
movl $0x10, 0x8(%rsp)
leaq 0x20(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x20a0
movl %eax, %ebp
testl %eax, %eax
js 0x40c3
leaq 0x237c(%rip), %rdi      # 0x6289
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
leaq 0x40(%rsp), %rdi
movl %ebp, %esi
callq 0x3aa7
leaq 0x358(%rsp), %r12
movq %r12, %rdi
leaq 0x2362(%rip), %rsi      # 0x6296
leaq 0x3(%rsp), %rdx
callq 0x48da
orl $-0x1, 0x140(%rsp)
movl %ebp, 0x144(%rsp)
andw $0x0, 0x148(%rsp)
andl $0x0, 0x14c(%rsp)
leaq 0x150(%rsp), %rdi
movq %r12, %rsi
callq 0x22f0
movb $0x0, 0x170(%rsp)
orq $-0x1, 0x174(%rsp)
movq %r12, %rdi
callq 0x2100
movq %r15, %rdi
leaq 0x140(%rsp), %rsi
callq 0x4834
callq 0x2c04
pushq $-0x1
popq %rdi
callq 0x2f01
leaq 0x150(%rsp), %rdi
jmp 0x4025
movl $0x1f4, %edx            # imm = 0x1F4
leaq 0x140(%rsp), %rbp
movq %rbp, %rdi
xorl %esi, %esi
callq 0x2150
movl $0x1f3, %edx            # imm = 0x1F3
movl %r12d, %edi
movq %rbp, %rsi
xorl %ecx, %ecx
callq 0x20b0
leaq (%rsp,%r14,4), %rbp
addq $0x40, %rbp
testl %eax, %eax
jle 0x402f
leaq 0x22b4(%rip), %rdi      # 0x629e
movl %r12d, %esi
leaq 0x140(%rsp), %r12
movq %r12, %rdx
xorl %eax, %eax
callq 0x2040
leaq 0x338(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
leaq 0x20(%rsp), %rdx
callq 0x48da
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3152
movq %rbx, %rdi
callq 0x2100
jmp 0x40c3
movl %r12d, %edi
callq 0x23f0
leaq 0x20(%rsp), %rdi
movl %r12d, %esi
callq 0x2e85
orl $-0x1, (%rbp)
leaq 0x22ae(%rip), %rdi      # 0x62fd
callq 0x2330
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
xorl %r12d, %r12d
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r12, %rcx
jbe 0x40ac
movslq (%rax,%r12,4), %rsi
shlq $0x6, %rsi
addq 0x61ac(%rip), %rsi      # 0xa228
movq %r15, %rdi
callq 0x49b4
leaq 0x1(%r12), %rdx
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, %rsi
subq %rax, %rsi
sarq $0x2, %rsi
cmpq %rsi, %rdx
je 0x40a7
decl 0x4(%rax,%r12,4)
movq %rdx, %r12
jmp 0x4061
callq 0x2c04
pushq $-0x1
popq %rdi
callq 0x2f01
leaq 0x20(%rsp), %rdi
callq 0x52a0
movl 0x4(%rsp), %edi
incq %r14
jmp 0x3eab
jmp 0x40e9
movq %rax, %rbp
movq %rbx, %rdi
jmp 0x40f9
movq %rax, %rbp
movq %r12, %rdi
jmp 0x40f9
jmp 0x40e9
jmp 0x40e9
jmp 0x4100
jmp 0x40e9
movq %rax, %rbp
jmp 0x410d
movq %rax, %rbp
leaq 0x150(%rsp), %rdi
callq 0x2100
jmp 0x410d
movq %rax, %rbp
leaq 0x20(%rsp), %rdi
callq 0x52a0
movq %rbp, %rdi
callq 0x20d0
movq 0x5eac(%rip), %rdi      # 0x9fc8
leaq 0x2188(%rip), %rsi      # 0x62ab
callq 0x2240
movq %rax, %rdi
callq 0x2110
pushq $0x5
popq %rdi
callq 0x21b0
callq 0x2380
jmp 0x40c3
leaq 0x1fb9(%rip), %rdi      # 0x60ff
callq 0x2330
pushq $-0x1
popq %rdi
callq 0x2f01
xorl %eax, %eax
pushq $0x1
popq %r9
jmp 0x3e26
leaq 0x2117(%rip), %rdi      # 0x627c
callq 0x21f0
jmp 0x4153
leaq 0x20be(%rip), %rdi      # 0x6231
leaq 0x201d(%rip), %rsi      # 0x6197
leaq 0x2026(%rip), %rcx      # 0x61a7
movl $0x1e1, %edx            # imm = 0x1E1
callq 0x2180
leaq 0x2035(%rip), %rdi      # 0x61c7
leaq 0x1ffe(%rip), %rsi      # 0x6197
leaq 0x2007(%rip), %rcx      # 0x61a7
movl $0x1ec, %edx            # imm = 0x1EC
callq 0x2180
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
jmp 0x41d3
jmp 0x41ec
jmp 0x41fb
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
jmp 0x4206
movq %rax, %rbx
jmp 0x420b
movq %rax, %rbx
movq %rbp, %rdi
callq 0x2100
jmp 0x41ef
movq %rax, %rbx
callq 0x2380
jmp 0x420b
movq %rax, %rdi
callq 0x4ae3
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x2100
jmp 0x41fe
movq %rax, %rbx
leaq 0x150(%rsp), %rdi
callq 0x2100
movq %rbx, %rdi
callq 0x23a0
nop