]> git.pond.sub.org Git - empserver/blob - src/server/update.c
Update known contributors comment.
[empserver] / src / server / update.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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  */
35
36 #include <config.h>
37
38 #include <errno.h>
39 #include <stdlib.h>
40 #ifndef _WIN32
41 #include <sys/wait.h>
42 #endif
43 #include <time.h>
44 #include "misc.h"
45 #include "player.h"
46 #include "empthread.h"
47 #include "prototypes.h"
48 #include "optlist.h"
49 #include "server.h"
50
51 empth_sem_t *update_sem;
52 time_t update_time;
53
54 static void update_wait(void *unused);
55 static int run_hook(char *cmd, char *name);
56
57 /*ARGSUSED*/
58 void
59 update_sched(void *unused)
60 {
61     int wind;
62     time_t now, delta;
63
64     update_sem = empth_sem_create("Update", 0);
65     empth_create(PP_SCHED, update_wait, (50 * 1024), 0, "UpdateWait",
66                  "Waits until players idle", 0);
67     time(&now);
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     while (1) {
74         time(&now);
75         next_update_time(&now, &update_time, &delta);
76         if (update_window > 0) {
77             wind = (random() % update_window);
78             update_time += wind;
79             delta += wind;
80         }
81         logerror("Next update at %s", ctime(&update_time));
82         logerror("Next update in %ld seconds", (long)delta);
83         /* sleep until update is scheduled to go off */
84         empth_sleep(update_time);
85         time(&now);
86         now += adj_update;
87         if (!gamehours(now)) {
88             logerror("No update permitted (hours restriction)");
89             continue;
90         }
91         if (!updatetime(&now)) {
92             logerror("No update wanted");
93             continue;
94         }
95         if (updates_disabled()) {
96             logerror("Updates disabled...skipping update");
97             continue;
98         }
99         empth_sem_signal(update_sem);
100     }
101     /*NOTREACHED*/
102 }
103
104 void
105 update_force(void *seconds)
106 {
107     time_t now;
108
109     time(&now);
110     empth_sleep(now + *(int *)seconds);
111     empth_sem_signal(update_sem);
112     empth_exit();
113 }
114
115 /*ARGSUSED*/
116 static void
117 update_wait(void *unused)
118 {
119     struct player *p;
120     int running;
121     time_t now;
122     int stacksize;
123     struct player *dp;
124
125     while (1) {
126         empth_sem_wait(update_sem);
127         update_pending = 1;
128         running = 0;
129         for (p = player_next(0); p != 0; p = player_next(p)) {
130             if (p->state != PS_PLAYING)
131                 continue;
132             if (p->command) {
133                 pr_flash(p, "Update aborting command\n");
134                 p->aborted = 1;
135                 empth_wakeup(p->proc);
136                 running++;
137             }
138         }
139         time(&now);
140         if (running) {
141             /* sleep a few, wait for aborts to take effect */
142             empth_sleep(now + 2);
143         }
144         if (*pre_update_hook) {
145             if (run_hook(pre_update_hook, "pre-update")) {
146                 update_pending = 0;
147                 continue;
148             }
149         }
150         /* 
151          * we rely on the fact that update's priority is the highest
152          * in the land so it can finish before it yields.
153          */
154         dp = player_new(-1);
155         if (!dp) {
156             logerror("can't create dummy player for update");
157             update_pending = 0;
158             continue;
159         }
160         stacksize = 100000 +
161 /* finish_sects */ WORLD_X * WORLD_Y * (2 * sizeof(double) +
162                                         sizeof(s_char *));
163
164         empth_create(PP_UPDATE, update_main, stacksize, 0,
165                      "UpdateRun", "Updates the world", dp);
166     }
167     /*NOTREACHED*/
168 }
169
170 static int
171 run_hook(char *cmd, char *name)
172 {
173     int status;
174     
175     fflush(NULL);
176     
177     status = system(cmd);
178     if (status == 0)
179         ;                       /* successful exit */
180     else if (status == -1)
181         logerror("couldn't execute command processor for %s hook (%s)",
182                  name, strerror(errno));
183 #ifndef _WIN32
184     else if (WIFEXITED(status))
185         logerror("%s hook terminated unsuccessfully (exit status %d)",
186                  name, WEXITSTATUS(status));
187     else if (WIFSIGNALED(status))
188         logerror("%s hook terminated abnormally (signal %d)",
189                  name, WTERMSIG(status));
190 #endif
191     else if (status)
192         logerror("%s hook terminated strangely (status %d)",
193                  name, status);
194     return status;
195 }