www.pudn.com > yidong.rar > ThreadSend.java


package sample;
/**
 * @File: ThreadSend.java
 * @Description: Thread Test for CMPP2.0, MT sent thread test
 * @Copyright: Copyright 2002 By AsiaInfo(c) ISMG. All right reserved
 * @Date: 2002-10-24
 * @Version 1.0
 */

import aiismg.jcmppapi30.CMPPAPI;
import aiismg.jcmppapi30.CMPPDeliverResp;
import java.io.*;

/**
 * @Description: a multi thread implementation class of MT send, for testing the
 *  function CMPPSendSingle(int, CMPPDeliverResp) of cmppapi2.0
 * @Note: Now the class is not supported CMPPSendBatch() now
 */
public class ThreadSend implements Runnable
{
  int ciErrorCode;
  public CMPPAPI loThreadTest = new CMPPAPI() ;
  SMArgument loSendArg = new SMArgument();


  /**
   * @Destination: parse the argument file at sending MT
   * @Return: int value; if successful, the value is larger than zero,it means the
   *  sending content length, else if less than or equal zero,means parsed file failed
   * @Note: the argument aoSendArg should not be null
   */
  public static int ciParseFile ( SMArgument aoSendArg )
  {
    if ( aoSendArg == null ) {
      System.out.println ( "Error: aoSendArg is null" ) ;
      return -1 ;
    }
    int liMsgLen = 0 ;
    String lsArgFile = null ;
    if ( (lsArgFile = ThreadTest.csGetArgFile() ) == null ) {
      System.out.println ( "Error: Get argument file failed" ) ;
      return -1 ;
    }   
    String lsLine;
    int i;
    FileInputStream loFileStream;
    BufferedReader loFileRead;
  
    try {
      loFileStream = new FileInputStream ( lsArgFile ) ;
      loFileRead = new BufferedReader ( new InputStreamReader ( (InputStream)loFileStream ) ) ;
      for ( i=0; i<17; i++ ) {
        lsLine = loFileRead.readLine() ;
        if ( lsLine == null ) {
          System.out.println ( "Error: read line " + i + " from file failed" ) ;
          liMsgLen = -1 ;
          break ;
        }
        switch ( i ) {
          case 0: /* Need Reply */
            aoSendArg.nNeedReply = Byte.parseByte ( lsLine ) ;
            break ;
          case 1: /* Msg Level */
            aoSendArg.nMsgLevel = Byte.parseByte ( lsLine ) ;
            break ;
          case 2: /* Service ID */
            System.arraycopy ( lsLine.getBytes(), 0, aoSendArg.sServiceID, 0, ((lsLine.length()>10)? 10 : lsLine.length()) ) ;
            break ;
          case 3: /* Msg Format */
            aoSendArg.nMsgFormat = Byte.parseByte ( lsLine ) ;
            break ;
          case 4: /* Fee Type */
            System.arraycopy ( lsLine.getBytes(), 0, aoSendArg.sFeeType, 0, ((lsLine.length()>2)? 2 : lsLine.length()) ) ;
            break ;
          case 5: /* Fee Code */
            System.arraycopy ( lsLine.getBytes(), 0, aoSendArg.sFeeCode, 0, ((lsLine.length()>6)? 6 : lsLine.length()) ) ;
            break ;
          case 6: /* Valid Time */
            System.arraycopy ( lsLine.getBytes(), 0, aoSendArg.sValidTime, 0, ((lsLine.length()>17)? 17 : lsLine.length()) ) ;
            break ;
          case 7: /* At Time */
            System.arraycopy ( lsLine.getBytes(), 0, aoSendArg.sAtTime, 0, ((lsLine.length()>17)? 17 : lsLine.length()) ) ;
            break ;
          case 8: /* Source Terminal ID */
            System.arraycopy ( lsLine.getBytes(), 0, aoSendArg.sSrcTermID, 0, ((lsLine.length()>21)? 21 : lsLine.length()) ) ;
            break ;
          case 9: /* Destination Terminal ID */
            System.arraycopy ( lsLine.getBytes(), 0, aoSendArg.sDestTermID, 0, ((lsLine.length()>32)? 32 : lsLine.length()) ) ;
            break ;
          case 10:/* Fee Terminal ID */
            System.arraycopy ( lsLine.getBytes(), 0, aoSendArg.sFeeTermID, 0, ((lsLine.length()>32)? 32 : lsLine.length()) ) ;
          case 11:/* Fee Terminal type */
            aoSendArg.cFeeTermType = Byte.parseByte ( lsLine );
            
          case 12:/* Dest Terminal type */
          	aoSendArg.cDestTermType = Byte.parseByte ( lsLine ) ;          	
          case 13:/* LinkID */
            System.arraycopy ( lsLine.getBytes(), 0, aoSendArg.sLinkID, 0, ((lsLine.length()>20)? 20 : lsLine.length()) ) ;      
          case 14: /* Destination Terminal ID file */
            aoSendArg.sDestTermIDFile = lsLine ;
            break ;
          case 15: /* Msg File */
            aoSendArg.sMsgFile = lsLine ;
            break ;
          case 16: /* Msg ID File */
            aoSendArg.sMsgIDFile = lsLine ;
            break ;
          default:
            System.out.println ( "Error: unbelievable, illegal i value:" + i ) ;
            liMsgLen = -1;
            break ;
        } // end switch ( i )
      } //end for i=0; i<13 
      loFileRead.close();
      loFileStream.close();
      if ( liMsgLen == -1 ) {
        return liMsgLen ;     
      }

      // get msg content from file
      try {
        String lsTotalLine = "" ;
        loFileStream = new FileInputStream ( aoSendArg.sMsgFile ) ;
        loFileRead = new BufferedReader ( new InputStreamReader(loFileStream) ) ;
        
        while ( (lsLine = loFileRead.readLine()) != null ) {
          lsTotalLine = lsTotalLine + lsLine ;
        }
        liMsgLen = (lsTotalLine.length() < 160 )? lsTotalLine.length() : 160 ;
        aoSendArg.sMsgCon = new byte[liMsgLen + 1] ;
        System.arraycopy (lsTotalLine.getBytes(), 0, aoSendArg.sMsgCon, 0, liMsgLen);
      } catch ( NullPointerException e ) {
        System.out.println ( "Error: Get File attribute failed, NullPointerException "
            + e.toString() ) ;
        liMsgLen = -1 ;
      } catch ( SecurityException e ) {
        System.out.println ( "Error: Failed to read file, SecurityException"
            + e.toString() ) ;
        liMsgLen = -1 ;
      } catch ( FileNotFoundException e ) {
        System.out.println ( "Error: Failed to read file, FileNotFoundException" 
            + e.toString() ) ;
        liMsgLen = -1 ;
      } catch ( IOException e ) {
        System.out.println ( "Error: I/O failed, IOException" + e.toString() ) ;
        liMsgLen = -1 ;
      }
      //end of get msg content from msg file
    } catch ( FileNotFoundException e ) {
      System.out.println ( "Error: Argument file open failed,FileNotFoundException:" +
          e.toString() ) ;
      liMsgLen = -1 ;
    } catch ( IOException e ) {
      System.out.println ( "Error: Argument file open failed,IOException:" + 
          e.toString() ) ;
      liMsgLen = -1 ;
    } catch ( SecurityException e ) {
      System.out.println ( "Error: Argument file open failed,SecurityException:" + 
          e.toString() ) ;
      liMsgLen = -1 ;
    }
    return liMsgLen ;
  } //end ciParseFile


  /**
   * MT sent thread run method
   */
  public void run()
  {
    int i;
    int liMsgLen ;
    byte[] lsMsgID = new byte[20+1];
    System.out.println(Thread.currentThread().getName());
    if ( (liMsgLen = ciParseFile (loSendArg)) <= 0 ) {
      System.out.println ( "Error: Parse file failed" ) ;
      return ;
    }
    int liThreadLoop = ThreadTest.ciGetThreadLoop() ;
    for (i=0;i