Code Dump
Posted 01-20-2010 at 09:42 AM by Joe2003
Code:
public class Monitor extends Thread
{
private int maxOccurance;
private int totalOccurance;
private int timePeriod;
private double average;
private final int HISTORY_LENGTH = 10;
private int[] history = new int[HISTORY_LENGTH];
private int historyPtr;
public Monitor(int time, int frequency)
{
maxOccurance = frequency;
timePeriod = time * 1000;
totalOccurance = 0;
historyPtr = 0;
average = maxOccurance;
this.start();
}
public void update()
{
totalOccurance++;
}
private double getAverage()
{
double total = 0.0;
for(int i = 0; i < HISTORY_LENGTH; i++)
{
total += history[i];
}
total = total / HISTORY_LENGTH;
return total;
}
public void run()
{
boolean loopCompleted = false;
double currentAverage = 0.0;
while(!loopCompleted)
{
if(historyPtr == HISTORY_LENGTH)
{
historyPtr = 0;
}
try
{
Thread.sleep(timePeriod);
}
catch(InterruptedException ex)
{
continue;
}
synchronized(this)
{
currentAverage = getAverage();
if(totalOccurance > maxOccurance || currentAverage > average)
{
loopCompleted = true;
history[historyPtr] = totalOccurance;
historyPtr++;
//At this point generate an event log and shutdown the server.
//If we get here it means that a DOS is happening
}
else
{
history[historyPtr] = totalOccuance;
historyPtr++;
totalOccurance = 0;
}
}
}
}
}
Total Comments 0




