⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smpptest.java

📁 Short Message Peer to Peer
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * Copyright (c) 1996-2001
 * Logica Mobile Networks Limited
 * All rights reserved.
 *
 * This software is distributed under Logica Open Source License Version 1.0
 * ("Licence Agreement"). You shall use it and distribute only in accordance
 * with the terms of the License Agreement.
 *
 */
package org.smpp.test;

import java.io.*;
import java.util.Properties;

import org.smpp.*;
import org.smpp.TCPIPConnection;
import org.smpp.pdu.*;
import org.smpp.debug.Debug;
import org.smpp.debug.Event;
import org.smpp.debug.FileDebug;
import org.smpp.debug.FileEvent;
import org.smpp.util.Queue;

/**
 * Class <code>SMPPTest</code> shows how to use the SMPP toolkit.
 * You can bound and unbind from the SMSC, you can send every possible 
 * pdu and wait for a pdu sent from the SMSC.
 *
 * @author Logica Mobile Networks SMPP Open Source Team
 * @version $Revision: 1.2 $
 */
public class SMPPTest {
	static final String copyright =
		"Copyright (c) 1996-2001 Logica Mobile Networks Limited\n"
			+ "This product includes software developed by Logica by whom copyright\n"
			+ "and know-how are retained, all rights reserved.\n";
	static final String version = "SMPP Open Source test & demonstration application, version 1.1\n";

	static {
		System.out.println(copyright);
		System.out.println(version);
	}

	/**
	 * Directory for creating of debug and event files.
	 */
	static final String dbgDir = "./";

	/**
	 * The debug object.
	 * @see FileDebug
	 */
	static Debug debug = new FileDebug(dbgDir, "test.dbg");

	/**
	 * The event object.
	 * @see FileEvent
	 */
	static Event event = new FileEvent(dbgDir, "test.evt");

	/**
	 * File with default settings for the application.
	 */
	static String propsFilePath = "./etc/smpptest.cfg";

	static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

	/**
	 * This is the SMPP session used for communication with SMSC.
	 */
	static Session session = null;

	/**
	 * Contains the parameters and default values for this test
	 * application such as system id, password, default npi and ton
	 * of sender etc.
	 */
	Properties properties = new Properties();

	/**
	 * If the application is bound to the SMSC.
	 */
	boolean bound = false;

	/**
	 * If the application has to keep reading commands
	 * from the keyboard and to do what's requested.
	 */
	private boolean keepRunning = true;

	/**
	 * Address of the SMSC.
	 */
	String ipAddress = null;

	/**
	 * The port number to bind to on the SMSC server.
	 */
	int port = 0;

	/**
	 * The name which identifies you to SMSC.
	 */
	String systemId = null;

	/**
	 * The password for authentication to SMSC.
	 */
	String password = null;

	/**
	 * How you want to bind to the SMSC: transmitter (t), receiver (r) or
	 * transciever (tr). Transciever can both send messages and receive
	 * messages. Note, that if you bind as receiver you can still receive
	 * responses to you requests (submissions).
	 */
	String bindOption = "t";

	/**
	 * Indicates that the Session has to be asynchronous.
	 * Asynchronous Session means that when submitting a Request to the SMSC
	 * the Session does not wait for a response. Instead the Session is provided
	 * with an instance of implementation of ServerPDUListener from the smpp
	 * library which receives all PDUs received from the SMSC. It's
	 * application responsibility to match the received Response with sended Requests.
	 */
	boolean asynchronous = false;

	/**
	 * This is an instance of listener which obtains all PDUs received from the SMSC.
	 * Application doesn't have explicitly call Session's receive() function,
	 * all PDUs are passed to this application callback object.
	 * See documentation in Session, Receiver and ServerPDUEventListener classes
	 * form the SMPP library.
	 */
	SMPPTestPDUEventListener pduListener = null;

	/**
	 * The range of addresses the smpp session will serve.
	 */
	AddressRange addressRange = new AddressRange();

	/*
	 * for information about these variables have a look in SMPP 3.4
	 * specification
	 */
	String systemType = "";
	String serviceType = "";
	Address sourceAddress = new Address();
	Address destAddress = new Address();
	String scheduleDeliveryTime = "";
	String validityPeriod = "";
	String shortMessage = "";
	int numberOfDestination = 1;
	String messageId = "";
	byte esmClass = 0;
	byte protocolId = 0;
	byte priorityFlag = 0;
	byte registeredDelivery = 0;
	byte replaceIfPresentFlag = 0;
	byte dataCoding = 0;
	byte smDefaultMsgId = 0;

	/**
	 * If you attemt to receive message, how long will the application
	 * wait for data.
	 */
	long receiveTimeout = Data.RECEIVE_BLOCKING;

	/**
	 * Initialises the application, lods default values for
	 * connection to SMSC and for various PDU fields.
	 */
	public SMPPTest() throws IOException {
		loadProperties(propsFilePath);
	}

	/**
	 * Sets global SMPP library debug and event objects.
	 * Runs the application.
	 * @see SmppObject#setDebug(Debug)
	 * @see SmppObject#setEvent(Event)
	 */
	public static void main(String args[]) {
		System.out.println("Initialising...");
		System.out.println("Charset used: " + new InputStreamReader(System.in).getEncoding());
		debug.activate();
		event.activate();
		SmppObject.setDebug(debug);
		SmppObject.setEvent(event);
		SMPPTest test = null;
		try {
			test = new SMPPTest();
		} catch (IOException e) {
			event.write(e, "");
			debug.write("exception initialising SMPPTest " + e);
			System.out.println("Exception initialising SMPPTest " + e);
		}
		if (test != null) {
			test.menu();
		}
	}

	/**
	 * Displays the menu and lets you choose from available options.
	 */
	public void menu() {
		keepRunning = true;
		String option = "1";
		int optionInt;

		while (keepRunning) {
			System.out.println();
			System.out.println("-  1 bind");
			System.out.println("-  2 submit (t/tr)");
			System.out.println("-  3 submit multi (t/tr)");
			System.out.println("-  4 data (t/tr)");
			System.out.println("-  5 query (t/tr)");
			System.out.println("-  6 replace (t/tr)");
			System.out.println("-  7 cancel (t/tr)");
			System.out.println("-  8 enquire link (t/tr)");
			System.out.println("-  9 unbind");
			System.out.println("- 10 receive message (tr/r)");
			System.out.println("-  0 exit");
			System.out.print("> ");
			optionInt = -1;
			try {
				option = keyboard.readLine();
				optionInt = Integer.parseInt(option);
			} catch (Exception e) {
				debug.write("exception reading keyboard " + e);
				optionInt = -1;
			}
			switch (optionInt) {
				case 1 :
					bind();
					break;
				case 2 :
					submit();
					break;
				case 3 :
					submitMulti();
					break;
				case 4 :
					data();
					break;
				case 5 :
					query();
					break;
				case 6 :
					replace();
					break;
				case 7 :
					cancel();
					break;
				case 8 :
					enquireLink();
					break;
				case 9 :
					unbind();
					break;
				case 10 :
					receive();
					break;
				case 0 :
					exit();
					break;
				case -1 :
					// default option if entering an option went wrong
					break;
				default :
					System.out.println("Invalid option. Choose between 0 and 10.");
					break;
			}
		}
	}

	/**
	 * The first method called to start communication
	 * betwen an ESME and a SMSC. A new instance of <code>TCPIPConnection</code>
	 * is created and the IP address and port obtained from user are passed
	 * to this instance. New <code>Session</code> is created which uses the created
	 * <code>TCPIPConnection</code>.
	 * All the parameters required for a bind are set to the <code>BindRequest</code>
	 * and this request is passed to the <code>Session</code>'s <code>bind</code>
	 * method. If the call is successful, the application should be bound to the SMSC.
	 *
	 * See "SMPP Protocol Specification 3.4, 4.1 BIND Operation."
	 * @see BindRequest
	 * @see BindResponse
	 * @see TCPIPConnection
	 * @see Session#bind(BindRequest)
	 * @see Session#bind(BindRequest,ServerPDUEventListener)
	 */
	private void bind() {
		debug.enter(this, "SMPPTest.bind()");
		try {

			if (bound) {
				System.out.println("Already bound, unbind first.");
				return;
			}

			BindRequest request = null;
			BindResponse response = null;
			String syncMode = (asynchronous ? "a" : "s");

			// type of the session
			syncMode = getParam("Asynchronous/Synchronnous Session? (a/s)", syncMode);
			if (syncMode.compareToIgnoreCase("a") == 0) {
				asynchronous = true;
			} else if (syncMode.compareToIgnoreCase("s") == 0) {
				asynchronous = false;
			} else {
				System.out.println(
					"Invalid mode async/sync, expected a or s, got " + syncMode + ". Operation canceled.");
				return;
			}

			// input values
			bindOption = getParam("Transmitter/Receiver/Transciever (t/r/tr)", bindOption);

			if (bindOption.compareToIgnoreCase("t") == 0) {
				request = new BindTransmitter();
			} else if (bindOption.compareToIgnoreCase("r") == 0) {
				request = new BindReceiver();
			} else if (bindOption.compareToIgnoreCase("tr") == 0) {
				request = new BindTransciever();
			} else {
				System.out.println(
					"Invalid bind mode, expected t, r or tr, got " + bindOption + ". Operation canceled.");
				return;
			}

			ipAddress = getParam("IP address of SMSC", ipAddress);
			port = getParam("Port number", port);

			TCPIPConnection connection = new TCPIPConnection(ipAddress, port);
			connection.setReceiveTimeout(20 * 1000);
			session = new Session(connection);

			systemId = getParam("Your system ID", systemId);
			password = getParam("Your password", password);

			// set values
			request.setSystemId(systemId);
			request.setPassword(password);
			request.setSystemType(systemType);
			request.setInterfaceVersion((byte) 0x34);
			request.setAddressRange(addressRange);

			// send the request
			System.out.println("Bind request " + request.debugString());
			if (asynchronous) {
				pduListener = new SMPPTestPDUEventListener(session);
				response = session.bind(request, pduListener);
			} else {
				response = session.bind(request);
			}
			System.out.println("Bind response " + response.debugString());
			if (response.getCommandStatus() == Data.ESME_ROK) {
				bound = true;
			}

		} catch (Exception e) {
			event.write(e, "");
			debug.write("Bind operation failed. " + e);
			System.out.println("Bind operation failed. " + e);
		} finally {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -