servlib/bundling/BPQCache.h
changeset 66 e1101c5d54a1
parent 64 1296a0283271
child 67 cfe2328ba1c6
equal deleted inserted replaced
65:333724f2f7cf 66:e1101c5d54a1
    24 #include "Bundle.h"
    24 #include "Bundle.h"
    25 #include <oasys/debug/Log.h>
    25 #include <oasys/debug/Log.h>
    26 #include <oasys/util/StringUtils.h>
    26 #include <oasys/util/StringUtils.h>
    27 #include "../reg/Registration.h"
    27 #include "../reg/Registration.h"
    28 #include "../reg/RegistrationTable.h"
    28 #include "../reg/RegistrationTable.h"
       
    29 #include <list>
       
    30 
    29 namespace dtn {
    31 namespace dtn {
    30 
    32 
    31 class BPQBlock;
    33 class BPQBlock;
    32 class BPQCacheEntry;
    34 class BPQCacheEntry;
    33 class EndpointID;
    35 class EndpointID;
    36 class RegistrationTable;
    38 class RegistrationTable;
    37 
    39 
    38 class BPQCache : public oasys::Logger {
    40 class BPQCache : public oasys::Logger {
    39 public:
    41 public:
    40 	BPQCache() :
    42 	BPQCache() :
    41         Logger("BPQCache", "/dtn/bundle/bpq") {}
    43         Logger("BPQCache", "/dtn/bundle/bpq"),
       
    44         cache_size_(0) {}
    42 
    45 
    43 	/**
    46 	/**
    44 	 * Add a new BPQ response to the to the cache
    47 	 * Add a new BPQ response to the to the cache
    45 	 */
    48 	 */
    46     bool add_response_bundle(Bundle* bundle, BPQBlock* block);
    49     bool add_response_bundle(Bundle* bundle, BPQBlock* block);
    53     bool answer_query(Bundle* bundle, BPQBlock* block);
    56     bool answer_query(Bundle* bundle, BPQBlock* block);
    54 
    57 
    55     /**
    58     /**
    56      * Number of bundles in the cache
    59      * Number of bundles in the cache
    57      */
    60      */
    58     size_t size() {return bpq_table_.size();}
    61     size_t size() { return bpq_table_.size();}
    59 
    62 
    60     static const size_t MAX_KEY_SIZE = 4096;
    63     static 			bool  cache_enabled_;
       
    64     static 			u_int max_cache_size_;
       
    65     static const 	u_int MAX_KEY_SIZE = 4096;
    61 
    66 
    62 protected:
    67 protected:
    63 
    68 
    64     /**
    69     /**
    65      * Build a new BPQCcacheEntry from this bundle.
    70      * Build a new BPQCcacheEntry from this bundle.
    66      * Copy the bundle into the fragment list
    71      * Copy the bundle into the fragment list
    67      */
    72      */
    68     void create_cache_entry(Bundle* bundle, BPQBlock* block, std::string key);
    73     void create_cache_entry(Bundle* bundle, BPQBlock* block, std::string key);
    69     void replace_cache_entry(Bundle* bundle, BPQBlock* block, std::string key);
    74 
    70     void append_cache_entry(Bundle* bundle, std::string key);
    75     /**
       
    76 	 * Remove existing cache entry along with all bundle fragments
       
    77 	 * and create a new entry
       
    78 	 */
       
    79     void replace_cache_entry(BPQCacheEntry* entry, Bundle* bundle,
       
    80     						 BPQBlock* block, std::string key);
       
    81 
       
    82     void remove_cache_entry(BPQCacheEntry* entry, std::string key);
       
    83     /**
       
    84      * Add received bundle fragment to the cache entry
       
    85      * @return  true if the new fragment completed the cache entry
       
    86      * 			false otherwise
       
    87      */
       
    88     bool append_cache_entry(BPQCacheEntry* entry, Bundle* bundle, std::string key);
       
    89 
       
    90 
       
    91 
       
    92     bool bpq_requires_fragment(BPQBlock* block, Bundle* fragment);
    71     int  update_bpq_block(Bundle* bundle, BPQBlock* block);
    93     int  update_bpq_block(Bundle* bundle, BPQBlock* block);
    72     bool try_to_deliver(BPQCacheEntry* entry);
    94     bool try_to_deliver(BPQCacheEntry* entry);
       
    95 
       
    96     void update_lru_keys(std::string key);
    73 
    97 
    74     /**
    98     /**
    75      * Calculate a hash table key from a bundle
    99      * Calculate a hash table key from a bundle
    76      * This is a  SHA256 hash of the concatenation of:
   100      * This is a  SHA256 hash of the concatenation of:
    77      * 		Matching Rule and Query Value
   101      * 		Matching Rule and Query Value
    84      * Table of partial BPQ bundles
   108      * Table of partial BPQ bundles
    85      */
   109      */
    86     typedef oasys::StringHashMap<BPQCacheEntry*> Cache;
   110     typedef oasys::StringHashMap<BPQCacheEntry*> Cache;
    87     Cache bpq_table_;
   111     Cache bpq_table_;
    88 
   112 
       
   113     std::list<std::string> lru_keys_;
       
   114     size_t cache_size_;
    89 };
   115 };
    90 
   116 
    91 } // namespace dtn
   117 } // namespace dtn
    92 
   118 
    93 #endif
   119 #endif