source: src/main/java/geniusweb/partiesserver/RunningPartiesUpdater.java@ 14

Last change on this file since 14 was 4, checked in by bart, 5 years ago

Faster example parties

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