?? smgp.java
字號:
package smgpgw;
//package smgw;
import java.io.*;
import java.util.Calendar;
/**
* Title: SMGP 數據封裝類
* Description:
* 該類用途 1、調用getCommand()把要發送的信息通過SMGP_Data類傳入,根據SMGP協議打包后,輸出到DataOutputStream流中
* 2、調用parseMessage()把接受的信息通過Byte[]傳入,根據SMGP協議解析后,返回SMGP_Data對象
* 另:如何設置類SMGP_Data請參閱SMGP_Data.java文件
* Copyright:yongliu wangping Copyright (c) 2001 /3
* Company:talkeweb
* @author
* @version 1.0
*/
public class Smgp {
/************************************************************************/
//SMGP協議中各種命令的常量定義 參見SMGPV1.1
public static final int SMGP_login = 0x00000001; //請求連接
public static final int SMGP_login_resp = 0x80000001; //請求連接應答
public static final int SMGP_submit = 0x00000002;
public static final int SMGP_submit_resp = 0x80000002;
public static final int SMGP_deliver = 0x00000003;
public static final int SMGP_deliver_resp = 0x80000003;
public static final int SMGP_active_test = 0x00000004;
public static final int SMGP_active_test_resp = 0x80000004;
/******************************************************************************
SMGP協議中返回的命令狀態信息代碼定義
*/
public static final int CORRECT = 0; //成功
public static final int SYSTEMBUZY_ERR = 1; //系統忙
public static final int EXCEEDCONNECT_ERR = 2; //超過最大連接數
public static final int MISSTRUCTURE_ERR = 10; //消息結構錯
public static final int MISCOMMOND_ERR = 11; //命令字錯
public static final int REPEATSEQ_ERR = 12; //序列號重復
public static final int MISIP_ERR = 20; //IP地址錯
public static final int MISAUTH_ERR = 21; //認證錯
public static final int HIGHVERSION_ERR = 22; //版本太高
public static final int MISMSGTYPE_ERR = 30; //非法消息類型(SMType)
public static final int MISPRIORITY_ERR = 31; //非法優先級(Priority)
public static final int MISFEETYPE_ERR = 32; //非法資費類型(FeeType)
public static final int MISFEECODE_ERR = 33; //非法資費代碼(FeeCode)
public static final int MISFORMAT_ERR = 34; //非法短消息格式(MsgFormat)
public static final int MISTIMESTRUCTURE_ERR = 35; //非法時間格式
public static final int MISLENGTH_ERR = 36; //非法短消息長度(MsgLength)
public static final int MISAVAILABILITY_ERR = 37; //有效期已過
public static final int MISQUERYTYPE_ERR = 38; //非法查詢類別(QueryType)
public static final int MISROUTE_ERR = 39; //路由錯誤
public static final int OTHERS_ERR = 9; //其他錯誤
public static byte[] Lmsgid = new byte[8];
private static final String READSTRINGERROR = "readStringError";
//-----------------------------------------------
/**
* 接口函數。調用該函數把發送信息按SMGP協議打包,打包結果輸入到流中
*/
//public static boolean getCommand( DataOutputStream dataOutStream,SMGP_Data SmgpData){
public synchronized static boolean getCommand(DataOutputStream dataOutStream,
SmgpData SmgpData) {
switch (SmgpData.headCmdID) {
case SMGP_active_test: //連接測試
return getActiveTestCommand(dataOutStream, SmgpData);
case SMGP_active_test_resp: //連接測試應答
return getActiveTestRepCommand(dataOutStream, SmgpData);
case SMGP_login_resp: //連接請求回應
return getConnectRepCommand(dataOutStream, SmgpData);
case SMGP_submit_resp: //submit回應
return getSubmitRepCommand(dataOutStream, SmgpData);
case SMGP_deliver: //下發短信
return getDeliverCommand(dataOutStream, SmgpData);
}
return true;
}
private static byte connectdata1(long num) {
byte x = (byte) (num / (256 * 256));
return x;
}
private static byte connectdata2(long num) {
byte x = (byte) ( (num / 256) % 256);
return x;
}
private static byte connectdata3(long num) {
byte x = (byte) (num % 256);
return x;
}
//-------------------------------------------------------------
private static boolean getActiveTestCommand(DataOutputStream dataOutStream,
SmgpData SmgpData) {
try {
int location = 0;
byte[] data = new byte[12];
byte[] tmp = toByteArray( (int) 12);
for (int i = 0; i < tmp.length; i++)
data[location++] = tmp[i];
tmp = toByteArray(SmgpData.headCmdID);
for (int i = 0; i < tmp.length; i++)
data[location++] = tmp[i];
tmp = toByteArray(SmgpData.headSeqcNo);
for (int i = 0; i < tmp.length; i++)
data[location++] = tmp[i];
dataOutStream.write(data);
dataOutStream.flush();
}
catch (IOException e) {
return false;
}
return true;
}
private static boolean getActiveTestRepCommand(DataOutputStream dataOutStream,
SmgpData SmgpData) {
try {
int location = 0;
byte[] data = new byte[12];
byte[] tmp = toByteArray( (int) 12);
for (int i = 0; i < tmp.length; i++)
data[location++] = tmp[i];
tmp = toByteArray(SmgpData.headCmdID);
for (int i = 0; i < tmp.length; i++)
data[location++] = tmp[i];
tmp = toByteArray(SmgpData.headSeqcNo);
for (int i = 0; i < tmp.length; i++)
data[location++] = tmp[i];
dataOutStream.write(data);
dataOutStream.flush();
}
catch (IOException e) {
return false;
}
return true;
}
private static boolean getConnectRepCommand(DataOutputStream dataOutStream,
SmgpData SmgpData) {
try {
int location = 0;
byte[] data = new byte[33];
byte[] tmp = toByteArray( (int) 33);
for (int i = 0; i < tmp.length; i++)
data[location++] = tmp[i];
tmp = toByteArray(SmgpData.headCmdID);
for (int i = 0; i < tmp.length; i++)
data[location++] = tmp[i];
tmp = toByteArray(SmgpData.headSeqcNo);
for (int i = 0; i < tmp.length; i++)
data[location++] = tmp[i];
tmp = toByteArray(SmgpData.status);
for (int i = 0; i < tmp.length; i++)
data[location++] = tmp[i];
writeString(SmgpData.AuthenticatorServer, data, 16, location);
location = location + 16;
data[location++] = SmgpData.version;
dataOutStream.write(data);
dataOutStream.flush();
}
catch (IOException e) {
return false;
}
return true;
}
//1215
public synchronized static boolean getSubmitRepCommand(DataOutputStream
dataOutStream, SmgpData SmgpData) {
try {
int location = 0;
byte[] data = new byte[26];
byte[] tmp = toByteArray( (int) 26);
for (int i = 0; i < tmp.length; i++)
data[location++] = tmp[i];
tmp = toByteArray(SmgpData.headCmdID);
for (int i = 0; i < tmp.length; i++)
data[location++] = tmp[i];
tmp = toByteArray(SmgpData.headSeqcNo);
for (int i = 0; i < tmp.length; i++)
data[location++] = tmp[i];
// System.err.println("nnnnnnnnnnnnnnnnnnnnnnnnnnnnn:" +SmgpData.MsgID);
writeString(SmgpData.MsgID, data, 10, location);
location = location + 10;
tmp = toByteArray(SmgpData.status);
for (int i = 0; i < tmp.length; i++)
data[location++] = tmp[i];
dataOutStream.write(data);
dataOutStream.flush();
}
catch (IOException e) {
System.err.println("getSubmitRepCommand:" + e.getMessage());
return false;
}
return true;
}
public synchronized static boolean getDeliverCommand(DataOutputStream
dataOutStream, SmgpData SmgpData) {
byte[] msgText = null;
int totalSize;
int msgLen;
try {
if (SmgpData.msgIsReport == 1) {}
else {
/**
* modified by me(yzx) at 2004-03-03:修正原來籠統的轉換
*/
if (SmgpData.msgMsgFormat == 8) { //lgh
//SmgpData.msgContent=new String(SmgpData.msgContent.getBytes ("UTF-16BE"),0);
SmgpData.msgMsgContent = new String(SmgpData.msgMsgContent.getBytes(),
"UTF-16BE");
SmgpData.msgMsgLength = (byte) SmgpData.msgMsgContent.getBytes(
"UTF-16BE").length;
msgText = SmgpData.msgMsgContent.getBytes(
"UTF");
}
else if (SmgpData.msgMsgFormat == 15) { //lgh
SmgpData.msgMsgContent = new String(SmgpData.msgMsgContent.getBytes(
"gb2312"));
SmgpData.msgMsgLength = (byte) SmgpData.msgMsgContent.getBytes(
"gb2312").length;
msgText = SmgpData.msgMsgContent.getBytes(
"gb2312");
}
else if (SmgpData.msgMsgFormat == 4) {
msgText = Smgp.convertStringToBytes(SmgpData.msgMsgContent);
Integer msgTextLen = new Integer(msgText.length);
SmgpData.msgMsgLength = msgTextLen.byteValue();
}
else {
msgText = SmgpData.msgMsgContent.getBytes();
Integer msgTextLen = new Integer(msgText.length);
SmgpData.msgMsgLength = msgTextLen.byteValue();
}
}
// System.err.println("delive msg length:" + SmgpData.msgMsgLength);
if (SmgpData.msgMsgLength < 0) {
msgLen = 256 + SmgpData.msgMsgLength;
}
else {
msgLen = SmgpData.msgMsgLength;
}
if (msgLen > 160) {
// method.error ("send date has too long!");
return false;
}
if (SmgpData.msgIsReport == 1)
msgLen = SmgpData.msgMsgLength = 122;
// System.err.println("delive msg length:" + SmgpData.msgMsgLength);
totalSize = 89 + msgLen;
int location = 0;
byte[] data = new byte[totalSize];
byte[] tmp = toByteArray(totalSize);
/**
* 寫入字節長度
*/
for (int i = 0; i < tmp.length; i++)
data[location++] = tmp[i];
/**
* 寫入命令字
*/
tmp = toByteArray(SmgpData.headCmdID);
for (int i = 0; i < tmp.length; i++)
data[location++] = tmp[i];
/**
* 寫入序列號
*/
tmp = toByteArray(SmgpData.headSeqcNo);
for (int i = 0; i < tmp.length; i++)
data[location++] = tmp[i];
// System.err.println("dddddddddddddddddddddddddd:"+(location-1));
writeString(SmgpData.MsgID, data, 10, location);
location = location + 10;
data[location++] = SmgpData.msgIsReport;
data[location++] = SmgpData.msgMsgFormat;
// System.err.println("qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq:" + SmgpData.msgRecvTime);
writeString(SmgpData.msgRecvTime, data, 14, location);
location = location + 14;
writeString(SmgpData.msgSrcTermID, data, 21, location);
location = location + 21;
writeString(SmgpData.msgDestTermID, data, 21, location);
location = location + 21;
data[location++] = SmgpData.msgMsgLength;
if (SmgpData.msgIsReport == 1) { //狀態報告
// System.err.println("report MsgID:" + SmgpServer.submitResp_msg_id);
// System.err.println("report msgSubmit_date:" + SmgpData.msgSubmit_date);
// System.err.println("report msgdone_date:" + SmgpData.msgdone_date);
// System.err.println("report msgStat:" + SmgpData.msgStat);
// System.err.println("report msgErr:" + SmgpData.msgErr);
writeString("Id:"+SmgpServer.submitResp_msg_id, data, 13, location);
location = location + 13;
writeString(" sub:001", data, 8, location);
location = location + 8;
writeString(" DIvrd:001", data, 10, location);
location = location + 10;
writeString(" Submit_date:"+SmgpData.msgSubmit_date, data, 23, location);
location = location + 23;
writeString(" done_date:"+SmgpData.msgdone_date, data, 21, location);
location = location + 21;
writeString(" stat:"+SmgpData.msgStat, data, 13, location);
location = location + 13;
writeString(" err:"+SmgpData.msgErr, data, 8, location);
location = location + 8;
writeString(" text:00000000000000000000", data, 26, location);
location = location + 26;
}
else {
for (int j = 0; j < msgText.length; j++) {
data[location++] = msgText[j];
}
}
SmgpData.msgReserve = " ";
if (writeString(SmgpData.msgReserve, data, 8, location) == false)
return false;
dataOutStream.write(data);
dataOutStream.flush();
}
catch (Exception e) {
System.out.println("deliver error=" + e);
e.printStackTrace();
return false;
}
return true;
}
//-----------------------------------------------
/**
* 解析頭內容
*/
private static void parseHead(DataInputStream dataInStream, SmgpData recvdMsg) {
try {
//note:流中不包含消息包長度的四個字節
recvdMsg.headCmdID = dataInStream.readInt();
recvdMsg.headSeqcNo = dataInStream.readInt();
}
catch (IOException e) {
recvdMsg.headCmdID = 0;
}
return;
}
//-------------------------------------------------------------
/**
* 接口函數。根據SMGP協議解析傳入的數據,解析后內容通過SMGP_Data返回
* 注:如果結果中cmdID == 0 則解析錯誤
*/
public static SmgpData parseMessage(byte[] receivedData) {
ByteArrayInputStream byteStream = new ByteArrayInputStream(receivedData);
DataInputStream dataInStream = new DataInputStream(byteStream);
SmgpData recvdMsg = new SmgpData();
/* recvdMsg.headCmdID=toInteger(receivedData,0);
recvdMsg.headSeqcNo=toInteger(receivedData,4);*/
parseHead(dataInStream, recvdMsg);
if (recvdMsg.headCmdID == 0)
return recvdMsg;
if (receivedData.length < 8) //只有頭信息8個字節
return recvdMsg;
switch (recvdMsg.headCmdID) {
case SMGP_login: //請求連接
parseBodyConnect(dataInStream, recvdMsg);
break;
case SMGP_deliver_resp: //下發短信應答
parseBodyDeliverRep(dataInStream, recvdMsg);
break;
case SMGP_submit: //提交短信
parseBodySubmit(dataInStream, recvdMsg);
break;
}
return recvdMsg;
}
private static void parseBodyConnect(DataInputStream dataInStream,
SmgpData SmgpData) {
try {
SmgpData.ClientID = readString(dataInStream, 8);
if (SmgpData.ClientID == READSTRINGERROR) {
SmgpData.status = MISAUTH_ERR;
return;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -