// File: /workspace/github_cpp_makefile_star2_O2/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 -0x25f(%rip), %rsi      # 0x2ee7
pushq $0xa
popq %rdi
callq 0x21d0
callq 0x2260
movl %eax, %ebx
leaq -0x2a0(%rbp), %r14
movq %r14, %rdi
callq 0x2300
leaq 0x108a(%rip), %rsi      # 0x41f5
pushq $0x10
popq %rdx
movq %r14, %rdi
callq 0x2050
movq -0x2a0(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x280(%rbp,%rax)
jne 0x3561
leaq -0x2a0(%rbp), %rdi
movl %ebx, %esi
callq 0x2310
movq %rax, %rdi
callq 0x20f0
leaq -0x2a0(%rbp), %rdi
callq 0x2240
leaq 0x4078(%rip), %rdi      # 0x7230
pushq $0x64
popq %rsi
callq 0x2280
testl %eax, %eax
jne 0x3584
movq 0x3e01(%rip), %rdi      # 0x6fd0
leaq 0x10ec(%rip), %rsi      # 0x42c2
callq 0x21a0
leaq 0x404e(%rip), %rsi      # 0x7230
movq %rax, %rdi
callq 0x21a0
movq %rax, %rdi
callq 0x20f0
leaq 0x4037(%rip), %rcx      # 0x7230
leaq -0x768(%rbp), %rdi
pushq $0x1
popq %rsi
movl $0x4652, %edx           # imm = 0x4652
callq 0x25de
leaq -0x768(%rbp), %rdi
callq 0x2942
pushq $0x38
popq %rdi
callq 0x2200
movq %rax, %rbx
pushq $0x8
popq %rdi
callq 0x2200
movq %rax, %r14
movq %rbx, %rdi
callq 0x21f0
leaq -0x682(%rip), %rdx      # 0x2bbc
movq %r14, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x2340
testl %eax, %eax
je 0x325b
leaq 0x1077(%rip), %rdi      # 0x42cd
callq 0x2140
pushq $0x2
popq %rdi
callq 0x2120
movl $0x6, -0x30(%rbp)
leaq 0x3f93(%rip), %rdi      # 0x7204
callq 0x3672
movb %al, -0x2c(%rbp)
leaq 0x3fb0(%rip), %rcx      # 0x7230
leaq -0x10f8(%rbp), %rdi
xorl %esi, %esi
movl $0x4651, %edx           # imm = 0x4651
callq 0x25de
leaq -0x10f8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
pushq $0x8
popq %rdx
callq 0x27b2
leaq 0x3f83(%rip), %rcx      # 0x7230
leaq -0xc30(%rbp), %rdi
xorl %esi, %esi
movl $0x4653, %edx           # imm = 0x4653
callq 0x25de
leaq -0xc30(%rbp), %rdi
leaq -0x30(%rbp), %rsi
pushq $0x8
popq %rdx
callq 0x27b2
movq 0x3cf6(%rip), %rdi      # 0x6fd0
leaq 0x1018(%rip), %rsi      # 0x42f9
callq 0x21a0
movq %rax, %rbx
leaq 0x3f14(%rip), %rdi      # 0x7204
callq 0x3622
movq %rbx, %rdi
movl %eax, %esi
callq 0x2310
movq %rax, %rdi
callq 0x20f0
movq 0x3cc2(%rip), %rdi      # 0x6fd0
leaq 0xfec(%rip), %rsi       # 0x4301
callq 0x21a0
movq %rax, %rbx
leaq 0x3ee0(%rip), %rdi      # 0x7204
callq 0x3630
movq %rbx, %rdi
movl %eax, %esi
callq 0x2310
movq %rax, %rdi
callq 0x20f0
movq 0x3c8e(%rip), %r15      # 0x6fd0
leaq 0x3ebb(%rip), %r13      # 0x7204
leaq 0xfb1(%rip), %rbx       # 0x4301
leaq 0x101b(%rip), %r12      # 0x4372
leaq -0x768(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x27fa
movq %r15, %rdi
leaq 0xf85(%rip), %rsi       # 0x42f9
callq 0x21a0
movq %rax, %r14
movq %r13, %rdi
callq 0x3622
movq %r14, %rdi
movl %eax, %esi
callq 0x2310
movq %rax, %rdi
callq 0x20f0
movq %r15, %rdi
movq %rbx, %rsi
callq 0x21a0
movq %rax, %r14
movq %r13, %rdi
callq 0x3630
movq %r14, %rdi
movl %eax, %esi
callq 0x2310
movq %rax, %rdi
callq 0x20f0
cmpb $0x1, 0x3ecf(%rip)      # 0x7294
jne 0x33de
movq %r15, %rdi
leaq 0xf37(%rip), %rsi       # 0x4308
callq 0x21a0
movq %rax, %rdi
callq 0x20f0
cmpb $0x1, 0x3eb0(%rip)      # 0x7295
jne 0x33fe
movq %r15, %rdi
leaq 0xf23(%rip), %rsi       # 0x4314
callq 0x21a0
movq %rax, %rdi
callq 0x20f0
cmpb $0x43, -0xa0(%rbp)
jne 0x3418
movq %r13, %rdi
callq 0x35ea
movb $0x1, 0x3e12(%rip)      # 0x7228
jmp 0x3445
movq %r13, %rdi
callq 0x3622
testl %eax, %eax
je 0x342e
movq %r13, %rdi
callq 0x35f8
jmp 0x3445
movq %r15, %rdi
leaq 0xee7(%rip), %rsi       # 0x431f
callq 0x21a0
movq %rax, %rdi
callq 0x20f0
cmpb $0x50, -0xa0(%rbp)
jne 0x345f
movq %r13, %rdi
callq 0x3606
movb $0x1, 0x3dcb(%rip)      # 0x7228
jmp 0x348c
movq %r13, %rdi
callq 0x3630
testl %eax, %eax
je 0x3475
movq %r13, %rdi
callq 0x3614
jmp 0x348c
movq %r15, %rdi
leaq 0xea7(%rip), %rsi       # 0x4326
callq 0x21a0
movq %rax, %rdi
callq 0x20f0
movq %r13, %rdi
callq 0x3672
movq %r12, %rsi
testb %al, %al
jne 0x34ae
movq %r13, %rdi
callq 0x365a
leaq 0xe84(%rip), %rsi       # 0x432e
testl %eax, %eax
je 0x34c3
movq %r15, %rdi
callq 0x21a0
movq %rax, %rdi
callq 0x20f0
jmp 0x3357
movq %r15, %rdi
leaq 0xe7d(%rip), %rsi       # 0x434a
callq 0x21a0
movq %rax, %rdi
callq 0x20f0
movq %r15, %rdi
leaq 0xe7b(%rip), %rsi       # 0x435f
callq 0x21a0
movq %rax, %rdi
callq 0x20f0
movq %r13, %rdi
callq 0x367a
movq %r13, %rdi
callq 0x3672
movb %al, -0x2c(%rbp)
leaq -0x1a88(%rbp), %r14
movq %r14, %rdi
xorl %esi, %esi
movl $0x4651, %edx           # imm = 0x4651
leaq 0x3d14(%rip), %rcx      # 0x7230
callq 0x25de
movq %r14, %rdi
leaq -0x30(%rbp), %rsi
pushq $0x8
popq %rdx
callq 0x27b2
leaq -0x15c0(%rbp), %r14
movq %r14, %rdi
xorl %esi, %esi
movl $0x4653, %edx           # imm = 0x4653
leaq 0x3ce8(%rip), %rcx      # 0x7230
callq 0x25de
movq %r14, %rdi
leaq -0x30(%rbp), %rsi
pushq $0x8
popq %rdx
callq 0x27b2
jmp 0x3357
movq 0x3a68(%rip), %rdi      # 0x6fd0
leaq 0xc77(%rip), %rsi       # 0x41e6
callq 0x21a0
movq %rax, %rdi
callq 0x20f0
pushq $0x1
popq %rdi
callq 0x21e0
leaq 0xd27(%rip), %rdi       # 0x42b2
callq 0x2140
jmp 0x357c
jmp 0x35a4
jmp 0x35a4
jmp 0x35a4
jmp 0x35a4
jmp 0x35a4
jmp 0x35a4
jmp 0x35a4
jmp 0x35a4
jmp 0x35a4
movq %rax, %rbx
leaq -0x2a0(%rbp), %rdi
callq 0x2270
movq %rbx, %rdi
callq 0x2330
nop