📄 sendsm.java
字号:
package sample;/* -------------------------------------------------------------------------- Name: SendSM.cpp Title: CMPP API of ISMG for CMPP 1.2 Package: Send short message sample code for Asiainfo CMPP 1.2 API Written: 2000/12/20 Asiainfo Revised: Synopsis: Editor: TAB=4 -----------------------------------------------------------------------------*/import aiismg.jcmppapi30.CMPPAPI;import java.io.*;public final class SendSM { private static final int SEND_MODE_SINGLE = 0; private static final int SEND_MODE_BATCH = 1; public static void main( String argv[] ) { SMArgument pArgs = new SMArgument(); int nOutput = 1; CMPPAPI pCMPPAPI = new CMPPAPI(); int nRepeat = 0; int nSendMode = SEND_MODE_SINGLE; String sArgFile = null; byte[] sMsgID = new byte[21]; int nMsgLen = 0, n; pArgs.sMsgCon = null; if( argv.length < 2 ) { Usage( ); System.exit( 1 ); } // get argument for( n=0; n<argv.length; ++n ) { if( argv[n].compareTo( "-B" ) == 0 ) { nSendMode = SEND_MODE_BATCH; } else if( argv[n].compareTo( "-S" ) == 0 ) { nSendMode = SEND_MODE_SINGLE; } else if( argv[n].compareTo("-r") == 0 ) { n++; nRepeat = Integer.parseInt( argv[n] ); }else if( argv[n].compareTo( "-n" ) == 0 ) { nOutput = 0; } else if( argv[n].compareTo( "-a" ) == 0 ) { n++; if( n >= argv.length ) { if( nOutput == 1 ) { Usage( ); } System.exit( 1 ); } sArgFile = argv[n]; } else { if( nOutput == 1 ) { Usage( ); } System.exit( 1 ); } } if( sArgFile == null ) { if( nOutput == 1 ) Usage( ); System.exit( 1 ); } // read arguments from file if( ReadArgumentFile( sArgFile, pArgs, nOutput ) != 0 ) { if( nOutput == 1 ) { System.out.println( "Fail to read argument from file %s!" +sArgFile ); } System.exit( 1 ); } else { if( nOutput == 1 ) { String sTmp; System.out.println( "need_reply=" + pArgs.nNeedReply ); System.out.println( "msg_level=" + pArgs.nMsgLevel ); sTmp = new String( pArgs.sServiceID ); System.out.println( "serviceid=" + sTmp ); System.out.println( "msg_format=" + pArgs.nMsgFormat ); sTmp = new String( pArgs.sFeeType ); System.out.println( "fee_type=" + sTmp ); sTmp = new String( pArgs.sFeeCode ); System.out.println( "fee_code=" + sTmp ); sTmp = new String( pArgs.sValidTime ); System.out.println( "valid_time=" + sTmp ); sTmp = new String( pArgs.sAtTime ); System.out.println( "at_time=" + sTmp ); sTmp = new String( pArgs.sSrcTermID ); System.out.println( "src_termid=" + sTmp ); sTmp = new String( pArgs.sDestTermID ); System.out.println( "dest_termid=" + sTmp ); sTmp = new String( pArgs.sFeeTermID ); System.out.println( "fee_termid=" + sTmp ); System.out.println( "feeTermtype=" + pArgs.cFeeTermType ); System.out.println( "destTermType=" + pArgs.cDestTermType ); sTmp = new String( pArgs.sLinkID ); System.out.println( "linkID=" + sTmp ); System.out.println( "dest_termid_file=" + pArgs.sDestTermIDFile ); System.out.println( "msg_file=" + pArgs.sMsgFile ); System.out.println( "msgid_file=" + pArgs.sMsgIDFile ); } } // initiate API if( pCMPPAPI.InitCMPPAPI( "../config/javacmppc.ini" ) != 0 ) { if( nOutput == 1 ) { System.out.println( "Fail to call InitCMPPAPI!" ); } System.exit( 1 ); } // get message from file File pMsgFile; FileInputStream pMsgIn; try{ pMsgFile = new File( pArgs.sMsgFile ); pMsgIn = new FileInputStream( pArgs.sMsgFile );/* if( pMsgFile.length() > 160 && pArgs.nMsgFormat == 0 ) nMsgLen = 160; else if( pMsgFile.length() > 140 && pArgs.nMsgFormat == 15 ) nMsgLen = 140; else */ nMsgLen = (int)pMsgFile.length(); if( nMsgLen == 0 ) { if( nOutput == 1 ) System.err.println( "Message file is empty: " + pArgs.sMsgFile ); System.exit(1); } pArgs.sMsgCon = new byte[nMsgLen]; pMsgIn.read( pArgs.sMsgCon, 0, pArgs.sMsgCon.length ); pMsgIn.close(); // remove the '\n' or '\r' on the tail of short message if( pArgs.sMsgCon[nMsgLen - 1] == '\n' || pArgs.sMsgCon[nMsgLen - 1] == '\r' ) { nMsgLen--; if( pArgs.sMsgCon[nMsgLen - 2] == '\n' || pArgs.sMsgCon[nMsgLen - 2] == '\r' ) { nMsgLen--; } } } catch( NullPointerException excpt ) { if( nOutput == 1 ) System.err.println( "Can not get file attr: " + pArgs.sMsgFile ); System.exit(1); } catch( SecurityException excpt ) { if( nOutput == 1 ) System.err.println( "Fail to read file " + pArgs.sMsgFile ); System.exit(1); } catch( FileNotFoundException fnfex ) { if( nOutput == 1 ) System.err.println( "Error: file " + pArgs.sMsgFile + " not found!" ); System.exit(1); } catch( IOException ioexpt ) { if( nOutput == 1 ) System.err.println( "File I/O Error: " + pArgs.sMsgFile ); System.exit(1); } for(int r=0;r<=nRepeat;r++) if( nSendMode == SEND_MODE_SINGLE ) { String sTmp = new String( pArgs.sMsgCon ); if( nOutput == 1 ) { System.out.println( "msg_len=" + nMsgLen ); System.out.println( "msg content=" + sTmp ); } if( pCMPPAPI.CMPPSendSingle( pArgs.nNeedReply, pArgs.nMsgLevel, pArgs.sServiceID, pArgs.nMsgFormat, pArgs.sFeeType, pArgs.sFeeCode, pArgs.sValidTime, pArgs.sAtTime, pArgs.sSrcTermID, pArgs.sDestTermID, nMsgLen, pArgs.sMsgCon, sMsgID, (byte)0, pArgs.sFeeTermID, (byte)0,(byte)0,pArgs.cFeeTermType,pArgs.cDestTermType,pArgs.sLinkID) != 0 ) { if( nOutput == 1 ) { System.out.println( "Fail to call CMPPSendSingle, error=" + pCMPPAPI.GetErrCode() ); } System.exit( 1 ); } else { // write msgid and error code to file FileOutputStream pFileOutput; // Caculate the string length int i, nLen; for( i=0; i < sMsgID.length; i++ ) if( sMsgID[i] == 0 ) break; for( nLen=0; nLen < pArgs.sDestTermID.length; nLen++ ) if( pArgs.sDestTermID[nLen] == 0 ) break; try { pFileOutput = new FileOutputStream( pArgs.sMsgIDFile ); pFileOutput.write( sMsgID, 0, i ); pFileOutput.write( '\t' ); pFileOutput.write( ( Integer.toString( pCMPPAPI.GetErrCode() ) ).getBytes() ); pFileOutput.write( '\t' ); pFileOutput.write(pArgs.sDestTermID,0, nLen ); pFileOutput.write( '\n' ); pFileOutput.close(); } catch( SecurityException excpt ) { if( nOutput == 1 ) System.err.println( "Fail to create file " + pArgs.sMsgIDFile ); System.exit(1); } catch( FileNotFoundException nfcpt ) { if( nOutput == 1 ) System.err.println( "No such file:" + pArgs.sMsgIDFile ); System.exit(1); } catch( IOException excpt ) { if( nOutput == 1 ) System.err.println( "Fail to write file " + pArgs.sMsgIDFile ); System.exit(1); } } } else { if( pCMPPAPI.CMPPSendBatch( pArgs.nNeedReply, pArgs.nMsgLevel, pArgs.sServiceID, pArgs.nMsgFormat, pArgs.sFeeType, pArgs.sFeeCode, pArgs.sValidTime, pArgs.sAtTime, pArgs.sSrcTermID, pArgs.sDestTermIDFile, nMsgLen, pArgs.sMsgCon, pArgs.sMsgIDFile, (byte)0, pArgs.sFeeTermID, (byte)0,(byte)0,pArgs.cFeeTermType,pArgs.cDestTermType,pArgs.sLinkID) != 0 ) { if( nOutput == 1 ) { System.out.println( "Fail to call CMPPSendBatch!" ); } System.exit( 1 ); } } System.exit( 0 ); } public static void Usage( ) { System.out.println( "Usage: SendSM [-B][-S][-n] <-a argument_file>" ); System.out.println( " -B: send one short message to multiple users" ); System.out.println( " -S: send one short message to single user" ); System.out.println( " -a: argument file" ); System.out.println( " -n: no message output" ); System.out.println( "e.g.: SendSM -S -a sm.arg # send single short message defined in file sm.arg" ); System.out.println( "e.g.: SendSM -B -a sm.arg # send batch short message defined in file sm.arg" ); } public static int ReadArgumentFile( String sFile, SMArgument pArgs, int nOutput ) { String sLine; int n, nLen, m; FileInputStream pFileStream; BufferedReader pFileRead; try{ pFileStream = new FileInputStream( sFile ); } catch( SecurityException excpt ) { if( nOutput == 1 ) System.err.println( "Fail to read file " + sFile ); return 1; } catch( FileNotFoundException fnfex ) { if( nOutput == 1 ) System.err.println( "Error: file " + sFile + " not found!" ); return 1; } pFileRead = new BufferedReader( new InputStreamReader( (InputStream)pFileStream ) ); for( n=0; n<17; ++n ) { try{ sLine = pFileRead.readLine(); if( sLine == null ) { if( nOutput == 1 ) { System.out.println( "Fail to read the " + (n+1) +"st parameter in argument file." ); } pFileStream.close(); return 1; } } catch( IOException excpt ) { if( nOutput == 1 ) { System.out.println( "Fail to read the " + (n+1) +"st parameter in argument file." ); } return 1; } switch( n ) { case 0: pArgs.nNeedReply = Byte.parseByte( sLine ); break; case 1: pArgs.nMsgLevel = Byte.parseByte( sLine ); break; case 2: if( sLine.length() > 10 ) { System.arraycopy( sLine.getBytes(), 0, pArgs.sServiceID, 0, 10 ); } else { System.arraycopy( sLine.getBytes(), 0, pArgs.sServiceID, 0, sLine.length() ); } break; case 3: pArgs.nMsgFormat = Byte.parseByte( sLine ); break; case 4: if( sLine.length() > 2 ) { System.arraycopy( sLine.getBytes(), 0, pArgs.sFeeType, 0, 2 ); } else { System.arraycopy( sLine.getBytes(), 0, pArgs.sFeeType, 0, sLine.length() ); } break; case 5: if( sLine.length() > 6 ) { System.arraycopy( sLine.getBytes(), 0, pArgs.sFeeCode, 0, 6 ); } else { System.arraycopy( sLine.getBytes(), 0, pArgs.sFeeCode, 0, sLine.length() ); } break; case 6: if( sLine.length() > 17 ) { System.arraycopy( sLine.getBytes(), 0, pArgs.sValidTime, 0, 17 ); } else { System.arraycopy( sLine.getBytes(), 0, pArgs.sValidTime, 0, sLine.length() ); } break; case 7: if( sLine.length() > 17 ) { System.arraycopy( sLine.getBytes(), 0, pArgs.sAtTime, 0, 17 ); } else { System.arraycopy( sLine.getBytes(), 0, pArgs.sAtTime, 0, sLine.length() ); } break; case 8: if( sLine.length() > 21 ) { System.arraycopy( sLine.getBytes(), 0, pArgs.sSrcTermID, 0, 21 ); } else { System.arraycopy( sLine.getBytes(), 0, pArgs.sSrcTermID, 0, sLine.length() ); } break; case 9: if( sLine.length() > 32 ) { System.arraycopy( sLine.getBytes(), 0, pArgs.sDestTermID, 0, 32 ); } else { System.arraycopy( sLine.getBytes(), 0, pArgs.sDestTermID, 0, sLine.length() ); } break; case 10:/* Fee Terminal ID */ System.arraycopy ( sLine.getBytes(), 0, pArgs.sFeeTermID, 0, ((sLine.length()>32)? 32 : sLine.length()) ) ; break; case 11:/* Fee Terminal type */ pArgs.cFeeTermType = Byte.parseByte ( sLine ); case 12:/* Dest Terminal type */ pArgs.cDestTermType = Byte.parseByte ( sLine ) ; case 13:/* LinkID */ System.arraycopy ( sLine.getBytes(), 0, pArgs.sLinkID, 0, ((sLine.length()>20)? 20 : sLine.length()) ) ; case 14: pArgs.sDestTermIDFile = sLine; break; case 15: pArgs.sMsgFile = sLine; break; case 16: pArgs.sMsgIDFile = sLine; break; } } try{ pFileStream.close(); } catch( IOException excpt ) { System.err.println( "Fail to close file: " + sFile ); } return 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -