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

📄 smppclient.java

📁 This is my first class to beginning with smpp. This is a smpp client with visual (not console as SMP
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
				if (asynchronous) {
					session.submit(request);
					System.out.println();
				} else {
					response = session.submit(request);
					System.out.println("Submit response "
							+ response.debugString());
					addStatus("Submit response " + response.debugString());
					messageId = response.getMessageId();
				}
			}

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

	private void exit() {
		debug.enter(this, "SMPPTest.exit()");
		if (bound) {
			unbind();
		}
		keepRunning = false;
		debug.exit(this);
	}

	/**
	 * Implements simple PDU listener which handles PDUs received from SMSC. It
	 * puts the received requests into a queue and discards all received
	 * responses. Requests then can be fetched (should be) from the queue by
	 * calling to the method <code>getRequestEvent</code>.
	 * 
	 * @see Queue
	 * @see ServerPDUEvent
	 * @see ServerPDUEventListener
	 * @see SmppObject
	 */
	private class SMPPTestPDUEventListener extends SmppObject implements
			ServerPDUEventListener {
		Session session;

		Queue requestEvents = new Queue();

		public SMPPTestPDUEventListener(Session session) {
			this.session = session;
		}

		public void handleEvent(ServerPDUEvent event) {
			PDU pdu = event.getPDU();
			if (pdu.isRequest()) {
				System.out.println("async request received, enqueuing "
						+ pdu.debugString());
				addStatus("async request received, enqueuing "
						+ pdu.debugString());
				synchronized (requestEvents) {
					requestEvents.enqueue(event);
					requestEvents.notify();
				}
			} else if (pdu.isResponse()) {
				System.out.println("async response received "
						+ pdu.debugString());
				addStatus("async response received " + pdu.debugString());
			} else {
				System.out
						.println("pdu of unknown class (not request nor "
								+ "response) received, discarding "
								+ pdu.debugString());
				addStatus("pdu of unknown class (not request nor "
						+ "response) received, discarding " + pdu.debugString());
			}
		}

		/**
		 * Returns received pdu from the queue. If the queue is empty, the
		 * method blocks for the specified timeout.
		 */
		public ServerPDUEvent getRequestEvent(long timeout) {
			ServerPDUEvent pduEvent = null;
			synchronized (requestEvents) {
				if (requestEvents.isEmpty()) {
					try {
						requestEvents.wait(timeout);
					} catch (InterruptedException e) {
						// ignoring, actually this is what we're waiting for
					}
				}
				if (!requestEvents.isEmpty()) {
					pduEvent = (ServerPDUEvent) requestEvents.dequeue();
				}
			}
			return pduEvent;
		}
	}

	/**
	 * Prompts the user to enter a string value for a parameter.
	 */
	private String getParam(JTextField textField, String defaultValue) {
		String value = "";
		value = textField.getText();
		if (value.compareTo("") == 0) {
			textField.setText(defaultValue);
			return defaultValue;
		} else {
			return value;
		}
	}

	/**
	 * Prompts the user to enter a byte value for a parameter.
	 */
	private byte getParam(JTextField textField, byte defaultValue) {
		return Byte.parseByte(getParam(textField, Byte.toString(defaultValue)));
	}

	/**
	 * Prompts the user to enter an integer value for a parameter.
	 */
	private int getParam(JTextField textField, int defaultValue) {
		return Integer.parseInt(getParam(textField, Integer
				.toString(defaultValue)));
	}

	/**
	 * Prompts the user to enter an address value with specified max length.
	 */
	private Address getAddress(JTextField ton, JTextField npi, JTextField addr,
			Address address, int maxAddressLength)
			throws WrongLengthOfStringException {
		byte tonA = getParam(ton, address.getTon());
		byte npiA = getParam(npi, address.getNpi());
		String addrA = getParam(addr, address.getAddress());
		address.setTon(tonA);
		address.setNpi(npiA);
		address.setAddress(addrA, maxAddressLength);
		return address;
	}

	/**
	 * Prompts the user to enter an address value with max length set to the
	 * default length Data.SM_ADDR_LEN.
	 */
	private Address getAddress(JTextField ton, JTextField npi, JTextField addr,
			Address address) throws WrongLengthOfStringException {
		return getAddress(ton, npi, addr, address, Data.SM_ADDR_LEN);
	}

	/**
	 * Loads configuration parameters from the file with the given name. Sets
	 * private variable to the loaded values.
	 */
	private void loadProperties(String fileName) throws IOException {
		System.out.println("Reading configuration file " + fileName + "...");
		addStatus("Reading configuration file " + fileName + "...");
		FileInputStream propsFile = new FileInputStream(fileName);
		properties.load(propsFile);
		propsFile.close();
		System.out.println("Setting default parameters...");
		addStatus("Setting default parameters...");
		byte ton;
		byte npi;
		String addr;
		String bindMode;
		int rcvTimeout;
		String syncMode;

		ipAddress = properties.getProperty("ip-address");
		port = getIntProperty("port", port);
		systemId = properties.getProperty("system-id");
		password = properties.getProperty("password");

		ton = getByteProperty("addr-ton", addressRange.getTon());
		npi = getByteProperty("addr-npi", addressRange.getNpi());
		addr = properties.getProperty("address-range", addressRange
				.getAddressRange());
		addressRange.setTon(ton);
		addressRange.setNpi(npi);
		try {
			addressRange.setAddressRange(addr);
		} catch (WrongLengthOfStringException e) {
			System.out
					.println("The length of address-range parameter is wrong.");
			addStatus("The length of address-range parameter is wrong.");
		}

		ton = getByteProperty("source-ton", sourceAddress.getTon());
		npi = getByteProperty("source-npi", sourceAddress.getNpi());
		addr = properties.getProperty("source-address", sourceAddress
				.getAddress());
		setAddressParameter("source-address", sourceAddress, ton, npi, addr);

		ton = getByteProperty("destination-ton", destAddress.getTon());
		npi = getByteProperty("destination-npi", destAddress.getNpi());
		addr = properties.getProperty("destination-address", destAddress
				.getAddress());
		setAddressParameter("destination-address", destAddress, ton, npi, addr);

		serviceType = properties.getProperty("service-type", serviceType);
		systemType = properties.getProperty("system-type", systemType);
		bindMode = properties.getProperty("bind-mode", bindOption);
		if (bindMode.equalsIgnoreCase("transmitter")) {
			bindMode = "t";
		} else if (bindMode.equalsIgnoreCase("receiver")) {
			bindMode = "r";
		} else if (bindMode.equalsIgnoreCase("transciever")) {
			bindMode = "tr";
		} else if (!bindMode.equalsIgnoreCase("t")
				&& !bindMode.equalsIgnoreCase("r")
				&& !bindMode.equalsIgnoreCase("tr")) {
			System.out.println("The value of bind-mode parameter in "
					+ "the configuration file " + fileName + " is wrong. "
					+ "Setting the default");
			addStatus("The value of bind-mode parameter in "
					+ "the configuration file " + fileName + " is wrong. "
					+ "Setting the default");
			bindMode = "t";
		}
		bindOption = bindMode;

		// receive timeout in the cfg file is in seconds, we need milliseconds
		// also conversion from -1 which indicates infinite blocking
		// in the cfg file to Data.RECEIVE_BLOCKING which indicates infinite
		// blocking in the library is needed.
		if (receiveTimeout == Data.RECEIVE_BLOCKING) {
			rcvTimeout = -1;
		} else {
			rcvTimeout = ((int) receiveTimeout) / 1000;
		}
		rcvTimeout = getIntProperty("receive-timeout", rcvTimeout);
		if (rcvTimeout == -1) {
			receiveTimeout = Data.RECEIVE_BLOCKING;
		} else {
			receiveTimeout = rcvTimeout * 1000;
		}

		syncMode = properties.getProperty("sync-mode", (asynchronous ? "async"
				: "sync"));
		if (syncMode.equalsIgnoreCase("sync")) {
			asynchronous = false;
		} else if (syncMode.equalsIgnoreCase("async")) {
			asynchronous = true;
		} else {
			asynchronous = false;
		}

		/*
		 * scheduleDeliveryTime validityPeriod shortMessage numberOfDestination
		 * messageId esmClass protocolId priorityFlag registeredDelivery
		 * replaceIfPresentFlag dataCoding smDefaultMsgId
		 */
	}

	/**
	 * Gets a property and converts it into byte.
	 */
	private byte getByteProperty(String propName, byte defaultValue) {
		return Byte.parseByte(properties.getProperty(propName, Byte
				.toString(defaultValue)));
	}

	/**
	 * Gets a property and converts it into integer.
	 */
	private int getIntProperty(String propName, int defaultValue) {
		return Integer.parseInt(properties.getProperty(propName, Integer
				.toString(defaultValue)));
	}

	/**
	 * Sets attributes of <code>Address</code> to the provided values.
	 */
	private void setAddressParameter(String descr, Address address, byte ton,
			byte npi, String addr) {
		address.setTon(ton);
		address.setNpi(npi);
		try {
			address.setAddress(addr);
		} catch (WrongLengthOfStringException e) {
			System.out.println("The length of " + descr
					+ " parameter is wrong.");
			addStatus("The length of " + descr + " parameter is wrong.");
		}
	}

	/**
	 * Receives one PDU of any type from SMSC and prints it on the screen.
	 * 
	 * @see Session#receive()
	 * @see Response
	 * @see ServerPDUEvent
	 */
	private void receive() {
		debug.enter(this, "SMPPTest.receive()");
		try {

			PDU pdu = null;
			System.out.print("Going to receive a PDU. ");
			addStatus("Going to receive a PDU. ");
			if (receiveTimeout == Data.RECEIVE_BLOCKING) {
				System.out
						.print("The receive is blocking, i.e. the application "
								+ "will stop until a PDU will be received.");
				addStatus("The receive is blocking, i.e. the application "
						+ "will stop until a PDU will be received.");
			} else {
				System.out.print("The receive timeout is " + receiveTimeout
						/ 1000 + " sec.");
				addStatus("The receive timeout is " + receiveTimeout / 1000
						+ " sec.");
			}
			System.out.println();
			if (asynchronous) {
				ServerPDUEvent pduEvent = pduListener
						.getRequestEvent(receiveTimeout);
				if (pduEvent != null) {
					pdu = pduEvent.getPDU();
				}
			} else {
				pdu = session.receive(receiveTimeout);
			}
			if (pdu != null) {
				System.out.println("Received PDU " + pdu.debugString());
				System.out.println(((SubmitSM)((Request)pdu)).getShortMessage());
				addStatus("Received PDU " + pdu.debugString());
				
				if (pdu.isRequest()) {
					Response response = ((Request) pdu).getResponse();
					// respond with default response
					System.out
							.println("Going to send default response to request "
									+ response.debugString());
					addStatus("Going to send default response to request "
							+ response.debugString());
					session.respond(response);
				}
			} else {
				System.out.println("No PDU received this time.");
				addStatus("No PDU received this time.");
			}

		} catch (Exception e) {
			event.write(e, "");
			debug.write("Receiving failed. " + e);
			System.out.println("Receiving failed. " + e);
			addStatus("Receiving failed. " + e);
		} finally {
			debug.exit(this);
		}
	}

	/**
	 * This method initializes receiveButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getReceiveButton() {
		if (receiveButton == null) {
			receiveButton = new JButton();
			receiveButton.setBounds(new Rectangle(119, 408, 86, 23));
			receiveButton.setText("Receive");
			receiveButton
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							// System.out.println("actionPerformed()"); // TODO
							// Auto-generated Event stub actionPerformed()
							receive();
						}
					});
		}
		return receiveButton;
	}

	/**
	 * This method initializes syncModeTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getSyncModeTextField() {
		if (syncModeTextField == null) {
			syncModeTextField = new JTextField();
			syncModeTextField.setBounds(new Rectangle(278, 86, 60, 23));
			syncModeTextField.setText("a");
			syncModeTextField.setToolTipText("'a', 's'");
		}
		return syncModeTextField;
	}

	/**
	 * This method initializes bindOptionTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getBindOptionTextField() {
		if (bindOptionTextField == null) {
			bindOptionTextField = new JTextField();
			bindOptionTextField.setBounds(new Rectangle(347, 86, 61, 23));
			bindOptionTextField.setText("tr");
			bindOptionTextField.setToolTipText("'t', 'r', 'tr'");
		}
		return bindOptionTextField;
	}

	/**
	 * This method initializes timesSubmitTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTimesSubmitTextField() {
		if (timesSubmitTextField == null) {
			timesSubmitTextField = new JTextField();
			timesSubmitTextField.setBounds(new Rectangle(375, 407, 106, 22));
			timesSubmitTextField.setText("1");
		}
		return timesSubmitTextField;
	}

	/**
	 * This method initializes sourceTonTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getSourceTonTextField() {
		if (sourceTonTextField == null) {
			sourceTonTextField = new JTextField();
			sourceTonTextField.setBounds(new Rectangle(45, 305, 65, 20));
		}
		return sourceTonTextField;
	}

	/**
	 * This method initializes sourceNpiTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getSourceNpiTextField() {
		if (sourceNpiTextField == null) {
			sourceNpiTextField = new JTextField();
			sourceNpiTextField.setBounds(new Rectangle(160, 303, 65, 22));
		}
		return sourceNpiTextField;
	}

	/**
	 * This method initializes destTonTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getDestTonTextField() {
		if (destTonTextField == null) {
			destTonTextField = new JTextField();
			destTonTextField.setBounds(new Rectangle(45, 373, 63, 20));
		}
		return destTonTextField;
	}

	/**
	 * This method initializes destNpiTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getDestNpiTextField() {
		if (destNpiTextField == null) {
			destNpiTextField = new JTextField();
			destNpiTextField.setBounds(new Rectangle(158, 372, 67, 20));
		}
		return destNpiTextField;
	}

	/**
	 * This method initializes messageTextPane
	 * 
	 * @return javax.swing.JTextPane
	 */
	private JTextPane getMessageTextPane() {
		if (messageTextPane == null) {
			messageTextPane = new JTextPane();
			messageTextPane.setBounds(new Rectangle(15, 149, 209, 99));
		}
		return messageTextPane;
	}

	private void addStatus(String st) {
		statusLabel1.setText(statusLabel2.getText());
		statusLabel2.setText(statusLabel3.getText());
		statusLabel3.setText(st);
	}

} // @jve:decl-index=0:visual-constraint="10,10"

⌨️ 快捷键说明

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