// File: /workspace/github_cpp_makefile_star2_O3/CDISC2020[P]Projet_satellite_agile/src/FDIR/main_FDIR.cpp
// Function: main
int  main (int argc,char* argv[]) 
{
	signal(SIGUSR1,ARINC_PID);

	// Ecriture du pid dans un fichier
	int pid=getpid();
	ofstream fichierPid;
	fichierPid.open("pid_FDIR");
	if(!fichierPid)	{
		cout << "Failed to open pid_FDIR" << endl;
		exit(1);
	}
	fichierPid << pid << endl;
	fichierPid.close();
	
	if (gethostname(s, 100) != 0) {
    		perror("S-> gethostname");
    		exit(1);
  	}
	cout << "Host name " << s << endl; 

	QueuingPort channel(1, 18002, s); // Server
	channel.Display();	

        pthread_attr_t *thread_attributes;
        pthread_t *thread;

        thread_attributes=(pthread_attr_t *)malloc(sizeof(pthread_attr_t));
        thread=(pthread_t *)malloc(sizeof(pthread_t));

        pthread_attr_init(thread_attributes);
        if (pthread_create(thread, thread_attributes, gestion_wd_arduino,(void *) NULL) != 0)
                perror ("Thread_Server-> Failure detector thread pb!");

	sleep(2);

	ModeStruct mode;
	mode.code=6;
	mode.rpiMode=myFDIR.isleader();

	QueuingPort channelPM(0, 18001, s);
        channelPM.SendQueuingMsg((char*)&mode, sizeof(ModeStruct));

	QueuingPort channelCom(0, 18003, s);
        channelCom.SendQueuingMsg((char*)&mode, sizeof(ModeStruct));


	char buffer[100];
	
	cout << "WD COM=" << myFDIR.read_watch_com() << endl;
	cout << "WD PM=" << myFDIR.read_watch_plan() << endl;

	while(1) 
	{	
		channel.RecvQueuingMsg(buffer);

		cout << "WD COM=" << myFDIR.read_watch_com() << endl;
		cout << "WD PM=" << myFDIR.read_watch_plan() << endl;

		if(rec_COM)
			cout << "COM ... WAK" << endl;
		if(rec_PM)
			cout << "PM ... WAK" << endl;

		if (buffer[0]=='C')
		{
			myFDIR.set_watch_com(); //On remet le compteur à 100
			set_wd_ard=true;
		}
		else
		{
			if (myFDIR.read_watch_com()==0) // on test si le compteur est a 0
			{
				cout << "com KC" << endl; //ça fait trop longtemps qu'on a pas de nouvelles
				//recouvrement_COM();
			}
			else
				myFDIR.dec_watch_com();// si on a pas de nouvelles, on decremente le compteur
		}
		
		if (buffer[0]=='P')
		{
			myFDIR.set_watch_plan();
			set_wd_ard=true;
		}
		else
		{
			if (myFDIR.read_watch_plan()==0) // on test si le compteur est a 0
			{
				cout << "plan KC" << endl; //ça fait trop longtemps qu'on a pas de nouvelles
				//recouvrement_PM();
			}
			else
				myFDIR.dec_watch_plan();// si on a pas de nouvelles, on decremente le compteur
		}
		
		if(!myFDIR.isleader())//si je ne suis pas leader, je vérifie ce qu'il se passe
		{
			if (myFDIR.read_arduino())
			{ 
				cout << "AH, YES, JE VOIS L'ARDUINO " << endl;
				//tout se passe bien, l'autre raspi est vivante
			}
			else
			{ 			
				cout << "J'AI DUPER LE SIGNAL" << endl;
				//dans l'ensemble ca se passe mal (citation historique)
				cout << "JE PASSE EN LEADER" << endl;

				myFDIR.switch_leader();		
				mode.rpiMode=myFDIR.isleader();

				QueuingPort channelPM(0, 18001, s);
       				channelPM.SendQueuingMsg((char*)&mode, sizeof(ModeStruct));

				QueuingPort channelCom(0, 18003, s);
        			channelCom.SendQueuingMsg((char*)&mode, sizeof(ModeStruct));	
			}
		}

		else
		{
			cout << "Je suis LEADER, youplala !"<<endl;
		}
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1a68, %rsp           # imm = 0x1A68
leaq -0x2f4(%rip), %rsi      # 0x34bb
movl $0xa, %edi
callq 0x2250
callq 0x22e0
movl %eax, %ebx
leaq -0x2a0(%rbp), %r14
movq %r14, %rdi
callq 0x2380
leaq 0x1a1f(%rip), %rsi      # 0x51f5
movq %r14, %rdi
movl $0x10, %edx
callq 0x2060
movq -0x2a0(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x280(%rbp,%rax)
jne 0x3c5a
leaq -0x2a0(%rbp), %rdi
movl %ebx, %esi
callq 0x2390
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2150
leaq -0x2a0(%rbp), %rdi
callq 0x22c0
leaq 0x4a29(%rip), %rdi      # 0x8270
movl $0x64, %esi
callq 0x2300
testl %eax, %eax
jne 0x3caa
movq 0x4770(%rip), %rdi      # 0x7fd0
leaq 0x1a5b(%rip), %rsi      # 0x52c2
movl $0xa, %edx
callq 0x2240
leaq 0x49f8(%rip), %rbx      # 0x8270
movq %rbx, %rdi
callq 0x20f0
movq 0x4749(%rip), %rdi      # 0x7fd0
movq %rbx, %rsi
movq %rax, %rdx
callq 0x2240
movq 0x4737(%rip), %rdi      # 0x7fd0
callq 0x2110
leaq 0x49cb(%rip), %rcx      # 0x8270
leaq -0x768(%rbp), %rdi
movl $0x1, %esi
movl $0x4652, %edx           # imm = 0x4652
callq 0x28b6
leaq -0x768(%rbp), %rdi
callq 0x2dd8
movl $0x38, %edi
callq 0x2280
movq %rax, %rbx
movl $0x8, %edi
callq 0x2280
movq %rax, %r14
movq %rbx, %rdi
callq 0x2270
leaq -0x858(%rip), %rdx      # 0x3098
movq %r14, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x23c0
testl %eax, %eax
je 0x390d
leaq 0x19c5(%rip), %rdi      # 0x52cd
callq 0x2190
movl $0x2, %edi
callq 0x2170
movl $0x6, -0x30(%rbp)
leaq 0x491f(%rip), %rdi      # 0x8244
callq 0x3dc2
movb %al, -0x2c(%rbp)
leaq 0x493c(%rip), %rcx      # 0x8270
leaq -0x1a88(%rbp), %rdi
xorl %esi, %esi
movl $0x4651, %edx           # imm = 0x4651
callq 0x28b6
leaq -0x1a88(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0x8, %edx
callq 0x2b76
leaq 0x490d(%rip), %rcx      # 0x8270
leaq -0x15c0(%rbp), %rdi
xorl %esi, %esi
movl $0x4653, %edx           # imm = 0x4653
callq 0x28b6
leaq -0x15c0(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0x8, %edx
callq 0x2b76
movq 0x463e(%rip), %rdi      # 0x7fd0
leaq 0x1960(%rip), %rsi      # 0x52f9
movl $0x7, %edx
callq 0x2240
leaq 0x489a(%rip), %rdi      # 0x8244
callq 0x3d72
movq 0x461a(%rip), %rdi      # 0x7fd0
movl %eax, %esi
callq 0x2390
movq %rax, %rdi
callq 0x2110
movq 0x4604(%rip), %rdi      # 0x7fd0
leaq 0x192e(%rip), %rsi      # 0x5301
movl $0x6, %edx
callq 0x2240
leaq 0x4860(%rip), %rdi      # 0x8244
callq 0x3d80
movq 0x45e0(%rip), %rdi      # 0x7fd0
movl %eax, %esi
callq 0x2390
movq %rax, %rdi
callq 0x2110
movq 0x45ca(%rip), %r15      # 0x7fd0
leaq 0x18ec(%rip), %r12      # 0x52f9
leaq 0x4830(%rip), %r13      # 0x8244
leaq 0x18e6(%rip), %rbx      # 0x5301
leaq 0x1950(%rip), %r14      # 0x5372
leaq -0x768(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x2bb8
movl $0x7, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2240
movq %r13, %rdi
callq 0x3d72
movq %r15, %rdi
movl %eax, %esi
callq 0x2390
movq %rax, %rdi
callq 0x2110
movl $0x6, %edx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2240
movq %r13, %rdi
callq 0x3d80
movq %r15, %rdi
movl %eax, %esi
callq 0x2390
movq %rax, %rdi
callq 0x2110
cmpb $0x1, 0x4844(%rip)      # 0x82d4
jne 0x3aae
movl $0xb, %edx
movq %r15, %rdi
leaq 0x1867(%rip), %rsi      # 0x5308
callq 0x2240
movq %r15, %rdi
callq 0x2110
cmpb $0x1, 0x4820(%rip)      # 0x82d5
jne 0x3ad3
movl $0xa, %edx
movq %r15, %rdi
leaq 0x184e(%rip), %rsi      # 0x5314
callq 0x2240
movq %r15, %rdi
callq 0x2110
cmpb $0x43, -0xa0(%rbp)
jne 0x3aed
movq %r13, %rdi
callq 0x3d3a
movb $0x1, 0x477d(%rip)      # 0x8268
jmp 0x3b1f
movq %r13, %rdi
callq 0x3d72
testl %eax, %eax
je 0x3b03
movq %r13, %rdi
callq 0x3d48
jmp 0x3b1f
movl $0x6, %edx
movq %r15, %rdi
leaq 0x180d(%rip), %rsi      # 0x531f
callq 0x2240
movq %r15, %rdi
callq 0x2110
cmpb $0x50, -0xa0(%rbp)
jne 0x3b39
movq %r13, %rdi
callq 0x3d56
movb $0x1, 0x4731(%rip)      # 0x8268
jmp 0x3b6b
movq %r13, %rdi
callq 0x3d80
testl %eax, %eax
je 0x3b4f
movq %r13, %rdi
callq 0x3d64
jmp 0x3b6b
movl $0x7, %edx
movq %r15, %rdi
leaq 0x17c8(%rip), %rsi      # 0x5326
callq 0x2240
movq %r15, %rdi
callq 0x2110
movq %r13, %rdi
callq 0x3dc2
movl $0x1a, %edx
movq %r14, %rsi
testb %al, %al
jne 0x3b97
movq %r13, %rdi
callq 0x3daa
movl $0x1b, %edx
leaq 0x179b(%rip), %rsi      # 0x532e
testl %eax, %eax
je 0x3bac
movq %r15, %rdi
callq 0x2240
movq %r15, %rdi
callq 0x2110
jmp 0x3a22
movl $0x14, %edx
movq %r15, %rdi
leaq 0x178f(%rip), %rsi      # 0x534a
callq 0x2240
movq %r15, %rdi
callq 0x2110
movl $0x12, %edx
movq %r15, %rdi
leaq 0x1788(%rip), %rsi      # 0x535f
callq 0x2240
movq %r15, %rdi
callq 0x2110
movq %r13, %rdi
callq 0x3dca
movq %r13, %rdi
callq 0x3dc2
movb %al, -0x2c(%rbp)
leaq -0x10f8(%rbp), %rdi
xorl %esi, %esi
movl $0x4651, %edx           # imm = 0x4651
leaq 0x4664(%rip), %rcx      # 0x8270
callq 0x28b6
leaq -0x10f8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0x8, %edx
callq 0x2b76
leaq -0xc30(%rbp), %rdi
xorl %esi, %esi
movl $0x4653, %edx           # imm = 0x4653
leaq 0x4635(%rip), %rcx      # 0x8270
callq 0x28b6
leaq -0xc30(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0x8, %edx
callq 0x2b76
jmp 0x3a22
movq 0x436f(%rip), %rbx      # 0x7fd0
leaq 0x157e(%rip), %rsi      # 0x51e6
movl $0x17, %edx
movq %rbx, %rdi
callq 0x2240
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2200
movsbl %al, %esi
movq 0x433d(%rip), %rdi      # 0x7fd0
callq 0x2030
movq %rax, %rdi
callq 0x2150
movl $0x1, %edi
callq 0x2260
leaq 0x1601(%rip), %rdi      # 0x52b2
callq 0x2190
jmp 0x3ca0
jmp 0x3cca
jmp 0x3cca
jmp 0x3cca
jmp 0x3cca
jmp 0x3cca
jmp 0x3cca
jmp 0x3cca
jmp 0x3cca
jmp 0x3cca
movq %rax, %rbx
movq 0x42f4(%rip), %rax      # 0x7fc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x298(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x2a0(%rbp,%rcx)
callq 0x21a0
leaq -0x1a8(%rbp), %rdi
callq 0x20d0
movq %rbx, %rdi
callq 0x23b0
nop