// File: /workspace/github_cpp_makefile_star2_O1/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      # 0x353f
movl $0xa, %edi
callq 0x2240
callq 0x22d0
movl %eax, %ebx
leaq -0x2a0(%rbp), %r14
movq %r14, %rdi
callq 0x2370
leaq 0x199b(%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 0x3cde
leaq -0x2a0(%rbp), %rdi
movl %ebx, %esi
callq 0x2380
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21f0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2140
leaq -0x2a0(%rbp), %rdi
callq 0x22b0
leaq 0x4995(%rip), %rdi      # 0x8260
movl $0x64, %esi
callq 0x22f0
testl %eax, %eax
jne 0x3d2e
movq 0x46ec(%rip), %rdi      # 0x7fd0
leaq 0x19d7(%rip), %rsi      # 0x52c2
movl $0xa, %edx
callq 0x2230
leaq 0x4964(%rip), %rbx      # 0x8260
movq %rbx, %rdi
callq 0x20f0
movq 0x46c5(%rip), %rdi      # 0x7fd0
movq %rbx, %rsi
movq %rax, %rdx
callq 0x2230
movq 0x46b3(%rip), %rdi      # 0x7fd0
callq 0x2110
leaq 0x4937(%rip), %rcx      # 0x8260
leaq -0x768(%rbp), %rdi
movl $0x1, %esi
movl $0x4652, %edx           # imm = 0x4652
callq 0x28a6
leaq -0x768(%rbp), %rdi
callq 0x2de8
movl $0x38, %edi
callq 0x2270
movq %rax, %rbx
movl $0x8, %edi
callq 0x2270
movq %rax, %r14
movq %rbx, %rdi
callq 0x2260
leaq -0x858(%rip), %rdx      # 0x311c
movq %r14, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x23b0
testl %eax, %eax
je 0x3991
leaq 0x1941(%rip), %rdi      # 0x52cd
callq 0x2180
movl $0x2, %edi
callq 0x2160
movl $0x6, -0x30(%rbp)
leaq 0x488b(%rip), %rdi      # 0x8234
callq 0x3e46
movb %al, -0x2c(%rbp)
leaq 0x48a8(%rip), %rcx      # 0x8260
leaq -0x1a88(%rbp), %rdi
xorl %esi, %esi
movl $0x4651, %edx           # imm = 0x4651
callq 0x28a6
leaq -0x1a88(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0x8, %edx
callq 0x2b66
leaq 0x4879(%rip), %rcx      # 0x8260
leaq -0x15c0(%rbp), %rdi
xorl %esi, %esi
movl $0x4653, %edx           # imm = 0x4653
callq 0x28a6
leaq -0x15c0(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0x8, %edx
callq 0x2b66
movq 0x45ba(%rip), %rdi      # 0x7fd0
leaq 0x18dc(%rip), %rsi      # 0x52f9
movl $0x7, %edx
callq 0x2230
leaq 0x4806(%rip), %rdi      # 0x8234
callq 0x3df6
movq 0x4596(%rip), %rdi      # 0x7fd0
movl %eax, %esi
callq 0x2380
movq %rax, %rdi
callq 0x2110
movq 0x4580(%rip), %rdi      # 0x7fd0
leaq 0x18aa(%rip), %rsi      # 0x5301
movl $0x6, %edx
callq 0x2230
leaq 0x47cc(%rip), %rdi      # 0x8234
callq 0x3e04
movq 0x455c(%rip), %rdi      # 0x7fd0
movl %eax, %esi
callq 0x2380
movq %rax, %rdi
callq 0x2110
movq 0x4546(%rip), %r15      # 0x7fd0
leaq 0x1868(%rip), %r12      # 0x52f9
leaq 0x479c(%rip), %r13      # 0x8234
leaq 0x1862(%rip), %rbx      # 0x5301
leaq 0x18cc(%rip), %r14      # 0x5372
leaq -0x768(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x2ba8
movl $0x7, %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2230
movq %r13, %rdi
callq 0x3df6
movq %r15, %rdi
movl %eax, %esi
callq 0x2380
movq %rax, %rdi
callq 0x2110
movl $0x6, %edx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2230
movq %r13, %rdi
callq 0x3e04
movq %r15, %rdi
movl %eax, %esi
callq 0x2380
movq %rax, %rdi
callq 0x2110
cmpb $0x1, 0x47b0(%rip)      # 0x82c4
jne 0x3b32
movl $0xb, %edx
movq %r15, %rdi
leaq 0x17e3(%rip), %rsi      # 0x5308
callq 0x2230
movq %r15, %rdi
callq 0x2110
cmpb $0x1, 0x478c(%rip)      # 0x82c5
jne 0x3b57
movl $0xa, %edx
movq %r15, %rdi
leaq 0x17ca(%rip), %rsi      # 0x5314
callq 0x2230
movq %r15, %rdi
callq 0x2110
cmpb $0x43, -0xa0(%rbp)
jne 0x3b71
movq %r13, %rdi
callq 0x3dbe
movb $0x1, 0x46e9(%rip)      # 0x8258
jmp 0x3ba3
movq %r13, %rdi
callq 0x3df6
testl %eax, %eax
je 0x3b87
movq %r13, %rdi
callq 0x3dcc
jmp 0x3ba3
movl $0x6, %edx
movq %r15, %rdi
leaq 0x1789(%rip), %rsi      # 0x531f
callq 0x2230
movq %r15, %rdi
callq 0x2110
cmpb $0x50, -0xa0(%rbp)
jne 0x3bbd
movq %r13, %rdi
callq 0x3dda
movb $0x1, 0x469d(%rip)      # 0x8258
jmp 0x3bef
movq %r13, %rdi
callq 0x3e04
testl %eax, %eax
je 0x3bd3
movq %r13, %rdi
callq 0x3de8
jmp 0x3bef
movl $0x7, %edx
movq %r15, %rdi
leaq 0x1744(%rip), %rsi      # 0x5326
callq 0x2230
movq %r15, %rdi
callq 0x2110
movq %r13, %rdi
callq 0x3e46
movl $0x1a, %edx
movq %r14, %rsi
testb %al, %al
jne 0x3c1b
movq %r13, %rdi
callq 0x3e2e
movl $0x1b, %edx
leaq 0x1717(%rip), %rsi      # 0x532e
testl %eax, %eax
je 0x3c30
movq %r15, %rdi
callq 0x2230
movq %r15, %rdi
callq 0x2110
jmp 0x3aa6
movl $0x14, %edx
movq %r15, %rdi
leaq 0x170b(%rip), %rsi      # 0x534a
callq 0x2230
movq %r15, %rdi
callq 0x2110
movl $0x12, %edx
movq %r15, %rdi
leaq 0x1704(%rip), %rsi      # 0x535f
callq 0x2230
movq %r15, %rdi
callq 0x2110
movq %r13, %rdi
callq 0x3e4e
movq %r13, %rdi
callq 0x3e46
movb %al, -0x2c(%rbp)
leaq -0x10f8(%rbp), %rdi
xorl %esi, %esi
movl $0x4651, %edx           # imm = 0x4651
leaq 0x45d0(%rip), %rcx      # 0x8260
callq 0x28a6
leaq -0x10f8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0x8, %edx
callq 0x2b66
leaq -0xc30(%rbp), %rdi
xorl %esi, %esi
movl $0x4653, %edx           # imm = 0x4653
leaq 0x45a1(%rip), %rcx      # 0x8260
callq 0x28a6
leaq -0xc30(%rbp), %rdi
leaq -0x30(%rbp), %rsi
movl $0x8, %edx
callq 0x2b66
jmp 0x3aa6
movq 0x42eb(%rip), %rbx      # 0x7fd0
leaq 0x14fa(%rip), %rsi      # 0x51e6
movl $0x17, %edx
movq %rbx, %rdi
callq 0x2230
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21f0
movsbl %al, %esi
movq 0x42b9(%rip), %rdi      # 0x7fd0
callq 0x2030
movq %rax, %rdi
callq 0x2140
movl $0x1, %edi
callq 0x2250
leaq 0x157d(%rip), %rdi      # 0x52b2
callq 0x2180
jmp 0x3d24
jmp 0x3d4e
jmp 0x3d4e
jmp 0x3d4e
jmp 0x3d4e
jmp 0x3d4e
jmp 0x3d4e
jmp 0x3d4e
jmp 0x3d4e
jmp 0x3d4e
movq %rax, %rbx
movq 0x4270(%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 0x2190
leaq -0x1a8(%rbp), %rdi
callq 0x20d0
movq %rbx, %rdi
callq 0x23a0
nop