// File: /workspace/github_cpp_makefile_star2_O1/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 0x5b8b(%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 0x2da8
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 0x1aec(%rip), %rsi      # 0x52c4
leaq 0x1b03(%rip), %rdx      # 0x52e2
leaq -0x48(%rbp), %rdi
callq 0x3398
leaq -0x58(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x1aec(%rip), %rsi      # 0x52e3
leaq 0x1b01(%rip), %rdx      # 0x52ff
leaq -0x68(%rbp), %rdi
callq 0x3398
leaq -0x90(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x1ae6(%rip), %rsi      # 0x5300
leaq 0x1ae1(%rip), %rdx      # 0x5302
leaq -0xa0(%rbp), %rdi
callq 0x3398
leaq -0x1b0(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1ac4(%rip), %rsi      # 0x5303
leaq 0x1acd(%rip), %rdx      # 0x5313
leaq -0x1c0(%rbp), %rdi
callq 0x3398
leaq -0xf0(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1ab0(%rip), %rsi      # 0x5314
leaq 0x1ab1(%rip), %rdx      # 0x531c
leaq -0x100(%rbp), %rdi
callq 0x3398
leaq -0xd0(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1a94(%rip), %rsi      # 0x531d
leaq 0x1a93(%rip), %rdx      # 0x5323
leaq -0xe0(%rbp), %rdi
callq 0x3398
leaq -0x230(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1938(%rip), %rsi      # 0x51e6
leaq 0x193d(%rip), %rdx      # 0x51f2
leaq -0x240(%rbp), %rdi
callq 0x3398
leaq -0x190(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1a51(%rip), %rsi      # 0x5324
leaq 0x1a60(%rip), %rdx      # 0x533a
leaq -0x1a0(%rbp), %rdi
callq 0x3398
leaq -0x270(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1a43(%rip), %rsi      # 0x533b
leaq 0x1a4f(%rip), %rdx      # 0x534e
leaq -0x280(%rbp), %rdi
callq 0x3398
leaq -0x210(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1a32(%rip), %rsi      # 0x534f
leaq 0x1a44(%rip), %rdx      # 0x5368
leaq -0x220(%rbp), %rdi
callq 0x3398
leaq -0x1e0(%rbp), %r14
movq 0x469a(%rip), %rbx      # 0x7fd8
cmpb $0x1, 0x59a3(%rip)      # 0x92e8
jne 0x3eda
movq %r14, %r12
movq -0x80(%rbp), %r14
movq -0x78(%rbp), %r15
cmpq %r14, %r15
je 0x397d
movq %r14, %r13
movq (%r13), %rdi
addq $0x10, %r13
cmpq %r13, %rdi
je 0x3970
callq 0x2280
addq $0x10, %r13
cmpq %r15, %r13
jne 0x395e
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 0x4722
movq %r12, %r14
leaq -0x180(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x3434
movq -0x180(%rbp), %rdi
leaq -0x170(%rbp), %rax
cmpq %rax, %rdi
je 0x39cd
callq 0x2280
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rsi
leaq -0x1c0(%rbp), %rdx
callq 0x4a26
cmpq -0x78(%rbp), %rax
je 0x3bc0
movl $0xa, %edx
movq %rbx, %rdi
leaq 0x196f(%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 0x5d1e(%rip), %r15      # 0x9760
movq %r15, %rdi
leaq 0x1928(%rip), %rsi      # 0x5374
xorl %eax, %eax
callq 0x20a0
movq %r15, %rdi
callq 0x2230
movups 0x192d(%rip), %xmm0     # 0x538f
movaps %xmm0, 0x5d07(%rip)     # 0x9770
movups 0x190f(%rip), %xmm0     # 0x537f
movaps %xmm0, 0x5ce9(%rip)     # 0x9760
movl $0x68732e, 0x5cfe(%rip) # imm = 0x68732E
leaq 0x5cd8(%rip), %rdi      # 0x9760
callq 0x2230
movl $0x1, %edi
callq 0x21d0
movl $0x15, %edx
movq %rbx, %rdi
leaq 0x18fc(%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 0x5c80(%rip), %r15      # 0x9760
movq %r15, %rdi
leaq 0x18ce(%rip), %rsi      # 0x53b8
xorl %eax, %eax
callq 0x20a0
movq %r15, %rdi
callq 0x2230
movq -0x220(%rbp), %rdx
leaq 0x5c59(%rip), %r15      # 0x9760
movq %r15, %rdi
leaq 0x18b2(%rip), %rsi      # 0x53c3
xorl %eax, %eax
callq 0x20a0
movq %r15, %rdi
callq 0x2230
movl $0x16, %edx
movq %rbx, %rdi
leaq 0x189f(%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 0x577f(%rip), %eax      # 0x92e0
movl %eax, 0x577d(%rip)      # 0x92e4
leaq -0x260(%rbp), %rdi
leaq -0x48(%rbp), %rsi
leaq -0x1c0(%rbp), %rdx
callq 0x4675
movq -0x260(%rbp), %rdi
callq 0x2490
movl $0x10, %edx
movq %rbx, %rdi
leaq 0x184c(%rip), %rsi      # 0x53e5
callq 0x2300
movl $0x1, %edi
callq 0x21d0
movq -0x260(%rbp), %rdi
leaq -0x250(%rbp), %rax
cmpq %rax, %rdi
je 0x3bc0
callq 0x2280
movq -0x80(%rbp), %rdi
movq -0x78(%rbp), %rsi
leaq -0x100(%rbp), %rdx
callq 0x4a26
cmpq -0x78(%rbp), %rax
je 0x3d07
leaq -0x150(%rbp), %rax
movq %rax, -0x160(%rbp)
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
addq %rsi, %rdx
leaq -0x160(%rbp), %rdi
callq 0x4722
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 0x4722
leaq -0x160(%rbp), %rdi
movq %r15, %rsi
callq 0x3575
movl %eax, %r13d
movq -0x200(%rbp), %rdi
leaq -0x1f0(%rbp), %rax
cmpq %rax, %rdi
je 0x3c5b
callq 0x2280
movq -0x160(%rbp), %rdi
leaq -0x150(%rbp), %rax
cmpq %rax, %rdi
je 0x3c73
callq 0x2280
testb %r13b, %r13b
je 0x3d07
movq -0x48(%rbp), %rdx
movq -0x100(%rbp), %rcx
movq -0x1a0(%rbp), %r8
leaq 0x5acb(%rip), %r15      # 0x9760
movq %r15, %rdi
leaq 0x16d5(%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 0x172f(%rip), %rsi      # 0x53f6
callq 0x2300
movq -0x1a0(%rbp), %rdx
movq -0x100(%rbp), %rcx
leaq -0x34c(%rbp), %rdi
leaq 0x171f(%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 0x3d41
movq %r13, %r14
movq (%r14), %rdi
addq $0x10, %r14
cmpq %r14, %rdi
je 0x3d2e
callq 0x2280
addq $0x10, %r14
cmpq %r15, %r14
jne 0x3d1d
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 0x4722
leaq -0x140(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x3434
movq -0x140(%rbp), %rdi
leaq -0x130(%rbp), %rax
cmpq %rax, %rdi
je 0x3d91
callq 0x2280
movq -0xc0(%rbp), %rdi
movq -0xb8(%rbp), %rsi
leaq -0xe0(%rbp), %rdx
callq 0x4a26
cmpq -0xb8(%rbp), %rax
je 0x3eda
leaq -0x110(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq %rsi, %rdx
leaq -0x120(%rbp), %rdi
callq 0x4722
leaq -0x1d0(%rbp), %rax
movq %rax, -0x1e0(%rbp)
movq -0xe0(%rbp), %rsi
movq -0xd8(%rbp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x4722
leaq -0x120(%rbp), %rdi
movq %r14, %rsi
callq 0x3575
movl %eax, %r13d
movq -0x1e0(%rbp), %rdi
leaq -0x1d0(%rbp), %rax
cmpq %rax, %rdi
je 0x3e2e
callq 0x2280
movq -0x120(%rbp), %rdi
leaq -0x110(%rbp), %rax
cmpq %rax, %rdi
je 0x3e46
callq 0x2280
testb %r13b, %r13b
je 0x3eda
movq -0x68(%rbp), %rdx
movq -0xe0(%rbp), %rcx
movq -0x280(%rbp), %r8
leaq 0x58f8(%rip), %r15      # 0x9760
movq %r15, %rdi
leaq 0x1502(%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 0x1572(%rip), %rsi      # 0x540c
callq 0x2300
movq -0x280(%rbp), %rdx
movq -0xe0(%rbp), %rcx
leaq -0x2e4(%rbp), %rdi
leaq 0x154c(%rip), %rsi      # 0x5407
xorl %eax, %eax
callq 0x20a0
leaq -0xa20(%rbp), %rdi
leaq -0x2e8(%rbp), %rsi
movl $0x68, %edx
callq 0x2b26
movl $0x1, 0x58e0(%rip)      # 0x97c4
movl $0x64, %edi
callq 0x2370
jmp 0x393e
movq %rax, %rbx
movq -0x1e0(%rbp), %rdi
leaq -0x1d0(%rbp), %rax
cmpq %rax, %rdi
je 0x3f13
callq 0x2280
jmp 0x3f13
movq %rax, %rbx
movq -0x120(%rbp), %rdi
leaq -0x110(%rbp), %rax
jmp 0x3f92
jmp 0x3f9e
movq %rax, %rbx
movq -0x200(%rbp), %rdi
leaq -0x1f0(%rbp), %rax
cmpq %rax, %rdi
je 0x3f45
callq 0x2280
jmp 0x3f45
movq %rax, %rbx
movq -0x160(%rbp), %rdi
leaq -0x150(%rbp), %rax
jmp 0x3f92
jmp 0x3f9e
movq %rax, %rbx
movq -0x140(%rbp), %rdi
leaq -0x130(%rbp), %rax
jmp 0x3f92
jmp 0x3f9e
jmp 0x3f9e
movq %rax, %rbx
movq -0x180(%rbp), %rdi
leaq -0x170(%rbp), %rax
jmp 0x3f92
movq %rax, %rbx
movq -0x260(%rbp), %rdi
leaq -0x250(%rbp), %rax
cmpq %rax, %rdi
je 0x3fa1
callq 0x2280
jmp 0x3fa1
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 0x3fcd
callq 0x2280
jmp 0x3fcd
movq %rax, %rbx
movq -0x280(%rbp), %rdi
leaq -0x270(%rbp), %rax
cmpq %rax, %rdi
je 0x3fea
callq 0x2280
jmp 0x3fea
movq %rax, %rbx
leaq -0x190(%rbp), %rax
movq -0x1a0(%rbp), %rdi
cmpq %rax, %rdi
je 0x4007
callq 0x2280
jmp 0x4007
movq %rax, %rbx
leaq -0x230(%rbp), %rax
movq -0x240(%rbp), %rdi
cmpq %rax, %rdi
je 0x4024
callq 0x2280
jmp 0x4024
movq %rax, %rbx
leaq -0xd0(%rbp), %rax
movq -0xe0(%rbp), %rdi
cmpq %rax, %rdi
je 0x4041
callq 0x2280
jmp 0x4041
movq %rax, %rbx
leaq -0xf0(%rbp), %rax
movq -0x100(%rbp), %rdi
cmpq %rax, %rdi
je 0x405e
callq 0x2280
jmp 0x405e
movq %rax, %rbx
leaq -0x1b0(%rbp), %rax
movq -0x1c0(%rbp), %rdi
cmpq %rax, %rdi
je 0x407b
callq 0x2280
jmp 0x407b
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
cmpq %r12, %rdi
je 0x4091
callq 0x2280
jmp 0x4091
movq %rax, %rbx
movq -0x68(%rbp), %rdi
cmpq %r15, %rdi
je 0x40a4
callq 0x2280
jmp 0x40a4
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r14, %rdi
je 0x40b7
callq 0x2280
jmp 0x40b7
movq %rax, %rbx
leaq -0x558(%rbp), %rdi
callq 0x20e0
jmp 0x40c8
movq %rax, %rbx
leaq -0xc0(%rbp), %rdi
callq 0x46d4
leaq -0x80(%rbp), %rdi
callq 0x46d4
movq %rbx, %rdi
callq 0x2470