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

Last change on this file since 40 was 40, checked in by bart, 3 years ago

Added python SimpleRunner GUI

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