+ * 'backendWaitLoop()' - Wait for input from stdin while handling side-channel
+ * queries.
+ */
+
+int /* O - 1 if data is ready, 0 if not */
+backendWaitLoop(
+ int snmp_fd, /* I - SNMP socket or -1 if none */
+ http_addr_t *addr, /* I - Address of device */
+ int use_bc, /* I - Use back-channel? */
+ _cups_sccb_t side_cb) /* I - Side-channel callback */
+{
+ int nfds; /* Number of file descriptors */
+ fd_set input; /* Input set for reading */
+ time_t curtime = 0, /* Current time */
+ snmp_update = 0;/* Last SNMP status update */
+ struct timeval timeout; /* Timeout for select() */
+
+
+ fprintf(stderr, "DEBUG: backendWaitLoop(snmp_fd=%d, addr=%p, side_cb=%p)\n",
+ snmp_fd, addr, side_cb);
+
+ /*
+ * Now loop until we receive data from stdin...
+ */
+
+ if (snmp_fd >= 0)
+ snmp_update = time(NULL) + 5;
+
+ for (;;)
+ {
+ /*
+ * Use select() to determine whether we have data to copy around...
+ */
+
+ FD_ZERO(&input);
+ FD_SET(0, &input);
+ if (side_cb)
+ FD_SET(CUPS_SC_FD, &input);
+
+ if (snmp_fd >= 0)
+ {
+ curtime = time(NULL);
+ timeout.tv_sec = curtime >= snmp_update ? 0 : snmp_update - curtime;
+ timeout.tv_usec = 0;
+
+ nfds = select(CUPS_SC_FD + 1, &input, NULL, NULL, &timeout);
+ }
+ else
+ nfds = select(CUPS_SC_FD + 1, &input, NULL, NULL, NULL);
+
+ if (nfds < 0)
+ {
+ /*
+ * Pause printing to clear any pending errors...
+ */
+
+ if (errno == EINTR)
+ {
+ fputs("DEBUG: Received an interrupt before any bytes were "
+ "written, aborting.\n", stderr);
+ return (0);
+ }
+
+ sleep(1);
+ continue;
+ }
+
+ /*
+ * Check for input on stdin...
+ */
+
+ if (FD_ISSET(0, &input))
+ break;
+
+ /*
+ * Check if we have a side-channel request ready...
+ */
+
+ if (side_cb && FD_ISSET(CUPS_SC_FD, &input))
+ {
+ /*
+ * Do the side-channel request, then start back over in the select
+ * loop since it may have read from print_fd...
+ */
+
+ if ((*side_cb)(0, -1, snmp_fd, addr, use_bc))
+ side_cb = NULL;
+ continue;
+ }
+
+ /*
+ * Do SNMP updates periodically...
+ */
+
+ if (snmp_fd >= 0 && curtime >= snmp_update)
+ {
+ if (backendSNMPSupplies(snmp_fd, addr, NULL, NULL))
+ snmp_fd = -1;
+ else
+ snmp_update = curtime + 5;
+ }
+ }
+
+ /*
+ * Return with success...
+ */
+
+ return (1);
+}
+
+
+/*
+ * End of "$Id: runloop.c 9565 2011-02-23 00:08:08Z mike $".