01: // Copyright (c) 2002 Cunningham & Cunningham, Inc.
02: // Released under the terms of the GNU General Public License version 2 or later.
03:
04: package eg.music;
05:
06: import fit.*;
07: import java.util.Date;
08:
09: public class Simulator {
10:
11: // This discrete event simulator supports three events
12: // each of which is open coded in the body of the simulator.
13:
14: static Simulator system = new Simulator();
15: static long time = new Date().getTime();
16:
17: public static long nextSearchComplete = 0;
18: public static long nextPlayStarted = 0;
19: public static long nextPlayComplete = 0;
20:
21: long nextEvent(long bound) {
22: long result = bound;
23: result = sooner(result, nextSearchComplete);
24: result = sooner(result, nextPlayStarted);
25: result = sooner(result, nextPlayComplete);
26: return result;
27: }
28:
29: long sooner(long soon, long event) {
30: return event > time && event < soon ? event : soon;
31: }
32:
33: void perform() {
34: if (time == nextSearchComplete) {
35: MusicLibrary.searchComplete();
36: }
37: if (time == nextPlayStarted) {
38: MusicPlayer.playStarted();
39: }
40: if (time == nextPlayComplete) {
41: MusicPlayer.playComplete();
42: }
43: }
44:
45: void advance(long future) {
46: while (time < future) {
47: time = nextEvent(future);
48: perform();
49: }
50: }
51:
52: static long schedule(double seconds) {
53: return time + (long) (1000 * seconds);
54: }
55:
56: void delay(double seconds) {
57: advance(schedule(seconds));
58: }
59:
60: public void waitSearchComplete() {
61: advance(nextSearchComplete);
62: }
63:
64: public void waitPlayStarted() {
65: advance(nextPlayStarted);
66: }
67:
68: public void waitPlayComplete() {
69: advance(nextPlayComplete);
70: }
71:
72: public void failLoadJam() {
73: ActionFixture.actor = new Dialog("load jamed",
74: ActionFixture.actor);
75: }
76:
77: }
|