// File: /workspace/github_cpp_makefile_star2_O2/sjtuwrk[P]SunwayMR/include/Messaging.hpp
// Function: Messaging::listenMessage(int)
void Messaging::listenMessage(int listenPort)
{
	listenStatus = NA;

	// init socket
	int server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (server_sockfd < 0) {
		listenStatus = FAILURE;
		pthread_mutex_unlock(&mutex_listen_status);
		return;
	}

	struct sockaddr_in server_address;

	server_address.sin_family = AF_INET;
	server_address.sin_addr.s_addr =  htonl(INADDR_ANY);
	server_address.sin_port = htons(listenPort);
	int server_len = sizeof(server_address);

	// bind
	int rb = bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
	if (rb < 0)
	{
		listenStatus = FAILURE;
		pthread_mutex_unlock(&mutex_listen_status);
		return;
	}

	//listen
	int n = listen(server_sockfd, 36);
	if (n < 0)
	{
		listenStatus = FAILURE;
		pthread_mutex_unlock(&mutex_listen_status);
		return;
	}

	listenStatus = SUCCESS;
	pthread_mutex_unlock(&mutex_listen_status);

	stringstream listenInfo;
	listenInfo << "Messaging: listening on port [" << listenPort << "]";
	Logging::logInfo(listenInfo.str());

	struct sockaddr_in client_address;
	int client_len = sizeof(sockaddr_in);
	while (1) {
		int client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, (socklen_t *)&client_len);
		if (client_sockfd < 0) {
			stringstream ss;
			ss << "Messaging::listenMessage: accept failed, errno: " << errno;
			Logging::logWarning(ss.str());
			continue;
		}
		string ip = inet_ntoa(client_address.sin_addr);
		// thread data encapsulation
		struct xyz_messaging_listen_thread_data_ *td =
				new xyz_messaging_listen_thread_data_(
						this, listenPort, ip, client_sockfd);
		// create a thread
		pthread_t worker;
		if (pthread_create(&worker, NULL, messageHandler, (void *)td) != 0) {
			Logging::logError("Messaging: failed to create new thread while listening");
			close(client_sockfd);
		}
	}

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x388, %rsp            # imm = 0x388
movl %esi, %ebp
movq %rdi, %rbx
andl $0x0, 0xe8(%rdi)
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x30d0
testl %eax, %eax
js 0x57f9
movl %eax, %r14d
leaq 0x68(%rsp), %rsi
movw $0x2, (%rsi)
andl $0x0, 0x4(%rsi)
movl %ebp, %eax
rolw $0x8, %ax
movw %ax, 0x2(%rsi)
pushq $0x10
popq %rdx
movl %r14d, %edi
callq 0x3070
testl %eax, %eax
js 0x5820
pushq $0x24
popq %rsi
movl %r14d, %edi
callq 0x33c0
leaq 0x8(%rbx), %rdi
testl %eax, %eax
jns 0x5848
movl $0x2, 0xe8(%rbx)
jmp 0x5831
movl $0x2, 0xe8(%rbx)
addq $0x8, %rbx
movq %rbx, %rdi
addq $0x388, %rsp            # imm = 0x388
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3280
movl $0x2, 0xe8(%rbx)
addq $0x8, %rbx
movq %rbx, %rdi
callq 0x3280
addq $0x388, %rsp            # imm = 0x388
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, 0xe8(%rbx)
callq 0x3280
leaq 0x200(%rsp), %rdi
callq 0x3230
leaq 0x210(%rsp), %rdi
leaq 0x69ca(%rip), %rsi      # 0xc23d
callq 0x3360
movq %rax, %rdi
movl %ebp, %esi
callq 0x35e0
leaq 0x704b(%rip), %rsi      # 0xc8d4
movq %rax, %rdi
callq 0x3360
leaq 0x218(%rsp), %rsi
leaq 0x78(%rsp), %rdi
callq 0x3550
leaq 0x78(%rsp), %rdi
callq 0x4dbc
leaq 0x78(%rsp), %rdi
callq 0x36e8
leaq 0xc(%rsp), %rax
movl $0x10, (%rax)
movl %r14d, %edi
leaq 0x38(%rsp), %rsi
leaq 0xc(%rsp), %rdx
callq 0x3130
movl %eax, %r13d
testl %eax, %eax
js 0x5993
movl 0x3c(%rsp), %edi
callq 0x3180
leaq 0x78(%rsp), %rdi
movq %rax, %rsi
leaq 0x10(%rsp), %rdx
callq 0x915a
pushq $0x38
popq %rdi
callq 0x3370
movq %rax, %r15
leaq 0x48(%rsp), %r12
movq %r12, %rdi
leaq 0x78(%rsp), %rsi
callq 0x3490
movq %rbx, (%r15)
movl %ebp, 0x8(%r15)
movq %r15, %rdi
addq $0x10, %rdi
movq %r12, %rsi
callq 0x3490
movl %r13d, 0x30(%r15)
movq %r12, %rdi
callq 0x36e8
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
leaq 0x133(%rip), %rdx       # 0x5a7a
movq %r15, %rcx
callq 0x3640
testl %eax, %eax
je 0x5984
leaq 0x10(%rsp), %r15
movq %r15, %rdi
leaq 0x692b(%rip), %rsi      # 0xc28d
leaq 0xb(%rsp), %rdx
callq 0x915a
movq %r15, %rdi
callq 0x4efc
movq %r15, %rdi
callq 0x36e8
movl %r13d, %edi
callq 0x36c0
leaq 0x78(%rsp), %rdi
callq 0x36e8
jmp 0x58c2
leaq 0x78(%rsp), %rdi
callq 0x3230
leaq 0x88(%rsp), %rdi
leaq 0x68b0(%rip), %rsi      # 0xc25c
callq 0x3360
movq %rax, %r15
callq 0x3050
movl (%rax), %esi
movq %r15, %rdi
callq 0x35e0
leaq 0x10(%rsp), %r15
movq %r15, %rdi
leaq 0x90(%rsp), %rsi
callq 0x3550
movq %r15, %rdi
callq 0x4e5c
movq %r15, %rdi
callq 0x36e8
leaq 0x78(%rsp), %rdi
callq 0x3260
jmp 0x58c2
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x36e8
jmp 0x5a43
jmp 0x5a40
jmp 0x5a62
jmp 0x5a51
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x36e8
jmp 0x5a54
jmp 0x5a51
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x36e8
jmp 0x5a2f
movq %rax, %rbx
pushq $0x38
popq %rsi
movq %r15, %rdi
callq 0x3380
jmp 0x5a54
jmp 0x5a51
jmp 0x5a62
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x3260
jmp 0x5a65
jmp 0x5a62
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x36e8
jmp 0x5a65
jmp 0x5a62
movq %rax, %rbx
leaq 0x200(%rsp), %rdi
callq 0x3260
movq %rbx, %rdi
callq 0x3610