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