Example #1
0
bool ccnl_is_timed_out(struct timeval *now, struct timeval *last_used, uint32_t timeout_s, uint32_t timeout_us)
{
    timex_t time = timex_set(timeout_s, timeout_us);
    timex_normalize(&time);
    struct timeval abs_timeout = { last_used->tv_sec + time.seconds, last_used->tv_usec + time.microseconds };
    return timevaldelta(now, &abs_timeout) > 0;
}
Example #2
0
struct timeval *
ccnl_run_events(void)
{
    static struct timeval now;
    long usec;

    ccnl_get_timeval(&now);
    //DEBUGMSG(1, "ccnl_run_events now: %ld:%ld\n", now.tv_sec, now.tv_usec);

    while (eventqueue) {
        struct ccnl_timer_s *t = eventqueue;
        usec = timevaldelta(&(t->timeout), &now);

        if (usec >= 0) {
            //DEBUGMSG(1, "ccnl_run_events nothing to do: %ld:%ld\n", now.tv_sec, now.tv_usec);
            now.tv_sec = usec / 1000000;
            now.tv_usec = usec % 1000000;
            return &now;
        }

        //DEBUGMSG(1, "ccnl_run_events run event handler: %ld:%ld\n", now.tv_sec, now.tv_usec);
        if (t->fct) {
            (t->fct)(t->node, t->intarg);
        }
        else if (t->fct2) {
            (t->fct2)(t->aux1, t->aux2);
        }

        eventqueue = t->next;
        ccnl_free(t);
    }

    return NULL;
}
Example #3
0
void
ccnl_sched_CTS_done(struct ccnl_sched_s *s, int cnt, int len)
{
#ifdef USE_CHEMFLOW
    cf_time now = ccnl_cf_now();
#else
    struct timeval now;
    long since;
#endif

    if (!s) {
        DEBUGMSG(VERBOSE, "ccnl_sched_CTS_done sched=%p cnt=%d len=%d\n",
             (void*)s, cnt, len);
        return;
    }
    DEBUGMSG(VERBOSE, "ccnl_sched_CTS_done sched=%p/%d cnt=%d len=%d (mycnt=%d)\n",
             (void*)s, s->mode, cnt, len, s->cnt);

    s->cnt -= cnt;
    if (s->cnt <= 0)
        return;

    if (s->mode == 0) {
        s->cts(s->aux1, s->aux2);
        return;
    }

#ifdef USE_CHEMFLOW
    if (CF_OK == cf_queue_dequeue_packet(s->q, 1)) {
        DEBUGMSG(VERBOSE, "  cf_dequeue successful; CTS\n");
        cf_queue_update_concentrations(s->q, now);
        cf_engine_reschedule_and_set_timer(engine, now);
        s->cts(s->aux1, s->aux2);
    }
#else
    ccnl_get_timeval(&now);

    since = timevaldelta(&(s->nextTX), &now);
    if (since <= 0) {
        now.tv_sec += s->ipi / 1000000;
        now.tv_usec += s->ipi % 1000000;
        memcpy(&(s->nextTX), &now, sizeof(now));
        s->cts(s->aux1, s->aux2);
        return;
    }
    DEBUGMSG(VERBOSE, "since=%ld\n", since);
//    ccnl_set_timer(since, (void(*)(void*,int))signal_cts, ccnl, ifndx);
    s->pendingTimer = ccnl_set_timer(since, s->cts, s->aux1, s->aux2);
    s->nextTX.tv_sec += s->ipi / 1000000;;
    s->nextTX.tv_usec += s->ipi % 1000000;;

//    s->cts();
#endif
}
Example #4
0
void
ccnl_sched_RTS(struct ccnl_sched_s *s, int cnt, int len,
               void *aux1, void *aux2)
{
#ifdef USE_CHEMFLOW
    cf_time now = ccnl_cf_now();
#else
    struct timeval now;
    long since;
#endif

    if (!s) {
        DEBUGMSG(VERBOSE, "ccnl_sched_RTS sched=%p len=%d aux1=%p aux2=%p\n",
             (void*)s, len, (void*)aux1, (void*)aux2);
        return;
    }
    DEBUGMSG(VERBOSE, "ccnl_sched_RTS sched=%p/%d len=%d aux1=%p aux2=%p\n",
             (void*)s, s->mode, len, (void*)aux1, (void*)aux2);

    s->cnt += cnt;
    s->aux1 = aux1;
    s->aux2 = aux2;

    if (s->mode == 0) {
        s->cts(aux1, aux2);
        return;
    }

#ifdef USE_CHEMFLOW
    for (; cnt; --cnt) {
        DEBUGMSG(VERBOSE, "  cf_enqueuen");
        if (CF_OK == cf_queue_enqueue_packet(s->q, 1)) {
            cf_queue_update_concentrations(s->q, now);
            cf_engine_reschedule_and_set_timer(engine, now);
        }
    }
#else
    ccnl_get_timeval(&now);
    since = timevaldelta(&(s->nextTX), &now);
    if (since <= 0) {
        now.tv_sec += s->ipi / 1000000;
        now.tv_usec += s->ipi % 1000000;
        memcpy(&(s->nextTX), &now, sizeof(now));
        s->cts(aux1, aux2);
        return;
    }
    DEBUGMSG(VERBOSE, "since=%ld\n", since);
//    ccnl_set_timer(since, (void(*)(void*,int))signal_cts, ccnl, ifndx);
    s->pendingTimer = ccnl_set_timer(since, s->cts, aux1, aux2);
    s->nextTX.tv_sec += s->ipi / 1000000;;
    s->nextTX.tv_usec += s->ipi % 1000000;;
#endif
}
struct timeval*
ccnl_run_events()
{
    static struct timeval now;
    long usec;

    gettimeofday(&now, 0);
    while (eventqueue) {
        struct ccnl_timer_s *t = eventqueue;
        usec = timevaldelta(&(t->timeout), &now);
        if (usec >= 0) {
            now.tv_sec = usec / 1000000;
            now.tv_usec = usec % 1000000;
            return &now;
        }
        else if (t->fct2)
            (t->fct2)(t->aux1, t->aux2);
        eventqueue = t->next;
        ccnl_free(t);
    }
    return NULL;
}
Example #6
0
struct ccnl_content_s *
ccnl_content_add2cache(struct ccnl_relay_s *ccnl, struct ccnl_content_s *c)
{
    DEBUGMSG(99, "ccnl_content_add2cache (%d/%d)\n", ccnl->contentcnt,
             ccnl->max_cache_entries);

    if (ccnl->max_cache_entries <= 0) {
        DEBUGMSG(1, "  content store disabled...\n");
        return NULL;
    }

    while (ccnl->max_cache_entries <= ccnl->contentcnt) {
        DEBUGMSG(1, "  remove Least Recently Used content...\n");
        struct ccnl_content_s *c2, *lru = NULL;

        for (c2 = ccnl->contents; c2; c2 = c2->next) {
            DEBUGMSG(1, "    '%s' -> %ld:%ld\n", ccnl_prefix_to_path(c2->name), c2->last_used.tv_sec, c2->last_used.tv_usec);
            if (!(c2->flags & CCNL_CONTENT_FLAGS_STATIC)
                && ((!lru) || timevaldelta(&c2->last_used, &lru->last_used) < 0)) {
                lru = c2;
            }
        }

        if (lru) {
            DEBUGMSG(1, "   replaced: '%s'\n", ccnl_prefix_to_path(lru->name));
            ccnl_content_remove(ccnl, lru);
        }
        else {
            DEBUGMSG(1, "   no dynamic content to remove...\n");
            break;
        }
    }

    DEBUGMSG(1, "  add new content to store: '%s'\n", ccnl_prefix_to_path(c->name));
    DBL_LINKED_LIST_ADD(ccnl->contents, c);
    ccnl->contentcnt++;
    return c;
}
Example #7
0
void
simu_eventloop()
{ 
    static struct timeval now;
    long usec;

    while (eventqueue) {
        struct ccnl_timer_s *t = eventqueue;
        // printf("  looping now %g\n", CCNL_NOW());
        gettimeofday(&now, 0);
        usec = timevaldelta(&(t->timeout), &now);
        if (usec >= 0) {
            // usleep(usec);
            struct timespec ts;
            ts.tv_sec = usec / 1000000;
            ts.tv_nsec = 1000 * (usec % 1000000);
            nanosleep(&ts, NULL);
        }
        t = eventqueue;
        eventqueue = t->next;
        if (t->fct) {
            void (*fct)(char,int) = t->fct;
            char c = t->node;
            int i = t->intarg;
            ccnl_free(t);
            (fct)(c, i);
        } else if (t->fct2) {
            void (*fct2)(void*,void*) = t->fct2;
            void *aux1 = t->aux1;
            void *aux2 = t->aux2;
            ccnl_free(t);
            (fct2)(aux1, aux2);
        }
    }
    DEBUGMSG(ERROR, "simu event loop: no more events to handle\n");
}