All pastes #899677 Raw Edit

Stuff

public text v1 · immutable
·published 2008-02-11 10:35 UTC
rendered paste body
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 <code>throttleDelay</code>
     */
    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 <code>delay</code>
     */
    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 <code>ThrottleDelay</code
     */
    public int getThrottledDelay() {
        return this.throttleDelay;
    }

   private static final int[] throttleFilters = {185, 4, 103, 132, 252, 41, 145, 72, 122, 181, 87, 155, 192, 236, 73};
   private long lastPacketRecieved;
   private int throttleDelay = 0;
   private int lastPacketID = 0;
}