home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
mqput.zip
/
MQPUT.C
Wrap
C/C++ Source or Header
|
2002-12-24
|
15KB
|
319 lines
/* @(#) samples/c/amqsput0.c, samples, p521, p521-L010411 1.21.1.2 01/04/09 15:42:26 */
/********************************************************************/
/* */
/* Program name: AMQSPUT0 */
/* */
/* Description: Sample C program that puts messages to */
/* a message queue (example using MQPUT) */
/* <START_COPYRIGHT> */
/* Licensed Materials - Property of IBM */
/* */
/* 47P8479, 5639-B43 */
/* */
/* (C) Copyright IBM Corporation 1994, 2001 All Rights Reserved */
/* US Government Users Restricted Rights - Use, duplication or */
/* disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */
/* */
/* <END_COPYRIGHT> */
/********************************************************************/
/* */
/* Function: */
/* */
/* */
/* AMQSPUT0 is a sample C program to put messages on a message */
/* queue, and is an example of the use of MQPUT. */
/* */
/* -- messages are sent to the queue named by the parameter */
/* */
/* -- gets lines from StdIn, and adds each to target */
/* queue, taking each line of text as the content */
/* of a datagram message; the sample stops when a null */
/* line (or EOF) is read. */
/* New-line characters are removed. */
/* If a line is longer than 99 characters it is broken up */
/* into 99-character pieces. Each piece becomes the */
/* content of a datagram message. */
/* If the length of a line is a multiple of 99 plus 1 */
/* e.g. 199, the last piece will only contain a new-line */
/* character so will terminate the input. */
/* */
/* -- writes a message for each MQI reason other than */
/* MQRC_NONE; stops if there is a MQI completion code */
/* of MQCC_FAILED */
/* */
/* Program logic: */
/* MQOPEN target queue for OUTPUT */
/* while end of input file not reached, */
/* . read next line of text */
/* . MQPUT datagram message with text line as data */
/* MQCLOSE target queue */
/* */
/* */
/********************************************************************/
/* */
/* AMQSPUT0 has 2 parameters */
/* - the name of the target queue (required) */
/* - queue manager name (optional) */
/* */
/********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* includes for MQI */
#include <cmqc.h>
int main(int argc, char **argv)
{
/* Declare file and character for sample input */
FILE *fp;
/* Declare MQI structures needed */
MQOD od = {MQOD_DEFAULT}; /* Object Descriptor */
MQMD md = {MQMD_DEFAULT}; /* Message Descriptor */
MQPMO pmo = {MQPMO_DEFAULT}; /* put message options */
/** note, sample uses defaults where it can **/
MQHCONN Hcon; /* connection handle */
MQHOBJ Hobj; /* object handle */
MQLONG O_options; /* MQOPEN options */
MQLONG C_options; /* MQCLOSE options */
MQLONG CompCode; /* completion code */
MQLONG OpenCode; /* MQOPEN completion code */
MQLONG Reason; /* reason code */
MQLONG CReason; /* reason code for MQCONN */
MQLONG messlen; /* message length */
char buffer[100]; /* message buffer */
char QMName[50]; /* queue manager name */
int i=0, J=0;
printf("Sample AMQSPUT0 start\n");
if (argc < 2)
{
printf("Required parameter missing - queue name\n");
exit(99);
}
/******************************************************************/
/* */
/* Connect to queue manager */
/* */
/******************************************************************/
QMName[0] = 0; /* default */
if (argc > 2)
strcpy(QMName, argv[2]);
MQCONN(QMName, /* queue manager */
&Hcon, /* connection handle */
&CompCode, /* completion code */
&CReason); /* reason code */
/* report reason and stop if it failed */
if (CompCode == MQCC_FAILED)
{
printf("MQCONN ended with reason code %ld\n", CReason);
exit( (int)CReason );
}
/******************************************************************/
/* */
/* Use parameter as the name of the target queue */
/* */
/******************************************************************/
strncpy(od.ObjectName, argv[1], (size_t)MQ_Q_NAME_LENGTH);
printf("target queue is %s\n", od.ObjectName);
/******************************************************************/
/* */
/* Open the target message queue for output */
/* */
/******************************************************************/
O_options = MQOO_OUTPUT /* open queue for output */
+ MQOO_FAIL_IF_QUIESCING; /* but not if MQM stopping */
MQOPEN(Hcon, /* connection handle */
&od, /* object descriptor for queue */
O_options, /* open options */
&Hobj, /* object handle */
&OpenCode, /* MQOPEN completion code */
&Reason); /* reason code */
/* report reason, if any; stop if failed */
if (Reason != MQRC_NONE)
{
printf("MQOPEN ended with reason code %ld\n", Reason);
}
if (OpenCode == MQCC_FAILED)
{
printf("unable to open queue for output\n");
}
/******************************************************************/
/* */
/* Read lines from the file and put them to the message queue */
/* Loop until null line or end of file, or there is a failure */
/* */
/******************************************************************/
CompCode = OpenCode; /* use MQOPEN result for initial test */
fp = stdin;
memcpy(md.Format, /* character string format */
MQFMT_STRING, (size_t)MQ_FORMAT_LENGTH);
/******************************************************************/
/* Use these options when connecting to Queue Managers that also */
/* support them, see the Application Programming Reference for */
/* details. */
/* These options cause the MsgId and CorrelId to be replaced, so */
/* that there is no need to reset them before each MQPUT */
/******************************************************************/
/* pmo.Options |= MQPMO_NEW_MSG_ID; */
/* pmo.Options |= MQPMO_NEW_CORREL_ID; */
memcpy(md.MsgId, /* reset MsgId to get a new one */
MQMI_NONE, sizeof(md.MsgId) );
memcpy(md.CorrelId, /* reset CorrelId to get a new one */
MQCI_NONE, sizeof(md.CorrelId) );
for(i=1; i < 9; i++)
{
/**************************************************************/
/* The following two statements are not required if the */
/* MQPMO_NEW_MSG_ID and MQPMO_NEW _CORREL_ID options are used */
/**************************************************************/
messlen = 32;
strcpy(buffer, "Hello ");
if (i == 1)
{
md.MsgFlags = MQMF_SEGMENT|MQMF_MSG_IN_GROUP;
md.Version = MQMD_VERSION_2;
md.MsgSeqNumber = 1;
md.Persistence = MQPER_NOT_PERSISTENT;
md.Offset = 0;
memset(md.GroupId, '\0', sizeof(md.GroupId));
strcpy(md.GroupId, "12345");
pmo.Version = MQPMO_VERSION_2;
pmo.Options = MQPMO_NEW_MSG_ID;
pmo.Options = pmo.Options | MQPMO_NO_SYNCPOINT;
}
if (i == 2)
{
md.MsgFlags = MQMF_LAST_SEGMENT|MQMF_MSG_IN_GROUP;
md.Offset = messlen;
}
if (i == 3)
{
md.MsgSeqNumber = 2;
md.MsgFlags = MQMF_SEGMENT|MQMF_LAST_MSG_IN_GROUP;
md.Offset = 0;
}
if (i == 4)
{
md.MsgFlags = MQMF_LAST_SEGMENT|MQMF_LAST_MSG_IN_GROUP;
md.Offset = messlen;
}
if (i == 5)
{
md.MsgFlags = MQMF_SEGMENT|MQMF_MSG_IN_GROUP;
md.Offset = 0;
md.MsgSeqNumber = 1;
memset(md.GroupId, '\0', sizeof(md.GroupId));
strcpy(md.GroupId, "ABCDE");
}
if (i == 6)
{
md.MsgFlags = MQMF_LAST_SEGMENT|MQMF_MSG_IN_GROUP;
md.Offset = messlen;
}
if (i == 7)
{
md.MsgSeqNumber = 2;
md.MsgFlags = MQMF_SEGMENT|MQMF_LAST_MSG_IN_GROUP;
md.Offset = 0;
}
if (i == 8)
{
md.MsgFlags = MQMF_LAST_SEGMENT|MQMF_LAST_MSG_IN_GROUP;
md.Offset = messlen;
}
MQPUT(Hcon, /* connection handle */
Hobj, /* object handle */
&md, /* message descriptor */
&pmo, /* default options (datagram) */
messlen, /* message length */
buffer, /* message buffer */
&CompCode, /* completion code */
&Reason); /* reason code */
/* report reason, if any */
if (Reason != MQRC_NONE)
{
printf("MQPUT ended with reason code %ld\n", Reason);
}
else
{
printf ("\n Put a message \n");
printf("\n Group id : ");
for (J = 0 ; J < 24 ; J++)
printf("%02X",md.GroupId[J] );
printf ("|");
printf("\n group id again is %s|", md.GroupId);
}
}
/******************************************************************/
/* */
/* Close the target queue (if it was opened) */
/* */
/******************************************************************/
if (OpenCode != MQCC_FAILED)
{
C_options = 0; /* no close options */
MQCLOSE(Hcon, /* connection handle */
&Hobj, /* object handle */
C_options,
&CompCode, /* completion code */
&Reason); /* reason code */
/* report reason, if any */
if (Reason != MQRC_NONE)
{
printf("MQCLOSE ended with reason code %ld\n", Reason);
}
}
/******************************************************************/
/* */
/* Disconnect from MQM if not already connected */
/* */
/******************************************************************/
if (CReason != MQRC_ALREADY_CONNECTED)
{
MQDISC(&Hcon, /* connection handle */
&CompCode, /* completion code */
&Reason); /* reason code */
/* report reason, if any */
if (Reason != MQRC_NONE)
{
printf("MQDISC ended with reason code %ld\n", Reason);
}
}
/******************************************************************/
/* */
/* END OF AMQSPUT0 */
/* */
/******************************************************************/
printf("Sample AMQSPUT0 end\n");
return(0);
}