From 2f7fd0b35c96f0bd93eabbcf4b1d5fd62b1b269c Mon Sep 17 00:00:00 2001 From: Christian Mehlis Date: Sat, 30 Nov 2013 01:22:17 +0100 Subject: [PATCH] convert all time related datastructure to struct timeval --- sys/net/ccn_lite/ccn-lite-relay.c | 7 +++---- sys/net/ccn_lite/ccnl-core.c | 29 ++++++++++++++++++----------- sys/net/ccn_lite/ccnl-core.h | 11 ++++++----- sys/net/ccn_lite/ccnl-includes.h | 1 - sys/net/ccn_lite/ccnl-platform.c | 25 ++++--------------------- sys/net/ccn_lite/ccnl-platform.h | 5 ++--- sys/net/ccn_lite/ccnl.h | 25 +++++++++++++++---------- 7 files changed, 48 insertions(+), 55 deletions(-) diff --git a/sys/net/ccn_lite/ccn-lite-relay.c b/sys/net/ccn_lite/ccn-lite-relay.c index 724166604b..9a16924c8e 100644 --- a/sys/net/ccn_lite/ccn-lite-relay.c +++ b/sys/net/ccn_lite/ccn-lite-relay.c @@ -68,7 +68,7 @@ ccnl_run_events(void) static struct timeval now; long usec; - rtc_time(&now); + ccnl_get_timeval(&now); DEBUGMSG(1, "ccnl_run_events now: %ld:%ld\n", now.tv_sec, now.tv_usec); while (eventqueue) { @@ -382,10 +382,9 @@ int ccnl_io_loop(struct ccnl_relay_s *ccnl) */ void ccnl_riot_relay_start(int max_cache_entries, int fib_threshold_prefix, int fib_threshold_aggregate) { - struct timeval now; - theRelay.startup_time = rtc_time(&now); + ccnl_get_timeval(&theRelay.startup_time); - DEBUGMSG(1, "This is ccn-lite-relay, starting at %lu:%lu\n", now.tv_sec, now.tv_usec); + DEBUGMSG(1, "This is ccn-lite-relay, starting at %lu:%lu\n", theRelay.startup_time.tv_sec, theRelay.startup_time.tv_usec); DEBUGMSG(1, " compile time: %s %s\n", __DATE__, __TIME__); DEBUGMSG(1, " max_cache_entries: %d\n", max_cache_entries); DEBUGMSG(1, " threshold_prefix: %d\n", fib_threshold_prefix); diff --git a/sys/net/ccn_lite/ccnl-core.c b/sys/net/ccn_lite/ccnl-core.c index 18501a1605..9437e46a95 100644 --- a/sys/net/ccn_lite/ccnl-core.c +++ b/sys/net/ccn_lite/ccnl-core.c @@ -394,7 +394,7 @@ ccnl_get_face_or_create(struct ccnl_relay_s *ccnl, int ifndx, uint16_t sender_id if (ifndx == f->ifndx && (f->faceid == sender_id)) { DEBUGMSG(1, "face found! ifidx=%d sender_id=%d faceid=%d\n", ifndx, sender_id, f->faceid); - f->last_used = CCNL_NOW(); + ccnl_get_timeval(&f->last_used); return f; } } @@ -459,7 +459,7 @@ ccnl_get_face_or_create(struct ccnl_relay_s *ccnl, int ifndx, uint16_t sender_id #endif - f->last_used = CCNL_NOW(); + ccnl_get_timeval(&f->last_used); DBL_LINKED_LIST_ADD(ccnl->faces, f); return f; @@ -738,7 +738,7 @@ ccnl_interest_new(struct ccnl_relay_s *ccnl, struct ccnl_face_s *from, *ppkd = 0; i->minsuffix = minsuffix; i->maxsuffix = maxsuffix; - i->last_used = CCNL_NOW(); + ccnl_get_timeval(&i->last_used); DBL_LINKED_LIST_ADD(ccnl->pit, i); return i; } @@ -752,7 +752,7 @@ int ccnl_interest_append_pending(struct ccnl_interest_s *i, for (pi = i->pending; pi; pi = pi->next) { // check whether already listed if (pi->face == from) { DEBUGMSG(40, " we found a matching interest, updating time\n"); - pi->last_used = CCNL_NOW(); + ccnl_get_timeval(&pi->last_used); return 0; } @@ -768,7 +768,7 @@ int ccnl_interest_append_pending(struct ccnl_interest_s *i, } pi->face = from; - pi->last_used = CCNL_NOW(); + ccnl_get_timeval(&pi->last_used); if (last) { last->next = pi; @@ -890,7 +890,7 @@ ccnl_content_new(struct ccnl_relay_s *ccnl, struct ccnl_buf_s **pkt, return NULL; } - c->last_used = CCNL_NOW(); + ccnl_get_timeval(&c->last_used); c->content = content; c->contentlen = contlen; c->pkt = *pkt; @@ -1107,6 +1107,12 @@ ccnl_forward_remove(struct ccnl_relay_s *ccnl, struct ccnl_forward_s *fwd) return fwd2; } +bool ccnl_is_timeouted(struct timeval *now, struct timeval *last_used, time_t timeout_s, time_t timeout_us) +{ + struct timeval abs_timeout = { last_used->tv_sec + timeout_s, last_used->tv_usec + timeout_us }; + return timevaldelta(now, &abs_timeout) > 0; +} + void ccnl_do_ageing(void *ptr, void *dummy) { @@ -1116,11 +1122,12 @@ void ccnl_do_ageing(void *ptr, void *dummy) struct ccnl_content_s *c = relay->contents; struct ccnl_interest_s *i = relay->pit; struct ccnl_face_s *f = relay->faces; - time_t t = CCNL_NOW(); - DEBUGMSG(999, "ccnl_do_ageing %d\n", (int) t); + struct timeval now; + ccnl_get_timeval(&now); + DEBUGMSG(999, "ccnl_do_ageing %ld:%ld\n", now.tv_sec, now.tv_usec); while (c) { - if ((c->last_used + CCNL_CONTENT_TIMEOUT) <= t + if (ccnl_is_timeouted(&now, &c->last_used, CCNL_CONTENT_TIMEOUT_SEC, CCNL_CONTENT_TIMEOUT_USEC) && !(c->flags & CCNL_CONTENT_FLAGS_STATIC)) { c = ccnl_content_remove(relay, c); } @@ -1131,7 +1138,7 @@ void ccnl_do_ageing(void *ptr, void *dummy) while (i) { // CONFORM: "Entries in the PIT MUST timeout rather // than being held indefinitely." - if ((i->last_used + CCNL_INTEREST_TIMEOUT) <= t || + if (ccnl_is_timeouted(&now, &i->last_used, CCNL_INTEREST_TIMEOUT_SEC, CCNL_INTEREST_TIMEOUT_SEC) || i->retries > CCNL_MAX_INTEREST_RETRANSMIT) { i = ccnl_interest_remove(relay, i); } @@ -1154,7 +1161,7 @@ void ccnl_do_ageing(void *ptr, void *dummy) while (f) { if (!(f->flags & CCNL_FACE_FLAGS_STATIC) - && (f->last_used + CCNL_FACE_TIMEOUT) <= t) { + && ccnl_is_timeouted(&now, &f->last_used, CCNL_FACE_TIMEOUT_SEC, CCNL_FACE_TIMEOUT_USEC)) { f = ccnl_face_remove(relay, f); } else { diff --git a/sys/net/ccn_lite/ccnl-core.h b/sys/net/ccn_lite/ccnl-core.h index 8415b54d01..b9fdcd4d89 100644 --- a/sys/net/ccn_lite/ccnl-core.h +++ b/sys/net/ccn_lite/ccnl-core.h @@ -52,6 +52,7 @@ enum {STAT_RCV_I, STAT_RCV_C, STAT_SND_I, STAT_SND_C, STAT_QLEN, STAT_EOP1}; #include #include +#include #include "ccnl.h" @@ -84,7 +85,7 @@ struct ccnl_if_s { // interface for packet IO }; struct ccnl_relay_s { - time_t startup_time; + struct timeval startup_time; int id; struct ccnl_face_s *faces; struct ccnl_forward_s *fib; @@ -146,7 +147,7 @@ struct ccnl_face_s { int ifndx; sockunion peer; int flags; - int last_used; // updated when we receive a packet + struct timeval last_used; // updated when we receive a packet struct ccnl_buf_s *outq, *outqend; // queue of packets to send struct ccnl_frag_s *frag; // which special datagram armoring struct ccnl_sched_s *sched; @@ -168,7 +169,7 @@ struct ccnl_interest_s { int minsuffix, maxsuffix; struct ccnl_buf_s *ppkd; // publisher public key digest struct ccnl_buf_s *pkt; // full datagram - int last_used; + struct timeval last_used; int retries; struct ccnl_forward_s *forwarded_over; }; @@ -176,7 +177,7 @@ struct ccnl_interest_s { struct ccnl_pendint_s { // pending interest struct ccnl_pendint_s *next; // , *prev; struct ccnl_face_s *face; - int last_used; + struct timeval last_used; }; struct ccnl_content_s { @@ -189,7 +190,7 @@ struct ccnl_content_s { int contentlen; // NON-CONFORM: "The [ContentSTore] MUST also implement the Staleness Bit." // >> CCNL: currently no stale bit, old content is fully removed << - int last_used; + struct timeval last_used; int served_cnt; }; diff --git a/sys/net/ccn_lite/ccnl-includes.h b/sys/net/ccn_lite/ccnl-includes.h index 5cd484c205..cfb311e39a 100644 --- a/sys/net/ccn_lite/ccnl-includes.h +++ b/sys/net/ccn_lite/ccnl-includes.h @@ -26,7 +26,6 @@ #include #include -#include "rtc.h" #include "sha256.h" #define RIOT_MSG_DEV (1) diff --git a/sys/net/ccn_lite/ccnl-platform.c b/sys/net/ccn_lite/ccnl-platform.c index 5b746cf33d..df362817e0 100644 --- a/sys/net/ccn_lite/ccnl-platform.c +++ b/sys/net/ccn_lite/ccnl-platform.c @@ -62,8 +62,7 @@ ccnl_set_timer(int usec, void (*fct)(void *aux1, void *aux2), } t->fct2 = fct; - //gettimeofday(&t->timeout, NULL); - rtc_time(&t->timeout); + ccnl_get_timeval(&t->timeout); usec += t->timeout.tv_usec; t->timeout.tv_sec += usec / 1000000; t->timeout.tv_usec = usec % 1000000; @@ -133,30 +132,14 @@ ccnl_rem_timer(void *h) } } -double -current_time(void) -{ - struct timeval tv; - static time_t start; - static time_t start_usec; - - ccnl_get_timeval(&tv); - - if (!start) { - start = tv.tv_sec; - start_usec = tv.tv_usec; - } - - return (double)(tv.tv_sec) - start + - ((double)(tv.tv_usec) - start_usec) / 1000000; -} - char * timestamp(void) { static char ts[30], *cp; + struct timeval now; + ccnl_get_timeval(&now); - sprintf(ts, "%.4g", CCNL_NOW()); + sprintf(ts, "%.4lu", (time_t) 100000 * now.tv_sec + now.tv_usec); cp = strchr(ts, '.'); if (!cp) { diff --git a/sys/net/ccn_lite/ccnl-platform.h b/sys/net/ccn_lite/ccnl-platform.h index 8e73c4f6da..c074879de3 100644 --- a/sys/net/ccn_lite/ccnl-platform.h +++ b/sys/net/ccn_lite/ccnl-platform.h @@ -20,9 +20,6 @@ #include -double current_time(void); -#define CCNL_NOW() current_time() - // (ms) I moved the following struct def here because it is used by all // containers apart from the kernel (this way I don't need to redefine it // for omnet. @@ -39,6 +36,8 @@ struct ccnl_timer_s { int handler; }; +void ccnl_get_timeval(struct timeval *tv); + long timevaldelta(struct timeval *a, struct timeval *b); void *ccnl_set_timer(int usec, void (*fct)(void *aux1, void *aux2), diff --git a/sys/net/ccn_lite/ccnl.h b/sys/net/ccn_lite/ccnl.h index 5b775e740b..9b82a888f0 100644 --- a/sys/net/ccn_lite/ccnl.h +++ b/sys/net/ccn_lite/ccnl.h @@ -21,21 +21,26 @@ * 2011-03-30 created */ -#define CCNL_MAX_INTERFACES 10 -#define CCNL_MAX_PACKET_SIZE NATIVE_MAX_DATA_LENGTH +#define CCNL_MAX_INTERFACES 2 /* transceiver and msg interfaces */ -#define CCNL_CONTENT_TIMEOUT 30 // sec -#define CCNL_INTEREST_TIMEOUT 4 // sec -#define CCNL_MAX_INTEREST_RETRANSMIT 2 +#define CCNL_CONTENT_TIMEOUT_SEC 2 +#define CCNL_CONTENT_TIMEOUT_USEC 0 -#define CCNL_FACE_TIMEOUT 15 // sec +#define CCNL_INTEREST_TIMEOUT_SEC 2 +#define CCNL_INTEREST_TIMEOUT_USEC 2 -#define CCNL_MAX_NAME_COMP 16 -#define CCNL_MAX_IF_QLEN 64 +#define CCNL_MAX_INTEREST_RETRANSMIT 4 -#define CCNL_DEFAULT_MAX_CACHE_ENTRIES 0 // means: no content caching -#define CCNL_MAX_NONCES 256 // for detected dups +#define CCNL_FACE_TIMEOUT_SEC 10 +#define CCNL_FACE_TIMEOUT_USEC 0 +#define CCNL_FWD_TIMEOUT_SEC 10 +#define CCNL_FWD_TIMEOUT_USEC 0 + +#define CCNL_MAX_NAME_COMP 16 +#define CCNL_MAX_IF_QLEN 64 + +#define CCNL_MAX_NONCES 256 // for detected dups // ---------------------------------------------------------------------- // our own CCN-lite extensions for the ccnb encoding: