]> git.pond.sub.org Git - empserver/blob - src/server/update.c
Rewrite how updates are triggered (closes #1504036):
[empserver] / src / server / update.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  update.c: Update scheduler
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1994
32  *     Steve McClure, 1996
33  *     Ron Koenderink, 2005
34  *     Markus Armbruster, 2007
35  */
36
37 #include <config.h>
38
39 #include <errno.h>
40 #ifndef _WIN32
41 #include <sys/wait.h>
42 #endif
43 #include <time.h>
44 #include "empthread.h"
45 #include "misc.h"
46 #include "optlist.h"
47 #include "player.h"
48 #include "prototypes.h"
49 #include "server.h"
50
51 static empth_t *update_thread;
52 empth_rwlock_t *update_lock;
53 int update_pending;
54 time_t update_time;
55 static int update_forced;
56
57 static void update_sched(void *);
58 static void update_force(void *);
59 static void update_run(void);
60 static int run_hook(char *cmd, char *name);
61
62 void
63 update_init(void)
64 {
65     struct player *dp;
66     int stacksize;
67
68     if (s_p_etu <= 0) {
69         logerror("bad value for s_p_etu (%d)", s_p_etu);
70         s_p_etu = 2 * 60;
71         logerror("setting s_p_etu to %d", s_p_etu);
72     }
73
74     update_lock = empth_rwlock_create("Update");
75     if (!update_lock)
76         exit_nomem();
77
78     dp = player_new(-1);
79     if (!dp)
80         exit_nomem();
81     /* FIXME ancient black magic; figure out true stack need */
82     stacksize = 100000 +
83 /* finish_sects */ WORLD_X * WORLD_Y * (2 * sizeof(double) +
84                                         sizeof(char *));
85     update_thread = empth_create(PP_UPDATE, update_sched, stacksize, 0,
86                                  "Update", dp);
87     if (!update_thread)
88         exit_nomem();
89 }
90
91 /*ARGSUSED*/
92 static void
93 update_sched(void *unused)
94 {
95     int wind;
96     time_t now, delta;
97
98     player->proc = empth_self();
99     player->cnum = 0;
100     player->god = 1;
101
102     for (;;) {
103         time(&now);
104         next_update_time(&now, &update_time, &delta);
105         if (update_window > 0) {
106             wind = (random() % update_window);
107             update_time += wind;
108             delta += wind;
109         }
110         logerror("Next update at %s", ctime(&update_time));
111         logerror("Next update in %ld seconds", (long)delta);
112         /* sleep until update is scheduled to go off */
113         update_forced = 0;
114         empth_sleep(update_time);
115         if (!update_forced) {
116             time(&now);
117             now += adj_update;
118             if (!gamehours(now)) {
119                 logerror("No update permitted (hours restriction)");
120                 continue;
121             }
122             if (!updatetime(&now)) {
123                 logerror("No update wanted");
124                 continue;
125             }
126             if (updates_disabled()) {
127                 logerror("Updates disabled...skipping update");
128                 continue;
129             }
130         }
131         update_run();
132     }
133     /*NOTREACHED*/
134 }
135
136 /*
137  * Trigger an update SECS_FROM_NOW seconds from now.
138  * Return 0 on success, -1 on failure.
139  */
140 int
141 update_trigger(time_t secs_from_now)
142 {
143     time_t *secp;
144
145     if (secs_from_now < 0)
146         return -1;
147
148     if (secs_from_now == 0) {
149         update_forced = 1;
150         empth_wakeup(update_thread);
151         return 0;
152     }
153
154     /* FIXME make triggers overwrite, not accumulate */
155     secp = malloc(sizeof(time_t));
156     if (!secp)
157         return -1;
158     *secp = secs_from_now;
159     if (!empth_create(PP_SCHED, update_force, 50 * 1024, 0,
160                       "forceUpdate", secp))
161         return -1;
162     return 0;
163 }
164
165 static void
166 update_force(void *seconds)
167 {
168     time_t now;
169
170     time(&now);
171     empth_sleep(now + *(time_t *)seconds);
172     update_forced = 1;
173     empth_wakeup(update_thread);
174     free(seconds);
175     empth_exit();
176 }
177
178 static void
179 update_run(void)
180 {
181     struct player *p;
182
183     update_pending = 1;
184     for (p = player_next(0); p != 0; p = player_next(p)) {
185         if (p->state != PS_PLAYING)
186             continue;
187         if (p->command) {
188             pr_flash(p, "Update aborting command\n");
189             p->aborted = 1;
190             empth_wakeup(p->proc);
191         }
192     }
193     empth_rwlock_wrlock(update_lock);
194     if (*pre_update_hook) {
195         if (run_hook(pre_update_hook, "pre-update")) {
196             update_pending = 0;
197             empth_rwlock_unlock(update_lock);
198             return;
199         }
200     }
201     update_main();
202     update_pending = 0;
203     empth_rwlock_unlock(update_lock);
204 }
205
206 static int
207 run_hook(char *cmd, char *name)
208 {
209     int status;
210     
211     fflush(NULL);
212     
213     status = system(cmd);
214     if (status == 0)
215         ;                       /* successful exit */
216     else if (status == -1)
217         logerror("couldn't execute command processor for %s hook (%s)",
218                  name, strerror(errno));
219 #ifndef _WIN32
220     else if (WIFEXITED(status))
221         logerror("%s hook terminated unsuccessfully (exit status %d)",
222                  name, WEXITSTATUS(status));
223     else if (WIFSIGNALED(status))
224         logerror("%s hook terminated abnormally (signal %d)",
225                  name, WTERMSIG(status));
226 #endif
227     else if (status)
228         logerror("%s hook terminated strangely (status %d)",
229                  name, status);
230     return status;
231 }