R
R
random2014-10-30 19:46:54
Java
random, 2014-10-30 19:46:54

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

1 answer(s)
P
pi314, 2014-10-30
@demon123

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? )))))))

@Losted
By DataBaseException I guess

Exactly... this exception can only come from getOption() . Otherwise, everything is as simple as the corner of the house. Some entity creates an instance of this class, passing the controller to the constructor, from which the initial "settings" will be pulled during initialization. Because the synchronize() method is public, obviously, the data in the "base" can change on the fly, and it can be pulled again to reread the current settings. (Whether this is really a database or something else is impossible to say - the details are abstracted away by the SysOptionController
facade . ) this and other public methods are synchronizedobviously they can be twitched by different threads. (By the way, synchronization is done quite crookedly and can result in performance problems ... but that's another topic).
From here it is more or less clear what this certain entity does. Most likely, it is designed to perform some action with "users" (for example, send them spam), which may or may not work the first time. There is a limit of time and attempts to process one user. The user is identified by IP and name. Most likely, there is a certain ExecutionService with a pool of threads. The thread receives an instance of BFController and, depending on what it does, can use the acc. methods:
addTry() - when the next "attempt"
starts needWait () - to check whether to continue attempts or the limit for the user has been exhausted
resetTries () - to reset the counter of attempts.
Apart from the timing curve, the semantics of the public interface are murky, based on some non-obvious assumptions about the use of the class, and, in addition, the implementation does not clean up memory in any way. In short, from the point of view object design - a confident C grade with a plus :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question