JML

net.sf.jqql.packets
Class PacketHistory

java.lang.Object
  extended by net.sf.jqql.packets.PacketHistory

public class PacketHistory
extends Object

packet monitor. this class use a hashtable and a linked list to manage received packet, other people can use his method to checkout a packet whether is received, for example resend thread use is to checkout whether answer of a packet is arrived. if is, don't resend. and received thread, once received a packet, save hash value of this packet into this class. and packet handler, also need it to checkout whether a packet is reduplicate. Of course, this method can't perfect avoid any needless resend, but at least have som help. on the side, this packet monitor also manage all request, because answer packet of these packet haven't any usable info. info are all in request packet, so ... 包监视器. 这个类使用一个hashtable和一个linked list来管理收到的包,其他人可以调用他的 方法来检查一个包是否收到,比如重发线程通过它来检查是否一个包的应答已经到达, 如果是,则不需要重发。还有接收线程,一旦收到一个包,就把这个包的hash值存入这 个类,还有包处理器,也要通过来检查是否一个包是重复的。当然这种方法还不能完美 的避免任何不必要的重发,但是至少有点帮助,-_-!.... 另外这个包监视器还要管理所有的请求,因为这些包的应答包里面没有什么有用的信息, 信息都在请求包里,所以...
(edit by notxx) change into using LinkedHashSet and HashMap to manage, speed would be faster. no need to create a Integer object as key, immediacy use packet itself. 改变成使用LinkedHashSet和HashMap来管理. 速度应该快一些. 不需要创建一个Integer对象作为key了, 直接使用Packet本身即可.

Author:
luma, notXX
See Also:
Packet.hashCode(), Packet.equals(Object)

Constructor Summary
PacketHistory()
          constructed function 构造函数
 
Method Summary
 boolean check(int hashValue, boolean add)
          checkout appointed hash value whether is exist 检查指定的hash值是否已经存在
 boolean check(Object packet, boolean add)
          this method checkout packet whether is received, remarkable is that checkout is for hash value of this packet, not for packet object, account of hash value is completing in hashCode in packet.
 boolean check(Packet packet, boolean add)
          this method checkout packet whether is received, remarkable is that checkout is for hash value of this packet, not for packet object, account of hash value is completing in hashCode in packet.
 void clear()
          clear packet monitor buffer 清空包监视缓冲区
 void putSent(OutPacket packet)
          push requst into hashMap 把请求推入哈希表
 OutPacket retrieveSent(InPacket packet)
          return request packet for this reply packet 返回这个回复包对应的请求包
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PacketHistory

public PacketHistory()
constructed function 构造函数

Method Detail

check

public boolean check(Packet packet,
                     boolean add)
this method checkout packet whether is received, remarkable is that checkout is for hash value of this packet, not for packet object, account of hash value is completing in hashCode in packet. if two packets, sequence number or command is different, hash value is different. 这个方法检查包是否已收到,要注意的是检查是针对这个包的hash值进行的, 并不是对packet这个对象,hash值的计算是在packet的hashCode中完成的, 如果两个packet的序号或者命令有不同,则hash值肯定不同。

Parameters:
packet - packet to be checkout 要检查的包
add - if is true, then while this packet is null, add hash of this packet, or don't add 如果为true,则当这个包不存在时,添加这个包的hash,否则不添加
Returns:
if this packet is received return true, or teturn false true如果这个包已经收到,否则false
See Also:
Packet.hashCode(), Packet.equals(Object);

check

public boolean check(int hashValue,
                     boolean add)
checkout appointed hash value whether is exist 检查指定的hash值是否已经存在

Parameters:
hashValue - 哈希值 // hash value
add - true means if not exist then add this hash value true表示如果不存在则添加这个哈希值
Returns:
true means exist true表示已经存在

check

public boolean check(Object packet,
                     boolean add)
this method checkout packet whether is received, remarkable is that checkout is for hash value of this packet, not for packet object, account of hash value is completing in hashCode in packet. if two packets, sequence number or command is different, hash value is different. 这个方法检查包是否已收到,要注意的是检查是针对这个包的hash值进行的, 并不是对packet这个对象,hash值的计算是在packet的hashCode中完成的, 如果两个packet的序号或者命令有不同,则hash值肯定不同。

Parameters:
packet - packet to be checkout 要检查的包
add -
add - if is true, then while this packet is null, add hash of this packet, or don't add 如果为true,则当这个包不存在时,添加这个包的hash,否则不添加
Returns:
if this packet is received return true, or teturn false true表示这个包已经收到,否则false

putSent

public void putSent(OutPacket packet)
push requst into hashMap 把请求推入哈希表

Parameters:
packet - request packet 请求包

retrieveSent

public OutPacket retrieveSent(InPacket packet)
return request packet for this reply packet 返回这个回复包对应的请求包

Parameters:
packet - InPacket
Returns:
your request packet 你的请求包

clear

public void clear()
clear packet monitor buffer 清空包监视缓冲区


JML