// File: /workspace/github_cpp_makefile_star2_O3/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 0x63bc(%rip), %r15      # 0xafd0
leaq 0x3588(%rip), %rsi      # 0x81a3
movl $0x26, %edx
movq %r15, %rdi
callq 0x22b0
movq 0x63b1(%rip), %rdi      # 0xafe0
leaq 0x660a(%rip), %rsi      # 0xb240
callq 0x2170
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2080
cmpl $-0x1, %eax
jne 0x4c70
leaq 0x3572(%rip), %rdi      # 0x81ca
leaq 0x3578(%rip), %rsi      # 0x81d7
leaq 0x3581(%rip), %rcx      # 0x81e7
movl $0x1ae, %edx            # imm = 0x1AE
callq 0x2180
movl %eax, %ebp
leaq 0x3579(%rip), %rdi      # 0x81f2
movl %eax, %esi
xorl %eax, %eax
callq 0x2050
leaq 0x368(%rsp), %rbx
movq $0x0, 0x8(%rbx)
movw $0x2, (%rbx)
movzwl 0x65a2(%rip), %eax      # 0xb240
rolw $0x8, %ax
movw %ax, 0x2(%rbx)
leaq 0x3550(%rip), %rdi      # 0x81fd
callq 0x2260
movl %eax, 0x4(%rbx)
movl %ebp, %edi
movq %rbx, %rsi
movl $0x10, %edx
callq 0x2070
cmpl $-0x1, %eax
jne 0x4ce8
leaq 0x3537(%rip), %rdi      # 0x8207
leaq 0x3500(%rip), %rsi      # 0x81d7
leaq 0x3509(%rip), %rcx      # 0x81e7
movl $0x1b9, %edx            # imm = 0x1B9
callq 0x2180
movl %ebp, %edi
movl $0x5, %esi
callq 0x2290
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0xb0(%rsp)
movdqa %xmm0, 0xa0(%rsp)
movq $-0x1, 0xc0(%rsp)
xorl %eax, %eax
cmpl $-0x1, 0xa0(%rsp,%rax,4)
je 0x4d2d
incq %rax
cmpq $0xa, %rax
jne 0x4d18
jmp 0x4d34
movl %ebp, 0xa0(%rsp,%rax,4)
movq 0x62a5(%rip), %r12      # 0xafe0
leaq 0x339d(%rip), %rbx      # 0x80df
leaq 0x339c(%rip), %r14      # 0x80e5
movl $0x36, %edx
movq %r15, %rdi
leaq 0x34b9(%rip), %rsi      # 0x8211
callq 0x22b0
movq %r12, %rdi
leaq 0xb(%rsp), %rsi
callq 0x22f0
movb 0xb(%rsp), %al
andb $-0x21, %al
cmpb $0x4e, %al
je 0x50c7
movl $0x28, %edx
movq %r15, %rdi
leaq 0x34c1(%rip), %rsi      # 0x8248
callq 0x22b0
movq %r12, %rdi
leaq 0xc(%rsp), %rsi
callq 0x2170
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2080
cmpl $-0x1, %eax
je 0x5595
movl %eax, %r13d
leaq 0x34(%rsp), %rax
movl $0x0, 0x8(%rax)
movq $0x0, (%rax)
movw $0x2, 0x30(%rsp)
movzwl 0xc(%rsp), %eax
rolw $0x8, %ax
movw %ax, 0x32(%rsp)
leaq 0x3418(%rip), %rdi      # 0x81fd
callq 0x2260
movl %eax, 0x34(%rsp)
movl %r13d, %edi
leaq 0x30(%rsp), %rsi
movl $0x10, %edx
callq 0x21f0
cmpl $-0x1, %eax
je 0x55b4
movl 0x6430(%rip), %r15d     # 0xb240
leaq 0x90(%rsp), %rax
movq %rax, 0x80(%rsp)
leaq 0x80(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x725c
movl %r15d, 0x170(%rsp)
movl $0xffffffff, 0x174(%rsp) # imm = 0xFFFFFFFF
movw $0x0, 0x178(%rsp)
movl $0x0, 0x17c(%rsp)
leaq 0x190(%rsp), %rax
movq %rax, 0x180(%rsp)
movq 0x80(%rsp), %rsi
movq 0x88(%rsp), %rdx
addq %rsi, %rdx
leaq 0x180(%rsp), %rdi
callq 0x71c6
movb $0x0, 0x1a0(%rsp)
movq $-0x1, 0x1a4(%rsp)
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x4ec4
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x2270
movl $0x36, %edx
movq 0x6100(%rip), %rdi      # 0xafd0
leaq 0x33ae(%rip), %rsi      # 0x8285
callq 0x22b0
movq 0x60ed(%rip), %rcx      # 0xafd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x2240
movsbl %al, %esi
movq 0x60cf(%rip), %rdi      # 0xafd0
callq 0x2030
movq %rax, %rdi
callq 0x2190
movl 0x632c(%rip), %edx      # 0xb240
movl 0xc(%rsp), %ecx
leaq 0x10(%rsp), %rdi
movl $0x2, %esi
leaq 0x170(%rsp), %r8
callq 0x2a12
movq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x2110
movl %r13d, %edi
movq %r15, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x2120
movq 0x607d(%rip), %rcx      # 0xafd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x2240
movsbl %al, %esi
movq 0x605f(%rip), %rdi      # 0xafd0
callq 0x2030
movq %rax, %rdi
callq 0x2190
movl 0xc(%rsp), %r15d
leaq 0x70(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0x60(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x725c
movl %r15d, 0xf0(%rsp)
movl %r13d, 0xf4(%rsp)
movw $0x0, 0xf8(%rsp)
movl $0x0, 0xfc(%rsp)
leaq 0x110(%rsp), %r15
movq %r15, 0x100(%rsp)
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
addq %rsi, %rdx
leaq 0x100(%rsp), %rdi
callq 0x71c6
movb $0x0, 0x120(%rsp)
movq $-0x1, 0x124(%rsp)
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x501c
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x2270
leaq 0x6205(%rip), %rdi      # 0xb228
leaq 0xf0(%rsp), %rsi
callq 0x6d68
xorl %eax, %eax
cmpl $-0x1, 0xa0(%rsp,%rax,4)
je 0x5047
incq %rax
cmpq $0xa, %rax
jne 0x5032
jmp 0x504f
movl %r13d, 0xa0(%rsp,%rax,4)
callq 0x33da
movq 0x100(%rsp), %rdi
cmpq %r15, %rdi
je 0x5071
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x2270
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x508d
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2270
movq 0x180(%rsp), %rdi
leaq 0x190(%rsp), %rax
cmpq %rax, %rdi
je 0x50b2
movq 0x190(%rsp), %rsi
incq %rsi
callq 0x2270
movb 0xb(%rsp), %al
andb $-0x21, %al
cmpb $0x4e, %al
movq 0x5f0f(%rip), %r15      # 0xafd0
jne 0x4d49
leaq 0x615a(%rip), %r12      # 0xb228
leaq 0x170(%rsp), %r14
pxor %xmm0, %xmm0
movdqa %xmm0, 0x160(%rsp)
movdqa %xmm0, 0x150(%rsp)
movdqa %xmm0, 0x140(%rsp)
movdqa %xmm0, 0x130(%rsp)
movdqa %xmm0, 0x120(%rsp)
movdqa %xmm0, 0x110(%rsp)
movdqa %xmm0, 0x100(%rsp)
movdqa %xmm0, 0xf0(%rsp)
xorl %eax, %eax
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl 0xa0(%rsp,%rax,4), %edx
cmpl $-0x1, %edx
je 0x5165
leal 0x3f(%rdx), %esi
testl %edx, %edx
cmovnsl %edx, %esi
movl %esi, %r8d
andl $-0x40, %r8d
movl %edx, %ecx
subl %r8d, %ecx
movl $0x1, %r8d
shlq %cl, %r8
sarl $0x6, %esi
movslq %esi, %rcx
orq %r8, 0xf0(%rsp,%rcx,8)
cmpl %edi, %edx
cmovgl %edx, %edi
incq %rax
cmpq $0xa, %rax
jne 0x5129
movdqu 0x2efa(%rip), %xmm0     # 0x8070
movdqa %xmm0, 0x30(%rsp)
incl %edi
leaq 0xf0(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq 0x30(%rsp), %r8
callq 0x2230
testl %eax, %eax
je 0x5569
cmpl $-0x1, %eax
je 0x5584
xorl %ebx, %ebx
movl 0xa0(%rsp,%rbx,4), %r13d
cmpl $-0x1, %r13d
je 0x5468
leal 0x3f(%r13), %eax
testl %r13d, %r13d
cmovnsl %r13d, %eax
sarl $0x6, %eax
cltq
movq 0xf0(%rsp,%rax,8), %rax
btq %r13, %rax
jae 0x5468
cmpl %ebp, %r13d
jne 0x5233
movl $0x10, 0xc(%rsp)
movl %ebp, %edi
leaq 0x10(%rsp), %rsi
leaq 0xc(%rsp), %rdx
callq 0x20c0
movl %eax, %r15d
testl %eax, %eax
js 0x5468
leaq 0x30be(%rip), %rdi      # 0x82c9
movl %r15d, %esi
xorl %eax, %eax
callq 0x2050
xorl %eax, %eax
cmpl $-0x1, 0xa0(%rsp,%rax,4)
je 0x537d
incq %rax
cmpq $0xa, %rax
jne 0x5217
jmp 0x5385
movl $0x1f4, %edx            # imm = 0x1F4
movq %r14, %rdi
xorl %esi, %esi
callq 0x2150
movl $0x1f3, %edx            # imm = 0x1F3
movl %r13d, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x20d0
leaq (%rsp,%rbx,4), %r15
addq $0xa0, %r15
testl %eax, %eax
jle 0x52de
leaq 0x3074(%rip), %rdi      # 0x82de
movl %r13d, %esi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2050
leaq 0xe0(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq %r14, %rdi
callq 0x2110
leaq (%rsp,%rax), %rdx
addq $0x170, %rdx            # imm = 0x170
leaq 0xd0(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x725c
movq %r15, %rdi
movq %r13, %rsi
callq 0x3c2c
movq 0xd0(%rsp), %rdi
leaq 0xe0(%rsp), %rax
cmpq %rax, %rdi
je 0x5468
movq 0xe0(%rsp), %rsi
jmp 0x5460
movl %r13d, %edi
callq 0x23f0
leaq 0x10(%rsp), %rdi
movl %r13d, %esi
callq 0x3782
movl $0xffffffff, (%r15)     # imm = 0xFFFFFFFF
leaq 0x303c(%rip), %rdi      # 0x833d
callq 0x2340
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x5353
xorl %r15d, %r15d
movslq (%rax,%r15,4), %rsi
shlq $0x6, %rsi
addq 0x5f04(%rip), %rsi      # 0xb228
movq %r12, %rdi
callq 0x78e6
leaq 0x1(%r15), %rcx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
cmpq %rdx, %rcx
je 0x534b
decl 0x4(%rax,%r15,4)
movq %rcx, %r15
cmpq %rcx, %rdx
ja 0x5315
callq 0x33da
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3842
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x5468
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
jmp 0x5463
movl %r15d, 0xa0(%rsp,%rax,4)
leaq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x2f3b(%rip), %rsi      # 0x82d6
leaq 0x2f3b(%rip), %rdx      # 0x82dd
callq 0x725c
movl $0xffffffff, 0x170(%rsp) # imm = 0xFFFFFFFF
movl %r15d, 0x174(%rsp)
movw $0x0, 0x178(%rsp)
movl $0x0, 0x17c(%rsp)
leaq 0x190(%rsp), %rax
movq %rax, 0x180(%rsp)
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
addq %rsi, %rdx
leaq 0x180(%rsp), %rdi
callq 0x71c6
movb $0x0, 0x1a0(%rsp)
movq $-0x1, 0x1a4(%rsp)
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x5429
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2270
movq %r12, %rdi
movq %r14, %rsi
callq 0x6d68
callq 0x33da
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3842
movq 0x180(%rsp), %rdi
leaq 0x190(%rsp), %rax
cmpq %rax, %rdi
je 0x5468
movq 0x190(%rsp), %rsi
incq %rsi
callq 0x2270
incq %rbx
cmpq $0xa, %rbx
jne 0x51a7
jmp 0x50d6
jmp 0x54bf
movq %rax, %r15
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x5503
movq 0x50(%rsp), %rsi
jmp 0x54e4
movq %rax, %r15
movq 0xd0(%rsp), %rdi
leaq 0xe0(%rsp), %rax
cmpq %rax, %rdi
je 0x5503
movq 0xe0(%rsp), %rsi
jmp 0x54e4
jmp 0x54bf
jmp 0x54bf
jmp 0x54e9
jmp 0x54bf
movq %rax, %r15
jmp 0x5503
movq %rax, %r15
movq 0x180(%rsp), %rdi
leaq 0x190(%rsp), %rax
cmpq %rax, %rdi
je 0x5503
movq 0x190(%rsp), %rsi
incq %rsi
jmp 0x54fe
movq %rax, %r15
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x5503
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2270
movq %r15, %rdi
callq 0x2100
movl $0x8, %edx
movq 0x5ab9(%rip), %rdi      # 0xafd0
leaq 0x2dcd(%rip), %rsi      # 0x82eb
callq 0x22b0
movq 0x5aa6(%rip), %rcx      # 0xafd0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x2240
movsbl %al, %esi
movq 0x5a88(%rip), %rdi      # 0xafd0
callq 0x2030
movq %rax, %rdi
callq 0x2190
movl $0x5, %edi
callq 0x21c0
callq 0x2380
jmp 0x5468
leaq 0x2bcf(%rip), %rdi      # 0x813f
callq 0x2340
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3842
jmp 0x50d6
leaq 0x2d31(%rip), %rdi      # 0x82bc
callq 0x2200
jmp 0x50d6
leaq 0x2cd5(%rip), %rdi      # 0x8271
leaq 0x2c34(%rip), %rsi      # 0x81d7
leaq 0x2c3d(%rip), %rcx      # 0x81e7
movl $0x1e1, %edx            # imm = 0x1E1
callq 0x2180
leaq 0x2c4c(%rip), %rdi      # 0x8207
leaq 0x2c15(%rip), %rsi      # 0x81d7
leaq 0x2c1e(%rip), %rcx      # 0x81e7
movl $0x1ec, %edx            # imm = 0x1EC
callq 0x2180
movq %rax, %rbx
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x5655
movq 0x70(%rsp), %rsi
jmp 0x5636
jmp 0x5652
jmp 0x5673
movq %rax, %rbx
movq 0x80(%rsp), %rdi
leaq 0x90(%rsp), %rax
cmpq %rax, %rdi
je 0x569b
movq 0x90(%rsp), %rsi
jmp 0x5693
movq %rax, %rbx
jmp 0x569b
movq %rax, %rbx
movq 0x100(%rsp), %rdi
cmpq %r15, %rdi
je 0x5655
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x2270
jmp 0x5655
movq %rax, %rbx
callq 0x2380
jmp 0x569b
movq %rax, %rdi
callq 0x714d
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x5676
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2270
jmp 0x5676
movq %rax, %rbx
movq 0x180(%rsp), %rdi
leaq 0x190(%rsp), %rax
cmpq %rax, %rdi
je 0x569b
movq 0x190(%rsp), %rsi
incq %rsi
callq 0x2270
movq %rbx, %rdi
callq 0x23a0
nop