// File: /workspace/github_cpp_makefile_star2_O2/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 0x519b(%rip), %rcx      # 0x82c0
leaq -0xa20(%rbp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
movl $0x4651, %edx           # imm = 0x4651
callq 0x270e
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x2320
movq %rbx, %rdi
callq 0x2a72
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
andq $0x0, -0x30(%rbp)
movaps %xmm0, -0x60(%rbp)
andq $0x0, -0x50(%rbp)
leaq -0x558(%rbp), %rdi
callq 0x2280
movl $0x3, -0x350(%rbp)
movl $0x11, -0x2e8(%rbp)
leaq 0x10f5(%rip), %rsi      # 0x427e
leaq -0x100(%rbp), %rdi
leaq -0xe0(%rbp), %rdx
callq 0x2100
leaq 0x10fa(%rip), %rsi      # 0x429d
leaq -0xe0(%rbp), %rdi
leaq -0x1c0(%rbp), %rdx
callq 0x2100
leaq 0x10fd(%rip), %rsi      # 0x42ba
leaq -0x1c0(%rbp), %rdi
leaq -0x180(%rbp), %rdx
callq 0x2100
leaq 0x10e6(%rip), %rsi      # 0x42bd
leaq -0x180(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
callq 0x2100
leaq 0x10dd(%rip), %rsi      # 0x42ce
leaq -0xa0(%rbp), %rdi
leaq -0x80(%rbp), %rdx
callq 0x2100
leaq 0x10cf(%rip), %rsi      # 0x42d7
leaq -0x80(%rbp), %rdi
leaq -0x1a0(%rbp), %rdx
callq 0x2100
leaq 0xfc7(%rip), %rsi       # 0x41e6
leaq -0x1a0(%rbp), %rdi
leaq -0x160(%rbp), %rdx
callq 0x2100
leaq 0x10a5(%rip), %rsi      # 0x42de
leaq -0x160(%rbp), %rdi
leaq -0x140(%rbp), %rdx
callq 0x2100
leaq 0x10a2(%rip), %rsi      # 0x42f5
leaq -0x140(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
callq 0x2100
leaq 0x109c(%rip), %rsi      # 0x4309
leaq -0xc0(%rbp), %rdi
leaq -0x120(%rbp), %rdx
callq 0x2100
leaq 0x54a9(%rip), %r15      # 0x8730
movq 0x3d4a(%rip), %rbx      # 0x6fd8
cmpb $0x1, 0x5023(%rip)      # 0x82b8
jne 0x363a
leaq -0x40(%rbp), %rdi
callq 0x3c00
leaq -0x240(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x2310
leaq -0x240(%rbp), %r12
movq %r12, %rdi
leaq -0x40(%rbp), %rsi
callq 0x2ee0
movq %r12, %rdi
callq 0x2150
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
leaq -0x180(%rbp), %rdx
callq 0x3e8f
movq -0x38(%rbp), %rsi
cmpq %rsi, %rax
je 0x3435
movq %rbx, %rdi
leaq 0x1026(%rip), %rsi      # 0x4323
callq 0x2260
movq %rax, %rdi
callq 0x2160
movq -0x1c0(%rbp), %rdx
movq -0x1a0(%rbp), %rcx
movq -0xc0(%rbp), %r8
movq %r15, %rdi
leaq 0x1005(%rip), %rsi      # 0x432e
xorl %eax, %eax
callq 0x2090
movq %r15, %rdi
callq 0x21e0
movups 0x100a(%rip), %xmm0     # 0x4349
movaps %xmm0, 0x53fa(%rip)     # 0x8740
movups 0xfec(%rip), %xmm0      # 0x4339
movaps %xmm0, 0x53dc(%rip)     # 0x8730
movl $0x68732e, 0x53f1(%rip) # imm = 0x68732E
movq %r15, %rdi
callq 0x21e0
pushq $0x1
popq %rdi
callq 0x21a0
movq %rbx, %rdi
leaq 0xfe4(%rip), %rsi       # 0x435c
callq 0x2260
movq %rax, %rdi
callq 0x2160
movq -0xc0(%rbp), %rdx
movq %r15, %rdi
leaq 0xfdc(%rip), %rsi       # 0x4372
xorl %eax, %eax
callq 0x2090
movq %r15, %rdi
callq 0x21e0
movq -0xc0(%rbp), %rdx
movq %r15, %rdi
leaq 0xfc7(%rip), %rsi       # 0x437d
xorl %eax, %eax
callq 0x2090
movq %r15, %rdi
callq 0x21e0
movq %rbx, %rdi
leaq 0xfb9(%rip), %rsi       # 0x4388
callq 0x2260
movq %rax, %rdi
callq 0x2160
movl 0x4ece(%rip), %eax      # 0x82b0
movl %eax, 0x4ecc(%rip)      # 0x82b4
leaq -0x120(%rbp), %rdi
leaq -0x100(%rbp), %rsi
leaq -0x180(%rbp), %rdx
callq 0x3bc8
movq -0x120(%rbp), %rdi
callq 0x2430
movq %rbx, %rdi
leaq 0xf87(%rip), %rsi       # 0x439f
callq 0x2260
pushq $0x1
popq %rdi
callq 0x21a0
leaq -0x120(%rbp), %rdi
callq 0x2150
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
callq 0x3e8f
cmpq -0x38(%rbp), %rax
je 0x3520
leaq -0x220(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x2310
leaq -0x280(%rbp), %r12
movq %r12, %rdi
leaq -0xa0(%rbp), %rsi
callq 0x2310
leaq -0x220(%rbp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x2fbb
movl %eax, %r13d
movq %r12, %rdi
callq 0x2150
movq %r14, %rdi
callq 0x2150
testb %r13b, %r13b
je 0x3520
movq -0x100(%rbp), %rdx
movq -0xa0(%rbp), %rcx
movq -0x160(%rbp), %r8
movq %r15, %rdi
leaq 0xe6d(%rip), %rsi       # 0x432e
xorl %eax, %eax
callq 0x2090
movq %r15, %rdi
callq 0x21e0
pushq $0x1
popq %rdi
callq 0x21a0
movq %rbx, %rdi
leaq 0xece(%rip), %rsi       # 0x43b0
callq 0x2260
movq -0x160(%rbp), %rdx
movq -0xa0(%rbp), %rcx
leaq -0x34c(%rbp), %rdi
leaq 0xebe(%rip), %rsi       # 0x43c1
xorl %eax, %eax
callq 0x2090
leaq -0xa20(%rbp), %rdi
leaq -0x350(%rbp), %rsi
pushq $0x68
popq %rdx
callq 0x28e2
leaq -0x60(%rbp), %rdi
callq 0x3c00
leaq -0x200(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x2310
leaq -0x200(%rbp), %r14
movq %r14, %rdi
leaq -0x60(%rbp), %rsi
callq 0x2ee0
movq %r14, %rdi
callq 0x2150
movq -0x60(%rbp), %rdi
movq -0x58(%rbp), %rsi
leaq -0x80(%rbp), %rdx
callq 0x3e8f
cmpq -0x58(%rbp), %rax
je 0x363a
leaq -0x1e0(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x2310
leaq -0x260(%rbp), %r14
movq %r14, %rdi
leaq -0x80(%rbp), %rsi
callq 0x2310
leaq -0x1e0(%rbp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x2fbb
movl %eax, %r13d
movq %r14, %rdi
callq 0x2150
movq %r12, %rdi
callq 0x2150
testb %r13b, %r13b
je 0x363a
movq -0xe0(%rbp), %rdx
movq -0x80(%rbp), %rcx
movq -0x140(%rbp), %r8
movq %r15, %rdi
leaq 0xd50(%rip), %rsi       # 0x432e
xorl %eax, %eax
callq 0x2090
movq %r15, %rdi
callq 0x21e0
pushq $0x1
popq %rdi
callq 0x21a0
movq %rbx, %rdi
leaq 0xdc7(%rip), %rsi       # 0x43c6
callq 0x2260
movq -0x140(%rbp), %rdx
movq -0x80(%rbp), %rcx
leaq -0x2e4(%rbp), %rdi
leaq 0xda4(%rip), %rsi       # 0x43c1
xorl %eax, %eax
callq 0x2090
leaq -0xa20(%rbp), %rdi
leaq -0x2e8(%rbp), %rsi
pushq $0x68
popq %rdx
callq 0x28e2
movl $0x1, 0x5150(%rip)      # 0x8794
pushq $0x64
popq %rdi
callq 0x2320
jmp 0x328e
movq %rax, %rbx
leaq -0x260(%rbp), %rdi
callq 0x2150
jmp 0x3665
movq %rax, %rbx
leaq -0x1e0(%rbp), %rdi
jmp 0x36b5
movq %rax, %rbx
leaq -0x280(%rbp), %rdi
callq 0x2150
jmp 0x3684
jmp 0x36bc
movq %rax, %rbx
leaq -0x220(%rbp), %rdi
jmp 0x36b5
jmp 0x36bc
movq %rax, %rbx
leaq -0x200(%rbp), %rdi
jmp 0x36b5
jmp 0x36bc
jmp 0x36bc
movq %rax, %rbx
leaq -0x240(%rbp), %rdi
jmp 0x36b5
movq %rax, %rbx
leaq -0x120(%rbp), %rdi
callq 0x2150
jmp 0x36bf
movq %rax, %rbx
leaq -0xc0(%rbp), %rdi
callq 0x2150
jmp 0x36d0
movq %rax, %rbx
leaq -0x140(%rbp), %rdi
callq 0x2150
jmp 0x36e1
movq %rax, %rbx
leaq -0x160(%rbp), %rdi
callq 0x2150
jmp 0x36f2
movq %rax, %rbx
leaq -0x1a0(%rbp), %rdi
callq 0x2150
jmp 0x3703
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x2150
jmp 0x3711
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
callq 0x2150
jmp 0x3722
movq %rax, %rbx
leaq -0x180(%rbp), %rdi
callq 0x2150
jmp 0x3733
movq %rax, %rbx
leaq -0x1c0(%rbp), %rdi
callq 0x2150
jmp 0x3744
movq %rax, %rbx
leaq -0xe0(%rbp), %rdi
callq 0x2150
jmp 0x3755
movq %rax, %rbx
leaq -0x100(%rbp), %rdi
callq 0x2150
jmp 0x3766
movq %rax, %rbx
leaq -0x558(%rbp), %rdi
callq 0x20d0
jmp 0x3777
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x3c0e
leaq -0x40(%rbp), %rdi
callq 0x3c0e
movq %rbx, %rdi
callq 0x2420