// File: /workspace/github_cpp_makefile_star2_O2/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 0x2340
movl %eax, %ebx
leaq -0x210(%rbp), %r14
movq %r14, %rdi
callq 0x23f0
leaq 0x1137(%rip), %rsi      # 0x5755
pushq $0x10
popq %rdx
movq %r14, %rdi
callq 0x2050
movq -0x210(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x1f0(%rbp,%rax)
jne 0x4866
leaq -0x210(%rbp), %rdi
movl %ebx, %esi
callq 0x2400
movq %rax, %rdi
callq 0x2150
leaq -0x210(%rbp), %rdi
callq 0x2320
leaq 0x3ca5(%rip), %rdi      # 0x8310
pushq $0x64
popq %rsi
callq 0x2370
testl %eax, %eax
jne 0x4889
movq 0x394e(%rip), %rdi      # 0x7fd0
leaq 0x10e3(%rip), %rsi      # 0x576c
callq 0x2240
leaq 0x3c7b(%rip), %rsi      # 0x8310
movq %rax, %rdi
callq 0x2240
movq %rax, %rdi
callq 0x2150
leaq -0xdf(%rip), %rsi       # 0x45cd
pushq $0x2
popq %rdi
callq 0x2290
movl $0x4c8, %edi            # imm = 0x4C8
callq 0x2250
movq %rax, %r14
leaq 0x3c48(%rip), %rcx      # 0x8310
movq %rax, %rdi
xorl %esi, %esi
movl $0x4652, %edx           # imm = 0x4652
callq 0x27de
movq %r14, 0x3c9a(%rip)      # 0x8378
movl $0x4c8, %edi            # imm = 0x4C8
callq 0x2250
movq %rax, %r14
leaq 0x3c1e(%rip), %rcx      # 0x8310
movq %rax, %rdi
xorl %esi, %esi
movl $0x4653, %edx           # imm = 0x4653
callq 0x27de
movq %r14, 0x3c78(%rip)      # 0x8380
movl $0x4c8, %edi            # imm = 0x4C8
callq 0x2250
movq %rax, %r14
leaq 0x3bf4(%rip), %rcx      # 0x8310
pushq $0x1
popq %rsi
movq %rax, %rdi
movl $0x4651, %edx           # imm = 0x4651
callq 0x27de
movq %r14, 0x3c55(%rip)      # 0x8388
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x2310
movq 0x3c34(%rip), %rdi      # 0x8378
callq 0x2b42
movq 0x3c30(%rip), %rdi      # 0x8380
callq 0x2b42
movq 0x3c2c(%rip), %rdi      # 0x8388
callq 0x2b42
movl $0x1, 0x3af9(%rip)      # 0x8264
pushq $0x38
popq %rdi
callq 0x22e0
movq %rax, %rbx
pushq $0x8
popq %rdi
callq 0x22e0
movq %rax, %r14
movq %rax, 0x3ae8(%rip)      # 0x8270
movq %rbx, %rdi
callq 0x22b0
leaq -0x3d3(%rip), %rdx      # 0x43c4
movq %r14, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x2430
testl %eax, %eax
je 0x47b4
leaq 0xfc8(%rip), %rdi       # 0x5777
callq 0x21c0
movl $0x2, 0x3aaa(%rip)      # 0x8268
pushq $0x38
popq %rdi
callq 0x22e0
movq %rax, %rbx
pushq $0x8
popq %rdi
callq 0x22e0
movq %rax, %r14
movq %rax, 0x3a9d(%rip)      # 0x8278
movq %rbx, %rdi
callq 0x22b0
leaq -0x269(%rip), %rdx      # 0x4581
movq %r14, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x2430
testl %eax, %eax
je 0x4807
leaq 0xfa1(%rip), %rdi       # 0x57a3
callq 0x21c0
movl $0x3, 0x3a5b(%rip)      # 0x826c
pushq $0x38
popq %rdi
callq 0x22e0
movq %rax, %rbx
pushq $0x8
popq %rdi
callq 0x22e0
movq %rax, %r14
movq %rax, 0x3a4a(%rip)      # 0x8278
movq %rbx, %rdi
callq 0x22b0
leaq -0x2a7(%rip), %rdx      # 0x4596
movq %r14, %rdi
movq %rbx, %rsi
xorl %ecx, %ecx
callq 0x2430
testl %eax, %eax
je 0x485a
leaq 0xf7a(%rip), %rdi       # 0x57cf
callq 0x21c0
pushq $0x64
popq %rbx
movl %ebx, %edi
callq 0x2310
jmp 0x485d
movq 0x3763(%rip), %rdi      # 0x7fd0
leaq 0xed2(%rip), %rsi       # 0x5746
callq 0x2240
movq %rax, %rdi
callq 0x2150
pushq $0x1
popq %rdi
callq 0x22a0
leaq 0xecc(%rip), %rdi       # 0x575c
callq 0x21c0
jmp 0x4881
jmp 0x48aa
jmp 0x489d
jmp 0x489d
movq %rax, %rbx
movq %r14, %rdi
callq 0x2220
jmp 0x48ad
movq %rax, %rbx
leaq -0x210(%rbp), %rdi
callq 0x2360
movq %rbx, %rdi
callq 0x2420
nopl (%rax)