2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, 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-2009
44 #include "empthread.h"
49 #include "prototypes.h"
54 * Can be used to suppress messages, or direct them to bulletins.
58 static time_t update_schedule_anchor;
59 static int update_wanted;
61 static empth_t *update_thread;
63 static int update_get_schedule(void);
64 static void update_sched(void *);
65 static void update_run(void);
73 update_schedule_anchor = (time(NULL) + 59) / 60 * 60;
74 if (update_get_schedule() < 0)
77 play_lock = empth_rwlock_create("Update");
84 /* FIXME ancient black magic; figure out true stack need */
86 /* finish_sects */ WORLD_X * WORLD_Y * (2 * sizeof(double) +
88 update_thread = empth_create(update_sched, stacksize, 0, "Update", dp);
94 * Get the schedule for future updates into update_time[].
95 * Return 0 on success, -1 on failure.
98 update_get_schedule(void)
100 time_t now = time(NULL);
102 if (read_schedule(schedulefil, update_time,
103 sizeof(update_time) / sizeof(*update_time),
104 now + 30, update_schedule_anchor) < 0) {
105 logerror("No update schedule!");
109 logerror("Update schedule read");
115 update_sched(void *unused)
117 time_t next_update, now;
119 player->proc = empth_self();
125 * Sleep until the next scheduled update or an unscheduled
128 next_update = update_time[0];
130 if (update_window > 0)
131 next_update += random() % update_window;
132 logerror("Next update at %s", ctime(&next_update));
133 /* sleep until update is scheduled to go off */
134 empth_sleep(next_update);
136 logerror("No update scheduled");
137 /* want to sleep forever, but empthread doesn't provide that */
138 while (empth_sleep(time(NULL) + (60 * 60 * 24)) >= 0) ;
142 if (next_update != 0 && now >= next_update) {
143 /* scheduled update time reached */
144 if (now >= next_update + 60)
145 logerror("Missed the update!");
146 else if (update_demand == UPD_DEMAND_SCHED && !demand_check())
148 else if (updates_disabled())
149 logerror("Updates disabled...skipping update");
152 update_schedule_anchor = update_time[0];
154 /* else unscheduled update if update_wanted is set */
161 update_get_schedule();
168 * Return 0 on success, -1 on failure.
173 logerror("Triggering unscheduled update");
175 empth_wakeup(update_thread);
180 * Reload the update schedule.
181 * Return 0 on success, -1 on failure.
184 update_reschedule(void)
186 empth_wakeup(update_thread);
195 for (p = player_next(NULL); p; p = player_next(p)) {
196 if (p->state != PS_PLAYING)
199 pr_flash(p, "Update aborting command\n");
200 p->may_sleep = PLAYER_SLEEP_NEVER;
202 empth_wakeup(p->proc);
205 empth_rwlock_wrlock(play_lock);
206 if (*pre_update_hook) {
207 if (run_hook(pre_update_hook, "pre-update")) {
208 empth_rwlock_unlock(play_lock);
215 empth_rwlock_unlock(play_lock);
219 run_hook(char *cmd, char *name)
225 status = system(cmd);
227 ; /* successful exit */
228 else if (status == -1)
229 logerror("couldn't execute command processor for %s hook (%s)",
230 name, strerror(errno));
232 else if (WIFEXITED(status))
233 logerror("%s hook terminated unsuccessfully (exit status %d)",
234 name, WEXITSTATUS(status));
235 else if (WIFSIGNALED(status))
236 logerror("%s hook terminated abnormally (signal %d)",
237 name, WTERMSIG(status));
240 logerror("%s hook terminated strangely (status %d)",