servlib/bundling/BundleDaemon.cc
changeset 12 7463e4bb80e4
parent 11 4dd7e0cb11a7
child 13 841ab1482c9c
--- a/servlib/bundling/BundleDaemon.cc	Tue Jun 14 16:52:53 2011 +0100
+++ b/servlib/bundling/BundleDaemon.cc	Wed Jun 22 13:07:38 2011 +0100
@@ -429,18 +429,55 @@
         bpq_bundles_->erase(bpq_bundles_->front());
     }
 
-    log_info("accept_bpq_response: add new response to cache - Query: %s",
-        (char*)bpq_block->query_val());
-
-    // add bundle to cache and store
-    bundle->set_in_bpq_cache(true);
-    bpq_bundles_->push_back(bundle);
-
-    if (add_to_store) {
-        bundle->set_in_datastore(true);
-        actions_->store_add(bundle);
+
+    log_debug("accept_bpq_response: check expiration for bundle");
+    struct timeval now;
+    gettimeofday(&now, 0);
+
+    // schedule the bundle expiration timer
+    struct timeval expiration_time;
+    expiration_time.tv_sec =
+        BundleTimestamp::TIMEVAL_CONVERSION +
+        bundle->creation_ts().seconds_ +
+        bundle->expiration();
+
+    expiration_time.tv_usec = now.tv_usec;
+
+    long int when = expiration_time.tv_sec - now.tv_sec;
+
+    if (when > 0) {
+        log_debug("scheduling expiration for bundle id %d at %u.%u "
+                    "(in %lu seconds)",
+                    bundle->bundleid(),
+                    (u_int)expiration_time.tv_sec, (u_int)expiration_time.tv_usec,
+                    when);
+
+        log_info("accept_bpq_response: add new response to cache - Query: %s",
+                 (char*)bpq_block->query_val());
+
+        // add bundle to cache and store
+        bundle->set_in_bpq_cache(true);
+        bpq_bundles_->push_back(bundle);
+
+        if (add_to_store) {
+            bundle->set_in_datastore(true);
+            actions_->store_add(bundle);
+        }
+
+    } else {
+        log_warn("scheduling IMMEDIATE expiration for bundle id %d: "
+                 "[expiration %llu, creation time %llu.%llu, offset %u, now %u.%u]",
+                   bundle->bundleid(), bundle->expiration(),
+                   bundle->creation_ts().seconds_,
+                   bundle->creation_ts().seqno_,
+                   BundleTimestamp::TIMEVAL_CONVERSION,
+                   (u_int)now.tv_sec, (u_int)now.tv_usec);
+        expiration_time = now;
     }
-  
+
+    bundle->set_expiration_timer(new ExpirationTimer(bundle));
+    bundle->expiration_timer()->schedule_at(&expiration_time);
+ 
     log_info("BPQ bundle cache now contains %d bundle(s)", bpq_bundles_->size());
     return true;
 }
@@ -477,7 +514,7 @@
             handle_event(&e);
 
             // TODO: update this logging
-            s10_bundle(S10_TXADMIN,response,NULL,0,0,bundle,"bpq response");
+            s10_bundle(S10_FROMCACHE,response,NULL,0,0,bundle,"bpq response");
             return true;
         }
     }
@@ -701,7 +738,7 @@
         stats_.generated_bundles_++;
         source_str = " (from cache)";
         //TODO: update this logging
-        s10_bundle(S10_OHCRAP,bundle,NULL,0,0,NULL,"__FILE__:__LINE__");
+        s10_bundle(S10_FROMCACHE,bundle,NULL,0,0,NULL,"__FILE__:__LINE__");
         break;
 
     default: