📄 receivedatahandler.java
字号:
package com.gctech.cngphb;
/** 数据接收者,除了登录时的返回消息,所有信息接收都通过这个线程。
*
* */
import java.io.IOException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import org.apache.log4j.Logger;
import com.gctech.cngphb.CngpCommunicator;
import com.gctech.cngphb.msg.CNGPDeliverMessage;
import com.gctech.cngphb.msg.CNGPDeliverMessageResp;
import com.gctech.cngphb.msg.CNGPSubmitMessageResp;
import com.gctech.cngphb.msg.Constants;
import com.gctech.util.Tools;
public class ReceiveDataHandler implements Runnable{
protected void processDeliver(CNGPDeliverMessage dlv){}
protected void processSubmitRes(CNGPSubmitMessageResp resp){}
CngpCommunicator commu;
public void setCommunicator(CngpCommunicator commu){
this.commu = commu;
}
//读取数据,放入不同队列
public void run(){
while (true){
try {
//除了数据长度的数据
byte[] data = commu.receive();
//没有数据,休眠1秒钟
if ( data == null ){
Thread.sleep(500L);
continue;
}
int commandId = Tools.byte2int(data);
switch ( commandId ){
//上行,状态报告消息,放入队列
case Constants.DELIVER_CMD:
CNGPDeliverMessage dlv = new CNGPDeliverMessage();
dlv.nSequenceID = Tools.byte2int(data, 8);
dlv.sMsgId = new String(data, 12, 10);
dlv.nIsReport = data[22];
dlv.nMsgFormat = data[23];
dlv.sRecvTime = new String(data, 24, 14);
dlv.sSrcTerminalId = new String(data, 38, 21);
dlv.sDestId = new String(data, 59, 21);
dlv.nMsgLength = Tools.unsignedByte2int(data[80]);
dlv.sMsgContent = new String(data, 81, dlv.nMsgLength);
//打印上行消息
logger.debug(dlv);
//处理消息
this.processDeliver(dlv);
// ReceiveDataList.getInstance().addMo(dlv);
//回复信息
CNGPDeliverMessageResp dlvResp = new CNGPDeliverMessageResp();
dlvResp.nSequenceID = dlv.nSequenceID;
dlvResp.sMsgId = dlv.sMsgId;
commu.deliverResp(dlvResp);
break;
case Constants.SUBMIT_CMD_RES:
//下行回复,增加到返回列表。
CNGPSubmitMessageResp resp = new CNGPSubmitMessageResp();
resp.nResult = Tools.byte2int(data, 4);
resp.nSequenceID = Tools.byte2int(data, 8);
resp.sMsgId = new String(data, 12, 10);
this.processSubmitRes(resp);
//ReceiveDataList.getInstance().addMt(resp);
break;
case Constants.ACTIVE_CMD:
//检测信息,直接回复
int seq = Tools.byte2int(data, 8);
commu.activeTestRes(seq);
break;
/* case Constants.ACTIVE_CMD_RES:
//检测回复,增加到检测信息列表。
CNGPActiveTestMessageResp activeResp = new CNGPActiveTestMessageResp();
activeResp.stat = Tools.byte2int(data, 4);
activeResp.nSequenceID = Tools.byte2int(data, 8);
ReceiveDataList.getInstance().addActive(activeResp);
break;*/
default:
logger.warn("can't find the command Id:"+commandId+"!");
break;
}
}catch(SocketTimeoutException sock){
logger.debug("waiting for data!");
try {
//edit by liyi ,timeout后不能重连
commu.relogin();
//Thread.sleep(500L);
}
catch (IOException ex1) {
logger.error(ex1, ex1);
}
}catch (SocketException se){
//重新连接网关。
try {
commu.relogin();
}
catch (IOException ex2) {
logger.error(ex2, ex2);
try {
Thread.sleep(3000L);
}
catch (InterruptedException ex3) {
logger.error(ex3, ex3);
}
}
}catch (Throwable ex) {
logger.error(ex, ex);
try {
Thread.sleep(1000L);
}
catch (InterruptedException ex1) {
logger.error(ex1, ex1);
}
}finally{
/*try {
Thread.sleep(10);
}
catch (InterruptedException ex1) {
logger.error(ex1, ex1);
}*/
}
}
}
static final Logger logger = Logger.getLogger(ReceiveDataHandler.class);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -