/**
*
* @File : exo_02c.cxx
*
* @Authors : M. Laporte
*
* @Date : 06/11/2001
*
* @Version : V1.0
*
* @Synopsis : Consommateur de messages dans une file de message gestion
*               de priorités
*
**/

#include <iostream>   
#include <exception>
#include <string>
#include <cerrno>        // ENOMSG

#include <unistd.h>      // getuid()
#include <sys/ipc.h>     // IPC_NOWAIT

#include "CException.h"
#include "CExcFctSyst.h"
#include "nsSysteme.h"  
#include "CstCodErr.h"
#include "nsMsg.h"

using namespace nsUtil;      // CException
using namespace nsSysteme;   // Msgrcv(), Msgget()
using namespace nsMsg;

int std::ppal (int argc, char * argv []) throw (exception)
{
    if (1 != argc)
        throw CException (string ("Usage : ") + argv [0], CstExcArg);

    int MsqId = Msgget (::getuid ());
    SRep Reponse;

    for (;;)
    {
        /* ajout pour variante */
        bool Recu = false;
        /* fin ajout pour variante */

        for (long i = 1; i <= CstMaxNumber; ++i)
        {
            try
            {
                Msgrcv (MsqId, & Reponse, CstMaxTaille, i, IPC_NOWAIT);
                cout << "Message lu : " << Reponse.m_Message << endl;
                Recu = true;
                break;
            }
            catch (const CExcFctSystIPC & E)
            {
                if (E.GetCodErr () != ENOMSG) throw;
            }
        }

        /* ajout pour variante */
        if (Recu) continue;

        Msgrcv (MsqId, & Reponse, CstMaxTaille, 0);
        cout << "Message lu : " << Reponse.m_Message << endl;
        /* fin ajout pour variante */
    }

    return 0;

}  //  ppal()