// File: sjtuwrk[P]SunwayMR/tools/SunwayMRHelper.hpp
// Function: SunwayMRHelper::messageReceived(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
void SunwayMRHelper::messageReceived(int localListenPort, string fromHost, int msgType, string &msg) {
	if (localListenPort != this->listenPort || fromHost == "" || msg == "") return;

	stringstream received;
	received << "SunwayMRHelper: listening port: " << localListenPort
			<< ", message received from: " << fromHost
			<< ", message type: " << msgType
			<< ", message content: " << msg;
	Logging::logVerbose(received.str());

	switch(msgType) {
	case HOST_RESOURCE_INFO:
	{
		vector<string> ss = splitString(msg, ' ');
		if (ss.size() == 3) {
			HostResource hr = {
					fromHost,
					atoi(ss[0].c_str()),
					atoi(ss[1].c_str()),
					atoi(ss[2].c_str())
			};
			update(hr);
		}
		break;
	}

	case FILE_INFO:
	{
		vector<string> ss = splitString(msg, ' '); // msg: fileUID, appUID, fileName
		if (ss.size() == 3) {
			int file_uid = atoi(ss[0].c_str());
			fileInfoMap[file_uid] = msg;
		}
		break;
	}

	case SHELL_COMMAND:
	{
		int ret = system(msg.c_str());
		if (ret == -1) {
			stringstream error;
			error << "SunwayMRHelper:: failed to execute shell: " << endl << msg.c_str() << endl;
			Logging::logError(error.str());
		}
		break;
	}

	default:
	{
		if(fileInfoMap.find(msgType) != fileInfoMap.end()) {
			string file_info = fileInfoMap[msgType];
			vector<string> vs = splitString(file_info, ' ');
			stringstream sstr;
			sstr << fileSaveDir << vs[1] << "/";
			bool ret = writeFile(sstr.str(), vs[2], msg);
			if (!ret) {
				stringstream error;
				error << "SunwayMRHelper: unable to write file: " << sstr.str() << vs[2];
				Logging::	logError(error.str());
			}
			fileInfoMap.erase(msgType);
			Logging::logInfo("SunwayMRHelper: file received: "
					+ sstr.str() + vs[2]);
		}

	}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x940, %rsp            # imm = 0x940
movq %rdx, -0x890(%rbp)
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movq -0x8(%rbp), %rcx
movq %rcx, -0x888(%rbp)
movl -0xc(%rbp), %eax
cmpl 0x13c(%rcx), %eax
jne 0xcac7
movq -0x890(%rbp), %rdi
leaq 0xd5d7(%rip), %rsi      # 0x1a07f
callq 0xef60
testb $0x1, %al
jne 0xcac7
movq -0x28(%rbp), %rdi
leaq 0xd5c3(%rip), %rsi      # 0x1a07f
callq 0xef60
testb $0x1, %al
jne 0xcac7
jmp 0xcacc
jmp 0xd4b2
leaq -0x1b0(%rbp), %rdi
callq 0x4280
leaq -0x1a0(%rbp), %rdi
leaq 0xdc73(%rip), %rsi      # 0x1a759
callq 0x43f0
movq %rax, -0x898(%rbp)
jmp 0xcaf4
movq -0x898(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0x4750
movq %rax, -0x8a0(%rbp)
jmp 0xcb0c
movq -0x8a0(%rbp), %rdi
leaq 0xdc60(%rip), %rsi      # 0x1a77a
callq 0x43f0
movq %rax, -0x8a8(%rbp)
jmp 0xcb28
movq -0x890(%rbp), %rsi
movq -0x8a8(%rbp), %rdi
callq 0x4380
movq %rax, -0x8b0(%rbp)
jmp 0xcb44
movq -0x8b0(%rbp), %rdi
leaq 0xdc42(%rip), %rsi      # 0x1a794
callq 0x43f0
movq %rax, -0x8b8(%rbp)
jmp 0xcb60
movq -0x8b8(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x4750
movq %rax, -0x8c0(%rbp)
jmp 0xcb78
movq -0x8c0(%rbp), %rdi
leaq 0xdc1f(%rip), %rsi      # 0x1a7a5
callq 0x43f0
movq %rax, -0x8c8(%rbp)
jmp 0xcb94
movq -0x8c8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x4380
jmp 0xcba6
leaq -0x1e0(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0x43e0
jmp 0xcbbb
leaq -0x1e0(%rbp), %rdi
callq 0x6f80
jmp 0xcbc9
leaq -0x1e0(%rbp), %rdi
callq 0x4878
movl -0x1c(%rbp), %eax
movl %eax, -0x8cc(%rbp)
testl %eax, %eax
je 0xcc43
jmp 0xcbe4
movl -0x8cc(%rbp), %eax
subl $0x1, %eax
je 0xcdba
jmp 0xcbf5
movl -0x8cc(%rbp), %eax
subl $0x2, %eax
je 0xce86
jmp 0xcfb9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
jmp 0xd4bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0x4878
jmp 0xd4bb
movq -0x28(%rbp), %rsi
leaq -0x1f8(%rbp), %rdi
movl $0x20, %edx
callq 0x4e00
jmp 0xcc5a
leaq -0x1f8(%rbp), %rdi
callq 0xe3f0
cmpq $0x3, %rax
jne 0xcd98
movq -0x890(%rbp), %rsi
leaq -0x228(%rbp), %rdi
callq 0x41a0
jmp 0xcc85
xorl %eax, %eax
movl %eax, %esi
leaq -0x1f8(%rbp), %rdi
movq %rdi, -0x8d8(%rbp)
callq 0xef40
movq %rax, %rdi
callq 0x4190
movq %rax, %rdi
callq 0x4690
movq -0x8d8(%rbp), %rdi
movl %eax, -0x208(%rbp)
movl $0x1, %esi
callq 0xef40
movq %rax, %rdi
callq 0x4190
movq %rax, %rdi
callq 0x4690
movq -0x8d8(%rbp), %rdi
movl %eax, -0x204(%rbp)
movl $0x2, %esi
callq 0xef40
movq %rax, %rdi
callq 0x4190
movq %rax, %rdi
callq 0x4690
movl %eax, -0x200(%rbp)
leaq -0x258(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0xf950
jmp 0xcd15
movq -0x888(%rbp), %rdi
leaq -0x258(%rbp), %rsi
callq 0xd4e0
jmp 0xcd2a
leaq -0x258(%rbp), %rdi
callq 0xf8e0
leaq -0x228(%rbp), %rdi
callq 0xf8e0
jmp 0xcd98
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
jmp 0xcda9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
jmp 0xcd8a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
leaq -0x258(%rbp), %rdi
callq 0xf8e0
leaq -0x228(%rbp), %rdi
callq 0xf8e0
jmp 0xcda9
leaq -0x1f8(%rbp), %rdi
callq 0xe390
jmp 0xd4a6
leaq -0x1f8(%rbp), %rdi
callq 0xe390
jmp 0xd4bb
movq -0x28(%rbp), %rsi
leaq -0x270(%rbp), %rdi
movl $0x20, %edx
callq 0x4e00
jmp 0xcdd1
leaq -0x270(%rbp), %rdi
callq 0xe3f0
cmpq $0x3, %rax
jne 0xce75
xorl %eax, %eax
movl %eax, %esi
leaq -0x270(%rbp), %rdi
callq 0xef40
movq %rax, %rdi
callq 0x4190
movq %rax, %rdi
callq 0x4690
movq -0x888(%rbp), %rdi
movl %eax, -0x274(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x8e8(%rbp)
addq $0x158, %rdi            # imm = 0x158
leaq -0x274(%rbp), %rsi
callq 0xf990
movq %rax, -0x8e0(%rbp)
jmp 0xce3b
movq -0x8e8(%rbp), %rsi
movq -0x8e0(%rbp), %rdi
callq 0x4050
jmp 0xce50
jmp 0xce75
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
leaq -0x270(%rbp), %rdi
callq 0xe390
jmp 0xd4bb
leaq -0x270(%rbp), %rdi
callq 0xe390
jmp 0xd4a6
movq -0x28(%rbp), %rdi
callq 0x4190
movq %rax, %rdi
callq 0x4360
movl %eax, -0x8ec(%rbp)
jmp 0xce9f
movl -0x8ec(%rbp), %eax
movl %eax, -0x278(%rbp)
cmpl $-0x1, -0x278(%rbp)
jne 0xcfb4
leaq -0x400(%rbp), %rdi
callq 0x4280
jmp 0xcec6
leaq -0x3f0(%rbp), %rdi
leaq 0xd8e5(%rip), %rsi      # 0x1a7b9
callq 0x43f0
movq %rax, -0x8f8(%rbp)
jmp 0xcee2
movq -0x8f8(%rbp), %rdi
movq 0x180d0(%rip), %rsi     # 0x24fc0
callq 0x4440
movq %rax, -0x900(%rbp)
jmp 0xcefe
movq -0x28(%rbp), %rdi
callq 0x4190
movq -0x900(%rbp), %rdi
movq %rax, %rsi
callq 0x43f0
movq %rax, -0x908(%rbp)
jmp 0xcf1f
movq -0x908(%rbp), %rdi
movq 0x18093(%rip), %rsi     # 0x24fc0
callq 0x4440
jmp 0xcf34
leaq -0x420(%rbp), %rdi
leaq -0x400(%rbp), %rsi
callq 0x43e0
jmp 0xcf49
leaq -0x420(%rbp), %rdi
callq 0x7300
jmp 0xcf57
leaq -0x420(%rbp), %rdi
callq 0x4878
leaq -0x400(%rbp), %rdi
callq 0x42b0
jmp 0xcfb4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
jmp 0xcfa3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
leaq -0x420(%rbp), %rdi
callq 0x4878
leaq -0x400(%rbp), %rdi
callq 0x42b0
jmp 0xd4bb
jmp 0xd4a6
movq -0x888(%rbp), %rdi
addq $0x158, %rdi            # imm = 0x158
leaq -0x1c(%rbp), %rsi
callq 0xfaa0
movq %rax, -0x910(%rbp)
jmp 0xcfd9
movq -0x888(%rbp), %rdi
movq -0x910(%rbp), %rax
movq %rax, -0x428(%rbp)
addq $0x158, %rdi            # imm = 0x158
callq 0xfad0
movq %rax, -0x430(%rbp)
leaq -0x428(%rbp), %rdi
leaq -0x430(%rbp), %rsi
callq 0xfa70
testb $0x1, %al
jne 0xd01d
jmp 0xd4a4
movq -0x888(%rbp), %rdi
addq $0x158, %rdi            # imm = 0x158
leaq -0x1c(%rbp), %rsi
callq 0xf990
movq %rax, -0x918(%rbp)
jmp 0xd03d
movq -0x918(%rbp), %rsi
leaq -0x450(%rbp), %rdi
callq 0x41a0
jmp 0xd052
leaq -0x468(%rbp), %rdi
leaq -0x450(%rbp), %rsi
movl $0x20, %edx
callq 0x4e00
jmp 0xd06c
leaq -0x5f0(%rbp), %rdi
callq 0x4280
jmp 0xd07a
leaq -0x5e0(%rbp), %rdi
leaq 0x18430(%rip), %rsi     # 0x254b8
callq 0x4380
movq %rax, -0x920(%rbp)
jmp 0xd096
leaq -0x468(%rbp), %rdi
movl $0x1, %esi
callq 0xef40
movq -0x920(%rbp), %rdi
movq %rax, %rsi
callq 0x4380
movq %rax, -0x928(%rbp)
jmp 0xd0bf
movq -0x928(%rbp), %rdi
leaq 0xd245(%rip), %rsi      # 0x1a312
callq 0x43f0
jmp 0xd0d4
leaq -0x618(%rbp), %rdi
leaq -0x5f0(%rbp), %rsi
callq 0x43e0
jmp 0xd0e9
leaq -0x468(%rbp), %rdi
movl $0x2, %esi
callq 0xef40
movq %rax, %rsi
leaq -0x638(%rbp), %rdi
callq 0x41a0
jmp 0xd10b
movq -0x28(%rbp), %rsi
leaq -0x658(%rbp), %rdi
callq 0x41a0
jmp 0xd11d
leaq -0x618(%rbp), %rdi
leaq -0x638(%rbp), %rsi
leaq -0x658(%rbp), %rdx
callq 0x5c40
movb %al, -0x929(%rbp)
jmp 0xd13f
leaq -0x658(%rbp), %rdi
callq 0x4878
leaq -0x638(%rbp), %rdi
callq 0x4878
leaq -0x618(%rbp), %rdi
callq 0x4878
movb -0x929(%rbp), %al
andb $0x1, %al
movb %al, -0x5f1(%rbp)
testb $0x1, -0x5f1(%rbp)
jne 0xd352
leaq -0x7e0(%rbp), %rdi
callq 0x4280
jmp 0xd18c
leaq -0x7d0(%rbp), %rdi
leaq 0xd64a(%rip), %rsi      # 0x1a7e4
callq 0x43f0
movq %rax, -0x938(%rbp)
jmp 0xd1a8
leaq -0x800(%rbp), %rdi
leaq -0x5f0(%rbp), %rsi
callq 0x43e0
jmp 0xd1bd
movq -0x938(%rbp), %rdi
leaq -0x800(%rbp), %rsi
callq 0x4380
movq %rax, -0x940(%rbp)
jmp 0xd1d9
leaq -0x468(%rbp), %rdi
movl $0x2, %esi
callq 0xef40
movq -0x940(%rbp), %rdi
movq %rax, %rsi
callq 0x4380
jmp 0xd1fb
leaq -0x800(%rbp), %rdi
callq 0x4878
leaq -0x820(%rbp), %rdi
leaq -0x7e0(%rbp), %rsi
callq 0x43e0
jmp 0xd21c
leaq -0x820(%rbp), %rdi
callq 0x7300
jmp 0xd22a
leaq -0x820(%rbp), %rdi
callq 0x4878
leaq -0x7e0(%rbp), %rdi
callq 0x42b0
jmp 0xd352
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
jmp 0xd496
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
jmp 0xd48a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
jmp 0xd47e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
jmp 0xd2de
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
jmp 0xd2d2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
leaq -0x658(%rbp), %rdi
callq 0x4878
leaq -0x638(%rbp), %rdi
callq 0x4878
leaq -0x618(%rbp), %rdi
callq 0x4878
jmp 0xd47e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
jmp 0xd341
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
leaq -0x800(%rbp), %rdi
callq 0x4878
jmp 0xd341
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
leaq -0x820(%rbp), %rdi
callq 0x4878
leaq -0x7e0(%rbp), %rdi
callq 0x42b0
jmp 0xd47e
movq -0x888(%rbp), %rdi
addq $0x158, %rdi            # imm = 0x158
leaq -0x1c(%rbp), %rsi
callq 0xfb00
jmp 0xd36b
leaq -0x880(%rbp), %rdi
leaq -0x5f0(%rbp), %rsi
callq 0x43e0
jmp 0xd380
leaq 0xd484(%rip), %rsi      # 0x1a80b
leaq -0x860(%rbp), %rdi
leaq -0x880(%rbp), %rdx
callq 0xfb80
jmp 0xd39c
leaq -0x468(%rbp), %rdi
movl $0x2, %esi
callq 0xef40
movq %rax, %rdx
leaq -0x840(%rbp), %rdi
leaq -0x860(%rbp), %rsi
callq 0xfb30
jmp 0xd3c5
leaq -0x840(%rbp), %rdi
callq 0x7140
jmp 0xd3d3
leaq -0x840(%rbp), %rdi
callq 0x4878
leaq -0x860(%rbp), %rdi
callq 0x4878
leaq -0x880(%rbp), %rdi
callq 0x4878
leaq -0x5f0(%rbp), %rdi
callq 0x42b0
leaq -0x468(%rbp), %rdi
callq 0xe390
leaq -0x450(%rbp), %rdi
callq 0x4878
jmp 0xd4a4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
jmp 0xd472
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
jmp 0xd466
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1b8(%rbp)
movl %eax, -0x1bc(%rbp)
leaq -0x840(%rbp), %rdi
callq 0x4878
leaq -0x860(%rbp), %rdi
callq 0x4878
leaq -0x880(%rbp), %rdi
callq 0x4878
leaq -0x5f0(%rbp), %rdi
callq 0x42b0
leaq -0x468(%rbp), %rdi
callq 0xe390
leaq -0x450(%rbp), %rdi
callq 0x4878
jmp 0xd4bb
jmp 0xd4a6
leaq -0x1b0(%rbp), %rdi
callq 0x42b0
addq $0x940, %rsp            # imm = 0x940
popq %rbp
retq
leaq -0x1b0(%rbp), %rdi
callq 0x42b0
movq -0x1b8(%rbp), %rdi
callq 0x4780
nopw %cs:(%rax,%rax)