]> git.pond.sub.org Git - empserver/blob - src/lib/common/wantupd.c
New update scheduler:
[empserver] / src / lib / common / wantupd.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  *  wantupd.c: Check to se if an update is wanted and/or allowed.
29  * 
30  *  Known contributors to this file:
31  *     Doug Hay, 1990
32  */
33
34 #include <config.h>
35
36 #include <fcntl.h>
37 #include <stdio.h>
38 #if defined(_WIN32)
39 #include <io.h>
40 #endif
41 #include <time.h>
42 #if !defined(_WIN32)
43 #include <unistd.h>
44 #endif
45 #include "file.h"
46 #include "misc.h"
47 #include "nat.h"
48 #include "optlist.h"
49 #include "prototypes.h"
50
51 static int
52 demand_update_time(time_t *now)
53 {
54     struct tm *tm;
55
56     tm = localtime(now);
57     return is_daytime_allowed(60 * tm->tm_hour + tm->tm_min,
58                               update_demandtimes);
59 }
60
61 int
62 demand_update_want(int *want, int *pop, int which)
63 {
64     natid cn;
65     struct natstr *natp;
66     int totpop;
67     int totwant;
68     int whichwants;
69
70     whichwants = totpop = totwant = 0;
71     for (cn = 1; 0 != (natp = getnatp(cn)); cn++) {
72         /* Only countries which are normal. */
73         /* Should probably include sanctuaries ..... */
74         if (natp->nat_stat == STAT_ACTIVE) {
75             totpop++;
76             if (natp->nat_update) {
77                 totwant++;
78                 if (which == cn)
79                     whichwants++;
80             }
81         }
82     }
83     *want = totwant;
84     *pop = totpop;
85     return whichwants;
86 }
87
88 /*
89  * Do we have sufficient votes for a demand update?
90  */
91 int
92 demand_check(void)
93 {
94     struct natstr *natp;
95     int want, pop, cn, veto;
96
97     demand_update_want(&want, &pop, 0);
98     if (want < update_wantmin) {
99         logerror("no demand update, want = %d, min = %d",
100                  want, update_wantmin);
101         return 0;
102     }
103
104     veto = 0;
105     for (cn = 1; 0 != (natp = getnatp(cn)); cn++) {
106         if (natp->nat_stat == STAT_ACTIVE) {
107             if (natp->nat_missed >= update_missed)
108                 veto = cn;
109         }
110     }
111
112     if (veto) {
113         logerror("no demand update, %d has missed more than %d updates",
114                  veto, update_missed);
115         return 0;
116     }
117
118     return 1;
119 }
120
121 /*
122  * Can we have an unscheduled demand update now?
123  */
124 int
125 demandupdatecheck(void)
126 {
127     time_t now = time(NULL);
128
129     return update_demand == UPD_DEMAND_ASYNC
130         && !updates_disabled()
131         && demand_update_time(&now)
132         && demand_check();
133 }
134
135 int
136 updates_disabled(void)
137 {
138     int fd;
139
140     if ((fd = open(disablefil, O_RDONLY, 0)) < 0)
141         return 0;
142     close(fd);
143     return 1;
144 }