📄 shortmsgcmpp.java
字号:
package cn.netjava.cngpclient.connection;
import java.io.*;
import java.security.*;
import java.util.logging.*;
import cn.netjava.cngpclient.log.LogManage;
import cn.netjava.cngpclient.publics.*;
/**
* 消息封包,解包实现
* 中国网通短信网关客户端--蓝杰实训项目
* @author www.NetJava.cn
*
*/
public class ShortMsgCmpp implements ShortMsg {
public ShortMsgCmpp() {
packetLength = 0;
totalSize = 0; //
headCmdID = 0; //
headCmdStatus = 0x00000000; //
headSeqcNo = 0; //
submit_msg_id = "0"; //
submit_destusr_tl = 1; //
msgPkTotal = 1; //
msgPkNumber = 1; //
// conSourceAddr=""; //
conAuth = new byte[16]; //
conBindType = 2; //
conVersion = 0; //
conTimestamp = 0; //
conTimestampBytes = new byte[4]; //
lastVersion = 0; //
repAuth = new byte[16]; //
spId = IsmgInfo.spId; //
msgId = ""; //
fwdFlag = 0; //
AccessorId = 0; //
smscSeqNo = 0; //
moMsgId = ""; //
msgSpId = ""; // ()
msgSrcAddr = ""; // ()
msgDestAddr = ""; //
msgServiceType = ""; //
msgFeeType = 1; //
msgInfoFee = 0; // ()
msgProtocolId = 0; //
msgMode = 1; // ,
msgPriority = 1; //
msgFeeUserType = 0; //
msgDataCoding = 0; //
msgDeliverTime = ""; //
msgDigest = ""; //
msgLength = 0; // (<320
msgContent = ""; //
msgValidityPeriod = ""; // ,.4协议
msgSchedule = ""; // ,...4协议
msgReserve = ""; //
TlsFlag = 0;
ackMsgID = 0;
respResult = 0;
parseResult = CmppConstants.success;
successId = 0;
spCode = "";
spIndex = 0;
pushType = 0;
sendCount = 0;
subType = 0;
}
public int getMsgLength() {
return packetLength;
}
public int getParseResult() {
return parseResult;
}
public int getCommandId() {
return headCmdID;
}
public boolean setCommandId(int i) {
headCmdID = i;
return true;
}
public int getHeadCmdStatus() {
return headCmdStatus;
}
public boolean setHeadCmdStatus(int i) {
headCmdStatus = i;
return true;
}
public byte getMsgEsmClass() {
return msgMode;
}
public String getMsgId() {
return msgId;
}
// public String getConSourceAddr() {
// return conSourceAddr;
// }
public void setSpCode(String ss) {
spCode = ss;
}
public byte getLastVersion() {
return lastVersion;
}
public boolean setLastVersion(byte b) {
lastVersion = b;
return true;
}
public byte[] getRepAuth() {
return repAuth;
}
public boolean setRepAuth(byte[] b) {
repAuth = b;
return true;
}
public byte[] getConAuth() {
return conAuth;
}
public boolean setConAuth(byte[] b) {
conAuth = b;
return true;
}
public boolean getRepAuthResult() {
return true;
}
public String getBindSysId() {
return null;
}
public boolean setBindType(byte i) {
conBindType = i;
return true;
}
public byte getBindType() {
return conBindType;
}
public boolean setAccessorId(int s) {
return true;
}
public String getDestId() {
return msgSrcAddr;
}
public byte getMsgMode() {
return msgMode;
}
public byte[] getPackage() {
ByteArrayOutputStream byteArrayOutStream = new ByteArrayOutputStream();
DataOutputStream dataOutStream = new DataOutputStream(
byteArrayOutStream);
try {
switch (headCmdID) {
case CmppConstants.Cmpp_Active:
headSeqcNo = ComConstants.getSeqNo();
getHead(dataOutStream, 16);
break;
case CmppConstants.Cmpp_Active_Resp:
getHead(dataOutStream, 16);
break;
case CmppConstants.Cmpp_Login:
headSeqcNo = ComConstants.getSeqNo();
getSpLogin(dataOutStream);
break;
case CmppConstants.Cmpp_Logout:
headSeqcNo = ComConstants.getSeqNo();
getHead(dataOutStream, 16);
break;
case CmppConstants.Cmpp_Deliver_Resp:
getDeliverRep(dataOutStream);
break;
case CmppConstants.Cmpp_Submit:
headSeqcNo = ComConstants.getSeqNo();
getSubmit(dataOutStream);
break;
case CmppConstants.Cmpp_Nack_Resp:
getHead(dataOutStream, 16);
break;
default:
return null;
// LogManage.ins() .myLog.logp(Level.WARNING ,"","","Recv unknow
// command_id: "+headCmdID);
}
byte[] b = byteArrayOutStream.toByteArray();
return b;
} catch (Exception e) {
LogManage.ins().myLog.logp(Level.WARNING, "", "",
"Get Package error: " + e);
return "Erro".getBytes();
}
}
public void parsePackage(byte[] receivedPackage) {
ByteArrayInputStream byteStream = new ByteArrayInputStream(
receivedPackage);
DataInputStream dataInStream = new DataInputStream(byteStream);
parseHead(dataInStream);
switch (headCmdID) {
case CmppConstants.Cmpp_Active:
break;
case CmppConstants.Cmpp_Active_Resp:
activeResult = (byte) headCmdStatus;
break;
case CmppConstants.Cmpp_Login_Resp:
parseBodyLoginRep(dataInStream);
break;
case CmppConstants.Cmpp_Logout_Resp:
break;
case CmppConstants.Cmpp_Submit_Resp:
try {
submit_msg_id = readMsgID(dataInStream, 10);
submit_msg_id_tem = getTemMSgID(msgID_b);
activeResult = (byte) headCmdStatus;
} catch (Exception ex) {
ex.printStackTrace();
}
break;
case CmppConstants.Cmpp_Deliver:
submit_msg_id = readMsgID(dataInStream, 10);
parseBodyDeliver(dataInStream);
break;
case CmppConstants.Cmpp_Nack_Resp:
break;
default:
LogManage.ins().myLog.logp(Level.WARNING, "", "",
" error command: " + headCmdID);
}
}
// 打包消息头
private void getHead(DataOutputStream dataOutStream, int bodySize) {
packetLength = bodySize;
respResult = 0;
try {
dataOutStream.writeInt(bodySize);
dataOutStream.writeInt(headCmdID);
dataOutStream.writeInt(0);
dataOutStream.writeInt(headSeqcNo);
} catch (IOException e) {
LogManage.ins().myLog
.logp(Level.WARNING, "", "", " get head: " + e);
respResult = 1;
}
}
// 解析消息头
private void parseHead(DataInputStream dataInStream) {
try {
headCmdID = dataInStream.readInt();
headCmdStatus = dataInStream.readInt();
headSeqcNo = dataInStream.readInt();
} catch (IOException e) {
LogManage.ins().myLog.logp(Level.WARNING, "", "", " parseHead: "
+ e);
parseResult = CmppConstants.error_msg_structure;
}
}
// set login MD5
public boolean setConSourceAddr() {
byte[] shareM;
try {
String time = cn.netjava.cngpclient.publics.PublicFuction
.getTimeStamp();
conTimestamp = Integer.parseInt(time);
String d = IsmgInfo.spId + "\0\0\0\0\0\0\0" + IsmgInfo.spPasswd
+ time;
Md5 md = new Md5();
connect_authenticatoricp = md.encrypt(d); // ICP认证码
return true;
} catch (Exception e) {
LogManage.ins().myLog.logp(Level.WARNING, "", "", " MD5: " + e);
return false;
}
}
// get login packet
private void getSpLogin(DataOutputStream dataOutStream) {
int bodySize;
try {
bodySize = 16 + 10 + 16 + 1 + 4 + 1;
respResult = 0;
getHead(dataOutStream, bodySize);
writeString(dataOutStream, IsmgInfo.spId, 6);
writeString(dataOutStream, connect_authenticatoricp, 16);
dataOutStream.writeByte((byte) 2);
dataOutStream.writeInt(conTimestamp);
dataOutStream.writeByte((byte) 16);
} catch (IOException e) {
respResult = 1;
LogManage.ins().myLog
.logp(Level.WARNING, "", "", " getLogin: " + e);
}
}
// 解析login回应
private void parseBodyLoginRep(DataInputStream dataInStream) {
try {
readString(dataInStream, 16);
lastVersion = dataInStream.readByte();
} catch (Exception e) {
parseResult = CmppConstants.error_msg_structure;
LogManage.ins().myLog.logp(Level.WARNING, "", "",
" parase Login Rsp: " + e);
return;
}
}
// 取得submit 包
private boolean getSubmit(DataOutputStream dataOutStream) {
byte[] contentBytes;
try {
if (msgDataCoding == 0) {
contentBytes = msgContent.getBytes("ISO8859-1");
} else if (msgDataCoding == 4) {
contentBytes = convertStringToBytes(msgContent);
} else if (msgDataCoding == 24) {
contentBytes = msgContent.getBytes("UTF-16BE");
} else if (msgDataCoding == 8) {
contentBytes = msgContent.getBytes("UTF-16");
} else if (msgDataCoding == 15)
contentBytes = msgContent.getBytes("gb2312");
else
contentBytes = msgContent.getBytes();
} catch (Exception e) {
contentBytes = msgContent.getBytes();
System.out.println("unsupportedEncoding Exception :" + e);
}
try {
if (msgServiceType == null)
msgServiceType = "";
totalSize = 16 + 10 + 1 + 1 + 1 + 10 + 2 + 1 + 6 + 1 + 17 + 17 + 21
+ 21 + 1 + 21 * submit_destusr_tl + 1 + contentBytes.length;
getHead(dataOutStream, totalSize);
writeString(dataOutStream, IsmgInfo.spId, 10);
dataOutStream.writeByte(subType);
dataOutStream.writeByte(msgMode);
dataOutStream.writeByte(msgPriority);
writeString(dataOutStream, msgServiceType, 10);
writeString(dataOutStream,
"0" + Integer.toString((int) msgFeeType), 2);
dataOutStream.writeByte(msgFeeUserType);
writeString(dataOutStream, Integer.toString(msgInfoFee), 6);
dataOutStream.writeByte(msgDataCoding);
writeString(dataOutStream, msgValidityPeriod, 17);
writeString(dataOutStream, msgSchedule, 17);
writeString(dataOutStream, msgSrcAddr, 21);
writeString(dataOutStream, feeTerminalId, 21);
dataOutStream.writeByte(submit_destusr_tl);
for (int i = 0; i < submit_destusr_tl; i++) {
writeString(dataOutStream, msgSubmitDest[i], 21);
}
dataOutStream.writeByte(contentBytes.length);
dataOutStream.write(contentBytes);
} catch (Exception e) {
LogManage.ins().myLog.logp(Level.WARNING, "", "",
" get Submit error : " + e);
return false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -