Example in ILE C: User queue

This program illustrates how to use APIs to create and manipulate a user queue.

Note: Read the Code license and disclaimer information for important legal information.
/*********************************************************************/
/*                                                                   */
/*Program Name: UQUEUEX                                              */
/*                                                                   */
/*Program Language:  ILE C                                           */
/*                                                                   */
/*Description:  This program illustrates how to use APIs to create   */
/*              and manipulate a user queue.                         */
/*                                                                   */
/*                                                                   */
/*Header Files Included: <stdio.h>                                   */
/*                       <signal.h>                                  */
/*                       <string.h>                                  */
/*                       <stdlib.h>                                  */
/*                       <miptrnam.h>                                */
/*                       <miqueue.h>                                 */
/*                       <pointer.h>                                 */
/*                       <quscrtuq.h>                                */
/*                       <qusdltuq.h>                                */
/*                       <qusec.h>                                   */
/*                                                                   */
/*APIs Used:     QUSCRTUQ - Create a user queue                      */
/*               QUSDLTUQ - Delete a user queue                      */
/*                                                                   */
/*********************************************************************/
/*********************************************************************/
/*********************************************************************/
/*                            Includes                               */
/*********************************************************************/

#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <milib.h>                       /* from QCLE/h              */
#include <miptrnam.h>                    /* from QCLE/h              */
#include <miqueue.h>                     /* from QCLE/h              */
#include <pointer.h>
#include <quscrtuq.h>                    /* from QSYSINC/h           */
#include <qusdltuq.h>                    /* from QSYSINC/h           */
#include <qusec.h>                       /* from QSYSINC/h           */

/*********************************************************************/
/*                            Structures                             */
/*********************************************************************/

typedef struct {
 Qus_EC_t ec_fields;
 char     exception_data[100];
} error_code_struct;

/*********************************************************************/
/*                                                                   */
/*                               Main                                */
/*                                                                   */
/*********************************************************************/

void main()
{
  char text_desc[50];
  error_code_struct error_code;
  _SYSPTR queuelib_sysptr,
          user_queue_obj_sysptr;
  _RSLV_Template_T  rslvsp_template;
  _ENQ_Msg_Prefix_T enq_msg_prefix;
  _DEQ_Msg_Prefix_T deq_msg_prefix;
  char enq_msg[50],
       deq_msg[50];
  int success=0;

  /*******************************************************************/
  /* Create a library to create the user queue into.                 */
  /*******************************************************************/

  system("CRTLIB LIB(QUEUELIB)");

  /*******************************************************************/
  /* Initialize the error code parameter.                            */
  /*******************************************************************/
  error_code.ec_fields.Bytes_Provided=sizeof(error_code_struct);

  /*******************************************************************/
  /* Call the QUSCRTUQ API to create a user queue.                   */
  /*                                                                 */
  /*   This will create a user queue called EXAMPLEQ in library      */
  /*   QUEUELIB, with the following attributes:                      */
  /*                                                                 */
  /*     1.  Extended attribute of "VALID     ", which could have    */
  /*         been any valid *NAME.                                   */
  /*     2.  A queue type of "F", or First-in, first-out.            */
  /*     3.  A key length of 0.  If the queue is not keyed, this     */
  /*         value must be 0.                                        */
  /*     4.  A maximum message size of 10 bytes.  This number can    */
  /*         be as large as 64K bytes.                               */
  /*     5.  The initial number of messages set to 10.               */
  /*     6.  Additional number of messages set to 10.                */
  /*     7.  Public authority of *USE.                               */
  /*     8.  A valid text description.                               */
  /*     9.  Replace option of *YES. This means that if a user queue */
  /*         already exists by the name specified, in the library    */
  /*         specified, that it will be replaced by this             */
  /*         request.                                                */
  /*    10.  Domain value of *USER.                                  */
  /*    11.  Pointer value of *NO.  Messages in the queue cannot     */
  /*         contain pointer data.                                   */
  /*******************************************************************/

  memcpy(text_desc, "THIS IS TEXT FOR THE EXAMPLE USER QUEUE          ",
         50);

  QUSCRTUQ("EXAMPLEQ  QUEUELIB  ",  /* Qualified user queue name     */
           "VALID     ",            /* Extended attribute            */
           "F",                     /* Queue type                    */
           0,                       /* Key length                    */
           10,                      /* Maximum message size          */
           10,                      /* Initial number of messages    */
           10,                      /* Additional number of messages */
           "*ALL      ",            /* Public authority              */
           text_desc,               /* Text Description              */
           "*YES      ",            /* Replace existing user queue   */
           &error_code,             /* Error code                    */
           "*USER     ",            /* Domain of user queue          */
           "*NO       ");           /* Allow pointer data            */

  /*******************************************************************/
  /* If an exception occurred, the API would have returned the       */
  /* exception in the error code parameter.  The bytes available     */
  /* field will be set to zero if no exception occurred and greater  */
  /* than zero if an exception did occur.                            */
  /*******************************************************************/

  if (error_code.ec_fields.Bytes_Available > 0)
  {
     printf("ATTEMPT TO CREATE A USER QUEUE FAILED WITH EXCEPTION:%.7s",
            error_code.ec_fields.Exception_Id);
     exit(1);
  }

  /*******************************************************************/
  /* Send information to the queue.                                  */
  /*                                                                 */
  /* We will need to use MI instructions to accomplish this.         */
  /* There are three steps that must be done:                        */
  /*                                                                 */
  /* 1. Resolve a system pointer to the library containing the user  */
  /*    queue object.                                                */
  /* 2. Using the system pointer to the library, resolve a system    */
  /*    pointer to user queue object in the library.                 */
  /* 3. Enqueue the entry using the system pointer for the user      */
  /*    queue.                                                       */
  /*                                                                 */
  /*******************************************************************/

  /*******************************************************************/
  /* First we must resolve to library QUEUELIB.                      */
  /*******************************************************************/
  memset(rslvsp_template.Obj.Name,' ',30);
  memcpy(rslvsp_template.Obj.Name,"QUEUELIB",8);
  rslvsp_template.Obj.Type_Subtype = _Library; /* found in milib.h   */
  rslvsp_template.Auth = _AUTH_NONE; /* found in milib.h             */

  _RSLVSP6(&queuelib_sysptr,         /* system pointer to be set     */
           &rslvsp_template,         /* resolve template             */
           &rslvsp_template.Auth);   /* authority to set in sysptr   */

  /*******************************************************************/
  /* We can now resolve to the user queue object.  We will pass the  */
  /* system pointer to library QUEUELIB to RSLVSP so the resolve     */
  /* will only search library QUEUELIB for the user queue object.    */
  /* This is necessary so that we ensure that we are using the       */
  /* correct object.                                                 */
  /*******************************************************************/
  memset(rslvsp_template.Obj.Name,' ',30);
  memcpy(rslvsp_template.Obj.Name, "EXAMPLEQ", 8);
  rslvsp_template.Obj.Type_Subtype = _Usrq; /* found in milib.h      */
  rslvsp_template.Auth = _AUTH_ALL;  /* found in milib.h             */

  _RSLVSP8(&user_queue_obj_sysptr,   /* system pointer to be set     */
           &rslvsp_template,         /* resolve template             */
           &queuelib_sysptr,         /* sysptr to library            */
           &rslvsp_template.Auth);   /* authority to set in sysptr   */

  /*******************************************************************/
  /* Enqueue the entry.                                              */
  /*******************************************************************/
  enq_msg_prefix.Msg_Len = 10;
  enq_msg_prefix.Msg[0] = '\0';        /* Only used for keyed queues*/
  memcpy(enq_msg, "EXAMPLE   ", 10);

  _ENQ(&user_queue_obj_sysptr,       /* system pointer to user queue */
       &enq_msg_prefix,              /* message prefix               */
       (_SPCPTR)enq_msg);            /* message text                 */

  /*******************************************************************/
  /* Dequeue the entry.                                              */
  /*******************************************************************/
  success = _DEQI(&deq_msg_prefix,   /* message prefix               */
                  (_SPCPTR)deq_msg,  /* message text                 */
                  &user_queue_obj_sysptr); /* sys ptr to user queue  */

  if(success)
  {
    printf("Queue entry information: %.10s\n", deq_msg);
  }
  else
  {
    printf("Entry not dequeued\n");
  }

  /*******************************************************************/
  /* Delete the user queue.                                          */
  /*******************************************************************/

  QUSDLTUQ("EXAMPLEQ  QUEUELIB  ",  /* Qualified user queue name     */
           &error_code);            /* Error code                    */

  /*******************************************************************/
  /* If an exception occurred, the API would have returned the       */
  /* exception in the error code parameter.  The bytes available     */
  /* field will be set to zero if no exception occurred and greater  */
  /* than zero if an exception did occur.                            */
  /*******************************************************************/

  if (error_code.ec_fields.Bytes_Available > 0)
  {
     printf("ATTEMPT TO DELETE A USER QUEUE FAILED WITH EXCEPTION:%.7s",
            error_code.ec_fields.Exception_Id);
     exit(1);
  }

  /*******************************************************************/
  /* Delete the library created for this example.                    */
  /*******************************************************************/

  system("DLTLIB LIB(QUEUELIB)");
}