]> git.pond.sub.org Git - empserver/blob - src/server/update.c
(s_p_etu): econfig key no longer used, remove.
[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 #define UPDATES 16
52
53 empth_rwlock_t *update_lock;
54 static empth_t *update_thread;
55 int update_pending;
56
57 time_t update_time[UPDATES];
58 static time_t update_schedule_anchor;
59 static int update_wanted;
60
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);
65
66 void
67 update_init(void)
68 {
69     struct player *dp;
70     int stacksize;
71
72     update_schedule_anchor = (time(NULL) + 59) / 60 * 60;
73     if (update_get_schedule() < 0)
74         exit(1);
75
76     update_lock = empth_rwlock_create("Update");
77     if (!update_lock)
78         exit_nomem();
79
80     dp = player_new(-1);
81     if (!dp)
82         exit_nomem();
83     /* FIXME ancient black magic; figure out true stack need */
84     stacksize = 100000 +
85 /* finish_sects */ WORLD_X * WORLD_Y * (2 * sizeof(double) +
86                                         sizeof(char *));
87     update_thread = empth_create(PP_UPDATE, update_sched, stacksize, 0,
88                                  "Update", dp);
89     if (!update_thread)
90         exit_nomem();
91 }
92
93 /*
94  * Get the schedule for future updates into update_time[].
95  * Return 0 on success, -1 on failure.
96  */
97 static int
98 update_get_schedule(void)
99 {
100     time_t now = time(NULL);
101
102     if (read_schedule(schedulefil, update_time, UPDATES,
103                       now + 30, update_schedule_anchor) < 0) {
104         logerror("No update schedule!");
105         update_time[0] = 0;
106         return -1;
107     }
108     logerror("Update schedule read");
109     return 0;
110 }
111
112 /*ARGSUSED*/
113 static void
114 update_sched(void *unused)
115 {
116     time_t next_update, now;
117
118     player->proc = empth_self();
119     player->cnum = 0;
120     player->god = 1;
121
122     for (;;) {
123         /*
124          * Sleep until the next scheduled update or an unscheduled
125          * wakeup.
126          */
127         next_update = update_time[0];
128         if (next_update) {
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);
134         } else {
135             logerror("No update scheduled");
136             /* want to sleep forever, but empthread doesn't provide that */
137             while (empth_sleep(60 * 60 * 24) >= 0) ;
138         }
139
140         now = time(NULL);
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())
146                 ;
147             else if (updates_disabled())
148                 logerror("Updates disabled...skipping update");
149             else
150                 update_wanted = 1;
151             update_schedule_anchor = update_time[0];
152         }
153         /* else unscheduled update if update_wanted is set */
154
155         if (update_wanted) {
156             update_wanted = 0;
157             update_run();
158         }
159
160         update_get_schedule();
161     }
162     /*NOTREACHED*/
163 }
164
165 /*
166  * Trigger an update.
167  * Return 0 on success, -1 on failure.
168  */
169 int
170 update_trigger(void)
171 {
172     logerror("Triggering unscheduled update");
173     update_wanted = 1;
174     empth_wakeup(update_thread);
175     return 0;
176 }
177
178 /*
179  * Reload the update schedule.
180  * Return 0 on success, -1 on failure.
181  */
182 int
183 update_reschedule(void)
184 {
185     empth_wakeup(update_thread);
186     return 0;
187 }
188
189 static void
190 update_run(void)
191 {
192     struct player *p;
193
194     update_pending = 1;
195     for (p = player_next(0); p != 0; p = player_next(p)) {
196         if (p->state != PS_PLAYING)
197             continue;
198         if (p->command) {
199             pr_flash(p, "Update aborting command\n");
200             p->aborted = 1;
201             empth_wakeup(p->proc);
202         }
203     }
204     empth_rwlock_wrlock(update_lock);
205     if (*pre_update_hook) {
206         if (run_hook(pre_update_hook, "pre-update")) {
207             update_pending = 0;
208             empth_rwlock_unlock(update_lock);
209             return;
210         }
211     }
212     update_main();
213     update_pending = 0;
214     empth_rwlock_unlock(update_lock);
215 }
216
217 static int
218 run_hook(char *cmd, char *name)
219 {
220     int status;
221     
222     fflush(NULL);
223     
224     status = system(cmd);
225     if (status == 0)
226         ;                       /* successful exit */
227     else if (status == -1)
228         logerror("couldn't execute command processor for %s hook (%s)",
229                  name, strerror(errno));
230 #ifndef _WIN32
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));
237 #endif
238     else if (status)
239         logerror("%s hook terminated strangely (status %d)",
240                  name, status);
241     return status;
242 }