2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire 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 3 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, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * update.c: Update scheduler
29 * Known contributors to this file:
32 * Ron Koenderink, 2005
33 * Markus Armbruster, 2007-2009
43 #include "empthread.h"
48 #include "prototypes.h"
53 * Can be used to suppress messages, or direct them to bulletins.
57 static time_t update_schedule_anchor;
58 static int update_wanted;
60 static empth_t *update_thread;
62 static int update_get_schedule(void);
63 static void update_sched(void *);
64 static void update_run(void);
72 update_schedule_anchor = (time(NULL) + 59) / 60 * 60;
73 if (update_get_schedule() < 0)
76 play_lock = empth_rwlock_create("Update");
83 /* FIXME ancient black magic; figure out true stack need */
85 /* finish_sects */ WORLD_X * WORLD_Y * (2 * sizeof(double) +
87 update_thread = empth_create(update_sched, stacksize, 0, "Update", dp);
93 * Get the schedule for future updates into update_time[].
94 * Return 0 on success, -1 on failure.
97 update_get_schedule(void)
99 time_t now = time(NULL);
101 if (read_schedule(schedulefil, update_time,
102 sizeof(update_time) / sizeof(*update_time),
103 now + 30, update_schedule_anchor) < 0) {
104 logerror("No update schedule!");
108 logerror("Update schedule read");
114 update_sched(void *unused)
116 time_t next_update, now;
118 player->proc = empth_self();
124 * Sleep until the next scheduled update or an unscheduled
127 next_update = update_time[0];
129 if (update_window > 0)
130 next_update += random() % update_window;
131 logerror("Next update at %s", ctime(&next_update));
132 /* sleep until update is scheduled to go off */
133 empth_sleep(next_update);
135 logerror("No update scheduled");
136 /* want to sleep forever, but empthread doesn't provide that */
137 while (empth_sleep(time(NULL) + (60 * 60 * 24)) >= 0) ;
141 if (next_update != 0 && now >= next_update) {
142 /* scheduled update time reached */
143 if (now >= next_update + 60)
144 logerror("Missed the update!");
145 else if (update_demand == UPD_DEMAND_SCHED && !demand_check())
147 else if (updates_disabled())
148 logerror("Updates disabled...skipping update");
151 update_schedule_anchor = update_time[0];
153 /* else unscheduled update if update_wanted is set */
160 update_get_schedule();
167 * Return 0 on success, -1 on failure.
172 logerror("Triggering unscheduled update");
174 empth_wakeup(update_thread);
179 * Reload the update schedule.
180 * Return 0 on success, -1 on failure.
183 update_reschedule(void)
185 empth_wakeup(update_thread);
194 for (p = player_next(NULL); p; p = player_next(p)) {
195 if (p->state != PS_PLAYING)
198 pr_flash(p, "Update aborting command\n");
199 p->may_sleep = PLAYER_SLEEP_NEVER;
201 empth_wakeup(p->proc);
204 empth_rwlock_wrlock(play_lock);
205 if (*pre_update_hook) {
206 if (run_hook(pre_update_hook, "pre-update")) {
207 empth_rwlock_unlock(play_lock);
214 empth_rwlock_unlock(play_lock);
218 run_hook(char *cmd, char *name)
224 status = system(cmd);
226 ; /* successful exit */
227 else if (status == -1)
228 logerror("couldn't execute command processor for %s hook (%s)",
229 name, strerror(errno));
231 else if (WIFEXITED(status))
232 logerror("%s hook terminated unsuccessfully (exit status %d)",
233 name, WEXITSTATUS(status));
234 else if (WIFSIGNALED(status))
235 logerror("%s hook terminated abnormally (signal %d)",
236 name, WTERMSIG(status));
239 logerror("%s hook terminated strangely (status %d)",