📄 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 + -