e6c38f
diff -up cups-1.4.7/scheduler/cupsd.h.avahi-3-timeouts cups-1.4.7/scheduler/cupsd.h
e6c38f
--- cups-1.4.7/scheduler/cupsd.h.avahi-3-timeouts	2011-01-11 07:05:58.000000000 +0000
e6c38f
+++ cups-1.4.7/scheduler/cupsd.h	2011-06-28 12:54:06.906728322 +0100
e6c38f
@@ -147,6 +147,15 @@ extern const char *cups_hstrerror(int);
e6c38f
 
e6c38f
 typedef void (*cupsd_selfunc_t)(void *data);
e6c38f
 
e6c38f
+#ifdef HAVE_AVAHI
e6c38f
+/*
e6c38f
+ * Timeout callback function type...
e6c38f
+ */
e6c38f
+
e6c38f
+typedef struct _cupsd_timeout_s cupsd_timeout_t;
e6c38f
+typedef void (*cupsd_timeoutfunc_t)(cupsd_timeout_t *timeout, void *data);
e6c38f
+#endif /* HAVE_AVAHI */
e6c38f
+
e6c38f
 
e6c38f
 /*
e6c38f
  * Globals...
e6c38f
@@ -188,6 +197,9 @@ VAR PSQUpdateQuotaProcPtr PSQUpdateQuota
e6c38f
 					/* Apple PrintService quota function */
e6c38f
 #endif /* __APPLE__ && HAVE_DLFCN_H */
e6c38f
 
e6c38f
+#ifdef HAVE_AVAHI
e6c38f
+VAR cups_array_t *Timeouts;		/* Timed callbacks for main loop */
e6c38f
+#endif /* HAVE_AVAHI */
e6c38f
 
e6c38f
 
e6c38f
 
e6c38f
@@ -241,6 +253,18 @@ extern void	cupsdRemoveSelect(int fd);
e6c38f
 extern void	cupsdStartSelect(void);
e6c38f
 extern void	cupsdStopSelect(void);
e6c38f
 
e6c38f
+#ifdef HAVE_AVAHI
e6c38f
+extern void     cupsdInitTimeouts(void);
e6c38f
+extern cupsd_timeout_t *cupsdAddTimeout (const struct timeval *tv,
e6c38f
+					 cupsd_timeoutfunc_t cb,
e6c38f
+					 void *data);
e6c38f
+extern cupsd_timeout_t *cupsdNextTimeout (long *delay);
e6c38f
+extern void     cupsdRunTimeout (cupsd_timeout_t *timeout);
e6c38f
+extern void     cupsdUpdateTimeout (cupsd_timeout_t *timeout,
e6c38f
+				    const struct timeval *tv);
e6c38f
+extern void     cupsdRemoveTimeout (cupsd_timeout_t *timeout);
e6c38f
+#endif /* HAVE_AVAHI */
e6c38f
+
e6c38f
 extern int	cupsdRemoveFile(const char *filename);
e6c38f
 
e6c38f
 
e6c38f
diff -up cups-1.4.7/scheduler/main.c.avahi-3-timeouts cups-1.4.7/scheduler/main.c
e6c38f
--- cups-1.4.7/scheduler/main.c.avahi-3-timeouts	2011-06-28 12:51:40.307369673 +0100
e6c38f
+++ cups-1.4.7/scheduler/main.c	2011-06-28 12:54:06.909728268 +0100
e6c38f
@@ -161,6 +161,10 @@ main(int  argc,				/* I - Number of comm
e6c38f
   int			launchd_idle_exit;
e6c38f
 					/* Idle exit on select timeout? */
e6c38f
 #endif	/* HAVE_LAUNCHD */
e6c38f
+#ifdef HAVE_AVAHI
e6c38f
+  cupsd_timeout_t	*tmo;		/* Next scheduled timed callback */
e6c38f
+  long			tmo_delay;	/* Time before it must be called */
e6c38f
+#endif /* HAVE_AVAHI */
e6c38f
 
e6c38f
 
e6c38f
 #ifdef HAVE_GETEUID
e6c38f
@@ -561,6 +565,14 @@ main(int  argc,				/* I - Number of comm
e6c38f
 
e6c38f
   httpInitialize();
e6c38f
 
e6c38f
+#ifdef HAVE_AVAHI
e6c38f
+ /*
e6c38f
+  * Initialize timed callback structures.
e6c38f
+  */
e6c38f
+
e6c38f
+  cupsdInitTimeouts();
e6c38f
+#endif /* HAVE_AVAHI */
e6c38f
+
e6c38f
   cupsdStartServer();
e6c38f
 
e6c38f
  /*
e6c38f
@@ -900,6 +912,16 @@ main(int  argc,				/* I - Number of comm
e6c38f
     }
e6c38f
 #endif /* __APPLE__ */
e6c38f
 
e6c38f
+#ifdef HAVE_AVAHI
e6c38f
+   /*
e6c38f
+    * If a timed callback is due, run it.
e6c38f
+    */
e6c38f
+
e6c38f
+    tmo = cupsdNextTimeout (&tmo_delay);
e6c38f
+    if (tmo && tmo_delay == 0)
e6c38f
+      cupsdRunTimeout (tmo);
e6c38f
+#endif /* HAVE_AVAHI */
e6c38f
+
e6c38f
 #ifndef __APPLE__
e6c38f
    /*
e6c38f
     * Update the network interfaces once a minute...
e6c38f
@@ -1925,6 +1947,10 @@ select_timeout(int fds)			/* I - Number 
e6c38f
   cupsd_job_t		*job;		/* Job information */
e6c38f
   cupsd_subscription_t	*sub;		/* Subscription information */
e6c38f
   const char		*why;		/* Debugging aid */
e6c38f
+#ifdef HAVE_AVAHI
e6c38f
+  cupsd_timeout_t	*tmo;		/* Timed callback */
e6c38f
+  long			tmo_delay;	/* Seconds before calling it */
e6c38f
+#endif /* HAVE_AVAHI */
e6c38f
 
e6c38f
 
e6c38f
  /*
e6c38f
@@ -1967,6 +1993,19 @@ select_timeout(int fds)			/* I - Number 
e6c38f
   }
e6c38f
 #endif /* __APPLE__ */
e6c38f
 
e6c38f
+#ifdef HAVE_AVAHI
e6c38f
+ /*
e6c38f
+  * See if there are any scheduled timed callbacks to run.
e6c38f
+  */
e6c38f
+
e6c38f
+  tmo = cupsdNextTimeout (&tmo_delay);
e6c38f
+  if (tmo)
e6c38f
+  {
e6c38f
+    timeout = tmo_delay;
e6c38f
+    why = "run a timed callback";
e6c38f
+  }
e6c38f
+#endif /* HAVE_AVAHI */
e6c38f
+
e6c38f
  /*
e6c38f
   * Check whether we are accepting new connections...
e6c38f
   */
e6c38f
diff -up cups-1.4.7/scheduler/Makefile.avahi-3-timeouts cups-1.4.7/scheduler/Makefile
e6c38f
--- cups-1.4.7/scheduler/Makefile.avahi-3-timeouts	2011-06-28 12:51:40.475366647 +0100
e6c38f
+++ cups-1.4.7/scheduler/Makefile	2011-06-28 12:54:06.906728322 +0100
e6c38f
@@ -39,7 +39,8 @@ CUPSDOBJS =	\
e6c38f
 		server.o \
e6c38f
 		statbuf.o \
e6c38f
 		subscriptions.o \
e6c38f
-		sysman.o
e6c38f
+		sysman.o \
e6c38f
+		timeout.o
e6c38f
 LIBOBJS =	\
e6c38f
 		filter.o \
e6c38f
 		mime.o \
e6c38f
diff -up cups-1.4.7/scheduler/timeout.c.avahi-3-timeouts cups-1.4.7/scheduler/timeout.c
e6c38f
--- cups-1.4.7/scheduler/timeout.c.avahi-3-timeouts	2011-06-28 12:54:06.910728250 +0100
e6c38f
+++ cups-1.4.7/scheduler/timeout.c	2011-06-28 12:54:06.910728250 +0100
e6c38f
@@ -0,0 +1,195 @@
e6c38f
+/*
e6c38f
+ * "$Id$"
e6c38f
+ *
e6c38f
+ *   Timeout functions for the Common UNIX Printing System (CUPS).
e6c38f
+ *
e6c38f
+ *   Copyright (C) 2010 Red Hat, Inc.
e6c38f
+ *   Authors:
e6c38f
+ *     Tim Waugh <twaugh@redhat.com>
e6c38f
+ *
e6c38f
+ *   Distribution and use rights are outlined in the file "LICENSE.txt"
e6c38f
+ *   which should have been included with this file.  If this file is
e6c38f
+ *   file is missing or damaged, see the license at "http://www.cups.org/".
e6c38f
+ *
e6c38f
+ * Contents:
e6c38f
+ *
e6c38f
+ *   cupsdInitTimeouts()  - Initialise timeout structure.
e6c38f
+ *   cupsdAddTimeout()    - Add a timed callback.
e6c38f
+ *   cupsdNextTimeout()   - Find the next enabled timed callback.
e6c38f
+ *   cupsdUpdateTimeout() - Adjust the time of a timed callback or disable it.
e6c38f
+ *   cupsdRemoveTimeout() - Discard a timed callback.
e6c38f
+ *   compare_timeouts()   - Compare timed callbacks for array sorting.
e6c38f
+ */
e6c38f
+
e6c38f
+#include <config.h>
e6c38f
+
e6c38f
+#ifdef HAVE_AVAHI /* Applies to entire file... */
e6c38f
+
e6c38f
+/*
e6c38f
+ * Include necessary headers...
e6c38f
+ */
e6c38f
+
e6c38f
+#include "cupsd.h"
e6c38f
+
e6c38f
+#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
e6c38f
+#  include <malloc.h>
e6c38f
+#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
e6c38f
+
e6c38f
+#ifdef HAVE_AVAHI
e6c38f
+#  include <avahi-common timeval.h="">
e6c38f
+#endif /* HAVE_AVAHI */
e6c38f
+
e6c38f
+
e6c38f
+struct _cupsd_timeout_s
e6c38f
+{
e6c38f
+  struct timeval when;
e6c38f
+  int enabled;
e6c38f
+  cupsd_timeoutfunc_t callback;
e6c38f
+  void *data;
e6c38f
+};
e6c38f
+
e6c38f
+/*
e6c38f
+ * Local functions...
e6c38f
+ */
e6c38f
+
e6c38f
+/*
e6c38f
+ * 'compare_timeouts()' - Compare timed callbacks for array sorting.
e6c38f
+ */
e6c38f
+
e6c38f
+static int
e6c38f
+compare_timeouts (cupsd_timeout_t *p0, cupsd_timeout_t *p1)
e6c38f
+{
e6c38f
+  if (!p0->enabled || !p1->enabled)
e6c38f
+  {
e6c38f
+    if (!p0->enabled && !p1->enabled)
e6c38f
+      return (0);
e6c38f
+
e6c38f
+    return (p0->enabled ? -1 : 1);
e6c38f
+  }
e6c38f
+
e6c38f
+  return (avahi_timeval_compare (&p0->when, &p1->when));
e6c38f
+}
e6c38f
+
e6c38f
+
e6c38f
+/*
e6c38f
+ * 'cupsdInitTimeouts()' - Initialise timeout structures.
e6c38f
+ */
e6c38f
+
e6c38f
+void
e6c38f
+cupsdInitTimeouts(void)
e6c38f
+{
e6c38f
+  Timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts, NULL);
e6c38f
+}
e6c38f
+
e6c38f
+
e6c38f
+/*
e6c38f
+ * 'cupsdAddTimeout()' - Add a timed callback.
e6c38f
+ */
e6c38f
+
e6c38f
+cupsd_timeout_t *				/* O - Timeout handle */
e6c38f
+cupsdAddTimeout(const struct timeval *tv,	/* I - Absolute time */
e6c38f
+		cupsd_timeoutfunc_t cb,		/* I - Callback function */
e6c38f
+		void *data)			/* I - User data */
e6c38f
+{
e6c38f
+  cupsd_timeout_t *timeout;
e6c38f
+
e6c38f
+  timeout = malloc (sizeof(cupsd_timeout_t));
e6c38f
+  if (timeout != NULL)
e6c38f
+  {
e6c38f
+    timeout->enabled = (tv != NULL);
e6c38f
+    if (tv)
e6c38f
+    {
e6c38f
+      timeout->when.tv_sec = tv->tv_sec;
e6c38f
+      timeout->when.tv_usec = tv->tv_usec;
e6c38f
+    }
e6c38f
+
e6c38f
+    timeout->callback = cb;
e6c38f
+    timeout->data = data;
e6c38f
+    cupsArrayAdd (Timeouts, timeout);
e6c38f
+  }
e6c38f
+
e6c38f
+  return timeout;
e6c38f
+}
e6c38f
+
e6c38f
+
e6c38f
+/*
e6c38f
+ * 'cupsdNextTimeout()' - Find the next enabled timed callback.
e6c38f
+ */
e6c38f
+
e6c38f
+cupsd_timeout_t *		/* O - Next enabled timeout or NULL */
e6c38f
+cupsdNextTimeout(long *delay)	/* O - Seconds before scheduled */
e6c38f
+{
e6c38f
+  cupsd_timeout_t *first = cupsArrayFirst (Timeouts);
e6c38f
+  struct timeval curtime;
e6c38f
+
e6c38f
+  if (first && !first->enabled)
e6c38f
+    first = NULL;
e6c38f
+
e6c38f
+  if (first && delay)
e6c38f
+  {
e6c38f
+    gettimeofday (&curtime, NULL);
e6c38f
+    if (avahi_timeval_compare (&curtime, &first->when) > 0)
e6c38f
+    {
e6c38f
+      *delay = 0;
e6c38f
+    } else {
e6c38f
+      *delay = 1 + first->when.tv_sec - curtime.tv_sec;
e6c38f
+      if (first->when.tv_usec < curtime.tv_usec)
e6c38f
+	(*delay)--;
e6c38f
+    }
e6c38f
+  }
e6c38f
+
e6c38f
+  return (first);
e6c38f
+}
e6c38f
+
e6c38f
+
e6c38f
+/*
e6c38f
+ * 'cupsdRunTimeout()' - Run a timed callback.
e6c38f
+ */
e6c38f
+
e6c38f
+void
e6c38f
+cupsdRunTimeout(cupsd_timeout_t *timeout)	/* I - Timeout */
e6c38f
+{
e6c38f
+  if (!timeout)
e6c38f
+    return;
e6c38f
+  timeout->enabled = 0;
e6c38f
+  if (!timeout->callback)
e6c38f
+    return;
e6c38f
+  timeout->callback (timeout, timeout->data);
e6c38f
+}
e6c38f
+
e6c38f
+/*
e6c38f
+ * 'cupsdUpdateTimeout()' - Adjust the time of a timed callback or disable it.
e6c38f
+ */
e6c38f
+
e6c38f
+void
e6c38f
+cupsdUpdateTimeout(cupsd_timeout_t *timeout,	/* I - Timeout */
e6c38f
+		   const struct timeval *tv)	/* I - Absolute time or NULL */
e6c38f
+{
e6c38f
+  cupsArrayRemove (Timeouts, timeout);
e6c38f
+  timeout->enabled = (tv != NULL);
e6c38f
+  if (tv)
e6c38f
+  {
e6c38f
+    timeout->when.tv_sec = tv->tv_sec;
e6c38f
+    timeout->when.tv_usec = tv->tv_usec;
e6c38f
+  }
e6c38f
+  cupsArrayAdd (Timeouts, timeout);
e6c38f
+}
e6c38f
+
e6c38f
+
e6c38f
+/*
e6c38f
+ * 'cupsdRemoveTimeout()' - Discard a timed callback.
e6c38f
+ */
e6c38f
+
e6c38f
+void
e6c38f
+cupsdRemoveTimeout(cupsd_timeout_t *timeout)	/* I - Timeout */
e6c38f
+{
e6c38f
+  cupsArrayRemove (Timeouts, timeout);
e6c38f
+  free (timeout);
e6c38f
+}
e6c38f
+
e6c38f
+
e6c38f
+#endif /* HAVE_AVAHI ... from top of file */
e6c38f
+
e6c38f
+/*
e6c38f
+ * End of "$Id$".
e6c38f
+ */