Answer the question
In order to leave comments, you need to log in
Explain the code, if possible with comments?
package bf;
import java.util.HashMap;
import bf.DataBaseException;
import bf.SysOptionController;
class Tries {
private long lastTry;
private int tryCount;
public Tries() {
tryCount = 0;
lastTry = System.currentTimeMillis();
}
public long getLastTry() {
return lastTry;
}
public int getTryCount() {
return tryCount;
}
public void addTryCount() {
this.tryCount++;
lastTry = System.currentTimeMillis();
}
public void reset() {
this.tryCount = 0;
}
}
public class BFController {
private long lockTime;
private int retries;
private final HashMap<String, Tries> map = new HashMap<String, Tries>();
public BFController(SysOptionController soc) throws DataBaseException {
synchronize(soc);
}
public final synchronized void synchronize(SysOptionController soc) throws DataBaseException {
retries = (Integer) soc.getOption("retries").getValue();
lockTime = (Integer) soc.getOption("lockmills").getValue();
}
public synchronized boolean needWait(String ipAnduser) {
Tries t = map.get(ipAnduser);
if (t == null) {
return false;
}
if (t.getTryCount() < retries) {
return false;
}
if (t.getLastTry() < System.currentTimeMillis() - lockTime) {
return false;
}
return true;
}
public synchronized void addTry(String ipAnduser) {
Tries t = map.get(ipAnduser);
if (t == null) {
t = new Tries();
map.put(ipAnduser, t);
}
t.addTryCount();
}
public synchronized void resetTries(String ipAnduser) {
Tries t = map.get(ipAnduser);
if (t == null) {
return;
}
t.reset();
}
}
Answer the question
In order to leave comments, you need to log in
The code is like a code... of moderate curvature. Pulls parameters from some database (timeout and number of attempts), puts some statuses in the Map by the key IP + User, and allows you to query their status (whether the timeout has expired and whether the specified number of attempts has been exhausted) / delete / reset ... in short, it looks a lot like a piece of spamming crap.
What exactly is not clear in this code?
Ask!.. I don't think anyone will just comment on every line of this sheet without asking a specific question :)
Update :
@gurinderu
Cool, but where does the info about the database come from? )))))))
By DataBaseException I guess
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question