Example #1
0
void tester(int port)
{
    int r;

    NS *ns = nsCreate();

    nsOnTime(ns, nowd() + 0.1, on_time, &port);

    r = nsRun(ns);

P   dbgPrint(stderr, "nsRun returned %d\n", r);

    nsDestroy(ns);
}
Example #2
0
int main(int argc, char *argv[])
{
    int i;

    Dispatcher *dis = disCreate();

    pipe(fd);

    disOnData(dis, fd[0], handle_fd0, NULL);
    disOnTime(dis, nowd() + 0.1, handle_timeout, NULL);

    for (i = 0; i < fd[0]; i++) {
        make_sure_that(!disOwnsFd(dis, i));
    }

    make_sure_that(disOwnsFd(dis, fd[0]));

    disRun(dis);

    return errors;
}
Example #3
0
/*
 * Prepare a call to select() based on the files and timeouts set in <dis>. The necessary parameters
 * to select() are returned through <nfds>, <rfds>, <wfds> and <tv> (exception-fds should be set to
 * NULL). <*tv> is set to point to an appropriate timeout value, or NULL if no timeout is to be set.
 * This function will clear <rfds> and <wfds>, so if callers want to add their own file descriptors,
 * they should do so after calling this function. This function returns -1 if the first timeout
 * should already have occurred, otherwise 0.
 */
int disPrepareSelect(Dispatcher *dis, int *nfds, fd_set *rfds, fd_set *wfds, struct timeval **tv)
{
    int fd;
    double delta_t;
    DIS_Timer *timer;

    *nfds = paCount(&dis->files);

    FD_ZERO(rfds);
    FD_ZERO(wfds);

P   dbgPrint(stderr, "File descriptors:");

    for (fd = 0; fd < *nfds; fd++) {
        DIS_File *file = paGet(&dis->files, fd);

        if (file == NULL) continue;

P       fprintf(stderr, " %d", fd);

        FD_SET(fd, rfds);

        if (bufLen(&file->outgoing) > 0) FD_SET(fd, wfds);

P       {
            fprintf(stderr, " (%s%s)",
                    FD_ISSET(fd, rfds) ? "r" : "",
                    FD_ISSET(fd, wfds) ? "w" : "");
        }
    }

P   fprintf(stderr, "\n");

P   dbgPrint(stderr, "%d pending timers:\n", listLength(&dis->timers));

P   for (timer = listHead(&dis->timers); timer; timer = listNext(timer)) {
        fprintf(stderr, "\t%f seconds\n", timer->t - nowd());
    }

    if ((timer = listHead(&dis->timers)) == NULL) {
        *tv = NULL;
    }
    else if ((delta_t = timer->t - nowd()) < 0) {
#if 0
P       dbgPrint(stderr, "First timer %f seconds ago, return -1\n", -delta_t);

        return -1;
#endif
        dis->tv.tv_sec = 0;
        dis->tv.tv_usec = 0;

        *tv = &dis->tv;
    }
    else {
P       dbgPrint(stderr, "First timer in %f seconds.\n", delta_t);

        dis->tv.tv_sec = (int) delta_t;
        dis->tv.tv_usec = 1000000 * fmod(delta_t, 1.0);

        *tv = &dis->tv;
    }

    return 0;
}