?? smpptest.java
字號:
messageId = response.getMessageId();
}
} catch (Exception e) {
event.write(e, "");
debug.write("Query operation failed. " + e);
System.out.println("Query operation failed. " + e);
} finally {
debug.exit(this);
}
}
/**
* Creates a new instance of <code>EnquireSM</code> class.
* This PDU is used to check that application level of the other party
* is alive. It can be sent both by SMSC and ESME.
*
* See "SMPP Protocol Specification 3.4, 4.11 ENQUIRE_LINK Operation."
* @see Session#enquireLink(EnquireLink)
* @see EnquireLink
* @see EnquireLinkResp
*/
private void enquireLink() {
debug.enter(this, "SMPPTest.enquireLink()");
try {
EnquireLink request = new EnquireLink();
EnquireLinkResp response;
System.out.println("Enquire Link request " + request.debugString());
if (asynchronous) {
session.enquireLink(request);
} else {
response = session.enquireLink(request);
System.out.println("Enquire Link response " + response.debugString());
}
} catch (Exception e) {
event.write(e, "");
debug.write("Enquire Link operation failed. " + e);
System.out.println("Enquire Link operation failed. " + e);
} finally {
debug.exit(this);
}
}
/**
* 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. ");
if (receiveTimeout == Data.RECEIVE_BLOCKING) {
System.out.print(
"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.");
}
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());
if (pdu.isRequest()) {
Response response = ((Request) pdu).getResponse();
// respond with default response
System.out.println("Going to send default response to request " + response.debugString());
session.respond(response);
}
} else {
System.out.println("No PDU received this time.");
}
} catch (Exception e) {
event.write(e, "");
debug.write("Receiving failed. " + e);
System.out.println("Receiving failed. " + e);
} finally {
debug.exit(this);
}
}
/**
* If bound, unbinds and then exits this application.
*/
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());
synchronized (requestEvents) {
requestEvents.enqueue(event);
requestEvents.notify();
}
} else if (pdu.isResponse()) {
System.out.println("async response received " + pdu.debugString());
} else {
System.out.println(
"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(String prompt, String defaultValue) {
String value = "";
String promptFull = prompt;
promptFull += defaultValue == null ? "" : " [" + defaultValue + "] ";
System.out.print(promptFull);
try {
value = keyboard.readLine();
} catch (IOException e) {
event.write(e, "");
debug.write("Got exception getting a param. " + e);
}
if (value.compareTo("") == 0) {
return defaultValue;
} else {
return value;
}
}
/**
* Prompts the user to enter a byte value for a parameter.
*/
private byte getParam(String prompt, byte defaultValue) {
return Byte.parseByte(getParam(prompt, Byte.toString(defaultValue)));
}
/**
* Prompts the user to enter an integer value for a parameter.
*/
private int getParam(String prompt, int defaultValue) {
return Integer.parseInt(getParam(prompt, Integer.toString(defaultValue)));
}
/**
* Prompts the user to enter an address value with specified max length.
*/
private Address getAddress(String type, Address address, int maxAddressLength)
throws WrongLengthOfStringException {
byte ton = getParam(type + " address TON", address.getTon());
byte npi = getParam(type + " address NPI", address.getNpi());
String addr = getParam(type + " address", address.getAddress());
address.setTon(ton);
address.setNpi(npi);
address.setAddress(addr, 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(String type, Address address) throws WrongLengthOfStringException {
return getAddress(type, 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 + "...");
FileInputStream propsFile = new FileInputStream(fileName);
properties.load(propsFile);
propsFile.close();
System.out.println("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.");
}
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");
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.");
}
}
}
/*
* $Log: SMPPTest.java,v $
* Revision 1.2 2003/09/30 09:07:32 sverkera
* Changed to use GSM 7Bit charset
*
* Revision 1.1 2003/07/23 00:28:39 sverkera
* Imported
*
*
* Old changelog:
* 15-09-01 ticp@logica.com added asynchronous processing capability
* as the library has this feature now
* 10-10-01 ticp@logica.com file moved to package com.logica.smpp.test,
* old package com.logica.smpptest not used anymore
* 11-10-01 ticp@logica.com the max address length checking reflected
* 11-10-01 ticp@logica.com added commants
*/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -