package org.gander.core.packet; import org.gander.core.Session; /** * @author Gander * @Date: 10-Feb-2008 * @Time: 16:59:20. */ public class PacketThrottleFilter { public PacketThrottleFilter() { this.lastPacketRecieved = System.currentTimeMillis(); this.setThrottleDelay(1500); // 1.5 seconds } /** * Check's if the packet * @param id the packetID to be nested * @return true if the delay is less than throttleDelay */ public boolean packetThrottled(int id) { if(id != this.lastPacketID && this.lastPacketID != 0) { return false; } boolean foundPacket = false; for(int i = 0; i < throttleFilters.length; i++){ if(throttleFilters[i] == id) { foundPacket = true; break; } } if(!foundPacket) { return foundPacket; } int check = (int)(System.currentTimeMillis() - this.lastPacketRecieved); if(check < this.throttleDelay) { return true; } this.lastPacketID = id; this.lastPacketRecieved = System.currentTimeMillis(); return false; } /** * Set's the Delay to the parameter * @param delay the delay to set the ThrottleDelay to the delay */ public void setThrottleDelay(int delay) { this.throttleDelay = delay; } /** * Get's the last packet time * @return the last packet Time */ public long getLastPacketTime(){ return this.lastPacketRecieved; } /** * Gets the Throttle delay * @return An integer representing the Throttle delay ThrottleDelay