mbsputw1.c

来自「Hulft-Message是用来在Internet上传输文件数据的商用Middl」· C语言 代码 · 共 219 行

C
219
字号
/* ---------------------------------------------------------------------------------

     Program name     : mbsputw1.c

     Description      : Sample C program that puts a message to a queue
                        with wait option

     (C) Copyright SAISON INFORMATION SYSTEMS CO.,LTD. 2004 All Rights Reserved.

--------------------------------------------------------------------------------- */
/*----------------------------------------------------------------------------------

     Function : 

        "mbsputw1" is a sample C program to put a message to a queue with
     wait option. "MBPUT" API is blocked when the specified queue is full. This
     program gets a message from StdIn, and puts this to the specified queue with
    "WAIT" as the UserDefineID.

        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
          (4) -w Timeout    : The value of the timeout of MBMSG

--------------------------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if defined(OS400)
#include "APIH/MBAPI"
#define usage   "Usage : CALL MBSPUTW1 PARM('-s' 'Server' '-p' 'Port' '-q' 'Queue' '-w' 'Timeout')"
#else
#include "mbapi.h"
#define usage   "Usage : mbsputw1 [-s Server] [-p Port] [-q Queue] [-w Timeout]"
#endif

#define PUT_WAIT_TIMEOUT 407                                /* 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 int     timeout;                           /* timeout value */
        unsigned short   port;                              /* server port */
        char             server[MBA_HOST_NAME_LEN+1];       /* server name */
        char             queue[MBA_QUEUE_NAME_LEN+1];       /* queue name */
        char             sin_buf[256];                      /* buffer for StdIn */
        char             user_define_id[] = "WAIT";         /* user definition ID */

/* ---------------------------------------------------------------------------------
     Initialize
--------------------------------------------------------------------------------- */
		memset( server, 0x00, sizeof(server) );
		memset( queue, 0x00, sizeof(queue) );
		port = 0;
		timeout = 0;

/* ---------------------------------------------------------------------------------
     Check the arguments
--------------------------------------------------------------------------------- */
        if( argc > 9 ){
                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 if( !strcmp( *argv, "-w" )){ /* timeout value */
                       if( argc <= 0 ){
                                printf( "%s\n", usage );
                                exit( -1 );
                       }
                       argv++;
                       argc--;
                       timeout = atoi( *argv );
                }
                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 );
        }

/* ---------------------------------------------------------------------------------
     Put the message to the queue
--------------------------------------------------------------------------------- */
        printf ( ">> Input data\n" );
        fgets( sin_buf,sizeof(sin_buf),stdin );
        memset( &Msg, 0x00, sizeof( Msg ));
        strcpy( Msg.UserDefineID, user_define_id );         /* user definition ID */
        Msg.DataType = MBA_DATA_TYPE_TEXT;                  /* data type */
        Msg.Wait_flag = MBA_WM_WAIT;                        /* wait flag */
        Msg.Timeout = timeout;                              /* timeout value */
        Msg.Buf = sin_buf;                                  /* message buffer */
        Msg.MsgSize = strlen( Msg.Buf );                    /* message size */
#if defined(OS400)
        Msg.ShiftCode = MBA_SFT_CUT;                        /* shift code */
        Msg.SpaceMode = MBA_SP_FULL;                        /* space mode */
#endif

        while( 1 ){
                rc = MBPUT( Hconn, Hque, &Msg, sizeof( Msg ), &err );
                if( rc ){ /* if API returns unsuccessfully */
                        if( err.DtlCode == PUT_WAIT_TIMEOUT ){ /* if API timed out */
                                printf( "There is no available space. Retry.\n" );
                                continue;
                        }

                        printf( "MBPUT          : err=%d dtl=%d sys=%d msg=%s\n",
                                err.ErrCode, err.DtlCode, err.SysCode, err.ErrMsg );
                        MBCLOSE( Hconn, Hque, &err );
                        MBDISCON( Hconn, &err );
                        exit( -1 );
                }
                else{
                        break;
                }
        }

/* ---------------------------------------------------------------------------------
     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 + =
减小字号Ctrl + -
显示快捷键?