📄 mbsgett1.c
字号:
/* ---------------------------------------------------------------------------------
Program name : mbsgett1.c
Description : Sample C program that gets messages from a queue
in the transaction mode
(C) Copyright SAISON INFORMATION SYSTEMS CO.,LTD. 2004 All Rights Reserved.
--------------------------------------------------------------------------------- */
/*----------------------------------------------------------------------------------
Function :
"mbsgett1" is a sample C program to get messages from a queue in the
transaction mode. This program gets messages with "TRANS" as UserDefineID
from specified queue, and writes them to StdOut.
This program has the following parameters. If the optional parameters
are omitted, the parameters in System-Environment file( mbcenv.conf or MBCENV)
will be used.
required :
optional :
(1) -s Server : The name of HULFT-Message Server
(2) -p Port : The port of HULFT-Message Server
(3) -q Queue : The name of the queue
--------------------------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(OS400)
#include "APIH/MBAPI"
#define usage "Usage : CALL MBSGETT1 PARM('-s' 'Server' '-p' 'Port' '-q' 'Queue')"
#else
#include "mbapi.h"
#define usage "Usage : mbsgett1 [-s Server] [-p Port] [-q Queue]"
#endif
#define BUFFER_SHORTAGE 10003 /* API error code */
#define NO_MSG_AVAILABLE 20009 /* API detail code */
int main( int argc, char **argv )
{
/* ---------------------------------------------------------------------------------
Declare structures needed
--------------------------------------------------------------------------------- */
MBHD *Hconn; /* connection handle */
MBQHD *Hque; /* queue handle */
MBCON Conn; /* MBCON structure */
MBQ Que; /* MBQ structure */
MBMSG Msg; /* MBMSG structure */
MBERR err; /* MBERR structure */
int rc; /* return code */
unsigned short port; /* server port */
char server[MBA_HOST_NAME_LEN+1]; /* server name */
char queue[MBA_QUEUE_NAME_LEN+1]; /* queue name */
char user_define_id[] = "TRANS"; /* user definition ID */
/* ---------------------------------------------------------------------------------
Initialize
--------------------------------------------------------------------------------- */
memset( server, 0x00, sizeof(server) );
memset( queue, 0x00, sizeof(queue) );
port = 0;
/* ---------------------------------------------------------------------------------
Check the arguments
--------------------------------------------------------------------------------- */
if( argc > 7 ){
printf( "%s\n", usage );
exit( -1 );
}
argv++;
argc--;
while( argc-- ){
if( !strcmp( *argv, "-s" )){ /* server name */
if( argc <= 0 ){
printf( "%s\n", usage );
exit( -1 );
}
argv++;
argc--;
strncpy( server, *argv, MBA_HOST_NAME_LEN );
}
else if( !strcmp( *argv, "-p" )){ /* server port */
if( argc <= 0 ){
printf( "%s\n", usage );
exit( -1 );
}
argv++;
argc--;
port = atoi( *argv );
}
else if( !strcmp( *argv, "-q" )){ /* queue name */
if( argc <= 0 ){
printf( "%s\n", usage );
exit( -1 );
}
argv++;
argc--;
strncpy( queue, *argv,MBA_QUEUE_NAME_LEN );
}
else{
printf( "%s\n", usage );
exit( -1 );
}
argv++;
}
/* ---------------------------------------------------------------------------------
Connect to HULFT-Message Server
--------------------------------------------------------------------------------- */
memset( &Conn, 0x00, sizeof( Conn ));
strcpy( Conn.Server, server ); /* server name */
Conn.Port = port; /* server port */
#if defined(OS400)
Conn.Ebcdiccode = MBA_ET_IBMSMLEX; /* ebcdic code type */
#endif
Hconn = MBCONN( &Conn, sizeof( Conn ), &err );
if( !Hconn ){ /* if API returns unsuccessfully */
printf( "MBCONN : err=%d dtl=%d sys=%d msg=%s\n",
err.ErrCode, err.DtlCode, err.SysCode, err.ErrMsg );
exit( -1 );
}
/* ---------------------------------------------------------------------------------
Open the queue
--------------------------------------------------------------------------------- */
memset( &Que, 0x00, sizeof( Que ));
strcpy( Que.Queue, queue ); /* queue name */
Hque = MBOPEN( Hconn, &Que, sizeof( Que ), &err );
if( !Hque ){ /* if API returns unsuccessfully */
printf( "MBOPEN : err=%d dtl=%d sys=%d msg=%s\n",
err.ErrCode, err.DtlCode, err.SysCode, err.ErrMsg );
MBDISCON( Hconn, &err );
exit( -1 );
}
/* ---------------------------------------------------------------------------------
Get the messages from the queue
--------------------------------------------------------------------------------- */
memset( &Msg, 0x00, sizeof( Msg ));
strcpy( Msg.UserDefineID, user_define_id ); /* user definition ID */
Msg.Transaction_flag = MBA_TM_TRANS; /* transaction flag */
Msg.Size = 256; /* message buffer size */
Msg.Buf = malloc( Msg.Size+1 ); /* message buffer */
if( !Msg.Buf ){
printf( "There is no available memory.\n" );
MBCLOSE( Hconn, Hque, &err );
MBDISCON( Hconn, &err );
exit( -1 );
}
memset( Msg.Buf, 0x00, Msg.Size+1 );
#if defined(OS400)
Msg.ShiftCode = MBA_SFT_ADD; /* shift code */
#endif
/* read messages */
while( 1 ){
rc = MBGET( Hconn, Hque, &Msg, sizeof( Msg ), &err );
if( err.ErrCode == BUFFER_SHORTAGE ){ /* if Msg.Size is too short */
Msg.Size = Msg.MsgSize;
Msg.Buf = realloc( Msg.Buf, Msg.Size+1 );
if( !Msg.Buf ){
printf( "There is no available memory.\n" );
MBCLOSE( Hconn, Hque, &err );
MBDISCON( Hconn, &err );
exit( -1 );
}
memset( Msg.Buf, 0x00, Msg.Size+1 );
continue;
}
if( err.DtlCode == NO_MSG_AVAILABLE ){
/* if the message does not exist */
break;
}
if( rc ){ /* if API returns unsuccessfully */
printf( "MBGET : err=%d dtl=%d sys=%d msg=%s\n",
err.ErrCode, err.DtlCode, err.SysCode, err.ErrMsg );
rc = MBROLLBK( Hconn, Hque, &err );
if( rc ){ /* if API returns unsuccessfully */
printf( "MBROLLBK : err=%d dtl=%d sys=%d msg=%s\n",
err.ErrCode, err.DtlCode, err.SysCode, err.ErrMsg );
}
free( Msg.Buf );
MBCLOSE( Hconn, Hque, &err );
MBDISCON( Hconn, &err );
exit( -1 );
}
printf( ">> " );
puts( Msg.Buf );
}
rc = MBCOMMIT( Hconn, Hque, &err );
if( rc ){ /* if API returns unsuccessfully */
printf( "MBCOMMIT : err=%d dtl=%d sys=%d msg=%s\n",
err.ErrCode, err.DtlCode, err.SysCode, err.ErrMsg );
free( Msg.Buf );
MBCLOSE( Hconn, Hque, &err );
MBDISCON( Hconn, &err );
exit( -1 );
}
free( Msg.Buf );
/* ---------------------------------------------------------------------------------
Close the queue
--------------------------------------------------------------------------------- */
rc = MBCLOSE( Hconn, Hque, &err );
if( rc ){ /* if API returns unsuccessfully */
printf( "MBCLOSE : err=%d dtl=%d sys=%d msg=%s\n",
err.ErrCode, err.DtlCode, err.SysCode, err.ErrMsg );
MBDISCON( Hconn, &err );
exit( -1 );
}
/* ---------------------------------------------------------------------------------
Disconnect from HULFT-Message Server
--------------------------------------------------------------------------------- */
rc = MBDISCON( Hconn, &err );
if( rc ){ /* if API returns unsuccessfully */
printf( "MBDISCON : err=%d dtl=%d sys=%d msg=%s\n",
err.ErrCode, err.DtlCode, err.SysCode, err.ErrMsg );
exit( -1 );
}
printf( "Normal termination\n" );
return( 0 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -