]> git.pond.sub.org Git - empserver/commitdiff
(nreport, filereport, ncache, addfree, findfree, delete_old_news)
authorRon Koenderink <rkoenderink@yahoo.ca>
Wed, 2 Mar 2005 23:53:10 +0000 (23:53 +0000)
committerRon Koenderink <rkoenderink@yahoo.ca>
Wed, 2 Mar 2005 23:53:10 +0000 (23:53 +0000)
(init_nreport, update_main, start_server): Switch news to be sorted
list with all new news items added at the end of the list.  The
removal of expired news items is done at update time.  Closes #766755.

include/prototypes.h
src/lib/subs/nreport.c
src/lib/update/main.c
src/server/main.c

index e001a6c284e778d23a25fba15569c7537944a109..fd8f627ecce8701dd0b0a29011ea281e8105fe4e 100644 (file)
@@ -546,6 +546,8 @@ extern int natarg(s_char *, s_char *);
 /* neigh.c */
 extern int neigh(coord, coord, natid);
 /* nreport.c */
+extern void delete_old_news();
+extern void init_nreport();
 extern void nreport(natid, int, natid, int);
 /* nuke.c */
 extern int nuk_postread(int, s_char *);
index e10415f53afc65222ecf802b8509f1435f3d7d67..e8b19ce8a3f520a13e818255a19970a828cb1092 100644 (file)
  *     Steve McClure, 1997
  */
 
-#include "misc.h"
+#include "prototypes.h"
 #include "news.h"
-#include "nat.h"
 #include "file.h"
-#include "empio.h"
-#include <fcntl.h>
 #include "optlist.h"
-#include "prototypes.h"
 
-static void filereport(int, int, int, int);
+#define SLOTS  5
+
+struct newscache {
+    struct nwsstr news;
+    int id;
+};
+
+static struct newscache cache[MAXNOC][SLOTS];
+static int news_tail;
+
+static struct newscache *
+ncache(int actor, int event, int victim, int times);
 
 void
 nreport(natid actor, int event, natid victim, int times)
 {
     int nice;
     int rel;
-    struct natstr *np;
+    struct natstr *natp;
+    struct newscache *ncp;
+
+    ncp = ncache(actor, event, victim, times);
+    putnews(ncp->id, &ncp->news);
 
-    filereport(actor, event, victim, times);
     /*
      * this is probably pretty expensive, but hopefully we
      * don't fire zillions of these things off every second.
@@ -65,9 +75,9 @@ nreport(natid actor, int event, natid victim, int times)
        return;
     if (!chance((double)-nice * times / 20.0))
        return;
-    if ((np = getnatp(victim)) == 0)
+    if ((natp = getnatp(victim)) == 0)
        return;
-    if ((rel = getrel(np, actor)) < HOSTILE)
+    if ((rel = getrel(natp, actor)) < HOSTILE)
        return;
 
     rel = HOSTILE;
@@ -80,79 +90,76 @@ nreport(natid actor, int event, natid victim, int times)
     setrel(victim, actor, rel);
 }
 
-struct free {
-    struct free *next;
-    int id;
-};
-
-struct free *freelist;
-
-static void
-addfree(int n)
-{
-    struct free *fp;
-
-    fp = (struct free *)malloc(sizeof(*fp));
-    fp->next = freelist;
-    fp->id = n;
-    freelist = fp;
-}
-
 /*
- * snoop through the news articles looking
- * for articles which have timed out.  Only
- * called when no free items left.
+ * Delete news articles that have expired.
  */
-static void
-findfree(void)
+void
+delete_old_news(void)
 {
-    time_t oldnewstime;
-    int n;
+    time_t expiry_time;
+    int i, j, k;
     struct nwsstr news;
 
-    oldnewstime = time(NULL) - days(news_keep_days);
-    for (n = 0; getnews(n, &news); n++) {
-       if (news.nws_when < oldnewstime)
-           addfree(n);
+    /* skip over expired news */
+    expiry_time = time(NULL) - days(news_keep_days);
+    for (i = 0; getnews(i, &news); i++) {
+       if (news.nws_when == 0 || news.nws_when >= expiry_time)
+           break;
     }
-    if (freelist == 0) {
-       if (!ef_extend(EF_NEWS, 100))
-           return;
-       findfree();
+    /* news id 0..I-1 have expired */
+    CANT_HAPPEN(i > news_tail);
+    /* no items to delete if I is equal zero */
+    if (i == 0)
+       return;
+
+    /* move unexpired news I.. to 0.., overwriting expired news */
+    for (j = 0; getnews(i + j, &news); j++) {
+       if (news.nws_when == 0)
+           break;
+       putnews(j, &news);
     }
-}
+    CANT_HAPPEN(i + j != news_tail);
+    news_tail = j;
 
-static int
-nextfree(void)
-{
-    struct free *fp;
-    int id;
+    /* mark slots no longer in use */
+    memset(&news, 0, sizeof(news));
+    for (k = 0; k < i; k++)
+       putnews(j + k, &news);
 
-    if (freelist == 0)
-       findfree();
-    if ((fp = freelist) == 0)
-       return 0;
-    freelist = fp->next;
-    id = fp->id;
-    free(fp);
-    return id;
+    /* clear cache because moving news invalidated it */
+    memset(&cache, 0, sizeof(cache));
 }
 
-#define SLOTS  5
-
-struct newscache {
+/*
+ * Initialize news reporting.
+ * Must run between open of file EF_NEWS and first nreport().
+ */
+void
+init_nreport(void)
+{
+    int newest_item;
     struct nwsstr news;
-    int id;
-};
 
+    for (newest_item = 0; getnews(newest_item, &news); newest_item++) {
+       if (news.nws_when == 0)
+           break;
+    }
+    news_tail = newest_item;
+}
+
+/*
+ * Look to see if the same message has been generated
+ * in the last 5 minutes, if so just increment the times
+ * field instead of creating a new message.
+ */
 static struct newscache *
-ncache(time_t now, int actor, int event, int victim, int times)
+ncache(int actor, int event, int victim, int times)
 {
-    static struct newscache cache[MAXNOC][SLOTS];
     register struct newscache *np;
     int i;
     int oldslot;
     time_t oldtime;
+    time_t now = time(NULL);
 
     oldslot = -1;
     oldtime = 0x7fffffff;
@@ -182,17 +189,7 @@ ncache(time_t now, int actor, int event, int victim, int times)
     np->news.nws_when = now;
     np->news.nws_vrb = event;
     np->news.nws_ntm = times;
-    np->id = nextfree();
+    ef_ensure_space(EF_NEWS, news_tail, 100);
+    np->id = news_tail++;
     return np;
 }
-
-static void
-filereport(int actor, int event, int victim, int times)
-{
-    struct newscache *np;
-    time_t now;
-
-    time(&now);
-    np = ncache(now, actor, event, victim, times);
-    ef_write(EF_NEWS, np->id, (s_char *)&np->news);
-}
index a05b3cf331aa57b93d27a8c2fa0980d89c8247f3..bfda5a493f7b2b749b9e2dd605625803f4ceaa29 100644 (file)
@@ -201,6 +201,7 @@ update_main(void *unused)
     ef_flush(EF_PLANE);
     ef_flush(EF_LAND);
     delete_old_announcements();
+    delete_old_news();
     /* Clear all the telegram flags */
     for (cn = 0; cn < MAXNOC; cn++)
        clear_telegram_is_new(cn);
index 51b1c5772bcdfda7f6d2bbef90316c494af62174..f7b511e6a04cd8f1b37afe738efa13dbeb1842c3 100644 (file)
@@ -309,6 +309,7 @@ start_server(int flags, char *config_file)
     ef_init();
     init_files();
     io_init();
+    init_nreport();
 
     if (opt_MOB_ACCESS) {
        /* This fixes up mobility upon restart */