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