servlib/bundling/BPQCache.cc
changeset 56 76420d9f6e62
parent 55 1938118cd06c
child 58 574358af597b
--- a/servlib/bundling/BPQCache.cc	Thu Sep 01 15:53:24 2011 +0100
+++ b/servlib/bundling/BPQCache.cc	Wed Oct 05 13:52:40 2011 +0100
@@ -15,12 +15,12 @@
  */
 
 #include "BPQCache.h"
+#include "BPQCacheEntry.h"
 #include "BPQBlock.h"
 #include "BPQResponse.h"
-#include "FragmentState.h"
+#include "BPQCacheEntry.h"
 #include "BundleDaemon.h"
-//#include <openssl/sha.h>
-//#include <openssl/err.h>
+#include <openssl/sha.h>
 
 namespace dtn {
 
@@ -43,13 +43,13 @@
 
 	} else {
 		log_debug("response found in cache");
-		FragmentState* state = iter->second;
+		BPQCacheEntry* entry = iter->second;
 
-		if ( state->check_completed() && ! bundle->is_fragment() ) {
+		if ( entry->is_complete() && ! bundle->is_fragment() ) {
 			log_debug("cache complete & bundle complete: "
 					  "accept the newer copy");
 
-			if ( state->bundle().object()->creation_ts() < bundle->creation_ts() ){
+			if ( entry->bundle().object()->creation_ts() < bundle->creation_ts() ){
 				log_debug("received bundle is newer than cached one: "
 						  "replace cache entry");
 
@@ -60,18 +60,18 @@
 										  "do nothing");
 			}
 
-		} else if ( state->check_completed() && bundle->is_fragment() ) {
+		} else if ( entry->is_complete() && bundle->is_fragment() ) {
 			log_debug("cache complete & bundle incomplete: "
 					  "not accepting new fragments");
 
 
-		} else if ( ! state->check_completed() && ! bundle->is_fragment() ) {
+		} else if ( ! entry->is_complete() && ! bundle->is_fragment() ) {
 			log_debug("cache incomplete & bundle complete: "
 					  "replace cache entry");
 
 			replace_cache_entry(bundle, key);
 
-		} else if ( ! state->check_completed() && bundle->is_fragment() ) {
+		} else if ( ! entry->is_complete() && bundle->is_fragment() ) {
 			log_debug("cache incomplete & bundle incomplete: "
 					  "append cache entry");
 
@@ -98,22 +98,21 @@
 
 	if ( cache_iter == bpq_table_.end() ) {
 		log_debug("no response found in cache for query");
-
 		return false;
 	}
 
 	log_debug("response found in cache");
-	FragmentState* state = cache_iter->second;
+	BPQCacheEntry* entry = cache_iter->second;
 	EndpointID local_eid = BundleDaemon::instance()->local_eid();
 
-	bool is_complete = state->check_completed();
+	bool is_complete = entry->is_complete();
 
 	Bundle* current_fragment;
 	BundleList::iterator frag_iter;
-	oasys::ScopeLock l(state->fragment_list().lock(), "BPQCache::answer_query");
+	oasys::ScopeLock l(entry->fragment_list().lock(), "BPQCache::answer_query");
 
-	for (frag_iter  = state->fragment_list().begin();
-		 frag_iter != state->fragment_list().end();
+	for (frag_iter  = entry->fragment_list().begin();
+		 frag_iter != entry->fragment_list().end();
 		 ++frag_iter) {
 
 		current_fragment = *frag_iter;
@@ -163,11 +162,11 @@
 	//			State bundle only contains metadata
 	//			The fragment list contains all the payload data
 
-	FragmentState* state = new FragmentState();
-	bundle->copy_metadata(state->bundle().object());
-	state->add_fragment(bundle);
+	BPQCacheEntry* entry = new BPQCacheEntry();
+	bundle->copy_metadata(entry->bundle().object());
+	entry->add_response(bundle);
 
-	bpq_table_[key] = state;
+	bpq_table_[key] = entry;
 }
 
 //----------------------------------------------------------------------
@@ -181,7 +180,7 @@
 		return;
 	}
 
-	FragmentState* state = iter->second;
+	BPQCacheEntry* entry = iter->second;
 
 	if ( bundle->is_fragment() ) {
 		log_debug("response found in cache, replacing with received bundle fragment "
@@ -194,23 +193,23 @@
 				  key.c_str(), bundle->payload().length());
 	}
 
-	oasys::ScopeLock l(state->fragment_list().lock(),
+	oasys::ScopeLock l(entry->fragment_list().lock(),
 	                   "BPQCache::replace_cache_entry");
 
-    while (! state->fragment_list().empty()) {
+    while (! entry->fragment_list().empty()) {
         BundleDaemon::post(
-			new BundleDeleteRequest(state->fragment_list().pop_back(),
+			new BundleDeleteRequest(entry->fragment_list().pop_back(),
             BundleProtocol::REASON_NO_ADDTL_INFO) );
     }
 
-	ASSERT(state->fragment_list().size() == 0); // moved into events
+	ASSERT(entry->fragment_list().size() == 0); // moved into events
 	l.unlock();
 
 
-	bundle->copy_metadata(state->bundle().object());
-	state->add_fragment(bundle);
+	bundle->copy_metadata(entry->bundle().object());
+	entry->add_response(bundle);
 
-	ASSERT(state->fragment_list().size() == 1);
+	ASSERT(entry->fragment_list().size() == 1);
 }
 
 //----------------------------------------------------------------------
@@ -227,17 +226,17 @@
 			  key.c_str(), bundle->frag_offset(),
 			  bundle->payload().length());
 
-	FragmentState* state = iter->second;
-	state->add_fragment(bundle);
+	BPQCacheEntry* entry = iter->second;
+	entry->add_response(bundle);
 
-	if ( state->check_completed() ) {
+	if ( entry->is_complete() ) {
 		log_info("appending received bundle completed cache copy "
 				"{key: %s, number of frags: %zu}",
-				key.c_str(), state->fragment_list().size());
+				key.c_str(), entry->fragment_list().size());
 	} else {
 		log_debug("appending received bundle has not completed cache copy "
 						"{key: %s, number of frags: %zu}",
-						key.c_str(), state->fragment_list().size());
+						key.c_str(), entry->fragment_list().size());
 	}
 }
 
@@ -295,44 +294,66 @@
     BPQBlock block(bundle);
     get_hash_key(&block, key);
 }
+
 //----------------------------------------------------------------------
 void
 BPQCache::get_hash_key(BPQBlock* block, std::string* key)
 {
-	char buf[BPQCache::MAX_KEY_SIZE];
-//	u_char hash[SHA256_DIGEST_LENGTH];
+    u_char hash[SHA256_DIGEST_LENGTH];
+    char buf[3];
+    key->clear();
+
+    SHA256_CTX sha256;
+    SHA256_Init(&sha256);
+    SHA256_Update(&sha256, block->query_val(), block->query_len());
+    SHA256_Final(hash, &sha256);
 
-	memset(buf, 0, sizeof(char) * BPQCache::MAX_KEY_SIZE);
-//	memset(hash,0, sizeof(char) * SHA256_DIGEST_LENGTH);
+    for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
+    {
+        snprintf(buf, 2, "%02x", hash[i]);
+        key->append(buf);
+    }
+}
+
+
+
 
-	// allow 3 char for the matching rule (1 byte)
-	//     & 1 char for the seperating dot
+
+
+//	char buf[BPQCache::MAX_KEY_SIZE];
+//	u_char hash[SHA256_DIGEST_LENGTH];
+//
+//	memset(buf, 0, sizeof(char) * BPQCache::MAX_KEY_SIZE);
+//	memset(hash,0, sizeof(char) * SHA256_DIGEST_LENGTH);
+//
+//	// allow 3 char for the matching rule (1 byte)
+//	//     & 1 char for the seperating dot
 //	if (block->query_len() <= BPQCache::MAX_KEY_SIZE - 4) {
-		snprintf(buf, BPQCache::MAX_KEY_SIZE, "%03u.%s",
-		             block->matching_rule(),
-		             block->query_val());
-		key->append(buf);
-/*
-	} else {
-		snprintf(buf, 4, "%03u.", block->matching_rule());
-		key->append(buf);
-
-//		TODO: come back and fix this hash stuff
-//	    SHA256(block->query_val(), block->query_len(), obuf);
-
+//		snprintf(buf, BPQCache::MAX_KEY_SIZE, "%03u.%s",
+//		             block->matching_rule(),
+//		             block->query_val());
+//		key->append(buf);
+//
+//	} else {
+//		snprintf(buf, 4, "%03u.", block->matching_rule());
+//		key->append(buf);
+//
+////		TODO: come back and fix this hash stuff
+//	    SHA256(block->query_val(), block->query_len(), buf);
+//
 //		SHA256_CTX sha256;
 //		SHA256_Init(&sha256);
 //		SHA256_Update(&sha256, block->query_val(), block->query_len());
 //		SHA256_Final(hash, &sha256);
-
-	    for (int i = 0; i < SHA256_DIGEST_LENGTH ; i++)
-	    {
-	        snprintf(buf, 2, "%02x", hash[i]);
-	        key->append(buf);
-	    }
-	}
-*/
-}
+//
+//	    for (int i = 0; i < SHA256_DIGEST_LENGTH ; i++)
+//	    {
+//	        snprintf(buf, 2, "%02x", hash[i]);
+//	        key->append(buf);
+//	    }
+//	}
+//
+//}
 
 } // namespace dtn