Source code: org/metacosm/framework/server/Schedule.java
1 /*
2 Metacosm, an object-oriented network game framework
3 Copyright (C) 1999-2001 Metacosm Development Team
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 package org.metacosm.framework.server;
21
22 /**
23 * The scheduler for a Metacosm game.
24 * <br>
25 * Use:
26 * <pre>
27 * Schedule schedule = new Schedule(this); // called by Game
28 * schedule.start();
29 * ...
30 * schedule.stop();
31 * </pre>
32 * @see Game
33 */
34 public final class Schedule implements Runnable {
35 /**
36 * This should be the default constructor.
37 */
38 public Schedule() {
39 // set scheduler first
40 this.scheduler = new org.metacosm.tests.TestScheduler();
41 }
42
43 /**
44 * It is an alternative constructor for this class.
45 * @param scheduler A Scheduler for this Schedule.
46 */
47 public Schedule(Scheduler scheduler) {
48 this.scheduler = scheduler;
49 }
50
51 /**
52 * Sets a Scheduler.
53 * May be used by a game manager.
54 */
55 public void setScheduler(Scheduler scheduler) {
56 if (this.threadSuspended == true) {
57 this.scheduler = scheduler;
58 }
59 // else?
60 }
61
62 /**
63 * Implements Runnable.
64 * Actually it is The Loop.
65 * This method must NEVER be executed directly.
66 */
67 public void run() {
68 GameClock clock = GameClock.getInstance();
69
70 while(nonStop) {
71 Logger.getInstance().record("Schedule loop!");
72
73 if (this.threadSuspended == true) {
74 Logger.getInstance().record("Schedule loop suspended!");
75 try {
76 synchronized(this) {
77 while(this.threadSuspended == true) {
78 wait();
79 }
80 }
81 } catch (InterruptedException e) {
82 /* do nothing*/
83 }
84 }
85
86 this.scheduler.processTurn();
87 clock.tick();
88 }
89
90 Logger.getInstance().record("Schedule.run() returns "+Thread.currentThread().toString());
91 }
92
93 /**
94 * Starts the loop.
95 */
96 public void start() {
97 if (this.thread == null) {
98 this.thread = new Thread(this);
99 Logger.getInstance().record("Thread Schedule = "+this.thread.toString());
100 this.thread.start();
101 }
102 resume();
103 }
104
105 /**
106 * Pauses the loop.
107 */
108 public void pause() {
109 this.threadSuspended = true;
110 }
111
112 /**
113 * Resumes a pause.
114 * @see Schedule#pause
115 */
116 public void resume() {
117 if (this.threadSuspended == true) {
118 this.threadSuspended = false;
119 Logger.getInstance().record("Schedule loop not suspended anymore!");
120 synchronized(this) {
121 notify();
122 }
123 }
124 }
125
126 /**
127 * Definitely stops the loop.
128 */
129 public void stop() {
130 // pause();
131 Logger.getInstance().record("Destroying Schedule thread");
132 nonStop = false;
133 // this.thread.destroy(); // The loop cannot be run again
134 // did not seemed to work (thread still alive in list)
135 }
136
137 /**
138 * Runs one turn.
139 * Is effective only during pauses.
140 */
141 public void processTurn() {
142 if (this.threadSuspended == true) {
143 this.scheduler.processTurn();
144 }
145 // else?
146 }
147
148 /**
149 * The Thread containing the loop.
150 */
151 private Thread thread;
152
153 /**
154 * A variable containing the state of the loop.
155 */
156 private volatile boolean threadSuspended = true;
157
158 /**
159 * A variable to allow the thread to end.
160 */
161 private volatile boolean nonStop = true;
162
163 /**
164 * The Scheduler used for the loop.
165 */
166 private Scheduler scheduler;
167 }