home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mqput.zip / MQPUT.C
C/C++ Source or Header  |  2002-12-24  |  15KB  |  319 lines

  1.  /* @(#) samples/c/amqsput0.c, samples, p521, p521-L010411  1.21.1.2 01/04/09 15:42:26 */
  2.  /********************************************************************/
  3.  /*                                                                  */
  4.  /* Program name: AMQSPUT0                                           */
  5.  /*                                                                  */
  6.  /* Description: Sample C program that puts messages to              */
  7.  /*              a message queue (example using MQPUT)               */
  8.  /* <START_COPYRIGHT>                                                */
  9.  /* Licensed Materials - Property of IBM                             */
  10.  /*                                                                  */
  11.  /* 47P8479, 5639-B43                                                */
  12.  /*                                                                  */
  13.  /* (C) Copyright IBM Corporation 1994, 2001 All Rights Reserved     */
  14.  /* US Government Users Restricted Rights - Use, duplication or      */
  15.  /* disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */
  16.  /*                                                                  */
  17.  /* <END_COPYRIGHT>                                                  */
  18.  /********************************************************************/
  19.  /*                                                                  */
  20.  /* Function:                                                        */
  21.  /*                                                                  */
  22.  /*                                                                  */
  23.  /*   AMQSPUT0 is a sample C program to put messages on a message    */
  24.  /*   queue, and is an example of the use of MQPUT.                  */
  25.  /*                                                                  */
  26.  /*      -- messages are sent to the queue named by the parameter    */
  27.  /*                                                                  */
  28.  /*      -- gets lines from StdIn, and adds each to target           */
  29.  /*         queue, taking each line of text as the content           */
  30.  /*         of a datagram message; the sample stops when a null      */
  31.  /*         line (or EOF) is read.                                   */
  32.  /*         New-line characters are removed.                         */
  33.  /*         If a line is longer than 99 characters it is broken up   */
  34.  /*         into 99-character pieces. Each piece becomes the         */
  35.  /*         content of a datagram message.                           */
  36.  /*         If the length of a line is a multiple of 99 plus 1       */
  37.  /*         e.g. 199, the last piece will only contain a new-line    */
  38.  /*         character so will terminate the input.                   */
  39.  /*                                                                  */
  40.  /*      -- writes a message for each MQI reason other than          */
  41.  /*         MQRC_NONE; stops if there is a MQI completion code       */
  42.  /*         of MQCC_FAILED                                           */
  43.  /*                                                                  */
  44.  /*    Program logic:                                                */
  45.  /*         MQOPEN target queue for OUTPUT                           */
  46.  /*         while end of input file not reached,                     */
  47.  /*         .  read next line of text                                */
  48.  /*         .  MQPUT datagram message with text line as data         */
  49.  /*         MQCLOSE target queue                                     */
  50.  /*                                                                  */
  51.  /*                                                                  */
  52.  /********************************************************************/
  53.  /*                                                                  */
  54.  /*   AMQSPUT0 has 2 parameters                                      */
  55.  /*                 - the name of the target queue (required)        */
  56.  /*                 - queue manager name (optional)                  */
  57.  /*                                                                  */
  58.  /********************************************************************/
  59.  #include <stdio.h>
  60.  #include <stdlib.h>
  61.  #include <string.h>
  62.     /* includes for MQI */
  63.  #include <cmqc.h>
  64.  
  65.  int main(int argc, char **argv)
  66.  {
  67.    /*  Declare file and character for sample input                   */
  68.    FILE *fp;
  69.  
  70.    /*   Declare MQI structures needed                                */
  71.    MQOD     od = {MQOD_DEFAULT};    /* Object Descriptor             */
  72.    MQMD     md = {MQMD_DEFAULT};    /* Message Descriptor            */
  73.    MQPMO   pmo = {MQPMO_DEFAULT};   /* put message options           */
  74.       /** note, sample uses defaults where it can **/
  75.  
  76.    MQHCONN  Hcon;                   /* connection handle             */
  77.    MQHOBJ   Hobj;                   /* object handle                 */
  78.    MQLONG   O_options;              /* MQOPEN options                */
  79.    MQLONG   C_options;              /* MQCLOSE options               */
  80.    MQLONG   CompCode;               /* completion code               */
  81.    MQLONG   OpenCode;               /* MQOPEN completion code        */
  82.    MQLONG   Reason;                 /* reason code                   */
  83.    MQLONG   CReason;                /* reason code for MQCONN        */
  84.    MQLONG   messlen;                /* message length                */
  85.    char     buffer[100];            /* message buffer                */
  86.    char     QMName[50];             /* queue manager name            */
  87.    int      i=0, J=0;
  88.  
  89.    printf("Sample AMQSPUT0 start\n");
  90.    if (argc < 2)
  91.    {
  92.      printf("Required parameter missing - queue name\n");
  93.      exit(99);
  94.    }
  95.  
  96.    /******************************************************************/
  97.    /*                                                                */
  98.    /*   Connect to queue manager                                     */
  99.    /*                                                                */
  100.    /******************************************************************/
  101.    QMName[0] = 0;    /* default */
  102.    if (argc > 2)
  103.      strcpy(QMName, argv[2]);
  104.    MQCONN(QMName,                  /* queue manager                  */
  105.           &Hcon,                   /* connection handle              */
  106.           &CompCode,               /* completion code                */
  107.           &CReason);               /* reason code                    */
  108.  
  109.    /* report reason and stop if it failed     */
  110.    if (CompCode == MQCC_FAILED)
  111.    {
  112.      printf("MQCONN ended with reason code %ld\n", CReason);
  113.      exit( (int)CReason );
  114.    }
  115.  
  116.    /******************************************************************/
  117.    /*                                                                */
  118.    /*   Use parameter as the name of the target queue                */
  119.    /*                                                                */
  120.    /******************************************************************/
  121.    strncpy(od.ObjectName, argv[1], (size_t)MQ_Q_NAME_LENGTH);
  122.    printf("target queue is %s\n", od.ObjectName);
  123.  
  124.    /******************************************************************/
  125.    /*                                                                */
  126.    /*   Open the target message queue for output                     */
  127.    /*                                                                */
  128.    /******************************************************************/
  129.    O_options = MQOO_OUTPUT           /* open queue for output        */
  130.            + MQOO_FAIL_IF_QUIESCING; /* but not if MQM stopping      */
  131.    MQOPEN(Hcon,                      /* connection handle            */
  132.           &od,                       /* object descriptor for queue  */
  133.           O_options,                 /* open options                 */
  134.           &Hobj,                     /* object handle                */
  135.           &OpenCode,                 /* MQOPEN completion code       */
  136.           &Reason);                  /* reason code                  */
  137.  
  138.    /* report reason, if any; stop if failed      */
  139.    if (Reason != MQRC_NONE)
  140.    {
  141.      printf("MQOPEN ended with reason code %ld\n", Reason);
  142.    }
  143.  
  144.    if (OpenCode == MQCC_FAILED)
  145.    {
  146.      printf("unable to open queue for output\n");
  147.    }
  148.  
  149.    /******************************************************************/
  150.    /*                                                                */
  151.    /*   Read lines from the file and put them to the message queue   */
  152.    /*   Loop until null line or end of file, or there is a failure   */
  153.    /*                                                                */
  154.    /******************************************************************/
  155.    CompCode = OpenCode;        /* use MQOPEN result for initial test */
  156.    fp = stdin;
  157.  
  158.    memcpy(md.Format,           /* character string format            */
  159.           MQFMT_STRING, (size_t)MQ_FORMAT_LENGTH);
  160.  
  161.    /******************************************************************/
  162.    /* Use these options when connecting to Queue Managers that also  */
  163.    /* support them, see the Application Programming Reference for    */
  164.    /* details.                                                       */
  165.    /* These options cause the MsgId and CorrelId to be replaced, so  */
  166.    /* that there is no need to reset them before each MQPUT          */
  167.    /******************************************************************/
  168.    /* pmo.Options |= MQPMO_NEW_MSG_ID;                               */
  169.    /* pmo.Options |= MQPMO_NEW_CORREL_ID;                            */
  170.  
  171.        memcpy(md.MsgId,           /* reset MsgId to get a new one    */
  172.               MQMI_NONE, sizeof(md.MsgId) );
  173.  
  174.        memcpy(md.CorrelId,        /* reset CorrelId to get a new one */
  175.               MQCI_NONE, sizeof(md.CorrelId) );
  176.  
  177.   for(i=1; i < 9; i++)
  178.   {
  179.        /**************************************************************/
  180.        /* The following two statements are not required if the       */
  181.        /* MQPMO_NEW_MSG_ID and MQPMO_NEW _CORREL_ID options are used */
  182.        /**************************************************************/
  183.     messlen = 32;
  184.     strcpy(buffer, "Hello ");
  185.  
  186.        if (i == 1)
  187.        {
  188.         md.MsgFlags = MQMF_SEGMENT|MQMF_MSG_IN_GROUP;
  189.         md.Version = MQMD_VERSION_2;
  190.         md.MsgSeqNumber = 1;
  191.         md.Persistence = MQPER_NOT_PERSISTENT;
  192.         md.Offset = 0;
  193.         memset(md.GroupId, '\0', sizeof(md.GroupId));
  194.         strcpy(md.GroupId, "12345");
  195.  
  196.         pmo.Version = MQPMO_VERSION_2;
  197.         pmo.Options = MQPMO_NEW_MSG_ID;
  198.         pmo.Options = pmo.Options | MQPMO_NO_SYNCPOINT;
  199.        }
  200.  
  201.        if (i == 2)
  202.        {
  203.         md.MsgFlags = MQMF_LAST_SEGMENT|MQMF_MSG_IN_GROUP;
  204.         md.Offset = messlen;
  205.        }
  206.  
  207.        if (i == 3)
  208.        {
  209.          md.MsgSeqNumber = 2;
  210.         md.MsgFlags = MQMF_SEGMENT|MQMF_LAST_MSG_IN_GROUP;
  211.         md.Offset = 0;
  212.        }
  213.  
  214.        if (i == 4)
  215.        {
  216.         md.MsgFlags = MQMF_LAST_SEGMENT|MQMF_LAST_MSG_IN_GROUP;
  217.         md.Offset = messlen;
  218.        }
  219.  
  220.        if (i == 5)
  221.        {
  222.         md.MsgFlags = MQMF_SEGMENT|MQMF_MSG_IN_GROUP;
  223.         md.Offset = 0;
  224.         md.MsgSeqNumber = 1;
  225.         memset(md.GroupId, '\0', sizeof(md.GroupId));
  226.         strcpy(md.GroupId, "ABCDE");
  227.        }
  228.        if (i == 6)
  229.        {
  230.         md.MsgFlags = MQMF_LAST_SEGMENT|MQMF_MSG_IN_GROUP;
  231.         md.Offset = messlen;
  232.        }
  233.        if (i == 7)
  234.        {
  235.          md.MsgSeqNumber = 2;
  236.         md.MsgFlags = MQMF_SEGMENT|MQMF_LAST_MSG_IN_GROUP;
  237.         md.Offset = 0;
  238.        }
  239.  
  240.        if (i == 8)
  241.        {
  242.         md.MsgFlags = MQMF_LAST_SEGMENT|MQMF_LAST_MSG_IN_GROUP;
  243.         md.Offset = messlen;
  244.        }
  245.  
  246.        MQPUT(Hcon,                /* connection handle               */
  247.              Hobj,                /* object handle                   */
  248.              &md,                 /* message descriptor              */
  249.              &pmo,                /* default options (datagram)      */
  250.              messlen,             /* message length                  */
  251.              buffer,              /* message buffer                  */
  252.              &CompCode,           /* completion code                 */
  253.              &Reason);            /* reason code                     */
  254.  
  255.        /* report reason, if any */
  256.        if (Reason != MQRC_NONE)
  257.        {
  258.          printf("MQPUT ended with reason code %ld\n", Reason);
  259.        }
  260.        else
  261.        {
  262.          printf ("\n Put a message \n");
  263.          printf("\n  Group id : ");
  264.          for (J = 0 ; J < 24 ; J++)
  265.            printf("%02X",md.GroupId[J] );
  266.          printf ("|");
  267.          printf("\n group id again is %s|", md.GroupId);
  268.  
  269.        }
  270.   }
  271.  
  272.    /******************************************************************/
  273.    /*                                                                */
  274.    /*   Close the target queue (if it was opened)                    */
  275.    /*                                                                */
  276.    /******************************************************************/
  277.    if (OpenCode != MQCC_FAILED)
  278.    {
  279.      C_options = 0;                  /* no close options             */
  280.      MQCLOSE(Hcon,                   /* connection handle            */
  281.              &Hobj,                  /* object handle                */
  282.              C_options,
  283.              &CompCode,              /* completion code              */
  284.              &Reason);               /* reason code                  */
  285.  
  286.      /* report reason, if any     */
  287.      if (Reason != MQRC_NONE)
  288.      {
  289.        printf("MQCLOSE ended with reason code %ld\n", Reason);
  290.      }
  291.    }
  292.  
  293.    /******************************************************************/
  294.    /*                                                                */
  295.    /*   Disconnect from MQM if not already connected                 */
  296.    /*                                                                */
  297.    /******************************************************************/
  298.    if (CReason != MQRC_ALREADY_CONNECTED)
  299.    {
  300.      MQDISC(&Hcon,                   /* connection handle            */
  301.             &CompCode,               /* completion code              */
  302.             &Reason);                /* reason code                  */
  303.  
  304.      /* report reason, if any     */
  305.      if (Reason != MQRC_NONE)
  306.      {
  307.        printf("MQDISC ended with reason code %ld\n", Reason);
  308.      }
  309.    }
  310.  
  311.    /******************************************************************/
  312.    /*                                                                */
  313.    /* END OF AMQSPUT0                                                */
  314.    /*                                                                */
  315.    /******************************************************************/
  316.    printf("Sample AMQSPUT0 end\n");
  317.    return(0);
  318.  }
  319.