/// <summary> /// Add the Data packet to the cache so that it is available to use to /// answer interests. If data.getMetaInfo().getFreshnessPeriod() is not /// negative, set the staleness time to now plus the maximum of /// data.getMetaInfo().getFreshnessPeriod() and minimumCacheLifetime, which is /// checked during cleanup to remove stale content. /// This also checks if cleanupIntervalMilliseconds /// milliseconds have passed and removes stale content from the cache. After /// removing stale content, remove timed-out pending interests from /// storePendingInterest(), then if the added Data packet satisfies any /// interest, send it through the face and remove the interest from the pending /// interest table. /// </summary> /// /// <param name="data"></param> public void add(Data data) { double nowMilliseconds = net.named_data.jndn.util.Common.getNowMilliseconds(); doCleanup(nowMilliseconds); if (data.getMetaInfo().getFreshnessPeriod() >= 0.0d) { // The content will go stale, so use staleTimeCache_. MemoryContentCache.StaleTimeContent content = new MemoryContentCache.StaleTimeContent(data, nowMilliseconds, minimumCacheLifetime_); // Insert into staleTimeCache, sorted on content.cacheRemovalTimeMilliseconds_. // Search from the back since we expect it to go there. int i = staleTimeCache_.Count - 1; while (i >= 0) { if (staleTimeCache_[i].getCacheRemovalTimeMilliseconds() <= content .getCacheRemovalTimeMilliseconds()) { break; } --i; } // Element i is the greatest less than or equal to // content.cacheRemovalTimeMilliseconds_, so insert after it. staleTimeCache_.Insert(i + 1, content); } else { // The data does not go stale, so use noStaleTimeCache_. ILOG.J2CsMapping.Collections.Collections.Add(noStaleTimeCache_, new MemoryContentCache.Content(data)); } // Remove timed-out interests and check if the data packet matches any // pending interest. // Go backwards through the list so we can erase entries. for (int i_0 = pendingInterestTable_.Count - 1; i_0 >= 0; --i_0) { MemoryContentCache.PendingInterest pendingInterest = pendingInterestTable_[i_0]; if (pendingInterest.isTimedOut(nowMilliseconds)) { ILOG.J2CsMapping.Collections.Collections.RemoveAt(pendingInterestTable_, i_0); continue; } if (pendingInterest.getInterest().matchesName(data.getName())) { try { // Send to the same face from the original call to onInterest. // wireEncode returns the cached encoding if available. logger_.log(ILOG.J2CsMapping.Util.Logging.Level.INFO, "MemoryContentCache: Reply w/ add Data {0}", data.getName()); pendingInterest.getFace().send(data.wireEncode()); } catch (IOException ex) { logger_.log(ILOG.J2CsMapping.Util.Logging.Level.SEVERE, ex.Message); return; } // The pending interest is satisfied, so remove it. ILOG.J2CsMapping.Collections.Collections.RemoveAt(pendingInterestTable_, i_0); } } }