// File: 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
subq $0x1b90, %rsp           # imm = 0x1B90
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x350(%rip), %rsi      # 0x33e0
movl $0xa, %edi
callq 0x2200
callq 0x22b0
movl %eax, -0x14(%rbp)
leaq -0x218(%rbp), %rdi
movq %rdi, -0x1a98(%rbp)
callq 0x2330
movq -0x1a98(%rbp), %rdi
leaq 0x1a97(%rip), %rsi      # 0x51fa
movl $0x10, %edx
callq 0x2050
jmp 0x376f
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x20f0
movb %al, -0x1a99(%rbp)
jmp 0x378f
movb -0x1a99(%rbp), %al
testb $0x1, %al
jne 0x379b
jmp 0x37f9
movq 0x482e(%rip), %rdi      # 0x7fd0
leaq 0x1a42(%rip), %rsi      # 0x51eb
callq 0x21b0
movq %rax, -0x1aa8(%rbp)
jmp 0x37b7
movq -0x1aa8(%rbp), %rdi
movq 0x47fb(%rip), %rsi      # 0x7fc0
callq 0x21c0
jmp 0x37cc
movl $0x1, %edi
callq 0x2210
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x22c0
jmp 0x3eb3
movl -0x14(%rbp), %esi
leaq -0x218(%rbp), %rdi
callq 0x2340
movq %rax, -0x1ab0(%rbp)
jmp 0x3811
movq -0x1ab0(%rbp), %rdi
movq 0x47a1(%rip), %rsi      # 0x7fc0
callq 0x21c0
jmp 0x3826
leaq -0x218(%rbp), %rdi
callq 0x2280
jmp 0x3834
leaq 0x4a05(%rip), %rdi      # 0x8240
movl $0x64, %esi
callq 0x22d0
cmpl $0x0, %eax
je 0x3862
leaq 0x1a66(%rip), %rdi      # 0x52b7
callq 0x2150
jmp 0x3858
movl $0x1, %edi
callq 0x2210
movq 0x4767(%rip), %rdi      # 0x7fd0
leaq 0x1a57(%rip), %rsi      # 0x52c7
callq 0x21b0
movq %rax, -0x1ab8(%rbp)
jmp 0x387e
movq -0x1ab8(%rbp), %rdi
leaq 0x49b4(%rip), %rsi      # 0x8240
callq 0x21b0
movq %rax, -0x1ac0(%rbp)
jmp 0x389a
movq -0x1ac0(%rbp), %rdi
movq 0x4718(%rip), %rsi      # 0x7fc0
callq 0x21c0
jmp 0x38af
leaq 0x498a(%rip), %rcx      # 0x8240
leaq -0x6f0(%rbp), %rdi
movl $0x1, %esi
movl $0x4652, %edx           # imm = 0x4652
callq 0x26a0
jmp 0x38ce
leaq -0x6f0(%rbp), %rdi
callq 0x2b00
jmp 0x38dc
movl $0x38, %edi
callq 0x2230
movq %rax, -0x6f8(%rbp)
movl $0x8, %edi
callq 0x2230
movq %rax, -0x700(%rbp)
movq -0x6f8(%rbp), %rdi
callq 0x2220
movq -0x700(%rbp), %rdi
movq -0x6f8(%rbp), %rsi
leaq -0x9bf(%rip), %rdx      # 0x2f60
xorl %eax, %eax
movl %eax, %ecx
callq 0x2370
cmpl $0x0, %eax
je 0x393d
leaq 0x199e(%rip), %rdi      # 0x52d2
callq 0x2150
jmp 0x393b
jmp 0x393d
movl $0x2, %edi
callq 0x2130
jmp 0x3949
movl $0x6, -0x708(%rbp)
leaq 0x48ba(%rip), %rdi      # 0x8214
callq 0x4040
movb %al, -0x1ac1(%rbp)
jmp 0x3967
movb -0x1ac1(%rbp), %al
andb $0x1, %al
movb %al, -0x704(%rbp)
leaq 0x48c4(%rip), %rcx      # 0x8240
leaq -0xbd0(%rbp), %rdi
xorl %esi, %esi
movl $0x4651, %edx           # imm = 0x4651
callq 0x26a0
jmp 0x3991
leaq -0xbd0(%rbp), %rdi
leaq -0x708(%rbp), %rsi
movl $0x8, %edx
callq 0x2900
jmp 0x39ab
leaq 0x488e(%rip), %rcx      # 0x8240
leaq -0x1098(%rbp), %rdi
xorl %esi, %esi
movl $0x4653, %edx           # imm = 0x4653
callq 0x26a0
jmp 0x39c7
leaq -0x1098(%rbp), %rdi
leaq -0x708(%rbp), %rsi
movl $0x8, %edx
callq 0x2900
jmp 0x39e1
movq 0x45e8(%rip), %rdi      # 0x7fd0
leaq 0x190f(%rip), %rsi      # 0x52fe
callq 0x21b0
movq %rax, -0x1ad0(%rbp)
jmp 0x39fd
leaq 0x4810(%rip), %rdi      # 0x8214
callq 0x3f90
movl %eax, -0x1ad4(%rbp)
jmp 0x3a11
movl -0x1ad4(%rbp), %esi
movq -0x1ad0(%rbp), %rdi
callq 0x2340
movq %rax, -0x1ae0(%rbp)
jmp 0x3a2c
movq -0x1ae0(%rbp), %rdi
movq 0x4586(%rip), %rsi      # 0x7fc0
callq 0x21c0
jmp 0x3a41
movq 0x4588(%rip), %rdi      # 0x7fd0
leaq 0x18b7(%rip), %rsi      # 0x5306
callq 0x21b0
movq %rax, -0x1ae8(%rbp)
jmp 0x3a5d
leaq 0x47b0(%rip), %rdi      # 0x8214
callq 0x3fb0
movl %eax, -0x1aec(%rbp)
jmp 0x3a71
movl -0x1aec(%rbp), %esi
movq -0x1ae8(%rbp), %rdi
callq 0x2340
movq %rax, -0x1af8(%rbp)
jmp 0x3a8c
movq -0x1af8(%rbp), %rdi
movq 0x4526(%rip), %rsi      # 0x7fc0
callq 0x21c0
jmp 0x3aa1
jmp 0x3aa3
jmp 0x3aa5
leaq -0x6f0(%rbp), %rdi
leaq -0x1100(%rbp), %rsi
callq 0x2970
jmp 0x3aba
movq 0x450f(%rip), %rdi      # 0x7fd0
leaq 0x1836(%rip), %rsi      # 0x52fe
callq 0x21b0
movq %rax, -0x1b00(%rbp)
jmp 0x3ad6
leaq 0x4737(%rip), %rdi      # 0x8214
callq 0x3f90
movl %eax, -0x1b04(%rbp)
jmp 0x3aea
movl -0x1b04(%rbp), %esi
movq -0x1b00(%rbp), %rdi
callq 0x2340
movq %rax, -0x1b10(%rbp)
jmp 0x3b05
movq -0x1b10(%rbp), %rdi
movq 0x44ad(%rip), %rsi      # 0x7fc0
callq 0x21c0
jmp 0x3b1a
movq 0x44af(%rip), %rdi      # 0x7fd0
leaq 0x17de(%rip), %rsi      # 0x5306
callq 0x21b0
movq %rax, -0x1b18(%rbp)
jmp 0x3b36
leaq 0x46d7(%rip), %rdi      # 0x8214
callq 0x3fb0
movl %eax, -0x1b1c(%rbp)
jmp 0x3b4a
movl -0x1b1c(%rbp), %esi
movq -0x1b18(%rbp), %rdi
callq 0x2340
movq %rax, -0x1b28(%rbp)
jmp 0x3b65
movq -0x1b28(%rbp), %rdi
movq 0x444d(%rip), %rsi      # 0x7fc0
callq 0x21c0
jmp 0x3b7a
testb $0x1, 0x4723(%rip)      # 0x82a4
je 0x3bb6
movq 0x4446(%rip), %rdi      # 0x7fd0
leaq 0x177c(%rip), %rsi      # 0x530d
callq 0x21b0
movq %rax, -0x1b30(%rbp)
jmp 0x3b9f
movq -0x1b30(%rbp), %rdi
movq 0x4413(%rip), %rsi      # 0x7fc0
callq 0x21c0
jmp 0x3bb4
jmp 0x3bb6
testb $0x1, 0x46e8(%rip)      # 0x82a5
je 0x3bf2
movq 0x440a(%rip), %rdi      # 0x7fd0
leaq 0x174c(%rip), %rsi      # 0x5319
callq 0x21b0
movq %rax, -0x1b38(%rbp)
jmp 0x3bdb
movq -0x1b38(%rbp), %rdi
movq 0x43d7(%rip), %rsi      # 0x7fc0
callq 0x21c0
jmp 0x3bf0
jmp 0x3bf2
movsbl -0x1100(%rbp), %eax
cmpl $0x43, %eax
jne 0x3c15
leaq 0x460f(%rip), %rdi      # 0x8214
callq 0x3f10
jmp 0x3c0c
movb $0x1, 0x4625(%rip)      # 0x8238
jmp 0x3c79
leaq 0x45f8(%rip), %rdi      # 0x8214
callq 0x3f90
movl %eax, -0x1b3c(%rbp)
jmp 0x3c29
movl -0x1b3c(%rbp), %eax
cmpl $0x0, %eax
jne 0x3c67
movq 0x4395(%rip), %rdi      # 0x7fd0
leaq 0x16e2(%rip), %rsi      # 0x5324
callq 0x21b0
movq %rax, -0x1b48(%rbp)
jmp 0x3c50
movq -0x1b48(%rbp), %rdi
movq 0x4362(%rip), %rsi      # 0x7fc0
callq 0x21c0
jmp 0x3c65
jmp 0x3c77
leaq 0x45a6(%rip), %rdi      # 0x8214
callq 0x3f30
jmp 0x3c75
jmp 0x3c77
jmp 0x3c79
movsbl -0x1100(%rbp), %eax
cmpl $0x50, %eax
jne 0x3c9c
leaq 0x4588(%rip), %rdi      # 0x8214
callq 0x3f50
jmp 0x3c93
movb $0x1, 0x459e(%rip)      # 0x8238
jmp 0x3d00
leaq 0x4571(%rip), %rdi      # 0x8214
callq 0x3fb0
movl %eax, -0x1b4c(%rbp)
jmp 0x3cb0
movl -0x1b4c(%rbp), %eax
cmpl $0x0, %eax
jne 0x3cee
movq 0x430e(%rip), %rdi      # 0x7fd0
leaq 0x1662(%rip), %rsi      # 0x532b
callq 0x21b0
movq %rax, -0x1b58(%rbp)
jmp 0x3cd7
movq -0x1b58(%rbp), %rdi
movq 0x42db(%rip), %rsi      # 0x7fc0
callq 0x21c0
jmp 0x3cec
jmp 0x3cfe
leaq 0x451f(%rip), %rdi      # 0x8214
callq 0x3f70
jmp 0x3cfc
jmp 0x3cfe
jmp 0x3d00
leaq 0x450d(%rip), %rdi      # 0x8214
callq 0x4040
movb %al, -0x1b59(%rbp)
jmp 0x3d14
movb -0x1b59(%rbp), %al
testb $0x1, %al
jne 0x3e7b
jmp 0x3d24
leaq 0x44e9(%rip), %rdi      # 0x8214
callq 0x4010
movl %eax, -0x1b60(%rbp)
jmp 0x3d38
movl -0x1b60(%rbp), %eax
cmpl $0x0, %eax
je 0x3d79
movq 0x4286(%rip), %rdi      # 0x7fd0
leaq 0x15e2(%rip), %rsi      # 0x5333
callq 0x21b0
movq %rax, -0x1b68(%rbp)
jmp 0x3d5f
movq -0x1b68(%rbp), %rdi
movq 0x4253(%rip), %rsi      # 0x7fc0
callq 0x21c0
jmp 0x3d74
jmp 0x3e79
movq 0x4250(%rip), %rdi      # 0x7fd0
leaq 0x15c8(%rip), %rsi      # 0x534f
callq 0x21b0
movq %rax, -0x1b70(%rbp)
jmp 0x3d95
movq -0x1b70(%rbp), %rdi
movq 0x421d(%rip), %rsi      # 0x7fc0
callq 0x21c0
jmp 0x3daa
movq 0x421f(%rip), %rdi      # 0x7fd0
leaq 0x15ac(%rip), %rsi      # 0x5364
callq 0x21b0
movq %rax, -0x1b78(%rbp)
jmp 0x3dc6
movq -0x1b78(%rbp), %rdi
movq 0x41ec(%rip), %rsi      # 0x7fc0
callq 0x21c0
jmp 0x3ddb
leaq 0x4432(%rip), %rdi      # 0x8214
callq 0x4060
jmp 0x3de9
leaq 0x4424(%rip), %rdi      # 0x8214
callq 0x4040
movb %al, -0x1b79(%rbp)
jmp 0x3dfd
movb -0x1b79(%rbp), %al
andb $0x1, %al
movb %al, -0x704(%rbp)
leaq 0x442e(%rip), %rcx      # 0x8240
leaq -0x15c8(%rbp), %rdi
xorl %esi, %esi
movl $0x4651, %edx           # imm = 0x4651
callq 0x26a0
jmp 0x3e27
leaq -0x15c8(%rbp), %rdi
leaq -0x708(%rbp), %rsi
movl $0x8, %edx
callq 0x2900
jmp 0x3e41
leaq 0x43f8(%rip), %rcx      # 0x8240
leaq -0x1a90(%rbp), %rdi
xorl %esi, %esi
movl $0x4653, %edx           # imm = 0x4653
callq 0x26a0
jmp 0x3e5d
leaq -0x1a90(%rbp), %rdi
leaq -0x708(%rbp), %rsi
movl $0x8, %edx
callq 0x2900
jmp 0x3e77
jmp 0x3e79
jmp 0x3eae
movq 0x414e(%rip), %rdi      # 0x7fd0
leaq 0x14ee(%rip), %rsi      # 0x5377
callq 0x21b0
movq %rax, -0x1b88(%rbp)
jmp 0x3e97
movq -0x1b88(%rbp), %rdi
movq 0x411b(%rip), %rsi      # 0x7fc0
callq 0x21c0
jmp 0x3eac
jmp 0x3eae
jmp 0x3aa3
movq -0x220(%rbp), %rdi
callq 0x2360
nop