]> git.pond.sub.org Git - empserver/blobdiff - src/server/update.c
Update copyright notice
[empserver] / src / server / update.c
index fc08c941f7ab304c1f3d2084029f2609010d2e3b..06124f43df5356cffe0ca9e347fe0fa902919279 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
 #endif
 #include <time.h>
 #include "empthread.h"
+#include "game.h"
 #include "misc.h"
 #include "optlist.h"
 #include "player.h"
 #include "prototypes.h"
 #include "server.h"
 
+/*
+ * Update is running.
+ * Can be used to suppress messages, or direct them to bulletins.
+ */
+int update_running;
+
+static time_t update_schedule_anchor;
+static int update_wanted;
+
 static empth_t *update_thread;
-empth_rwlock_t *update_lock;
-int update_pending;
-time_t update_time;
-static int update_forced;
 
+static int update_get_schedule(void);
 static void update_sched(void *);
-static void update_force(void *);
 static void update_run(void);
 static int run_hook(char *cmd, char *name);
 
@@ -65,14 +71,12 @@ update_init(void)
     struct player *dp;
     int stacksize;
 
-    if (s_p_etu <= 0) {
-       logerror("bad value for s_p_etu (%d)", s_p_etu);
-       s_p_etu = 2 * 60;
-       logerror("setting s_p_etu to %d", s_p_etu);
-    }
+    update_schedule_anchor = (time(NULL) + 59) / 60 * 60;
+    if (update_get_schedule() < 0)
+       exit(1);
 
-    update_lock = empth_rwlock_create("Update");
-    if (!update_lock)
+    play_lock = empth_rwlock_create("Update");
+    if (!play_lock)
        exit_nomem();
 
     dp = player_new(-1);
@@ -82,97 +86,107 @@ update_init(void)
     stacksize = 100000 +
 /* finish_sects */ WORLD_X * WORLD_Y * (2 * sizeof(double) +
                                        sizeof(char *));
-    update_thread = empth_create(PP_UPDATE, update_sched, stacksize, 0,
+    update_thread = empth_create(update_sched, stacksize, 0,
                                 "Update", dp);
     if (!update_thread)
        exit_nomem();
 }
 
+/*
+ * Get the schedule for future updates into update_time[].
+ * Return 0 on success, -1 on failure.
+ */
+static int
+update_get_schedule(void)
+{
+    time_t now = time(NULL);
+
+    if (read_schedule(schedulefil, update_time,
+                     sizeof(update_time) / sizeof(*update_time),
+                     now + 30, update_schedule_anchor) < 0) {
+       logerror("No update schedule!");
+       update_time[0] = 0;
+       return -1;
+    }
+    logerror("Update schedule read");
+    return 0;
+}
+
 /*ARGSUSED*/
 static void
 update_sched(void *unused)
 {
-    int wind;
-    time_t now, delta;
+    time_t next_update, now;
 
     player->proc = empth_self();
     player->cnum = 0;
     player->god = 1;
 
     for (;;) {
-       time(&now);
-       next_update_time(&now, &update_time, &delta);
-       if (update_window > 0) {
-           wind = (random() % update_window);
-           update_time += wind;
-           delta += wind;
+       /*
+        * Sleep until the next scheduled update or an unscheduled
+        * wakeup.
+        */
+       next_update = update_time[0];
+       if (next_update) {
+           if (update_window > 0)
+               next_update += random() % update_window;
+           logerror("Next update at %s", ctime(&next_update));
+           /* sleep until update is scheduled to go off */
+           empth_sleep(next_update);
+       } else {
+           logerror("No update scheduled");
+           /* want to sleep forever, but empthread doesn't provide that */
+           while (empth_sleep(time(NULL) + (60 * 60 * 24)) >= 0) ;
        }
-       logerror("Next update at %s", ctime(&update_time));
-       logerror("Next update in %ld seconds", (long)delta);
-       /* sleep until update is scheduled to go off */
-       update_forced = 0;
-       empth_sleep(update_time);
-       if (!update_forced) {
-           time(&now);
-           now += adj_update;
-           if (!gamehours(now)) {
-               logerror("No update permitted (hours restriction)");
-               continue;
-           }
-           if (!updatetime(&now)) {
-               logerror("No update wanted");
-               continue;
-           }
-           if (updates_disabled()) {
+
+       now = time(NULL);
+       if (next_update != 0 && now >= next_update) {
+           /* scheduled update time reached */
+           if (now >= next_update + 60)
+               logerror("Missed the update!");
+           else if (update_demand == UPD_DEMAND_SCHED && !demand_check())
+               ;
+           else if (updates_disabled())
                logerror("Updates disabled...skipping update");
-               continue;
-           }
+           else
+               update_wanted = 1;
+           update_schedule_anchor = update_time[0];
        }
-       update_run();
+       /* else unscheduled update if update_wanted is set */
+
+       if (update_wanted) {
+           update_wanted = 0;
+           update_run();
+       }
+
+       update_get_schedule();
     }
     /*NOTREACHED*/
 }
 
 /*
- * Trigger an update SECS_FROM_NOW seconds from now.
+ * Trigger an update.
  * Return 0 on success, -1 on failure.
  */
 int
-update_trigger(time_t secs_from_now)
+update_trigger(void)
 {
-    time_t *secp;
-
-    if (secs_from_now < 0)
-       return -1;
-
-    if (secs_from_now == 0) {
-       update_forced = 1;
-       empth_wakeup(update_thread);
-       return 0;
-    }
-
-    /* FIXME make triggers overwrite, not accumulate */
-    secp = malloc(sizeof(time_t));
-    if (!secp)
-       return -1;
-    *secp = secs_from_now;
-    if (!empth_create(PP_SCHED, update_force, 50 * 1024, 0,
-                     "forceUpdate", secp))
-       return -1;
+    logerror("Triggering unscheduled update");
+    update_wanted = 1;
+    empth_wakeup(update_thread);
     return 0;
 }
 
-static void
-update_force(void *seconds)
+/*
+ * Reload the update schedule.
+ * Return 0 on success, -1 on failure.
+ */
+int
+update_reschedule(void)
 {
-    time_t now;
-
-    time(&now);
-    empth_sleep(now + *(time_t *)seconds);
-    update_forced = 1;
     empth_wakeup(update_thread);
-    free(seconds);
-    empth_exit();
+    return 0;
 }
 
 static void
@@ -180,7 +194,7 @@ update_run(void)
 {
     struct player *p;
 
-    update_pending = 1;
+    play_wrlock_wanted = 1;
     for (p = player_next(0); p != 0; p = player_next(p)) {
        if (p->state != PS_PLAYING)
            continue;
@@ -190,17 +204,18 @@ update_run(void)
            empth_wakeup(p->proc);
        }
     }
-    empth_rwlock_wrlock(update_lock);
+    empth_rwlock_wrlock(play_lock);
     if (*pre_update_hook) {
        if (run_hook(pre_update_hook, "pre-update")) {
-           update_pending = 0;
-           empth_rwlock_unlock(update_lock);
+           play_wrlock_wanted = 0;
+           empth_rwlock_unlock(play_lock);
            return;
        }
     }
+    update_running = 1;
     update_main();
-    update_pending = 0;
-    empth_rwlock_unlock(update_lock);
+    play_wrlock_wanted = update_running = 0;
+    empth_rwlock_unlock(play_lock);
 }
 
 static int