📄 smgp.java~2~
字号:
package com.pansonlu.smgp;
/**
* <p>Title: SMGP协议包</p>
* <p>Description: 湖南电信短信网关通讯程序-实现对短信网关的连接,短信的收发
* 各种消息的打包,解析</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: sunrise Ltd.</p>
* @author pansonlu
* @version 1.0
*/
import java.io.*;
import com.pansonlu.common.util.*;
public class SMGP {
/** 数据包长度(包头和包体的长度之和。单位:字节) **/
private int PacketLength = 0;
/** 请求标识 **/
public int RequestID = 0;
/** 序列号(由客户端分配,循环递增) **/
public int SequenceID = 0;
/** ClientID 8 Octet String CP编号或者SMGW编号编号规则参见总册第6节 **/
public String ClientID = "";
public String AuthenticatorClient = "";
/** 登录类型(0=发送短消息, 1=接收短消息,2=收发短消息,其他保留) **/
public byte LoginMode = 2;
/** 时间戳的明文,由客户端产生,格式为MMDDHHMMSS,即月日时分秒,10位数字的整型,右对齐 **/
public int TimeStamp = 0;
/** 客户端支持的版本号(高位4bit表示主版本号,低位4bit表示次版本号) **/
public byte Version = (byte)SMGParameter.Version;
public int Status = 0;
public String AuthenticatorServer = "";
/** 是否状态报告(0=不是,1=是) **/
public byte IsReport = 0;
/************************5.3.3 submit 包*******************************/
public byte MsgLength = 2;
/** 短消息子类型(0=取消订阅,1=订阅请求,2=点播,3=订阅,其他保留) **/
public byte SubType = 2;
/** 是否要求返回状态报告(0=不要求,1=要求) **/
public byte NeedReport = 0;
/** 发送优先级(从0到9) **/
public byte Priority = 1;
/** ServiceID 10 Octet String 业务类型 **/
public String ServiceID = "";
/** FeeType 2 Octet String 收费类型(参见收费类型代码表)
* 00 免费
01 按条收费
02 包月
03 封顶
其他 保留
**/
public String FeeType = "00";
/**FeeCode 6 Octet String 资费代码(单位为分)**/
public String FeeCode = "TEST";
public String FixedFee= "";
/** MsgFormat 1 Octet String 短消息格式(参见短消息格式代码表) **/
public int MsgFormat = 15;
/** ValidTime 17 Octet String 有效时间,格式遵循SMPP3.3协议 **/
public String ValidTime = "";
/** AtTime 17 Octet String 定时发送时间,格式遵循SMPP3.3协议 **/
public String AtTime = "";
/** SrcTermID 21 Octet String 短消息发送用户号码 **/
// 短消息发送用户号码118+CP代码
public String SrcTermID = "";
/** ChargeTermID 21 Octet String 计费用户号码 **/
public String ChargeTermID = "";
/** DestTermIDCount 1 Integer 短消息接收号码总数(≤100) **/
public byte DestTermIDCount = 1;
/** DestTermID 21* DestTerm Count Octet String
* 短消息接收号码(连续存储DestTermIDCount个号码) **/
public String DestTermID = "";
/** MsgContent ≤252 Octet String 短消息内容 **/
public String MsgContent = "";
/** Reserve 8 Octet String 保留 **/
public String Reserve = "";
/************************ 5.3.4 submit_resp ***************/
/** 网关产生的短消息流水号,
* 由三部分组成:网关代码:3字节(BCD码)时间:4字节(BCD码)序列号:3字节(BCD码) **/
public String MsgID = "0";
/**序列号:3字节(BCD码)**/
public int BCDMsgID = 1;
/************************* 5.3.5 deliver ******************/
/** 短消息接收时间(格式:yyyymmddhhmiss,例如20010301200000) **/
public String RecvTime = "";
/** 5.4 状态报告格式 **/
/** 状态报告对应原短消息的MsgID **/
public String Report_ID = "";
/** 短消息提交时间(格式:yyyymmddhhmiss,例如010331200000) **/
public String Report_Submit_date = "";
/** 短消息下发时间(格式:yyyymmddhhmiss,例如010331200000) **/
public String Report_Done_date = "";
/** 短消息状态(参见短消息状态表) **/
public String Report_Stat = "";
/** 参见错误代码表 ***/
public String Report_Error = "";
public final static int SMGP_LOGIN = 0x00000001; //CP或SMGW登录请求 **/
public final static int SMGP_LOGIN_RESP = 0x80000001; //CP或SMGW登录回应 **/
public final static int SMGP_SUBMIT = 0x00000002; //CP发送短消息请求 **/
public final static int SMGP_SUBMIT_RESP = 0x80000002; //CP发送短消息回应 **/
public final static int SMGP_DELIVER = 0x00000003; //SMGW向CP发送短消息请求 **/
public final static int SMGP_DELIVER_RESP = 0x80000003; //SMGW向CP发送短消息回应 **/
public final static int SMGP_ACTIVE_TEST = 0x00000004; //测试通信链路是否正常请求(由客户端发起,CP和SMGW可以通过定时发送此请求来维持连接) **/
public final static int SMGP_ACTIVE_TEST_RESP = 0x80000004; //测试通信链路是否正常回应 **/
public final static int SMGP_QUERY = 0x00000007; //CP统计查询请求 **/
public final static int SMGP_QUERY_RESP = 0x80000007; //CP统计查询回应 **/
public final static int SMGP_EXIT = 0x00000006; //CP统计查询回应 **/
public final static int SMGP_EXIT_RESP = 0x80000006; //CP统计查询回应 **/
/** 消息包 **/
public byte[] dataPack = null;
//一个空的构造器 ,用来解析数据包
public SMGP() {
}
//根据请求标识(RequestID)来构造消息
public SMGP(int requestID){
this.RequestID = requestID;
this.SequenceID = getSequenceID();
}
/**
* 打包函数,返回打包后数据字节数组
* @return 打包后数据字节数组
*/
public byte[] pack(){
byte[] retBB = null;
switch(RequestID){
case SMGP.SMGP_LOGIN:
retBB = this.makeLoginMsgPack();
break;
case SMGP.SMGP_LOGIN_RESP:
break;
case SMGP.SMGP_DELIVER:
break;
case SMGP.SMGP_DELIVER_RESP:
retBB = this.makeDeliverRespMsgPack();
break;
case SMGP.SMGP_SUBMIT:
retBB = this.makeSubmitMsgPack();
break;
case SMGP.SMGP_SUBMIT_RESP:
break;
case SMGP.SMGP_ACTIVE_TEST:
retBB = this.makeActiveMsgPack();
break;
case SMGP.SMGP_ACTIVE_TEST_RESP:
retBB = this.makeActiveRespMsgPack();
break;
case SMGP.SMGP_QUERY:
break;
case SMGP.SMGP_QUERY_RESP:
break;
}
return retBB;
}
/**
* 打包消息头
* @param dataOutStream 由调用者传送来的数据输出流
*/
private void packHead( DataOutputStream dataOutStream ) throws Exception
{
try {
dataOutStream.writeInt(PacketLength); //写入包长度
dataOutStream.writeInt(RequestID); //写入命令或响应类型
dataOutStream.writeInt(SequenceID); //写入消息流水号
} catch(IOException e) {
System.out.println("[SMGP] SMGP.packHead() thrown IOException"+e);
throw e;
}
}
/**取得5.3.1 login的消息包
* 7.4.1.1 SMGP_CONNECT消息定义(SP->ISMG)
**/
private byte[] makeLoginMsgPack(){
ByteArrayOutputStream byteArrayOutStream = new ByteArrayOutputStream();
DataOutputStream dataOutStream = new DataOutputStream(byteArrayOutStream);
try{
//MT消息长度
this.PacketLength = 42;
this.ClientID = SMGParameter.SP_Id;
String tempTimeStamp = PublicFunction.getTimeStamp();
this.TimeStamp = Integer.parseInt(tempTimeStamp);
//this.AuthenticatorClient = ClientID +"\0\0\0\0\0\0\0"+SMGParameter.SP_Pwd + this.TimeStamp ;
this.AuthenticatorClient = ClientID +"\0\0\0\0\0\0\0"+SMGParameter.SP_Pwd+ tempTimeStamp;
System.out.println();
System.out.println();
MD5 md5 = new MD5();
this.AuthenticatorClient = md5.encrypt(this.AuthenticatorClient);
//打包MT消息报头
packHead(dataOutStream);
//打包消息体
writeString(dataOutStream,ClientID,8); //写入源地址
writeString_old(dataOutStream,AuthenticatorClient,16); //写入用于鉴别源地址的数据
dataOutStream.writeByte(LoginMode); //写入登录类型
dataOutStream.writeInt(TimeStamp); //写入时间戳的明文
dataOutStream.writeByte(Version); //写入双方协商的版本号
//返回消息的字节流
dataPack = byteArrayOutStream.toByteArray();
return this.dataPack;
}catch(Exception e){
e.printStackTrace();
System.out.println("[SMGP] makeLoginMsgPack.pack() error : "+e.getMessage());
return null;
}
}
/**取得5.3.3 submit的消息包
* 5.3.3 submit
**/
private byte[] makeSubmitMsgPack(){
ByteArrayOutputStream byteArrayOutStream = new ByteArrayOutputStream();
DataOutputStream dataOutStream = new DataOutputStream(byteArrayOutStream);
try{
//根据FeeType修改SubType值
this.SubType = 3;
if(this.FeeType.equals("02") || this.FeeType.equals("04")){
this.SubType = 4;
}
//对消息内容进行转码
String str = MsgContent;
int i = 0;
if(this.MsgFormat == 15){
i = new String(str.getBytes("gb2312"),-4).getBytes().length;
while(i>126){
str = str.substring(0,str.length() -1);
i = new String(str.getBytes("gb2312"),-4).getBytes().length;
}
MsgContent = new String(str.getBytes("gb2312"), -4);
}
else
if(this.MsgFormat == 24){
i = new String(str.getBytes("UnicodeBigUnmarked"),-4).getBytes().length;
while(i>126){
str = str.substring(0,str.length() -1);
i = new String(str.getBytes("UnicodeBigUnmarked"),-4).getBytes().length;
}
MsgContent = new String(str.getBytes("UnicodeBigUnmarked"), -4);
}
this.MsgLength = (byte)i;
// System.out.println("MsgLength:"+MsgLength+" MsgContent:"+MsgContent);
//MT消息长度
this.PacketLength = 147 + MsgLength;
//打包MT消息报头
packHead(dataOutStream);
//打包消息体
dataOutStream.writeByte(SubType);
dataOutStream.writeByte(NeedReport);
dataOutStream.writeByte(Priority);
//注意FeeCode和FixedFee的顺序
writeString(dataOutStream,ServiceID,10); //写入业务类型
writeString(dataOutStream,FeeType,2); //写入收费类型
writeString(dataOutStream,FeeCode,6); //写入资费代码
writeString(dataOutStream,FixedFee,6); //写入包月费/封顶费(单位为分)
dataOutStream.writeByte(MsgFormat); //写入短消息格式
writeString(dataOutStream,ValidTime,17); //写入有效时间
writeString(dataOutStream,AtTime,17); //写入定时发送时间
writeString(dataOutStream,SrcTermID,21); //写入源号码,需加118
//writeString(dataOutStream,SrcTermID,21); //写入源号码,测试网关不加118
writeString(dataOutStream,ChargeTermID,21);
dataOutStream.writeByte(DestTermIDCount);
writeString(dataOutStream,DestTermID,21);
dataOutStream.writeByte(MsgLength);
//dataOutStream.writeBytes(MsgContent);
writeString_old(dataOutStream,MsgContent,MsgLength);
writeString(dataOutStream,Reserve,8);
//返回消息的字节流
dataPack = byteArrayOutStream.toByteArray();
return this.dataPack;
}catch(Exception e){
e.printStackTrace();
System.out.println("[SMGP] makeLoginMsgPack.pack() error : "+e.getMessage());
return null;
}
}
/**取得5.3.1 active_test的消息包
* 5.3.7 active_test
**/
private byte[] makeDeliverRespMsgPack(){
ByteArrayOutputStream byteArrayOutStream = new ByteArrayOutputStream();
DataOutputStream dataOutStream = new DataOutputStream(byteArrayOutStream);
try{
//MT消息长度
this.PacketLength = 26;
//打包MT消息报头
packHead(dataOutStream);
//打包消息体
writeString(dataOutStream,MsgID,10);
dataOutStream.writeInt(this.Status);
//返回消息的字节流
dataPack = byteArrayOutStream.toByteArray();
return this.dataPack;
}catch(Exception e){
e.printStackTrace();
System.out.println("[SMGP] makeActiveMsgPack.pack() error : "+e.getMessage());
return null;
}
}
/**取得5.3.1 active_test的消息包
* 5.3.7 active_test
**/
private byte[] makeActiveMsgPack(){
ByteArrayOutputStream byteArrayOutStream = new ByteArrayOutputStream();
DataOutputStream dataOutStream = new DataOutputStream(byteArrayOutStream);
try{
//MT消息长度
this.PacketLength = 12;
//打包MT消息报头
packHead(dataOutStream);
//返回消息的字节流
dataPack = byteArrayOutStream.toByteArray();
return this.dataPack;
}catch(Exception e){
e.printStackTrace();
System.out.println("[SMGP] makeActiveMsgPack.pack() error : "+e.getMessage());
return null;
}
}
/**取得5.3.8. acitve_test_resp的消息包
**/
private byte[] makeActiveRespMsgPack(){
ByteArrayOutputStream byteArrayOutStream = new ByteArrayOutputStream();
DataOutputStream dataOutStream = new DataOutputStream(byteArrayOutStream);
try{
//MT消息长度
this.PacketLength = 12;
//打包MT消息报头
packHead(dataOutStream);
//返回消息的字节流
dataPack = byteArrayOutStream.toByteArray();
return this.dataPack;
}catch(Exception e){
e.printStackTrace();
System.out.println("[SMGP] makeActiveMsgPack.pack() error : "+e.getMessage());
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -