// File: /workspace/github_cpp_makefile_star2_O3/CDISC2020[P]Projet_satellite_agile/src/communication/comGroundManager.cpp
// Function: Communic_Sol(void*)
void* Communic_Sol(void* args)
{
	QueuingPort channelOutPM(0, 18001, s); 	// Client PM
	usleep(500000);
	channelOutPM.Display();

    	vector<string> files, tms;
	vector<string>::iterator it;
	ifstream ifile;

	PlanFilePath pfp_plan, pfp_tm;
	pfp_plan.code = 3;
	pfp_tm.code = 17;

	// Modifiable
	// Dossiers reception
	string dir_plan = string("src/communication/planRecuSol/");
	string dir_tm = string("src/communication/tmRecuSol/");
	string dir_log = string("./");

	// Fichier a chercher
	string name_dem = "demande_imgs.txt";
	string name_plan = "plan.txt";
	string name_tm = "tm.txt";
	string name_log = "LogError.txt";

	// Dossier une fois traité
	string c_plan = "src/planManager/plans/";
	string c_tm = "src/planManager/tm/";
	string c_log = "src/communication/toSend/";

	bool verification_plan;
	bool verification_tm;
	
	while(1)
	{
		if(mode == true)
		{
			files.clear();
    			getdir(dir_plan,files);	

			/*****************************[ CGM ---IMGS---> SOL ]**********************************/
			it=find(files.begin(),files.end(),name_dem);
			if(it!=files.end()) // Si fichier présent dans la liste
			{
				cout << "File found" << endl;
				
				sprintf(cmde, "mv %s%s %s", dir_log.c_str(), name_log.c_str(), c_log.c_str());
				system(cmde);

				// envoyer tous fichiers de toSend existentes
				sprintf(cmde, "sh src/communication/uploadStoG.sh"); 
				system(cmde);
				sleep(1);
				cout << "Images and log sent \n" << endl;

				//enlever du satellite les fichiers envoyés au sol
				sprintf(cmde, "rm %s*.jpg", c_log.c_str());
				system(cmde);
				sprintf(cmde, "rm %s*.txt", c_log.c_str());
				system(cmde);
				cout << "Image and log removed\n" << endl;

				ptImageSent=ptImageReceived;


				string rep_demande_img= dir_plan + name_dem;
				remove(rep_demande_img.c_str());
				cout << "Request removed\n";
				sleep(1);
			}
			
			/*****************************[ CGM ---PLAN---> PM ]**********************************/
			it=find(files.begin(),files.end(),name_plan);
			if(it!=files.end()) // Si fichier présent dans la liste
			{
 				verification_plan = tryFile(dir_plan, name_plan); //verifier si on reussi a ouvrir le plan

				if(verification_plan)
				{
					// deplace dans src/planMagager/plans avec system()
					sprintf(cmde, "mv %s%s %s", dir_plan.c_str(), name_plan.c_str(), c_plan.c_str());
					system(cmde);
					sleep(1);
					cout << "Plan sent to PM\n";

					sprintf(pfp_plan.filepath, "%s%s", c_plan.c_str(), name_plan.c_str());
					channelOutPM.SendQueuingMsg((char*)&pfp_plan, sizeof(PlanFilePath));
				}
			}

			/*****************************[ CGM ---TM---> PM ]**********************************/
			tms.clear();
    			getdir(dir_tm,tms);			
			it=find(tms.begin(),tms.end(),name_tm);
			if(it!=tms.end()) // Si fichier présent dans la liste
			{
				verification_tm = tryFile(dir_tm,name_tm);

				if(verification_tm)
				{
						// deplace dans src/planMagager/tm avec system()
					sprintf(cmde, "mv %s%s %s", dir_tm.c_str(), name_tm.c_str(), c_tm.c_str());
					system(cmde);
					sleep(1);
					cout << "TM sent to PM\n";

					sprintf(pfp_tm.filepath, "%s%s", c_tm.c_str(), name_tm.c_str());

					channelOutPM.SendQueuingMsg((char*)&pfp_tm, sizeof(PlanFilePath));
				}
			}
		}

//		else{
//			cout << "In follower mode\n" << endl;}

		wtc = 1;

		usleep(100);
	}

	return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x9f8, %rsp            # imm = 0x9F8
leaq 0x5c1b(%rip), %rcx      # 0x92f0
leaq -0xa20(%rbp), %rbx
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %esi, %esi
movl $0x4651, %edx           # imm = 0x4651
callq 0x2866
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x2370
movq %rbx, %rdi
callq 0x2d88
movq %r14, -0x70(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
movq %r14, -0xb0(%rbp)
movaps %xmm0, -0xc0(%rbp)
leaq -0x558(%rbp), %rdi
callq 0x22e0
movl $0x3, -0x350(%rbp)
movl $0x11, -0x2e8(%rbp)
leaq -0x38(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x1b7c(%rip), %rsi      # 0x52c4
leaq 0x1b93(%rip), %rdx      # 0x52e2
leaq -0x48(%rbp), %rdi
callq 0x3304
leaq -0x58(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x1b7c(%rip), %rsi      # 0x52e3
leaq 0x1b91(%rip), %rdx      # 0x52ff
leaq -0x68(%rbp), %rdi
callq 0x3304
leaq -0x90(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x1b76(%rip), %rsi      # 0x5300
leaq 0x1b71(%rip), %rdx      # 0x5302
leaq -0xa0(%rbp), %rdi
callq 0x3304
leaq -0x1b0(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1b54(%rip), %rsi      # 0x5303
leaq 0x1b5d(%rip), %rdx      # 0x5313
leaq -0x1c0(%rbp), %rdi
callq 0x3304
leaq -0xf0(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1b40(%rip), %rsi      # 0x5314
leaq 0x1b41(%rip), %rdx      # 0x531c
leaq -0x100(%rbp), %rdi
callq 0x3304
leaq -0xd0(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1b24(%rip), %rsi      # 0x531d
leaq 0x1b23(%rip), %rdx      # 0x5323
leaq -0xe0(%rbp), %rdi
callq 0x3304
leaq -0x230(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x19c8(%rip), %rsi      # 0x51e6
leaq 0x19cd(%rip), %rdx      # 0x51f2
leaq -0x240(%rbp), %rdi
callq 0x3304
leaq -0x190(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1ae1(%rip), %rsi      # 0x5324
leaq 0x1af0(%rip), %rdx      # 0x533a
leaq -0x1a0(%rbp), %rdi
callq 0x3304
leaq -0x270(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1ad3(%rip), %rsi      # 0x533b
leaq 0x1adf(%rip), %rdx      # 0x534e
leaq -0x280(%rbp), %rdi
callq 0x3304
leaq -0x210(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1ac2(%rip), %rsi      # 0x534f
leaq 0x1ad4(%rip), %rdx      # 0x5368
leaq -0x220(%rbp), %rdi
callq 0x3304
leaq -0x1e0(%rbp), %r14
movq 0x472a(%rip), %rbx      # 0x7fd8
cmpb $0x1, 0x5a33(%rip)      # 0x92e8
jne 0x3e4d
movq %r14, %r12
movq -0x80(%rbp), %r14
movq -0x78(%rbp), %r15
cmpq %r14, %r15
je 0x38ed
movq %r14, %r13
movq (%r13), %rdi
addq $0x10, %r13
cmpq %r13, %rdi
je 0x38e0
callq 0x2280
addq $0x10, %r13
cmpq %r15, %r13
jne 0x38ce
movq %r14, -0x78(%rbp)
leaq -0x170(%rbp), %rax
movq %rax, -0x180(%rbp)
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
addq %rsi, %rdx
leaq -0x180(%rbp), %rdi
callq 0x469e
movq %r12, %r14
leaq -0x180(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x33a4
movq -0x180(%rbp), %rdi
leaq -0x170(%rbp), %rax
cmpq %rax, %rdi
je 0x393d
callq 0x2280
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rsi
leaq -0x1c0(%rbp), %rdx
callq 0x49a6
movq -0x78(%rbp), %rsi
cmpq %rsi, %rax
je 0x3b37
movl $0xa, %edx
movq %rbx, %rdi
leaq 0x19fc(%rip), %rsi      # 0x5369
callq 0x2300
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x22a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x21a0
movq -0xa0(%rbp), %rdx
movq -0x240(%rbp), %rcx
movq -0x220(%rbp), %r8
leaq 0x5dab(%rip), %r15      # 0x9760
movq %r15, %rdi
leaq 0x19b5(%rip), %rsi      # 0x5374
xorl %eax, %eax
callq 0x20a0
movq %r15, %rdi
callq 0x2230
movups 0x19ba(%rip), %xmm0     # 0x538f
movaps %xmm0, 0x5d94(%rip)     # 0x9770
movups 0x199c(%rip), %xmm0     # 0x537f
movaps %xmm0, 0x5d76(%rip)     # 0x9760
movl $0x68732e, 0x5d8b(%rip) # imm = 0x68732E
leaq 0x5d65(%rip), %rdi      # 0x9760
callq 0x2230
movl $0x1, %edi
callq 0x21d0
movl $0x15, %edx
movq %rbx, %rdi
leaq 0x1989(%rip), %rsi      # 0x53a2
callq 0x2300
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x22a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x21a0
movq -0x220(%rbp), %rdx
leaq 0x5d0d(%rip), %r15      # 0x9760
movq %r15, %rdi
leaq 0x195b(%rip), %rsi      # 0x53b8
xorl %eax, %eax
callq 0x20a0
movq %r15, %rdi
callq 0x2230
movq -0x220(%rbp), %rdx
leaq 0x5ce6(%rip), %r15      # 0x9760
movq %r15, %rdi
leaq 0x193f(%rip), %rsi      # 0x53c3
xorl %eax, %eax
callq 0x20a0
movq %r15, %rdi
callq 0x2230
movl $0x16, %edx
movq %rbx, %rdi
leaq 0x192c(%rip), %rsi      # 0x53ce
callq 0x2300
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x22a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x21a0
movl 0x580c(%rip), %eax      # 0x92e0
movl %eax, 0x580a(%rip)      # 0x92e4
leaq -0x260(%rbp), %rdi
leaq -0x48(%rbp), %rsi
leaq -0x1c0(%rbp), %rdx
callq 0x45e7
movq -0x260(%rbp), %rdi
callq 0x2490
movl $0x10, %edx
movq %rbx, %rdi
leaq 0x18d9(%rip), %rsi      # 0x53e5
callq 0x2300
movl $0x1, %edi
callq 0x21d0
movq -0x260(%rbp), %rdi
leaq -0x250(%rbp), %rax
cmpq %rax, %rdi
je 0x3b33
callq 0x2280
movq -0x78(%rbp), %rsi
movq -0x80(%rbp), %rdi
leaq -0x100(%rbp), %rdx
callq 0x49a6
cmpq -0x78(%rbp), %rax
je 0x3c7a
leaq -0x150(%rbp), %rax
movq %rax, -0x160(%rbp)
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
addq %rsi, %rdx
leaq -0x160(%rbp), %rdi
callq 0x469e
leaq -0x1f0(%rbp), %rax
movq %rax, -0x200(%rbp)
movq -0x100(%rbp), %rsi
movq -0xf8(%rbp), %rdx
addq %rsi, %rdx
leaq -0x200(%rbp), %r15
movq %r15, %rdi
callq 0x469e
leaq -0x160(%rbp), %rdi
movq %r15, %rsi
callq 0x34e5
movl %eax, %r13d
movq -0x200(%rbp), %rdi
leaq -0x1f0(%rbp), %rax
cmpq %rax, %rdi
je 0x3bce
callq 0x2280
movq -0x160(%rbp), %rdi
leaq -0x150(%rbp), %rax
cmpq %rax, %rdi
je 0x3be6
callq 0x2280
testb %r13b, %r13b
je 0x3c7a
movq -0x48(%rbp), %rdx
movq -0x100(%rbp), %rcx
movq -0x1a0(%rbp), %r8
leaq 0x5b58(%rip), %r15      # 0x9760
movq %r15, %rdi
leaq 0x1762(%rip), %rsi      # 0x5374
xorl %eax, %eax
callq 0x20a0
movq %r15, %rdi
callq 0x2230
movl $0x1, %edi
callq 0x21d0
movl $0x10, %edx
movq %rbx, %rdi
leaq 0x17bc(%rip), %rsi      # 0x53f6
callq 0x2300
movq -0x1a0(%rbp), %rdx
movq -0x100(%rbp), %rcx
leaq -0x34c(%rbp), %rdi
leaq 0x17ac(%rip), %rsi      # 0x5407
xorl %eax, %eax
callq 0x20a0
leaq -0xa20(%rbp), %rdi
leaq -0x350(%rbp), %rsi
movl $0x68, %edx
callq 0x2b26
movq -0xc0(%rbp), %r13
movq -0xb8(%rbp), %r15
cmpq %r13, %r15
je 0x3cb4
movq %r13, %r14
movq (%r14), %rdi
addq $0x10, %r14
cmpq %r14, %rdi
je 0x3ca1
callq 0x2280
addq $0x10, %r14
cmpq %r15, %r14
jne 0x3c90
movq %r13, -0xb8(%rbp)
movq %r12, %r14
leaq -0x130(%rbp), %rax
movq %rax, -0x140(%rbp)
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq %rsi, %rdx
leaq -0x140(%rbp), %rdi
callq 0x469e
leaq -0x140(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x33a4
movq -0x140(%rbp), %rdi
leaq -0x130(%rbp), %rax
cmpq %rax, %rdi
je 0x3d04
callq 0x2280
movq -0xc0(%rbp), %rdi
movq -0xb8(%rbp), %rsi
leaq -0xe0(%rbp), %rdx
callq 0x49a6
cmpq -0xb8(%rbp), %rax
je 0x3e4d
leaq -0x110(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq %rsi, %rdx
leaq -0x120(%rbp), %rdi
callq 0x469e
leaq -0x1d0(%rbp), %rax
movq %rax, -0x1e0(%rbp)
movq -0xe0(%rbp), %rsi
movq -0xd8(%rbp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x469e
leaq -0x120(%rbp), %rdi
movq %r14, %rsi
callq 0x34e5
movl %eax, %r13d
movq -0x1e0(%rbp), %rdi
leaq -0x1d0(%rbp), %rax
cmpq %rax, %rdi
je 0x3da1
callq 0x2280
movq -0x120(%rbp), %rdi
leaq -0x110(%rbp), %rax
cmpq %rax, %rdi
je 0x3db9
callq 0x2280
testb %r13b, %r13b
je 0x3e4d
movq -0x68(%rbp), %rdx
movq -0xe0(%rbp), %rcx
movq -0x280(%rbp), %r8
leaq 0x5985(%rip), %r15      # 0x9760
movq %r15, %rdi
leaq 0x158f(%rip), %rsi      # 0x5374
xorl %eax, %eax
callq 0x20a0
movq %r15, %rdi
callq 0x2230
movl $0x1, %edi
callq 0x21d0
movl $0xe, %edx
movq %rbx, %rdi
leaq 0x15ff(%rip), %rsi      # 0x540c
callq 0x2300
movq -0x280(%rbp), %rdx
movq -0xe0(%rbp), %rcx
leaq -0x2e4(%rbp), %rdi
leaq 0x15d9(%rip), %rsi      # 0x5407
xorl %eax, %eax
callq 0x20a0
leaq -0xa20(%rbp), %rdi
leaq -0x2e8(%rbp), %rsi
movl $0x68, %edx
callq 0x2b26
movl $0x1, 0x596d(%rip)      # 0x97c4
movl $0x64, %edi
callq 0x2370
jmp 0x38ae
movq %rax, %rbx
movq -0x1e0(%rbp), %rdi
leaq -0x1d0(%rbp), %rax
cmpq %rax, %rdi
je 0x3e86
callq 0x2280
jmp 0x3e86
movq %rax, %rbx
movq -0x120(%rbp), %rdi
leaq -0x110(%rbp), %rax
jmp 0x3f05
jmp 0x3f11
movq %rax, %rbx
movq -0x200(%rbp), %rdi
leaq -0x1f0(%rbp), %rax
cmpq %rax, %rdi
je 0x3eb8
callq 0x2280
jmp 0x3eb8
movq %rax, %rbx
movq -0x160(%rbp), %rdi
leaq -0x150(%rbp), %rax
jmp 0x3f05
jmp 0x3f11
movq %rax, %rbx
movq -0x140(%rbp), %rdi
leaq -0x130(%rbp), %rax
jmp 0x3f05
jmp 0x3f11
jmp 0x3f11
movq %rax, %rbx
movq -0x180(%rbp), %rdi
leaq -0x170(%rbp), %rax
jmp 0x3f05
movq %rax, %rbx
movq -0x260(%rbp), %rdi
leaq -0x250(%rbp), %rax
cmpq %rax, %rdi
je 0x3f14
callq 0x2280
jmp 0x3f14
movq %rax, %rbx
leaq -0x38(%rbp), %r14
leaq -0x58(%rbp), %r15
leaq -0x90(%rbp), %r12
movq -0x220(%rbp), %rdi
leaq -0x210(%rbp), %rax
cmpq %rax, %rdi
je 0x3f40
callq 0x2280
jmp 0x3f40
movq %rax, %rbx
movq -0x280(%rbp), %rdi
leaq -0x270(%rbp), %rax
cmpq %rax, %rdi
je 0x3f5d
callq 0x2280
jmp 0x3f5d
movq %rax, %rbx
leaq -0x190(%rbp), %rax
movq -0x1a0(%rbp), %rdi
cmpq %rax, %rdi
je 0x3f7a
callq 0x2280
jmp 0x3f7a
movq %rax, %rbx
leaq -0x230(%rbp), %rax
movq -0x240(%rbp), %rdi
cmpq %rax, %rdi
je 0x3f97
callq 0x2280
jmp 0x3f97
movq %rax, %rbx
leaq -0xd0(%rbp), %rax
movq -0xe0(%rbp), %rdi
cmpq %rax, %rdi
je 0x3fb4
callq 0x2280
jmp 0x3fb4
movq %rax, %rbx
leaq -0xf0(%rbp), %rax
movq -0x100(%rbp), %rdi
cmpq %rax, %rdi
je 0x3fd1
callq 0x2280
jmp 0x3fd1
movq %rax, %rbx
leaq -0x1b0(%rbp), %rax
movq -0x1c0(%rbp), %rdi
cmpq %rax, %rdi
je 0x3fee
callq 0x2280
jmp 0x3fee
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
cmpq %r12, %rdi
je 0x4004
callq 0x2280
jmp 0x4004
movq %rax, %rbx
movq -0x68(%rbp), %rdi
cmpq %r15, %rdi
je 0x4017
callq 0x2280
jmp 0x4017
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r14, %rdi
je 0x402a
callq 0x2280
jmp 0x402a
movq %rax, %rbx
leaq -0x558(%rbp), %rdi
callq 0x20e0
jmp 0x403b
movq %rax, %rbx
leaq -0xc0(%rbp), %rdi
callq 0x4646
leaq -0x80(%rbp), %rdi
callq 0x4646
movq %rbx, %rdi
callq 0x2470