📄 cmpp.java~1~
字号:
}
}
/**取得CMPP CMPP_CONNECT_RESP的消息包
* 7.4.1.2 CMPP_CONNECT_RESP消息定义(ISMG -> SP)
**/
private byte[] makeDeliverRespMsgPack(){
ByteArrayOutputStream byteArrayOutStream = new ByteArrayOutputStream();
DataOutputStream dataOutStream = new DataOutputStream(byteArrayOutStream);
try{
//MT消息长度
if(Version == 48) this.Total_Length = 12 + 12;
if(Version == 32) this.Total_Length = 12 + 9;
//打包MT消息报头
packHead(dataOutStream);
//打包消息体
dataOutStream.writeLong(this.Msg_Id); //写入信息标识
if(Version == 48) dataOutStream.writeInt(this.Result); //写入结果
if(Version == 32) dataOutStream.writeByte(this.Result); //写入结果
//返回消息的字节流
dataPack = byteArrayOutStream.toByteArray();
return this.dataPack;
}catch(Exception e){
e.printStackTrace();
System.out.println("[CMPP]makeDeliverRespMsgPack.pack() error : "+e.getMessage());
return null;
}
}
/** 打包CMPP_SUBMIT 消息
* 8.4.3.1 CMPP_SUBMIT消息定义(SP->ISMG)
**/
private byte[] makeSubmitMsgPack(){
ByteArrayOutputStream byteArrayOutStream = new ByteArrayOutputStream();
DataOutputStream dataOutStream = new DataOutputStream(byteArrayOutStream);
try{
//MT消息长度
if(Version == 48) this.Total_Length = 12 + 10 + 12 + 33 + 4 + 8 + 6 + 34 + 22 + 32 + 2 + 20 + this.Msg_Length;
if(Version == 32) this.Total_Length = 12 + 9;
//打包MT消息报头
packHead(dataOutStream);
//打包消息体
System.out.println("Msg_Id = " + Msg_Id + ",this.Total_Length ="+this.Total_Length);
dataOutStream.writeLong(Msg_Id);
dataOutStream.writeByte(Pk_total);
dataOutStream.writeByte(Pk_number);
dataOutStream.writeByte(Registered_Delivery);
dataOutStream.writeByte(Msg_level);
writeString(dataOutStream,Service_Id,10);
dataOutStream.writeByte(Fee_UserType);
if(Version == 48) writeString(dataOutStream,Fee_terminal_Id,32);
if(Version == 32) writeString(dataOutStream,Fee_terminal_Id,21);
dataOutStream.writeByte(Fee_terminal_type);
dataOutStream.writeByte(TP_pid);
dataOutStream.writeByte(TP_udhi);
dataOutStream.writeByte(Msg_Fmt);
writeString(dataOutStream,Msg_src,6);
writeString(dataOutStream,FeeType,2);
writeString(dataOutStream,FeeCode,6);
writeString(dataOutStream,ValId_Time,17);
writeString(dataOutStream,At_Time,17);
writeString(dataOutStream,Src_terminal_Id,21);
dataOutStream.writeByte(DestUsr_tl);
writeString(dataOutStream,Dest_terminal_Id,32);
dataOutStream.writeByte(Dest_terminal_type);
dataOutStream.writeByte(Msg_Length);
int iLength = Msg_Length;
if(Msg_Length < 0 )
iLength = 256 + Msg_Length;
Msg_Content = new String(Msg_Content.getBytes("gb2312"),-4);
writeString(dataOutStream,Msg_Content,iLength);
if(Version == 48) writeString(dataOutStream,LinkID,20);//写入结果
//返回消息的字节流
dataPack = byteArrayOutStream.toByteArray();
return this.dataPack;
}catch(Exception e){
e.printStackTrace();
System.out.println("[CMPP]makeSubmitMsgPack.pack() error : "+e.getMessage());
return null;
}
}
/**
* 打包消息头
* @param dataOutStream 由调用者传送来的数据输出流
*/
private void packHead( DataOutputStream dataOutStream ) throws Exception
{
try {
dataOutStream.writeInt(Total_Length); //写入包长度
dataOutStream.writeInt(Command_Id); //写入命令或响应类型
dataOutStream.writeInt(Sequence_Id); //写入消息流水号
} catch(IOException e) {
System.out.println("[CMPP] CMPP.packHead() thrown IOException"+e);
throw e;
}
}
/**
* 解吸接受到的数据包,转换成CMPP MSG
* @param recvPack
* @return
*/
public void parsePack(byte[] recvPack) throws Exception{
ByteArrayInputStream byteStream = new ByteArrayInputStream(recvPack);
DataInputStream dataInStream = new DataInputStream(byteStream);
//读取消息头
try {
this.Command_Id = dataInStream.readInt(); //消息类型
this.Sequence_Id = dataInStream.readInt(); //消息流水号(可以用来完成消息的确认)
} catch(IOException e) {
System.out.println("[CMPP] CMPP.parseHead() error : "+e);
throw e;
}
//读取消息体
try{
switch(Command_Id){
case CMPP.CMPP_CONNECT:
break;
case CMPP.CMPP_CONNECT_RESP:
this.parseConnectRespPack(dataInStream);
break;
case CMPP.CMPP_DELIVER:
this.parseDeliverPack(dataInStream);
break;
case CMPP.CMPP_DELIVER_RESP:
break;
case CMPP.CMPP_SUBMIT:
break;
case CMPP.CMPP_SUBMIT_RESP:
this.parseSubmitRespPack(dataInStream);
break;
case CMPP.CMPP_ACTIVE_TEST:
break;
case CMPP.CMPP_ACTIVE_TEST_RESP:
break;
case CMPP.CMPP_QUERY:
break;
case CMPP.CMPP_QUERY_RESP:
break;
}
}
catch(Exception e){
throw e;
}
}
/**
* 解析CMPP_CONNECT_RESP信息包
* 7.4.1.2 CMPP_CONNECT_RESP消息定义(ISMG -> SP)
* @param dataInputStream
*/
private void parseConnectRespPack( DataInputStream dataInputStream ) throws Exception
{
try{
if(this.Version ==32) this.Status = dataInputStream.readByte();
if(this.Version ==48) this.Status = dataInputStream.readInt();
this.AuthenticatorISMG = this.readString(dataInputStream,16);
this.Version = dataInputStream.readByte();
}catch(Exception e){
System.out.println("[CMPP]CMPP.parseConnectRespPack error " + e);
throw e;
}
}
/**
* 解析CMPP_DELIVER信息包
* 8.4.5.1 CMPP_DELIVER消息定义(ISMG->SP)
* @param dataInputStream
*/
private void parseDeliverPack( DataInputStream dataInputStream ) throws Exception
{
try{
this.Msg_Id = dataInputStream.readLong();
//目的号码。SP的服务代码,一般4--6位,或者是前缀为服务代码的长号码;该号码是手机用户短消息的被叫号码。
this.Dest_terminal_Id = this.readString(dataInputStream,21);
this.Service_Id = this.readString(dataInputStream,10);
this.TP_pid = dataInputStream.readByte();
this.TP_udhi = dataInputStream.readByte();
this.Msg_Fmt = dataInputStream.readByte();
if(Version == 48){ //CMPP3.0
this.Src_terminal_Id = this.readString(dataInputStream,32);
this.Src_terminal_type = dataInputStream.readByte();
}
else if(Version == 32){ //CMPP2.0
this.Src_terminal_Id = this.readString(dataInputStream, 21);
}
this.Registered_Delivery = dataInputStream.readByte();
this.Msg_Length = dataInputStream.readByte();
//当字节数大于128时候
if(this.Msg_Length <0){
this.Msg_Content = this.readString(dataInputStream,(256+this.Msg_Length));
}
else{
this.Msg_Content = this.readString(dataInputStream,this.Msg_Length);
}
if(Version == 48){ //CMPP3.0
this.LinkID = this.readString(dataInputStream,20);
}
else if(Version == 32){ //CMPP2.0
this.Reserved = this.readString(dataInputStream,8);
}
}catch(Exception e){
System.out.println("[CMPP]CMPP.parseDeliverPack error " + e);
throw e;
}
}
/**
* 解析CMPP_SUBMIT_RESP信息包
* 8.4.3.2 CMPP_SUBMIT_RESP消息定义(ISMG à SP)
* @param dataInputStream
*/
private void parseSubmitRespPack( DataInputStream dataInputStream ) throws Exception
{
try{
this.Msg_Id = dataInputStream.readLong();
//目的号码。SP的服务代码,一般4--6位,或者是前缀为服务代码的长号码;该号码是手机用户短消息的被叫号码。
if(Version == 48){ //CMPP3.0
this.Result = (byte)dataInputStream.readInt();
}
else if(Version == 32){ //CMPP2.0
this.Result = (byte)dataInputStream.readByte();
}
System.out.println("MsgId="+this.Msg_Id+",status="+this.Result);
}catch(Exception e){
System.out.println("[CMPP]CMPP.parseSubmitRespPack error " + e);
throw e;
}
}
/**
* 解析CMPP_QUERY_RESP信息包
* 8.4.4.2 CMPP_QUERY_RESP消息的定义(ISMG àSP)
* @param dataInputStream
*/
private void parseQueryRespPack( DataInputStream dataInputStream ) throws Exception
{
try{
this.Time = readString(dataInputStream,8);
this.Query_Type = dataInputStream.readByte();
this.Query_Code = readString(dataInputStream,10);
this.MT_TLMsg = dataInputStream.readInt();
this.MT_Tlusr = dataInputStream.readInt();
this.MT_Scs = dataInputStream.readInt();
this.MT_WT = dataInputStream.readInt();
this.MT_FL = dataInputStream.readInt();
this.MO_Scs = dataInputStream.readInt();
this.MO_WT = dataInputStream.readInt();
this.MO_FL = dataInputStream.readInt();
}catch(Exception e){
System.out.println("[CMPP]CMPP.parseQueryRespPack error " + e);
throw e;
}
}
/**
* 向数据输出流写入指定长度字符串,不足右补“\0”
* @param dataOutStream 由调用者传送来的数据输出流
* @param str 写入的字符
* @param StrLen 指定长度
* @throws Exception
*/
private void writeString_old(DataOutputStream dataOutStream,String str,int StrLen)
throws Exception
{
int i;
try{
if(str!=null)
{
dataOutStream.writeBytes(str) ;
System.out.println("str len = "+str.length());
for (i=1;i<=StrLen-str.length ();i++)
{
dataOutStream.writeByte('\0');
}
}else{
for (i=1;i<=StrLen;i++)
{
dataOutStream.writeByte('\0');
}
}
}catch(IOException e){
throw e;
}
}
/**
* 向数据输出流写入指定长度字符串,不足右补“\0”
* @param dataOutStream 由调用者传送来的数据输出流
* @param str 写入的字符
* @param StrLen 指定长度
* @throws Exce
**/
private void writeString(DataOutputStream dataOutStream,String str,int StrLen)
throws Exception {
if(str==null || str.length()==0){
for(int i=0;i<StrLen;i++){
dataOutStream.writeByte('\0');
}
return;
}
// byte[] bytes = str.getBytes();
// int theLength = Math.min(bytes.length,StrLen);
// dataOutStream.write(bytes,0,theLength);
int theLength = str.length() < StrLen ? str.length() : StrLen;
dataOutStream.writeBytes(str.substring(0,theLength));
//不足部分补'\0'
for(int i=0;i<StrLen-theLength;i++){
dataOutStream.writeByte('\0');
}
}
/**
* 从流中读取一个字符串,返回的String 不包含'\0'
*/
private static String readString(DataInputStream inStream,int StrLen)
{
byte[] readByte = new byte[1000];
int i = 0;
//当消息长度为大于(128时候)
if(StrLen <0)
StrLen = 256 + StrLen;
try{
while(true){
readByte[i] = inStream.readByte() ;
i++;
if (i>=1000)
return "";
if(i > StrLen-1)
break;
}
} catch(IOException e){
return "erro";
}
String result = new String(readByte,0,i);
return result.trim ();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -