📄 cmppapiexample.java
字号:
package com.bci.cmpp.example;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.UnknownHostException;
import com.bci.cmpp.AlreadyBoundException;
import com.bci.cmpp.Connection;
import com.bci.cmpp.NotBoundException;
import com.bci.cmpp.message.CMPPConnectResp;
import com.bci.cmpp.message.CMPPProtocolException;
import com.bci.cmpp.message.CMPPTerminateResp;
import com.bci.cmpp.message.InvalidParameterValueException;
import com.bci.cmpp.util.APIConfig;
import com.bci.cmpp.util.ASCIIEncoding;
import com.bci.cmpp.util.AlphabetEncoding;
import com.bci.cmpp.util.GBKEncoding;
import com.bci.cmpp.util.HPRoman8Encoding;
import com.bci.cmpp.util.InvalidDateFormatException;
import com.bci.cmpp.util.Latin1Encoding;
import com.bci.cmpp.util.UCS2Encoding;
import com.bci.cmpp.util.UTF16Encoding;
public class CMPPAPIExample {
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(CMPPAPIExample.class);
protected Configuration conf = null;
protected String hostName = null;
protected int port = 0;
protected String spCode = null;
protected String icpId = null;
protected String password = null;
protected String serviceId = null;
protected String sourceAddr = null;
protected String feeTerminalId = null;
protected Connection myConnection = null;
/**
* @return 返回 hostName。
*/
public String getHostName() {
return hostName;
}
/**
* @param hostName
* 要设置的 hostName。
*/
public void setHostName(String hostName) {
this.hostName = hostName;
}
/**
* @return 返回 password。
*/
public String getPassword() {
return password;
}
/**
* @param password
* 要设置的 password。
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return 返回 port。
*/
public int getPort() {
return port;
}
/**
* @param port
* 要设置的 port。
*/
public void setPort(int port) {
this.port = port;
}
/**
* @return 返回 serviceId。
*/
public String getServiceId() {
return serviceId;
}
/**
* @param serviceId
* 要设置的 serviceId。
*/
public void setServiceId(String serviceId) {
this.serviceId = serviceId;
}
/**
* @return 返回 spCode。
*/
public String getSpCode() {
return spCode;
}
/**
* @param spCode
* 要设置的 spCode。
*/
public void setSpCode(String spCode) {
this.spCode = spCode;
}
public CMPPAPIExample() {
super();
}
public void configure(String fileName) throws ConfigurationException {
conf = new Configuration(fileName);
this.hostName = conf.getValue("hostName");
this.port = Integer.parseInt(conf.getValue("port"));
this.spCode = conf.getValue("spCode");
this.password = conf.getValue("password");
this.icpId = conf.getValue("icpId");
this.serviceId = conf.getValue("serviceId");
this.sourceAddr = conf.getValue("sourceAddr");
this.feeTerminalId = conf.getValue("feeTerminalId");
logger.info("read configure complete.");
}
public String getProperty(String name) {
return conf.getValue(name);
}
/**
* @return 返回 icpId。
*/
public String getIcpId() {
return icpId;
}
/**
* @param icpId
* 要设置的 icpId。
*/
public void setIcpId(String icpId) {
this.icpId = icpId;
}
/**
* @return 返回 feeTerminalId。
*/
public String getFeeTerminalId() {
return feeTerminalId;
}
/**
* @param feeTerminalId
* 要设置的 feeTerminalId。
*/
public void setFeeTerminalId(String feeTerminalId) {
this.feeTerminalId = feeTerminalId;
}
/**
* @return 返回 sourceAddr。
*/
public String getSourceAddr() {
return sourceAddr;
}
/**
* @param sourceAddr
* 要设置的 sourceAddr。
*/
public void setSourceAddr(String sourceAddr) {
this.sourceAddr = sourceAddr;
}
protected boolean isConnected() {
if (this.myConnection != null && this.myConnection.isBound())
return true;
else
return false;
}
protected boolean disConnect() {
if (this.myConnection == null)
return true;
try {
if (myConnection != null && myConnection.isBound()) {
CMPPTerminateResp ubr = myConnection.unbind();
if (ubr != null && ubr.getCommandStatus() == 0) {
logger.info("Successfully unbound from the SMSG");
myConnection.closeLink();
} else {
logger.info("There was an error unbinding.");
}
}
} catch (NotBoundException e) {
e.printStackTrace();
} catch (CMPPProtocolException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
myConnection = null;
return true;
}
protected boolean reConnect(int type, boolean asyc) {
try {
Thread.sleep(1000);
myConnection = new Connection(this.hostName, this.port, asyc);
logger.info("Binding to the SMSG.." + this.icpId + "|"
+ this.password);
CMPPConnectResp resp = myConnection.bind(type, this.icpId,
this.password);
if (resp != null) {
logger.info("The SMSG version is " + resp.getVersion());
if (resp.getCommandStatus() != 0)
logger.info("SMSG bind failed.");
}
} catch (UnknownHostException e) {
e.printStackTrace();
return false;
} catch (InterruptedException e) {
e.printStackTrace();
return false;
} catch (InvalidParameterValueException e) {
e.printStackTrace();
return false;
} catch (AlreadyBoundException e) {
e.printStackTrace();
return false;
} catch (CMPPProtocolException e) {
e.printStackTrace();
return false;
} catch (IllegalArgumentException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
logger.info("Bind successful.");
return true;
}
public static String encodeHex(byte[] bytes) {
StringBuffer buf = new StringBuffer(bytes.length * 2);
int i;
for (i = 0; i < bytes.length; i++) {
if (((int) bytes[i] & 0xff) < 0x10) {
buf.append("0");
}
buf.append(Long.toString((int) bytes[i] & 0xff, 16));
}
return buf.toString();
}
public static final byte[] hexToBytes(String ss) {
byte[] bytes = new byte[ss.length() / 2];
char[] chars = ss.toLowerCase().toCharArray();
int byteCount = 0;
for (int i = 0; i < chars.length; i += 2) {
byte newByte = 0x00;
newByte |= hexCharToByte(chars[i]);
newByte <<= 4;
newByte |= hexCharToByte(chars[i + 1]);
bytes[byteCount] = newByte;
byteCount++;
}
return bytes;
}
private static final byte hexCharToByte(char ch) {
switch (ch) {
case '0':
return 0x00;
case '1':
return 0x01;
case '2':
return 0x02;
case '3':
return 0x03;
case '4':
return 0x04;
case '5':
return 0x05;
case '6':
return 0x06;
case '7':
return 0x07;
case '8':
return 0x08;
case '9':
return 0x09;
case 'a':
return 0x0A;
case 'b':
return 0x0B;
case 'c':
return 0x0C;
case 'd':
return 0x0D;
case 'e':
return 0x0E;
case 'f':
return 0x0F;
}
return 0x00;
}
public static void main(String arg[]) {
// String ss = "000000e000000005000000002741b4400af136f23031333"
// + "93200000000000000000000000000000000000000000000000000000001083133393831383139383"
// + "43900000000000000000000008b06080400030201003100330035003500310031003800380038003"
// + "700366bdb4e3b5e2d65595bfc62114eec003a60c54eba4e0d662f8d444ea796367ea776844e13522"
// + "9002c62114eec65e04ea796367ea74e5f8981641e002165e0975e5c31662f9001679d82b15f004e2"
// + "a623f561b20266ca194b1003f591a572891ce59165f005c556211770b4e5f53ef0000000000000000";
// byte[] buf = hexToBytes(ss);
// try {
// com.bci.cmpp.message.CMPPDeliver cm = new com.bci.cmpp.message.CMPPDeliver(
// buf);
// System.out.println(cm.getMsgCoding());
// int dc = cm.getMsgCoding();
// AlphabetEncoding alphabetEncoding = ASCIIEncoding.getInstance();
// try {
// if (dc == (UCS2Encoding.getInstance().getDataCoding()))
// alphabetEncoding = UCS2Encoding.getInstance();
// else if (dc == (GBKEncoding.getInstance().getDataCoding()))
// alphabetEncoding = GBKEncoding.getInstance();
// else if (dc == (HPRoman8Encoding.getInstance().getDataCoding()))
// alphabetEncoding = HPRoman8Encoding.getInstance();
// else if (dc == (Latin1Encoding.getInstance().getDataCoding()))
// alphabetEncoding = Latin1Encoding.getInstance();
// else if (dc == (UTF16Encoding.getInstance().getDataCoding()))
// alphabetEncoding = UTF16Encoding.getInstance();
// } catch (UnsupportedEncodingException e) {
// e.printStackTrace();
// }
//
// System.out.println(encodeHex(cm.getMessage()));
// byte[] b = new byte[cm.getMsgLength() - 7];
// System.arraycopy(cm.getMessage(), 7, b, 0, b.length);
// System.out.println(alphabetEncoding.decodeString(b));
// } catch (InvalidParameterValueException e) {
// e.printStackTrace();
// } catch (InvalidDateFormatException e) {
// e.printStackTrace();
// } catch (UnsupportedEncodingException e) {
// e.printStackTrace();
// }
// PropertyConfigurator.configure("config/log4j.properties");
// long bindTimeout = APIConfig.getInstance().getLong(
// APIConfig.LINK_TIMEOUT, 120000L);
// System.out.println(bindTimeout);
String cnText="永坪炼厂出厂价保持稳定:永坪公路90#汽油5300,5#柴油5060;铁路90#汽油5350,5#柴油5000 百克网-商讯";
try {
byte[] ucsHex= UCS2Encoding.getInstance().encodeString(cnText);
System.out.println("UCS2 Bytes:"+encodeHex(ucsHex));
System.out.println("UCS2 String:"+UCS2Encoding.getInstance().decodeString(ucsHex));
byte[] gbkHex= GBKEncoding.getInstance().encodeString(cnText);
System.out.println("GBK Bytes:"+encodeHex(gbkHex));
System.out.println("GBK String:"+GBKEncoding.getInstance().decodeString(ucsHex));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -