#include "prog.h"

void TraiterMsg (long type, int id)
{
 SMsg1 Msg1;
 SMsg2 Msg2;
 SMsg3 Msg3;
 switch (type)
 {
 case 1l:
   msgrcv(id, &Msg1, sizeof(SMsg1)-sizeof(long), 1l, 0);
   while(strcmp(Msg1.Nom, "")!=0 || strcmp (Msg1.Prenom, "") !=0){
     printf("Type 1 extrait : %s %s \n", Msg1.Nom, Msg1.Prenom);
     msgrcv(id, &Msg1, sizeof(SMsg1)-sizeof(long), 1l, 0);
   }
 break;
 case 2l:
   msgrcv(id, &Msg2, sizeof(SMsg2)-sizeof(long), 2l, 0);
   while(strcmp(Msg2.Nom, "")!=0 || strcmp (Msg2.Prenom, "") !=0){
     printf("Type 2 extrait: %s %s %s\n", Msg2.Nom, Msg2.Prenom,Msg2.Age);
     msgrcv(id, &Msg2, sizeof(SMsg2)-sizeof(long), 2l, 0);
   }
 break;
 case 3l:
   msgrcv(id, &Msg3, sizeof(SMsg3)-sizeof(long), 3l, 0);
   while(strcmp(Msg3.Nom, "")!=0 || strcmp (Msg3.Prenom, "") !=0){
     printf("Type 3 extrait: %s %s %s %s\n", Msg3.Nom, Msg3.Prenom,Msg3.Age,Msg3.Adresse);
     msgrcv(id, &Msg3, sizeof(SMsg3)-sizeof(long), 3l, 0);
   }
 break;
 }
}


void PosterMsg(int id)
{
 int type;
 SMsg1 Msg1 = {1l, "", ""};
 SMsg2 Msg2 = {2l, "", "",""};
 SMsg3 Msg3 = {3l, "", "","",""};
 int M1 = 1, M2 = 1, M3 = 1;
 while (M1 != 0 || M2 != 0 || M3 != 0)
   {
     printf("M1= %d M2= %d M3= %d\n", M1, M2, M3);
     printf("donnez le type de message(1,2,3) \n");
     fflush(stdin);
     scanf("%d", &type);
     switch(type)
     {
       case 1:
         printf("Message de type Msg1\n");
         printf("---------------------\n");
         printf("Nom: \n");
         fflush(stdin);
         gets(Msg1.Nom);
         gets(Msg1.Nom);
         printf("Prenom: \n");
         fflush(stdin);
         gets(Msg1.Prenom);
         msgsnd(id, &Msg1, sizeof(SMsg1) - sizeof(long), 0);
         if (strcmp(Msg1.Nom, "") == 0 && strcmp(Msg1.Prenom, "") == 0)
          M1 = 0;
       break;
       case 2:
         printf("Message de type Msg2\n");
         printf("---------------------\n");
         printf("Nom: \n");
         fflush(stdin);
         gets(Msg2.Nom);
         gets(Msg2.Nom);
         printf("Prenom: \n");
         fflush(stdin);
         gets(Msg2.Prenom);
         printf("Age: \n");
         fflush(stdin);
         gets(Msg2.Age);
         msgsnd(id, &Msg2, sizeof(SMsg2) - sizeof(long), 0);
         if (strcmp(Msg2.Nom, "") == 0 && strcmp(Msg2.Prenom, "") == 0)
          M2 = 0;
       break;
       case 3:
         printf("Message de type Msg3\n");
         printf("---------------------\n");
         printf("Nom: \n");
         fflush(stdin);
         gets(Msg3.Nom);
         gets(Msg3.Nom);
         fflush(stdin);
         printf("Prenom: \n");
         fflush(stdin);
         gets(Msg3.Prenom);
         printf("Age: \n");
         fflush(stdin);
         gets(Msg3.Age);
         printf("Adresse: \n");
         fflush(stdin);
         gets(Msg3.Adresse);
         msgsnd(id, &Msg3, sizeof(SMsg3) - sizeof(long), 0);
         if (strcmp(Msg3.Nom, "") == 0 && strcmp(Msg3.Prenom, "") == 0)
          M3 = 0;
       break;
    }
  }
}


void main(){
  key_t cle;
  int IdMsg;
  pid_t pid;
  int statut;
  cle=ftok("msg.c",0);
  IdMsg=msgget(cle,IPC_CREAT|IPC_EXCL| S_IRUSR|S_IWUSR);
  if(IdMsg==-1) erreur("Erreur msgget");
    printf("cle %d Id %d\n",cle,IdMsg);
  pid = fork();
  if (pid == 0){
    TraiterMsg(1l,IdMsg);
    printf("Fils 1 termine\n");
    exit(0);
  }
  pid = fork();
  if (pid == 0){
    TraiterMsg(2l,IdMsg);
    printf("Fils 2 termine\n");
    exit(0);
  }
  pid = fork();
  if (pid == 0){
    TraiterMsg(3l,IdMsg);
    printf("Fils 3 termine\n");
    exit(0);
  }
  PosterMsg(IdMsg);
  wait(&statut);
  wait(&statut);
  wait(&statut);
  // appel pour liberer la file de message
  msgctl(IdMsg, IPC_RMID, NULL);
}

void erreur(const char * message){
  perror(message);
  exit(-1);
}
