?? agentcrbtclienthandler.java
字號:
/**
* AgentCrbtWSHandler.java
* @版權: Copyright (C) 2007
* @公司:北京漢銘信通科技有限公司
* @url: www.aceway.com.cn
*/
package com.aceway.vas.xjcrgw.ws.handler;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import com.aceway.vas.commons.tcp.IClientHandler;
import com.aceway.vas.sjcraw.cbgp201.MsgHead;
import com.aceway.vas.sjcraw.cbgp201.common.MsgInfo;
/**
* 此類是 CRBT_AGENT 模塊的客戶端消息包轉發處理類 負責判斷通信層消息包是否完整,并轉發給響應的消息處理器
*
* @author zhou tao
*/
public class AgentCrbtClientHandler implements IClientHandler {
// 消息接收器列表
private static List msgReceiveList = new ArrayList();
/**
* 唯一實例
*/
private static AgentCrbtClientHandler instance = new AgentCrbtClientHandler();
//最后一次數據傳輸時間
private static long lastDataTransTime;
public static synchronized long getLastDataTransTime() {
return lastDataTransTime;
}
private AgentCrbtClientHandler() {
}
public static AgentCrbtClientHandler getInstance() {
return instance;
}
/**
* 添加消息處理器
* @param l
*/
public void addMsgReceiver(IMsgReceiveable l) {
synchronized (msgReceiveList) {
if (!msgReceiveList.contains(l)) {
msgReceiveList.add(l);
}
}
}
/**
* 連接已經建立的時候響應
*/
public void onConnect(String ip, int port) {
IMsgReceiveable msgRece = null;
for (int i = msgReceiveList.size() - 1; i >= 0; i--) {
msgRece = (IMsgReceiveable) msgReceiveList.get(i);
msgRece.receiveonConnectedMsg(ip, port);
}
}
// 當連接斷開的時候,響應
public void onDisconnect() {
}
/**
* 對bytes進行切片, 但不用對object進行切片,
* 這里的bytes并不定是一個完整的消息包,它是服務器端傳過來的消息,可能是里面包括好幾個消息包 對bytes進行切片.
*
* @param bytes
* @return int 如果能切到一個完整的消息包,則返回這個完整消息包的長度,否則返回-1
*/
public int slice(byte[] bytes) {
byte[] b = bytes;
// 得到消息包長度,判斷獲取的消息包是否完整
MsgHead msgHead = new MsgHead(b);
int commandLength = Integer.parseInt(msgHead.getCommandLength());
if (commandLength > b.length) {
return b.length;
} else {
return -1;
}
}
/**
* 將一個完整的消息包回傳給應用層,bytes是一個完整的消息包 并做消息包轉發
*
* @param bytes
* 完整消息包
*/
public void onReceiveMsg(byte[] bytes) {
//更新最后一次數據傳輸時間
synchronized (this) {
lastDataTransTime = System.currentTimeMillis();
}
MsgHead msghead = new MsgHead(bytes);
String category = msghead.getCategory();
String subCommand = msghead.getSubCommand();
// 收到完整消息包,通知消息接收器處理消息
IMsgReceiveable msgRece = null;
for (int i = msgReceiveList.size() - 1; i >= 0; i--) {
msgRece = (IMsgReceiveable) msgReceiveList.get(i);
if ((category.equals(MsgInfo.CATEGORY_BUSSINESS)
&& subCommand.equals(MsgInfo.SUB_COMMAND_RESPONSE))) {
// 業務操作請求應答
msgRece.receiveOperRespMsg(bytes);
} else if ((category.equals(MsgInfo.CATEGORY_NET_CONNECT)
&& subCommand.equals(MsgInfo.SUB_COMMAND_RESPONSE))) {
// 網絡連接請求應答
msgRece.receiveNetConnRespMsg(bytes);
} else if ((category.equals(MsgInfo.CATEGORY_NET_CONNECT)
&& subCommand.equals(MsgInfo.SUB_COMMAND_REQUEST))) {
//網絡連接請求(雙方可以互發)
msgRece.receiveNetConnReqMsg(bytes);
}
}
}
/**
* 將服務器端傳過來的對象回傳給應用層
*/
public void onReceiveMsg(Serializable obj) {
}
/**
* 將已經發送的消息包回傳給應用層
*/
public void onSendedMsg(byte[] bytes) {
//更新最后一次數據傳輸時間
synchronized(this) {
lastDataTransTime = System.currentTimeMillis();
}
IMsgReceiveable msgRece = null;
for (int i = msgReceiveList.size() - 1; i >= 0; i--) {
msgRece = (IMsgReceiveable) msgReceiveList.get(i);
msgRece.receiveOnSendMsg(bytes);
}
}
/**
* 將已經發送的對象回傳給應用層
*/
public void onSendedMsg(Serializable obj) {
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -