more table based cache stuff, fixed openssl by configuring --with-bsp
authoraidan
Wed, 05 Oct 2011 13:52:40 +0100
changeset 56 76420d9f6e62
parent 55 1938118cd06c
child 57 bb780f1beb9b
more table based cache stuff, fixed openssl by configuring --with-bsp
servlib/Makefile
servlib/bundling/BPQCache.cc
servlib/bundling/BPQCache.h
servlib/bundling/BundleDaemon.cc
--- a/servlib/Makefile	Thu Sep 01 15:53:24 2011 +0100
+++ b/servlib/Makefile	Wed Oct 05 13:52:40 2011 +0100
@@ -29,6 +29,7 @@
     bundling/BPQBlockProcessor.cc       \
     bundling/BPQBlock.cc                \
     bundling/BPQCache.cc				\
+    bundling/BPQCacheEntry.cc			\
     bundling/BPQResponse.cc             \
 	bundling/Bundle.cc			        \
 	bundling/BundleActions.cc		    \
--- 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
 
--- a/servlib/bundling/BPQCache.h	Thu Sep 01 15:53:24 2011 +0100
+++ b/servlib/bundling/BPQCache.h	Wed Oct 05 13:52:40 2011 +0100
@@ -28,7 +28,7 @@
 namespace dtn {
 
 class BPQBlock;
-class FragmentState;
+class BPQCacheEntry;
 class EndpointID;
 class BPQResponse;
 
@@ -44,6 +44,8 @@
 
     /**
      * Try to answer a BPQ query with a response in the cache
+     * @return  true if the query was successfully answered in full
+     * 			false otherwise
      */
     bool answer_query(Bundle* bundle, BPQBlock* block);
 
@@ -74,7 +76,7 @@
     /**
      * Table of partial BPQ bundles
      */
-    typedef oasys::StringHashMap<FragmentState*> Cache;
+    typedef oasys::StringHashMap<BPQCacheEntry*> Cache;
     Cache bpq_table_;
 
 };
--- a/servlib/bundling/BundleDaemon.cc	Thu Sep 01 15:53:24 2011 +0100
+++ b/servlib/bundling/BundleDaemon.cc	Wed Oct 05 13:52:40 2011 +0100
@@ -923,7 +923,6 @@
 
     }
 
-    // If the even source is PEER or STORE,
     // try to handle a BPQ block
     if ( event->source_ == EVENTSRC_APP   || 
          event->source_ == EVENTSRC_PEER  || 
@@ -931,9 +930,9 @@
          event->source_ == EVENTSRC_FRAGMENTATION) {
 
         // TODO: change this to allow fragment responses be sent
-        if ( ! bundle->is_fragment() ) {
-            handle_bpq_block(bundle, event);
-        }
+//        if ( ! bundle->is_fragment() ) {
+		handle_bpq_block(bundle, event);
+//        }
     }
 
     // If the bundle contains a BPQ query that was successfully answered
@@ -2684,9 +2683,10 @@
 
     if (bpq_block.kind() == BPQBlock::KIND_QUERY) {
     	if (bpq_cache()->answer_query(bundle, &bpq_block)) {
+    		log_info_p("/dtn/daemon/bpq", "Query: %s answered completely",
+    				(char*)bpq_block.query_val());
             event->daemon_only_ = true;
         }
-    	// TODO: make sure updated block is put back into bundle
     }
     else if (bpq_block.kind() == BPQBlock::KIND_RESPONSE) {
     	// don't accept local responses