/*
** Copyright (c) Digital Equipment Corporation, 1994
** All Rights Reserved.
**
** This software is furnished under a license and may be used and copied
** only  in  accordance  with  the  terms  of such  license and with the
** inclusion of the above copyright notice. This software or  any  other
** copies thereof may not be provided or otherwise made available to any
** other person. No title to and ownership of  the  software  is  hereby
** transferred.
**
** The information in this software is subject to change without  notice
** and  should  not be  construed  as  a commitment by Digital Equipment
** Corporation.
**
** Digital assumes no responsibility for the use or  reliability  of its
** software on equipment which is not supplied by Digital.
**                                                                           
**    FILE:          x_getsel.c
**
**    DESCRIPTION:   Example program that highlights using pams_get_msg()
**                   with selection by class and type of message.
**                   This example does only simple selections (i.e. ones
**                   that DON'T require the use of pams_set_select()).
**
**    CREATION DATE: 26 November 1993
**
**    REQUIREMENTS:  The queue named "example_q_1" must be defined in your
**                   init file as a primary queue.
**
*/

/**  .......................  **/
/**  C library include files  **/
/**  .......................  **/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/**  .........................  **/
/**  DECmessageQ include files  **/
/**  .........................  **/
#include "p_entry.h"
#include "p_return.h"
#include "p_symbol.h"


/**  .......................  **/
/**        Local constants    **/
/**  .......................  **/
#define MAX_MESSAGE_SIZE  50

/**  .......................  **/
/**        Message classes    **/
/**  .......................  **/
#define FIRST_CLASS  1
#define SECOND_CLASS 2
#define THIRD_CLASS  3

/**  .......................  **/
/**        Message types      **/
/**  .......................  **/
#define EVEN 0
#define ODD  1


/**  .......................  **/
/**        Structures         **/
/**  .......................  **/
typedef struct _Msg {
   char  buffer[MAX_MESSAGE_SIZE];
   short size;
   short class;
   short type;
} Msg;



/**  ..............................  **/
/**   Function Forward Declarations  **/
/**  ..............................  **/
int32  ExampleAttach( );
int32  ExamplePut( );
int32  ExampleGetByClass( );
int32  ExampleGetByType( );
void  ExampleExit( );
char  *ClassName( );
char  *TypeName( );
void  MsgPrint( );


int32
main( )
{
   int32        dmq_status;
   q_address   my_primary_queue;


   printf( "\nGet Select Example starting...\n" );

   /*  Attach to DECmessageQ  */
   if ( ExampleAttach( &my_primary_queue ) != PAMS__SUCCESS )
       exit( 0 );

   /*  Put some messages into my own queue  */
   dmq_status = ExamplePut( &my_primary_queue );

   /*  Get them back by class  */
   dmq_status = ExampleGetByClass();

   /*  Put the messages back into my queue  */
   dmq_status = ExamplePut( &my_primary_queue );

   /*  Now get them back by type  */
   dmq_status = ExampleGetByType();

   /*  Exit from DECmessageQ  */
   ExampleExit();

   printf( "Get Example complete.\n" );
   return( dmq_status );

} /*  end of main  */



int32
ExampleAttach( q_addr )
   q_address   *q_addr;
{
   int32        dmq_status;
   int32        attach_mode;
   int32        q_name_len;
   int32        q_type;
   static char q_name[12];


   /*
   **  Attach by name to the primary queue "example_q_1"
   */
   strcpy( q_name, "example_q_1" );
   attach_mode = PSYM_ATTACH_BY_NAME;
   q_type      = PSYM_ATTACH_PQ;
   q_name_len  = (int32) strlen( q_name );
  
   dmq_status  = pams_attach_q(
                      &attach_mode,
                      q_addr,
                      &q_type,
                      q_name,
                      &q_name_len,
                      (int32 *) 0,    /*  Use default name space  */
                      (int32 *) 0,      
                      (char *) 0,     
                      (char *) 0,     
                      (char *) 0 );

   if ( dmq_status == PAMS__SUCCESS )
      printf( "Attached to queue: \"%s\"\n", q_name );
   else
      printf( "Error attaching to queue: \"%s\"; status returned is: %ld.\n", 
               q_name, dmq_status );

   return ( dmq_status );

} /*  end of ExampleAttach  */


  
int32
ExamplePut( q_addr )
   q_address   *q_addr;
{
   int         i;
   char        priority;
   char        delivery;
   char        uma;
   int32        dmq_status;
   int32        timeout;
   struct PSB  put_psb;
   static Msg  MsgList[] = {
   /*  buffer            size  class         type
       ------            ----  -----         ----  */
   { "first message",   0,   FIRST_CLASS,  ODD },
   { "second message",  0,   SECOND_CLASS, EVEN },
   { "third message",   0,   THIRD_CLASS,  ODD },
   { "fourth message",  0,   FIRST_CLASS,  EVEN },
   { "fifth message",   0,   SECOND_CLASS, ODD },
   { "sixth message",   0,   THIRD_CLASS,  EVEN },
   { "seventh message", 0,   FIRST_CLASS,  ODD },
   { "eighth message",  0,   SECOND_CLASS, EVEN },
   { "nineth message",  0,   THIRD_CLASS,  ODD },
   { "tenth message",   0,   FIRST_CLASS,  EVEN } };

   /*
   **  Put the messages in MessageList to the queue passed in
   */
   printf( "\nPutting messages:\n" );
   priority = 0;                 /* Regular priority; use 0, NOT '0'     */
   delivery = PDEL_MODE_NN_MEM;  /* No Notification and nonrecoverable   */
   timeout  = 100;               /* Wait 10 seconds before giving up     */
   uma      = PDEL_UMA_DISCL;    /* If can't deliver it, DISCard and Log */

   for ( i = 0; i < 10; i++ )
   {
       MsgList[i].size  = (short) strlen( MsgList[i].buffer );
       dmq_status       = pams_put_msg(
                               MsgList[i].buffer,
                               &priority,
                               q_addr,        /* passed in */
                               &MsgList[i].class,
                               &MsgList[i].type,
                               &delivery,
                               &MsgList[i].size,
                               &timeout,
                               &put_psb,
                               &uma,
                               (q_address *) 0, 
                               (char *) 0,
                               (char *) 0,
                               (char *) 0 );
     
      if ( dmq_status == PAMS__SUCCESS )
         MsgPrint( "\n\tPut Message:", &MsgList[i] );
      else
         printf( "\nError putting message %d; status returned is: %ld.\n", i,
                  dmq_status );
   }/* end for */

   printf("\n");
   return ( dmq_status );

} /*  end of ExamplePut  */



int32
ExampleGetByClass( )
{
   char        priority;
   short       msg_area_len;
   short       sel_filter[2];
   int         i;
   int32        dmq_status;
   q_address   msg_source;
   Msg         msg;


   /*
   **  Get the FIRST_CLASS, then SECOND_CLASS, then THIRD_CLASS messages
   **  and print them out.
   */
   for ( i = FIRST_CLASS; i <= THIRD_CLASS; i++ )
   {
       printf( "\nGetting %s messages\n", ClassName( i ) );

       /* Set up sel_filter to only get messages of the specified class */
       sel_filter[0] = i;
       sel_filter[1] = PSEL_PQ_CLASS;  

       /*  Get all priority messages  */
       priority = 0;    
       msg_area_len    = MAX_MESSAGE_SIZE;
       dmq_status        = PAMS__SUCCESS;

      while ( dmq_status == PAMS__SUCCESS )
      {
         /*  Initialize the message area to hold the message  */
         memset( msg.buffer, '\0', MAX_MESSAGE_SIZE );

         /*  Get a message  */
         dmq_status = pams_get_msg(
                             msg.buffer,
                             &priority,
                             &msg_source,
                             &msg.class,
                             &msg.type,
                             &msg_area_len,
                             &msg.size,
                             (int32 *) sel_filter,
                             (struct PSB *) 0,
                             (struct show_buffer *) 0,
                             (int32 *) 0,
                             (char *) 0,
                             (char *) 0,
                             (char *) 0 );

         switch ( dmq_status )
         {
            case PAMS__SUCCESS :
               MsgPrint( "\n\tGot message:", &msg );
            break;

            case PAMS__NOMOREMSG :
               /*  Status returned when queue is empty  */
            break;

            default :
               printf( "\nError getting message; status returned is %ld.\n",
                        dmq_status );
            break;
         }/* end case dmq_status */

      } /* end while */
   } /* end for */

   printf("\n");
   return ( dmq_status );

} /* end of ExampleGetByClass  */


int32
ExampleGetByType( )
{
   char        priority;
   short       msg_area_len;
   short       sel_filter[2];
   int         i;
   int32        dmq_status;
   q_address   msg_source;
   Msg         msg;


   /*
   **  Get the messages of type EVEN then messages of type ODD and print
   **  them out.
   */
   for ( i = EVEN; i <= ODD; i++ )
   {
      printf( "\nGetting %s messages\n", TypeName( i ) );

      /*
      **    Set the selection filter to select only those messages
      **    which have the same type as we are looking for.
      */
      sel_filter[0]  = i;
      sel_filter[1]  = PSEL_PQ_TYPE;

      priority       = 0;   
      msg_area_len   = MAX_MESSAGE_SIZE;
      dmq_status     = PAMS__SUCCESS;

      while ( dmq_status == PAMS__SUCCESS )
      {
         /*  Initialize the message area to hold the message  */
         memset( msg.buffer, '\0', MAX_MESSAGE_SIZE );

         /*  Get a message  */
         dmq_status = pams_get_msg(
                           msg.buffer,
                           &priority,
                           &msg_source,
                           &msg.class,
                           &msg.type,
                           &msg_area_len,
                           &msg.size,
                           (int32 *) sel_filter,
                           (struct PSB *) 0,
                           (struct show_buffer *) 0,
                           (int32 *) 0,
                           (char *) 0,
                           (char *) 0,
                           (char *) 0 );

         switch ( dmq_status )
         {
            case PAMS__SUCCESS :
               MsgPrint( "\n\tGot message:", &msg );
            break;

            case PAMS__NOMOREMSG :
               /*  Status returned when queue is empty  */
            break;

            default :
               printf( "\nError getting message; status returned is %ld.\n",
                       dmq_status );
            break;
         }/* end case dmq_status */

      } /* end while */

   } /* end for */

   printf("\n");
   return ( dmq_status );

} /* end of ExampleGetByType  */


void
ExampleExit( )
{
   int32        dmq_status;

   dmq_status    = pams_exit();

   if ( dmq_status == PAMS__SUCCESS )
       printf( "\nExited from DECmessageQ.\n" );
   else
       printf( "Error exiting; status returned is %ld.\n", dmq_status );

} /*  end of ExampleExit  */


char *
ClassName( val )
   int     val;
{
   switch ( val )
   {
      case FIRST_CLASS :
         return ( "FIRST" );
      
      case SECOND_CLASS :
         return ( "SECOND" );

      case THIRD_CLASS :
         return ( "THIRD" );
    
      default :
         return ( "UNKNOWN" );
   }

} /*  end of ClassName  */


char *
TypeName( val )
   int     val;
{
   switch ( val )
   {
      case EVEN :
         return ( "EVEN" );
         
      case ODD :
         return ( "ODD" );

      default :
         return ( "UNKNOWN" );
   }

} /*  end of TypeName  */


void
MsgPrint( header, msg )
   char    *header;
   Msg     *msg;
{
   if ( header )
      printf( "%s\n", header );

   printf( "\tText:\t\"%s\"\n", msg->buffer );
   printf( "\tClass:\t%s\n", ClassName(msg->class) );
   printf( "\tType:\t%s\n", TypeName(msg->type) );

} /*  end of MsgPrint  */
