servlib/bundling/BPQCache.h
changeset 55 1938118cd06c
child 56 76420d9f6e62
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/servlib/bundling/BPQCache.h	Thu Sep 01 15:53:24 2011 +0100
@@ -0,0 +1,84 @@
+/*
+ *    Copyright 2004-2006 Intel Corporation
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#ifndef __BPQ_CACHE__
+#define __BPQ_CACHE__
+
+#ifdef HAVE_CONFIG_H
+#  include <dtn-config.h>
+#endif
+
+#include "Bundle.h"
+#include <oasys/debug/Log.h>
+#include <oasys/util/StringUtils.h>
+
+namespace dtn {
+
+class BPQBlock;
+class FragmentState;
+class EndpointID;
+class BPQResponse;
+
+class BPQCache : public oasys::Logger {
+public:
+	BPQCache() :
+        Logger("BPQCache", "/dtn/bundle/bpq") {}
+
+	/**
+	 * Add a new BPQ response to the to the cache
+	 */
+    bool add_response_bundle(Bundle* bundle, BPQBlock* block);
+
+    /**
+     * Try to answer a BPQ query with a response in the cache
+     */
+    bool answer_query(Bundle* bundle, BPQBlock* block);
+
+    /**
+     * Number of bundles in the cache
+     */
+    size_t size() {return bpq_table_.size();}
+
+    static const size_t MAX_KEY_SIZE = 4096;
+
+protected:
+
+    void create_cache_entry(Bundle* bundle, std::string key);
+    void replace_cache_entry(Bundle* bundle, std::string key);
+    void append_cache_entry(Bundle* bundle, std::string key);
+    int  update_bpq_block(Bundle* bundle, BPQBlock* block);
+
+    /**
+     * Calculate a hash table key from a bundle
+     * This is a concatenation of the Matching Rule and the Query
+     *
+     * If the query is too long, use a hash of the query
+     */
+    void get_hash_key(Bundle* bundle, std::string* key);
+    void get_hash_key(BPQBlock* block, std::string* key);
+
+
+    /**
+     * Table of partial BPQ bundles
+     */
+    typedef oasys::StringHashMap<FragmentState*> Cache;
+    Cache bpq_table_;
+
+};
+
+} // namespace dtn
+
+#endif