public FloodManager(final int msecPerTick, final FloodFilter... filters) { _tickLength = msecPerTick; _filters = filters; int max = 1; for (FloodFilter filter : _filters) max = Math.max(filter.getTickLimit() + 1, max); _tickAmount = max; NetFlusher.add( new Runnable() { @Override public void run() { flush(); } }, 60000); }
public Result isFlooding(final boolean increment) { final int currentTick = getCurrentTick(); if (currentTick - _lastTick >= _ticks.length) { _lastTick = currentTick; Arrays.fill(_ticks, (short) 0); } else if (_lastTick > currentTick) { log.warn( "The current tick (" + currentTick + ") is smaller than the last (" + _lastTick + ")!", new IllegalStateException()); _lastTick = currentTick; } else while (currentTick != _lastTick) { _lastTick++; _ticks[_lastTick % _ticks.length] = 0; } if (increment) _ticks[_lastTick % _ticks.length]++; for (FloodFilter filter : _filters) { int previousSum = 0; int currentSum = 0; for (int i = 0; i <= filter.getTickLimit(); i++) { int value = _ticks[(_lastTick - i) % _ticks.length]; if (i != 0) previousSum += value; if (i != filter.getTickLimit()) currentSum += value; } if (previousSum > filter.getRejectLimit() || currentSum > filter.getRejectLimit()) return Result.REJECTED; if (previousSum > filter.getWarnLimit() || currentSum > filter.getWarnLimit()) return Result.WARNED; } return Result.ACCEPTED; }