// File: 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
subq $0x280, %rsp            # imm = 0x280
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
callq 0x33d0
movl %eax, -0x14(%rbp)
leaq -0x218(%rbp), %rdi
movq %rdi, -0x240(%rbp)
callq 0x3480
movq -0x240(%rbp), %rdi
leaq 0x1d65(%rip), %rsi      # 0x8767
movl $0x10, %edx
callq 0x3060
jmp 0x6a0e
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x3150
movb %al, -0x241(%rbp)
jmp 0x6a2e
movb -0x241(%rbp), %al
testb $0x1, %al
jne 0x6a3a
jmp 0x6a8c
movq 0x458f(%rip), %rdi      # 0xafd0
leaq 0x1d10(%rip), %rsi      # 0x8758
callq 0x3280
movq %rax, -0x250(%rbp)
jmp 0x6a56
movq -0x250(%rbp), %rdi
movq 0x455c(%rip), %rsi      # 0xafc0
callq 0x32b0
jmp 0x6a6b
movl $0x1, %edi
callq 0x3310
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x6e29
movl -0x14(%rbp), %esi
leaq -0x218(%rbp), %rdi
callq 0x3490
movq %rax, -0x258(%rbp)
jmp 0x6aa4
movq -0x258(%rbp), %rdi
movq 0x450e(%rip), %rsi      # 0xafc0
callq 0x32b0
jmp 0x6ab9
leaq -0x218(%rbp), %rdi
callq 0x33a0
jmp 0x6ac7
leaq 0x4892(%rip), %rdi      # 0xb360
movl $0x64, %esi
callq 0x3410
cmpl $0x0, %eax
je 0x6af5
leaq 0x1c8a(%rip), %rdi      # 0x876e
callq 0x3200
jmp 0x6aeb
movl $0x1, %edi
callq 0x3310
movq 0x44d4(%rip), %rdi      # 0xafd0
leaq 0x1c7b(%rip), %rsi      # 0x877e
callq 0x3280
movq %rax, -0x260(%rbp)
jmp 0x6b11
movq -0x260(%rbp), %rdi
leaq 0x4841(%rip), %rsi      # 0xb360
callq 0x3280
movq %rax, -0x268(%rbp)
jmp 0x6b2d
movq -0x268(%rbp), %rdi
movq 0x4485(%rip), %rsi      # 0xafc0
callq 0x32b0
jmp 0x6b42
leaq -0x1b9(%rip), %rsi      # 0x6990
movl $0x2, %edi
callq 0x3300
movl $0x4c8, %edi            # imm = 0x4C8
callq 0x3290
movq %rax, -0x270(%rbp)
jmp 0x6b66
movq -0x270(%rbp), %rdi
leaq 0x47ec(%rip), %rcx      # 0xb360
xorl %esi, %esi
movl $0x4652, %edx           # imm = 0x4652
callq 0x39a0
jmp 0x6b82
movq -0x270(%rbp), %rax
movq %rax, 0x4838(%rip)      # 0xb3c8
movl $0x4c8, %edi            # imm = 0x4C8
callq 0x3290
movq %rax, -0x278(%rbp)
jmp 0x6ba3
movq -0x278(%rbp), %rdi
leaq 0x47af(%rip), %rcx      # 0xb360
xorl %esi, %esi
movl $0x4653, %edx           # imm = 0x4653
callq 0x39a0
jmp 0x6bbf
movq -0x278(%rbp), %rax
movq %rax, 0x4803(%rip)      # 0xb3d0
movl $0x4c8, %edi            # imm = 0x4C8
callq 0x3290
movq %rax, -0x280(%rbp)
jmp 0x6be0
movq -0x280(%rbp), %rdi
leaq 0x4772(%rip), %rcx      # 0xb360
movl $0x1, %esi
movl $0x4651, %edx           # imm = 0x4651
callq 0x39a0
jmp 0x6bff
movq -0x280(%rbp), %rax
movq %rax, 0x47cb(%rip)      # 0xb3d8
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x3390
jmp 0x6c19
movq 0x47a8(%rip), %rdi      # 0xb3c8
callq 0x3e00
jmp 0x6c27
movq 0x47a2(%rip), %rdi      # 0xb3d0
callq 0x3e00
jmp 0x6c35
movq 0x479c(%rip), %rdi      # 0xb3d8
callq 0x3e00
jmp 0x6c43
movl $0x1, 0x4667(%rip)      # 0xb2b4
movl $0x38, %edi
callq 0x3350
movq %rax, -0x230(%rbp)
movl $0x8, %edi
callq 0x3350
movq %rax, -0x238(%rbp)
movq -0x238(%rbp), %rax
movq %rax, 0x4643(%rip)      # 0xb2c0
movq -0x230(%rbp), %rdi
callq 0x3320
movq -0x238(%rbp), %rdi
movq -0x230(%rbp), %rsi
leaq -0x61e(%rip), %rdx      # 0x6680
xorl %eax, %eax
movl %eax, %ecx
callq 0x34d0
cmpl $0x0, %eax
je 0x6d25
leaq 0x1ad6(%rip), %rdi      # 0x8789
callq 0x3200
jmp 0x6cba
jmp 0x6d25
movq -0x270(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
callq 0x3260
jmp 0x6e29
movq -0x278(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
callq 0x3260
jmp 0x6e29
movq -0x280(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
callq 0x3260
jmp 0x6e29
movl $0x2, 0x4589(%rip)      # 0xb2b8
movl $0x38, %edi
callq 0x3350
movq %rax, -0x230(%rbp)
movl $0x8, %edi
callq 0x3350
movq %rax, -0x238(%rbp)
movq -0x238(%rbp), %rax
movq %rax, 0x4569(%rip)      # 0xb2c8
movq -0x230(%rbp), %rdi
callq 0x3320
movq -0x238(%rbp), %rdi
movq -0x230(%rbp), %rsi
leaq -0x460(%rip), %rdx      # 0x6920
xorl %eax, %eax
movl %eax, %ecx
callq 0x34d0
cmpl $0x0, %eax
je 0x6d9e
leaq 0x1a20(%rip), %rdi      # 0x87b5
callq 0x3200
jmp 0x6d9c
jmp 0x6d9e
movl $0x3, 0x4514(%rip)      # 0xb2bc
movl $0x38, %edi
callq 0x3350
movq %rax, -0x230(%rbp)
movl $0x8, %edi
callq 0x3350
movq %rax, -0x238(%rbp)
movq -0x238(%rbp), %rax
movq %rax, 0x44f0(%rip)      # 0xb2c8
movq -0x230(%rbp), %rdi
callq 0x3320
movq -0x238(%rbp), %rdi
movq -0x230(%rbp), %rsi
leaq -0x4a9(%rip), %rdx      # 0x6950
xorl %eax, %eax
movl %eax, %ecx
callq 0x34d0
cmpl $0x0, %eax
je 0x6e17
leaq 0x19d3(%rip), %rdi      # 0x87e1
callq 0x3200
jmp 0x6e15
jmp 0x6e17
jmp 0x6e19
jmp 0x6e1b
movl $0x64, %edi
callq 0x3390
jmp 0x6e27
jmp 0x6e19
leaq -0x218(%rbp), %rdi
callq 0x33f0
movq -0x220(%rbp), %rdi
callq 0x34b0
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)