2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2013, 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-2012
44 #include "empthread.h"
50 #include "prototypes.h"
55 * Can be used to suppress messages, or direct them to bulletins.
59 static time_t update_schedule_anchor;
60 static int update_wanted;
62 static empth_t *update_thread;
64 static int update_get_schedule(void);
65 static void update_sched(void *);
66 static void update_run(void);
74 update_schedule_anchor = (time(NULL) + 59) / 60 * 60;
75 if (update_get_schedule() < 0)
81 /* FIXME ancient black magic; figure out true stack need */
83 /* finish_sects */ WORLD_X * WORLD_Y * (2 * sizeof(double) +
85 update_thread = empth_create(update_sched, stacksize, 0, "Update", dp);
91 * Get the schedule for future updates into update_time[].
92 * Return 0 on success, -1 on failure.
95 update_get_schedule(void)
97 time_t now = time(NULL);
98 int n = sizeof(update_time) / sizeof(*update_time);
101 ef_truncate(EF_UPDATES, 0);
102 ef_extend(EF_UPDATES, n - 1);
103 if (read_schedule(schedulefil, update_time, n,
104 now + 30, update_schedule_anchor) < 0) {
105 logerror("No update schedule!");
106 ef_truncate(EF_UPDATES, 0);
109 logerror("Update schedule read");
110 for (i = 0; update_time[i]; i++) ;
111 ef_truncate(EF_UPDATES, i);
117 update_sched(void *unused)
119 time_t next_update, now;
121 player->proc = empth_self();
127 * Sleep until the next scheduled update or an unscheduled
130 next_update = update_time[0];
132 if (update_window > 0)
133 next_update += roll0(update_window);
134 logerror("Next update at %s", ctime(&next_update));
135 /* sleep until update is scheduled to go off */
136 empth_sleep(next_update);
138 logerror("No update scheduled");
139 /* want to sleep forever, but empthread doesn't provide that */
140 while (empth_sleep(time(NULL) + (60 * 60 * 24)) >= 0) ;
144 if (next_update != 0 && now >= next_update) {
145 /* scheduled update time reached */
146 if (now >= next_update + 60)
147 logerror("Missed the update!");
148 else if (update_demand == UPD_DEMAND_SCHED && !demand_check())
150 else if (updates_disabled())
151 logerror("Updates disabled...skipping update");
154 update_schedule_anchor = update_time[0];
156 /* else unscheduled update if update_wanted is set */
163 update_get_schedule();
170 * Return 0 on success, -1 on failure.
175 logerror("Triggering unscheduled update");
177 empth_wakeup(update_thread);
182 * Reload the update schedule.
183 * Return 0 on success, -1 on failure.
186 update_reschedule(void)
188 empth_wakeup(update_thread);
197 for (p = player_next(NULL); p; p = player_next(p)) {
198 if (p->state != PS_PLAYING)
201 pr_flash(p, "Update aborting command\n");
202 p->may_sleep = PLAYER_SLEEP_NEVER;
204 empth_wakeup(p->proc);
207 empth_rwlock_wrlock(update_lock);
208 if (*pre_update_hook) {
209 if (run_hook(pre_update_hook, "pre-update")) {
210 empth_rwlock_unlock(update_lock);
217 empth_rwlock_unlock(update_lock);
221 run_hook(char *cmd, char *name)
227 status = system(cmd);
229 ; /* successful exit */
230 else if (status == -1)
231 logerror("couldn't execute command processor for %s hook (%s)",
232 name, strerror(errno));
234 else if (WIFEXITED(status))
235 logerror("%s hook terminated unsuccessfully (exit status %d)",
236 name, WEXITSTATUS(status));
237 else if (WIFSIGNALED(status))
238 logerror("%s hook terminated abnormally (signal %d)",
239 name, WTERMSIG(status));
242 logerror("%s hook terminated strangely (status %d)",