// File: /workspace/github_cpp_makefile_star2_O3/CDISC2020[P]Projet_satellite_agile/src/planManager/main_PM.cpp
// Function: main
int  main (int argc,char* argv[])
{
	// Ecriture du pid dans un fichier
	int pid=getpid();
	ofstream fichierPid;
	fichierPid.open("pid_PM");
	if(!fichierPid) {
		cout << "Failed to open pid_PM" << endl;
		exit(1);
	}
	fichierPid << pid << endl;
	fichierPid.close();

	// Recuperation du hostname
	if (gethostname(s, 100) != 0){
	    perror("S-> gethostname");
	    exit(1);
	}
	cout << "Host name " << s << endl;

	signal(SIGINT, (sig_t)bye);

	channelFDIR = new QueuingPort(0, 18002, s);		// Client FDIR
	channelSM = new QueuingPort(0, 18003, s); 		// Client COM
	channelReceptionPM = new QueuingPort(1, 18001, s); 	// Server

	usleep(500000);

	channelFDIR->Display();
	channelSM->Display();
	channelReceptionPM->Display();

	// generate thread
	pthread_attr_t *thread_attributes;
	pthread_t *thread;

	/* creation du thread Server1 */
	tid_FD = 1;

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

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

	/* creation du thread Client1 */
	tid_FP = 2;

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

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

	/* creation du thread Client2 */
	tid_A = 3;

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

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

	while(1) {usleep(100);}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x200, %rsp            # imm = 0x200
callq 0x33a0
movl %eax, %ebx
leaq -0x210(%rbp), %r14
movq %r14, %rdi
callq 0x3450
leaq 0x1331(%rip), %rsi      # 0x7785
movq %r14, %rdi
movl $0x10, %edx
callq 0x3060
movq -0x210(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x1f0(%rbp,%rax)
jne 0x66ed
leaq -0x210(%rbp), %rdi
movl %ebx, %esi
callq 0x3460
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3290
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
leaq -0x210(%rbp), %rdi
callq 0x3380
leaq 0x3e8b(%rip), %rdi      # 0xa350
movl $0x64, %esi
callq 0x33d0
testl %eax, %eax
jne 0x673d
movq 0x3af2(%rip), %rdi      # 0x9fd0
leaq 0x12b7(%rip), %rsi      # 0x779c
movl $0xa, %edx
callq 0x3300
leaq 0x3e5a(%rip), %rbx      # 0xa350
movq %rbx, %rdi
callq 0x3130
movq 0x3acb(%rip), %rdi      # 0x9fd0
movq %rbx, %rsi
movq %rax, %rdx
callq 0x3300
movq 0x3ab9(%rip), %rdi      # 0x9fd0
callq 0x3150
leaq -0x120(%rip), %rsi      # 0x6403
movl $0x2, %edi
callq 0x3310
movl $0x4c8, %edi            # imm = 0x4C8
callq 0x32b0
movq %rax, %r14
leaq 0x3e0f(%rip), %rcx      # 0xa350
movq %rax, %rdi
xorl %esi, %esi
movl $0x4652, %edx           # imm = 0x4652
callq 0x3ac6
movq %r14, 0x3e61(%rip)      # 0xa3b8
movl $0x4c8, %edi            # imm = 0x4C8
callq 0x32b0
movq %rax, %r14
leaq 0x3de5(%rip), %rcx      # 0xa350
movq %rax, %rdi
xorl %esi, %esi
movl $0x4653, %edx           # imm = 0x4653
callq 0x3ac6
movq %r14, 0x3e3f(%rip)      # 0xa3c0
movl $0x4c8, %edi            # imm = 0x4C8
callq 0x32b0
movq %rax, %r14
leaq 0x3dbb(%rip), %rcx      # 0xa350
movq %rax, %rdi
movl $0x1, %esi
movl $0x4651, %edx           # imm = 0x4651
callq 0x3ac6
movq %r14, 0x3e1a(%rip)      # 0xa3c8
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x3370
movq 0x3df9(%rip), %rdi      # 0xa3b8
callq 0x3fe8
movq 0x3df5(%rip), %rdi      # 0xa3c0
callq 0x3fe8
movq 0x3df1(%rip), %rdi      # 0xa3c8
callq 0x3fe8
movl $0x1, 0x3cbe(%rip)      # 0xa2a4
movl $0x38, %edi
callq 0x3350
movq %rax, %rbx
movl $0x8, %edi
callq 0x3350
movq %rax, %r14
movq %rax, 0x3ca9(%rip)      # 0xa2b0
movq %rbx, %rdi
callq 0x3330
leaq -0x4fa(%rip), %rdx      # 0x611c
movq %r14, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x34b0
testl %eax, %eax
je 0x6633
leaq 0x1179(%rip), %rdi      # 0x77a7
callq 0x3210
movl $0x2, 0x3c6b(%rip)      # 0xa2a8
movl $0x38, %edi
callq 0x3350
movq %rax, %rbx
movl $0x8, %edi
callq 0x3350
movq %rax, %r14
movq %rax, 0x3c5a(%rip)      # 0xa2b8
movq %rbx, %rdi
callq 0x3330
leaq -0x2af(%rip), %rdx      # 0x63be
movq %r14, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x34b0
testl %eax, %eax
je 0x668a
leaq 0x114e(%rip), %rdi      # 0x77d3
callq 0x3210
movl $0x3, 0x3c18(%rip)      # 0xa2ac
movl $0x38, %edi
callq 0x3350
movq %rax, %rbx
movl $0x8, %edi
callq 0x3350
movq %rax, %r14
movq %rax, 0x3c03(%rip)      # 0xa2b8
movq %rbx, %rdi
callq 0x3330
leaq -0x2f1(%rip), %rdx      # 0x63d3
movq %r14, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x34b0
testl %eax, %eax
je 0x66e1
leaq 0x1123(%rip), %rdi      # 0x77ff
callq 0x3210
movl $0x64, %edi
callq 0x3370
jmp 0x66e1
movq 0x38dc(%rip), %rbx      # 0x9fd0
leaq 0x107b(%rip), %rsi      # 0x7776
movl $0x15, %edx
movq %rbx, %rdi
callq 0x3300
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3290
movsbl %al, %esi
movq 0x38aa(%rip), %rdi      # 0x9fd0
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movl $0x1, %edi
callq 0x3320
leaq 0x1048(%rip), %rdi      # 0x778c
callq 0x3210
jmp 0x6733
jmp 0x675e
jmp 0x6751
jmp 0x6751
movq %rax, %rbx
movq %r14, %rdi
callq 0x3270
jmp 0x6761
movq %rax, %rbx
movq 0x3860(%rip), %rax      # 0x9fc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x208(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x210(%rbp,%rcx)
callq 0x3220
leaq -0x118(%rbp), %rdi
callq 0x30f0
movq %rbx, %rdi
callq 0x3490
nop