📄 abstractcngpcommu.java
字号:
package com.gctech.cngphb;
import java.io.IOException;
import java.net.Socket;
import com.gctech.cngphb.util.CongestionState;
import com.gctech.cngphb.util.SequenceManager;
import com.gctech.cngphb.msg.Constants;
import com.gctech.cngphb.util.CNGPTimestampFormat;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.InputStream;
import org.apache.log4j.Logger;
import com.gctech.cngphb.msg.CNGPSubmitMessage;
import com.gctech.cngphb.msg.CNGPHead;
import com.gctech.cngphb.msg.CNGPDeliverMessageResp;
import java.io.OutputStream;
import com.gctech.util.MD5;
import com.gctech.util.Tools;
/**
* <p>Title: CNGP API缺省实现.</p>
* <p>Description: 固网短信SP API缺省实现.</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: GCTECH</p>
* @author 王红宝
* @version $Id: AbstractCngpCommu.java,v 1.1.1.1 2004/07/26 02:44:14 lijz Exp $
*/
public abstract class AbstractCngpCommu implements CngpCommunicator {
//检测的间隔,是CNGP中的C,取3分钟
static final long ACTIVE_TEST_INTERVAL = 3L*60L*1000L;
//超时时间,3分钟。
static final int TIME_OUT = 3*60*1000;
//最后一次通信时间
private long lastCommunicate;
//服务器地址
String host;
//服务器端口
int port;
//登录ID
String clientId;
//认证密码
String auth;
//登录方式
byte loginMode;
//版本
byte version;
public AbstractCngpCommu() {
}
Socket socket = null;
//重新登录
public int relogin() throws IOException{
if ( socket != null && socket.isConnected() ){
socket.close();
}
return login();
}
int login() throws IOException{
return login(this.host, this.port,
this.clientId, this.auth, this.loginMode, this.version);
}
abstract protected byte[] getPlain(String clientId, String auth, String time);
/**
* 登录请求.
* */
public int login(String host, int port,
String clientId, String auth, byte loginMode, byte version) throws IOException{
logger.debug("login to " + host +":"+port+" with clientId:"+
clientId +",auth:"+auth+",login mode is:"+loginMode);
this.host = host;
this.port = port;
this.clientId = clientId;
this.auth = auth;
this.loginMode = loginMode;
this.version = version;
socket = new Socket(host, port);
socket.setSoTimeout( TIME_OUT );
byte[] inMsg = new byte[48];
//复制包头
System.arraycopy( Tools.int2byte(48), 0, inMsg, 0, 4) ;
System.arraycopy(Tools.int2byte(Constants.LOGIN_CMD), 0, inMsg, 4, 4) ;
//状态为0
System.arraycopy(Tools.int2byte(SequenceManager.nextSeq()), 0, inMsg, 12, 4);
//复制包体 clientId,客户端密码,登录类型,时间戳,版本
byte[] temp = clientId.getBytes();
System.arraycopy(temp, 0, inMsg, 16, temp.length);
//MD5客户端密码
//1.组合md5的原始字节
//ClientID+7字节的二进制0 +Shared secret+TimeStamp
String time = CNGPTimestampFormat.getInstance().currentTimestamp();
//2.MD5 hash
MD5 md5 = new MD5();
byte[] plain = this.getPlain(clientId, auth, time);
byte md5Msg[] = md5.getMD5ofBytes(plain, plain.length) ;
System.arraycopy(md5Msg, 0, inMsg, 26, md5Msg.length);
//登录模式、时间戳及版本号
inMsg[42] = loginMode;
byte[] bTime = Tools.int2byte(Integer.parseInt(time));
System.arraycopy(bTime, 0, inMsg, 43, 4);
inMsg[47] = version;
//发送登录消息
send(inMsg);
logger.debug("send login msg OK!");
//接收登录消息
InputStream in = socket.getInputStream();
byte[] dataLength = new byte[4];
in.read(dataLength);
int msgSize = Tools.byte2int(dataLength);
logger.debug("login response size is:" + msgSize);
int msgLength = msgSize - 4;
byte[] data = new byte[33];
int realLen = in.read(data);
logger.debug("login response size is:" + realLen);
int status = Tools.byte2int(data, 4);
if ( status != 0 ){
logger.error("login response is:"+status+",please check the code!");
return status;
}
/*int seqId = Tools.byte2int(head, 12);
byte[] resp = null ;
int remain = msgSize - 16;
resp = new byte[remain];
in.read(resp);
byte[] plain2 = ("" + status
+ new String(md5Msg) + auth).getBytes();
byte[] myHash = md5.getMD5ofBytes( plain2, plain2.length);
//比较hash值
for ( int i = 0; i < 16; i++ ){
if ( myHash[i] != resp[i] )
{
logger.error("the server login response hash error!");
return -100;
}
}*/
return 0;
}
void arraycopy(String temp, byte[] dest, int start){
if ( temp != null ){
System.arraycopy(temp.getBytes(), 0, dest, start, temp.getBytes().length);
}
}
//发送下行
public int submit(CNGPSubmitMessage msg) throws IOException {
int nDestLength = 21*msg.nDestCount;
int length = 126 + nDestLength + 1 + msg.nMsgLength + 5;
logger.debug("submit msg length is:" + length);
byte[] inMsg = new byte[length];
//封装包头
System.arraycopy( Tools.int2byte(length), 0, inMsg, 0, 4);
System.arraycopy(Tools.int2byte(Constants.SUBMIT_CMD), 0, inMsg, 4, 4);
System.arraycopy(Tools.int2byte(msg.nSequenceID), 0, inMsg, 12, 4);
//封装包体
//SP ID
byte[] temp = this.clientId.getBytes();
System.arraycopy(temp, 0, inMsg, 16, temp.length);
inMsg[26] = msg.subType;
inMsg[27] = msg.nNeedReply;
inMsg[28] = msg.nMsgLevel;
temp = msg.sServiceId.getBytes();
System.arraycopy(temp, 0, inMsg, 29, temp.length);
temp = msg.sFeeType.getBytes();
System.arraycopy(temp, 0, inMsg, 39, temp.length);
inMsg[41] = msg.nFeeUserType;
temp = msg.sFeeCode.getBytes();
System.arraycopy(temp, 0, inMsg, 42, temp.length);
inMsg[48] = msg.nMsgFormat;
arraycopy(msg.sValidTime, inMsg, 49);
arraycopy(msg.sAtTime, inMsg, 66);
temp = msg.sSrcId.getBytes();
System.arraycopy(temp, 0, inMsg, 83, temp.length);
//如果是对计费终端计费
if ( msg.nFeeUserType == Constants.FEE_USER_TYPE_CHARGE_TERM ){
arraycopy(msg.sFeeMobile, inMsg, 66);
}
inMsg[125] = msg.nDestCount;
temp = msg.sDestTerminalId.getBytes();
System.arraycopy(temp, 0, inMsg, 126, temp.length);
int start = nDestLength+126;
byte[] contentLen = Tools.short2byte(msg.nMsgLength);
inMsg[start] = contentLen[1];
start++;
/* if ( msg.nMsgFormat == 15 ){
temp = msg.sMsgContent.getBytes("GB2312");
}else{*/
temp = msg.sMsgContent.getBytes();
// }
System.arraycopy(temp, 0, inMsg, start, temp.length);
start += msg.nMsgLength;
temp = Tools.short2byte(msg.procTag);
System.arraycopy(temp, 0, inMsg, start, temp.length);
start += temp.length;
temp = Tools.short2byte(msg.procLength);
System.arraycopy(temp, 0, inMsg, start, temp.length);
start += temp.length;
inMsg[start] = msg.procValue;
//发送消息
send(inMsg);
logger.debug("send msg ok!");
return 0;
}
//上行回复
public void deliverResp(CNGPDeliverMessageResp dlvResp) throws IOException{
byte[] inMsg = new byte[31];
System.arraycopy(Tools.int2byte(26), 0, inMsg, 0, 4);
System.arraycopy(Tools.int2byte(Constants.DELIVER_CMD_RES), 0, inMsg, 4, 4);
//命令状态为0,不做复制
System.arraycopy(Tools.int2byte(dlvResp.nSequenceID), 0, inMsg, 12, 4);
byte[] msgId = dlvResp.sMsgId.getBytes();
System.arraycopy(msgId, 0, inMsg, 16, msgId.length);
inMsg[26] = CongestionState.CongestionState[0];
inMsg[27] = CongestionState.CongestionState[1];
inMsg[28] = CongestionState.CongestionState[2];
inMsg[29] = CongestionState.CongestionState[3];
inMsg[30] = CongestionState.CongestionState[4];
send(inMsg);
}
//发送测试消息
public boolean activeTest(int seq) throws IOException {
byte[] inMsg = new byte[16];
//复制包头
System.arraycopy( Tools.int2byte(16), 0, inMsg, 0, 4) ;
System.arraycopy(Tools.int2byte(Constants.ACTIVE_CMD), 0, inMsg, 4, 4);
System.arraycopy(Tools.int2byte(seq), 0, inMsg, 12,4);
send(inMsg);
return true;
}
//检测回复
public boolean activeTestRes(int seq) throws IOException {
byte[] inMsg = new byte[16];
//复制包头
System.arraycopy( Tools.int2byte(16), 0, inMsg, 0, 4) ;
System.arraycopy(Tools.int2byte(Constants.ACTIVE_CMD_RES), 0, inMsg, 4, 4);
System.arraycopy(Tools.int2byte(seq), 0, inMsg, 12,4);
send(inMsg);
return true;
}
//接受消息
public byte[] receive() throws IOException {
DataInputStream in = new DataInputStream(socket.getInputStream());
//读取长度
int msgLength = in.readInt();
logger.debug("receive msgSize is:" + msgLength);
if ( msgLength >= 16 && msgLength <= 2048 ){
//读取数据
byte[] resp = new byte[msgLength-4];
in.read(resp);
this.lastCommunicate = System.currentTimeMillis();
return resp;
}else{
return null;
}
}
//退出
public void quit() throws IOException {
if ( socket != null ){
byte[] inMsg = new byte[16];
//复制包头
System.arraycopy( Tools.int2byte(16), 0, inMsg, 0, 4) ;
System.arraycopy(Tools.int2byte(Constants.EXIT_CMD), 0, inMsg, 4, 4);
System.arraycopy(Tools.int2byte(SequenceManager.nextSeq()), 0, inMsg, 12,4);
try {
//发送请求
send(inMsg);
}finally{
//关闭socket
socket.close();
socket = null;
}
}
}
//发送消息
private void send( byte[] msg ) throws IOException{
OutputStream out = socket.getOutputStream();
out.write( msg ) ;
out.flush();
this.lastCommunicate = System.currentTimeMillis();
}
public static void main(String[] args) throws Throwable{
byte b = (byte)244;
if ( b > 0 )
System.out.println(b);
else{
System.out.println(Integer.toBinaryString(b));
System.out.println(b);
System.out.println(b & 0x7f);
}
}
//如果当前时间减去最后一次通信时间大于检测时间,返回真。
public boolean needActiveTest(){
return (System.currentTimeMillis() - this.lastCommunicate)>ACTIVE_TEST_INTERVAL;
}
static final Logger logger = Logger.getLogger(AbstractCngpCommu.class);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -