2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * update.c: Update scheduler
30 * Known contributors to this file:
33 * Ron Koenderink, 2005
34 * Markus Armbruster, 2007
44 #include "empthread.h"
48 #include "prototypes.h"
53 empth_rwlock_t *update_lock;
54 static empth_t *update_thread;
57 time_t update_time[UPDATES];
58 static time_t update_schedule_anchor;
59 static int update_wanted;
61 static int update_get_schedule(void);
62 static void update_sched(void *);
63 static void update_run(void);
64 static int run_hook(char *cmd, char *name);
73 logerror("bad value for s_p_etu (%d)", s_p_etu);
75 logerror("setting s_p_etu to %d", s_p_etu);
78 update_schedule_anchor = (time(NULL) + 59) / 60 * 60;
79 if (update_get_schedule() < 0)
82 update_lock = empth_rwlock_create("Update");
89 /* FIXME ancient black magic; figure out true stack need */
91 /* finish_sects */ WORLD_X * WORLD_Y * (2 * sizeof(double) +
93 update_thread = empth_create(PP_UPDATE, update_sched, stacksize, 0,
100 * Get the schedule for future updates into update_time[].
101 * Return 0 on success, -1 on failure.
104 update_get_schedule(void)
106 time_t now = time(NULL);
108 if (read_schedule(schedulefil, update_time, UPDATES,
109 now + 30, update_schedule_anchor) < 0) {
110 logerror("No update schedule!");
114 logerror("Update schedule read");
120 update_sched(void *unused)
122 time_t next_update, now;
124 player->proc = empth_self();
130 * Sleep until the next scheduled update or an unscheduled
133 next_update = update_time[0];
135 if (update_window > 0)
136 next_update += random() % update_window;
137 logerror("Next update at %s", ctime(&next_update));
138 /* sleep until update is scheduled to go off */
139 empth_sleep(next_update);
141 logerror("No update scheduled");
142 /* want to sleep forever, but empthread doesn't provide that */
143 while (empth_sleep(60 * 60 * 24) >= 0) ;
147 if (next_update != 0 && now >= next_update) {
148 /* scheduled update time reached */
149 if (now >= next_update + 60)
150 logerror("Missed the update!");
151 else if (update_demand == UPD_DEMAND_SCHED && !demand_check())
153 else if (updates_disabled())
154 logerror("Updates disabled...skipping update");
157 update_schedule_anchor = update_time[0];
159 /* else unscheduled update if update_wanted is set */
166 update_get_schedule();
173 * Return 0 on success, -1 on failure.
178 logerror("Triggering unscheduled update");
180 empth_wakeup(update_thread);
185 * Reload the update schedule.
186 * Return 0 on success, -1 on failure.
189 update_reschedule(void)
191 empth_wakeup(update_thread);
201 for (p = player_next(0); p != 0; p = player_next(p)) {
202 if (p->state != PS_PLAYING)
205 pr_flash(p, "Update aborting command\n");
207 empth_wakeup(p->proc);
210 empth_rwlock_wrlock(update_lock);
211 if (*pre_update_hook) {
212 if (run_hook(pre_update_hook, "pre-update")) {
214 empth_rwlock_unlock(update_lock);
220 empth_rwlock_unlock(update_lock);
224 run_hook(char *cmd, char *name)
230 status = system(cmd);
232 ; /* successful exit */
233 else if (status == -1)
234 logerror("couldn't execute command processor for %s hook (%s)",
235 name, strerror(errno));
237 else if (WIFEXITED(status))
238 logerror("%s hook terminated unsuccessfully (exit status %d)",
239 name, WEXITSTATUS(status));
240 else if (WIFSIGNALED(status))
241 logerror("%s hook terminated abnormally (signal %d)",
242 name, WTERMSIG(status));
245 logerror("%s hook terminated strangely (status %d)",