[1] | 1 | package geniusweb.partiesserver;
|
---|
| 2 |
|
---|
| 3 | import java.util.logging.Level;
|
---|
| 4 |
|
---|
| 5 | import geniusweb.partiesserver.repository.RunningPartiesRepo;
|
---|
| 6 | import geniusweb.partiesserver.repository.RunningParty;
|
---|
| 7 | import tudelft.utilities.logging.ReportToLogger;
|
---|
| 8 | import tudelft.utilities.logging.Reporter;
|
---|
| 9 |
|
---|
| 10 | /**
|
---|
| 11 | *
|
---|
| 12 | * This object keeps the {@link RunningPartiesRepo} up to date. Main job is to
|
---|
| 13 | * remove parties that have been timed out. Start and run only once.
|
---|
| 14 | */
|
---|
| 15 | public class RunningPartiesUpdater implements Runnable {
|
---|
| 16 | private final long period;
|
---|
| 17 | private final Reporter log = new ReportToLogger("partiesserver");
|
---|
| 18 | private final RunningPartiesRepo running;
|
---|
| 19 | /**
|
---|
| 20 | * when a negotiation runs out of time, the protocol may want to inform all
|
---|
| 21 | * parties about the failed session. If the parties are all killed at that
|
---|
| 22 | * moment, this is impossible. Therefore we wait KILL_DELAY extra time before
|
---|
| 23 | * really killing the parties.
|
---|
| 24 | */
|
---|
| 25 | private static final long KILL_DELAY = 2000;
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * @param repo the {@link RunningPartiesRepo} that must be updated
|
---|
| 29 | * automatically
|
---|
| 30 | * @param period the period with which to check time-out (ms). Recommended 1000.
|
---|
| 31 | * After constructing you can run this in separate thread or just
|
---|
| 32 | * call {@link #run()}
|
---|
| 33 | *
|
---|
| 34 | */
|
---|
| 35 | public RunningPartiesUpdater(RunningPartiesRepo repo, final long period) {
|
---|
| 36 | this.running = repo;
|
---|
| 37 | this.period = period;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
| 41 | *
|
---|
| 42 | * run the updater. Please run only one of this to avoid neeedless CPU load.
|
---|
| 43 | */
|
---|
| 44 | @Override
|
---|
| 45 | public void run() {
|
---|
| 46 | try {
|
---|
| 47 | while (true) {
|
---|
| 48 | Thread.sleep(period);
|
---|
| 49 | update();
|
---|
| 50 | }
|
---|
| 51 | } catch (InterruptedException e) {
|
---|
| 52 | System.err.println("ERROR: RunningPartiesUpdater was interrupted!");
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | /**
|
---|
| 57 | * Called when some file that may be relevant has changed. Synchronized to avoid
|
---|
| 58 | * weird states if filesystem changes rapidly. We ignore everything but jar
|
---|
| 59 | * files.
|
---|
| 60 | */
|
---|
| 61 | void update() {
|
---|
| 62 | for (RunningParty party : running.list()) {
|
---|
| 63 | long now = System.currentTimeMillis();
|
---|
| 64 | if (now >= party.getEndDate().getTime() + KILL_DELAY) {
|
---|
| 65 | running.remove(party.getID());
|
---|
| 66 | log.log(Level.WARNING, "party " + party.getID() + " was timed out and terminated");
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | }
|
---|