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


package sample;
/*  --------------------------------------------------------------------------
    Name:       RecvSM.cpp
    Title:      CMPP API of ISMG for CMPP 1.1
    Package:    Receive short message sample code for Asiainfo CMPP 1.1 API

    Written:    2000/12/20  Asiainfo
    Revised:    

    Synopsis:
    Editor:     TAB = 4
 -----------------------------------------------------------------------------*/

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

public class RecvSM {

	public static void main( String argv[] )
	{
		String sParamFile = null, sMsgFile = null;
		int		nOutput = 1;
		int			n;
		int			nTimeout = 0, nFunType = 0;
		CMPPDeliverResp theDeliverResp = new CMPPDeliverResp();
		CMPPAPI pCMPPAPI = new CMPPAPI();
		int 		nRepeat = 0;	
		// get argument
		for( n = 0; n < argv.length; n++ ) {
			if( argv[n].compareTo( "-n" ) == 0 ) {
	      nOutput = 0;
			} else if( argv[n].compareTo( "-t" ) == 0 ) {
				n++;
	      nTimeout = Integer.parseInt( argv[n] );
			} else if( argv[n].compareTo( "-p" ) == 0 ) {
				n++;
	      sParamFile = argv[n];
			} else if( argv[n].compareTo( "-r" ) ==0 ) {
			  n++;
			 nRepeat = Integer.parseInt( argv[n] );		
			} else if( argv[n].compareTo( "-m" ) == 0 ) {
				n++;
	      sMsgFile = argv[n];
			} else {
				if( nOutput == 1 ) {
					Usage( );
				}
				System.exit( 1 );
			}
		}
		
		if( sMsgFile == null || sParamFile == null ) {
			if( nOutput == 1 ) {
				System.out.println( "No parameter file or message file!" );
			}
			System.exit( 1 );	
		}
		
		// initiate API
		if( pCMPPAPI.InitCMPPAPI( "../config/javacmppc.ini" ) != 0 ) {
			if( nOutput == 1 ) {
				System.out.println( "Fail to call InitCMPPAPI!" );
			}
			System.exit( 1 );	
		}
		for(int r=0; r<= nRepeat; r++ )	
		if( pCMPPAPI.CMPPDeliver( nTimeout, theDeliverResp ) != 0 ) {
			if( nOutput == 1 ) {
				System.out.println( "Fail to call CMPPDeliver!" );
			}
			System.exit( 1 );
		}

		// write receiving short message into files
		FileOutputStream pFile;
		try{
			int nLen;
			String sTmpBuff;
			pFile = new FileOutputStream( sParamFile );
			pFile.write( theDeliverResp.sMsgID.getBytes() );
			pFile.write( '\n' );
			sTmpBuff = Byte.toString( theDeliverResp.nMsgLevel );
			pFile.write( sTmpBuff.getBytes() );
			for( nLen = 0; nLen < theDeliverResp.sServiceID.length; nLen++ )
				if( theDeliverResp.sServiceID[nLen] == 0 )
					break;
			pFile.write( '\n' );
			pFile.write( theDeliverResp.sServiceID, 0, nLen );
//			pFile.write( theDeliverResp.sServiceID );
			pFile.write( '\n' );
			sTmpBuff = Byte.toString( theDeliverResp.nMsgFormat );
			pFile.write( sTmpBuff.getBytes() );
			pFile.write( '\n' );
			for( nLen = 0; nLen < theDeliverResp.sSrcTermID.length; nLen++ )
				if( theDeliverResp.sSrcTermID[nLen] == 0 )
					break;
			pFile.write( theDeliverResp.sSrcTermID, 0, nLen );
//			pFile.write( theDeliverResp.sSrcTermID );
			pFile.write( '\n' );
			sTmpBuff = Byte.toString( theDeliverResp.nIsReply );
			pFile.write( sTmpBuff.getBytes() );
			pFile.write( '\n' );
			sTmpBuff = Integer.toString( theDeliverResp.nMsgLen );
			pFile.write( sTmpBuff.getBytes() );
			pFile.write( '\n' );
			for( nLen = 0; nLen < theDeliverResp.sDestTermID.length; nLen++ )
				if( theDeliverResp.sDestTermID[nLen] == 0 )
					break;
			pFile.write( theDeliverResp.sDestTermID, 0, nLen );
			pFile.write( '\n' );
			if( nFunType == 1 )
			{
				for( nLen = 0; nLen < theDeliverResp.sDestTermID.length; nLen++ )
					if( theDeliverResp.sDestTermID[nLen] == 0 )
						break;
				pFile.write( theDeliverResp.sDestTermID, 0, nLen );
//				pFile.write( theDeliverResp.sDestTermID );
				pFile.write( '\n' );
			}
			pFile.close();
		} catch( SecurityException excpt ) {
			 if( nOutput == 1 )
			   System.err.println( "Fail to create file " + sParamFile );
			 System.exit(1);
		} catch( FileNotFoundException nfcpt ) {
			 if( nOutput == 1 )
			   System.err.println( "No such file:" + sParamFile );
			 System.exit(1);
		} catch( IOException excpt ) {
			if( nOutput == 1 )
			  System.err.println( "Fail to write file " + sParamFile );
			System.exit(1);
		}

		try{
			pFile = new FileOutputStream( sMsgFile );
			if( theDeliverResp.nMsgLen > 160 )
				theDeliverResp.nMsgLen = 160;
			pFile.write( theDeliverResp.sMsgContent, 0, theDeliverResp.nMsgLen );
			pFile.close();
		} catch( SecurityException excpt ) {
			 if( nOutput == 1 )
			   System.err.println( "Fail to create file " + sMsgFile );
			 System.exit(1);
		} catch( FileNotFoundException nfcpt ) {
			 if( nOutput == 1 )
			   System.err.println( "No such file:" + sMsgFile );
			 System.exit(1);
		} catch( IOException excpt ) {
			if( nOutput == 1 )
			  System.err.println( "Fail to write file " + sMsgFile );
			System.exit(1);
		}
	
		System.exit( 0 );
	}
	
	public static void Usage( )
	{
		System.out.println( "Usage: RecvSM [-n] [-f function] <-t timeout> <-p parameter_file> <-m message_file>" );
		System.out.println( "       -n: no message output" );
		System.out.println( "       -t: waiting time, in second. 0 means wait forever." );	
		System.out.println( "       -p: receiving short message parameter file name." );
		System.out.println( "       -m: receiving short message content file name." );
		System.out.println( "If call CMPPDeliverCallback, RecvSM will receive MO messages continuously until user break." );
		System.out.println( "e.g.:  RecvSM -t 10 -p sm.par -m sm.txt" );
	}
	
}