// File: CDISC2020[P]Projet_satellite_agile/src/communication/comGroundManager.cpp
// Function: main
int main (int argc, char* argv[])
{
	// Ecriture du pid dans un fichier
	int pid=getpid();
	ofstream fichierPid;
	fichierPid.open("pid_COM");
	if(!fichierPid)	{
		cout << "Failed to open pid_COM" << 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;

	// Init des tableaux
	for(int i=0; i<1024; i++) buffer[i]='\0';
	for(int i=0; i<50; i++) cmde[i]=' ';

	// Signal
	signal(SIGINT, (sig_t)bye);


	/* CREATION DES THREADS	*/
	pthread_attr_t *thread_attributes;
	pthread_t *thread;

/////////* creation du thread processus communication au sol */
	tid_sol = 1;

	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, &Communic_Sol, (void*) NULL) != 0)
		perror ("Thread_Server-> Connection to ground error!");

/////////* creation du thread processus communication interne */
	tid_interne=2;

	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, &Communic_Interne, (void *) NULL) != 0)
		perror ("Thread_Server-> Internal connection error!");

/////////* creation du thread processus I'm alive */
	tid_alive=3;

	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, &am_alive, (void *)  NULL) != 0)
		perror ("Thread_Server-> I am alive error!");

	while (1) {usleep(100);}  /* DOES NOTHING */ ;

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x270, %rsp            # imm = 0x270
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
callq 0x23b0
movl %eax, -0x14(%rbp)
leaq -0x218(%rbp), %rdi
movq %rdi, -0x248(%rbp)
callq 0x2440
movq -0x248(%rbp), %rdi
leaq 0x1a65(%rip), %rsi      # 0x6407
movl $0x10, %edx
callq 0x2060
jmp 0x49ae
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x2160
movb %al, -0x249(%rbp)
jmp 0x49ce
movb -0x249(%rbp), %al
testb $0x1, %al
jne 0x49da
jmp 0x4a38
movq 0x45f7(%rip), %rdi      # 0x8fd8
leaq 0x1a10(%rip), %rsi      # 0x63f8
callq 0x2290
movq %rax, -0x258(%rbp)
jmp 0x49f6
movq -0x258(%rbp), %rdi
movq 0x45c4(%rip), %rsi      # 0x8fc8
callq 0x22b0
jmp 0x4a0b
movl $0x1, %edi
callq 0x2320
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x23c0
jmp 0x4cc1
movl -0x14(%rbp), %esi
leaq -0x218(%rbp), %rdi
callq 0x2460
movq %rax, -0x260(%rbp)
jmp 0x4a50
movq -0x260(%rbp), %rdi
movq 0x456a(%rip), %rsi      # 0x8fc8
callq 0x22b0
jmp 0x4a65
leaq -0x218(%rbp), %rdi
callq 0x23a0
jmp 0x4a73
leaq 0x5886(%rip), %rdi      # 0xa300
movl $0x64, %esi
callq 0x23d0
cmpl $0x0, %eax
je 0x4aa1
leaq 0x197f(%rip), %rdi      # 0x640f
callq 0x2200
jmp 0x4a97
movl $0x1, %edi
callq 0x2320
movq 0x4530(%rip), %rdi      # 0x8fd8
leaq 0x1970(%rip), %rsi      # 0x641f
callq 0x2290
movq %rax, -0x268(%rbp)
jmp 0x4abd
movq -0x268(%rbp), %rdi
leaq 0x5835(%rip), %rsi      # 0xa300
callq 0x2290
movq %rax, -0x270(%rbp)
jmp 0x4ad9
movq -0x270(%rbp), %rdi
movq 0x44e1(%rip), %rsi      # 0x8fc8
callq 0x22b0
jmp 0x4aee
movl $0x0, -0x228(%rbp)
cmpl $0x400, -0x228(%rbp)    # imm = 0x400
jge 0x4b27
movslq -0x228(%rbp), %rcx
leaq 0x585e(%rip), %rax      # 0xa370
movb $0x0, (%rax,%rcx)
movl -0x228(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x228(%rbp)
jmp 0x4af8
movl $0x0, -0x22c(%rbp)
cmpl $0x32, -0x22c(%rbp)
jge 0x4b5d
movslq -0x22c(%rbp), %rcx
leaq 0x5c28(%rip), %rax      # 0xa770
movb $0x20, (%rax,%rcx)
movl -0x22c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x22c(%rbp)
jmp 0x4b31
movl $0x2, %edi
leaq -0x229(%rip), %rsi      # 0x4940
callq 0x2310
movl $0x1, 0x472c(%rip)      # 0x92a4
movl $0x38, %edi
callq 0x2350
movq %rax, -0x238(%rbp)
movl $0x8, %edi
callq 0x2350
movq %rax, -0x240(%rbp)
movq -0x238(%rbp), %rdi
callq 0x2330
movq -0x240(%rbp), %rdi
movq -0x238(%rbp), %rsi
leaq -0x135b(%rip), %rdx     # 0x3860
xorl %eax, %eax
movl %eax, %ecx
callq 0x24b0
cmpl $0x0, %eax
je 0x4bd9
leaq 0x185a(%rip), %rdi      # 0x642a
callq 0x2200
jmp 0x4bd7
jmp 0x4bd9
movl $0x2, 0x46c5(%rip)      # 0x92a8
movl $0x38, %edi
callq 0x2350
movq %rax, -0x238(%rbp)
movl $0x8, %edi
callq 0x2350
movq %rax, -0x240(%rbp)
movq -0x238(%rbp), %rdi
callq 0x2330
movq -0x240(%rbp), %rdi
movq -0x238(%rbp), %rsi
leaq -0x646(%rip), %rdx      # 0x45e0
xorl %eax, %eax
movl %eax, %ecx
callq 0x24b0
cmpl $0x0, %eax
je 0x4c44
leaq 0x181b(%rip), %rdi      # 0x6456
callq 0x2200
jmp 0x4c42
jmp 0x4c44
movl $0x3, 0x465e(%rip)      # 0x92ac
movl $0x38, %edi
callq 0x2350
movq %rax, -0x238(%rbp)
movl $0x8, %edi
callq 0x2350
movq %rax, -0x240(%rbp)
movq -0x238(%rbp), %rdi
callq 0x2330
movq -0x240(%rbp), %rdi
movq -0x238(%rbp), %rsi
leaq -0x3e1(%rip), %rdx      # 0x48b0
xorl %eax, %eax
movl %eax, %ecx
callq 0x24b0
cmpl $0x0, %eax
je 0x4caf
leaq 0x17db(%rip), %rdi      # 0x6481
callq 0x2200
jmp 0x4cad
jmp 0x4caf
jmp 0x4cb1
jmp 0x4cb3
movl $0x64, %edi
callq 0x2390
jmp 0x4cbf
jmp 0x4cb1
movq -0x220(%rbp), %rdi
callq 0x2480
nopl (%rax)