message_cache.cc 3.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

// $Id$

Jelte Jansen's avatar
Jelte Jansen committed
17 18
#include <config.h>

19 20 21
#include <nsas/nsas_entry_compare.h>
#include <nsas/hash_table.h>
#include <nsas/hash_deleter.h>
22 23
#include "message_cache.h"
#include "cache_entry_key.h"
24 25

using namespace isc::nsas;
26
using namespace isc::dns;
27
using namespace std;
28 29 30 31 32

namespace isc {
namespace cache {

MessageCache::MessageCache(boost::shared_ptr<RRsetCache> rrset_cache,
33 34
    uint32_t cache_size, uint16_t message_class):
    message_class_(message_class),
35 36 37 38 39 40
    rrset_cache_(rrset_cache),
    message_table_(new NsasEntryCompare<MessageEntry>, cache_size),
    message_lru_((3 * cache_size),
                  new HashDeleter<MessageEntry>(message_table_))
{
}
zhanglikun's avatar
zhanglikun committed
41

42
bool
43 44 45
MessageCache::lookup(const isc::dns::Name& qname,
                     const isc::dns::RRType& qtype,
                     isc::dns::Message& response)
46
{
zhanglikun's avatar
zhanglikun committed
47
    std::string entry_name = genCacheEntryName(qname, qtype);
48
    std::cout << "[XX] MESSAGECACHE LOOKUP: " << entry_name << std::endl;
zhanglikun's avatar
zhanglikun committed
49
    HashKey entry_key = HashKey(entry_name, RRClass(message_class_));
50
    MessageEntryPtr msg_entry = message_table_.get(entry_key);
51
    if(msg_entry) {
52
        message_lru_.touch(msg_entry);
53
        return (msg_entry->genMessage(time(NULL), response));
54 55
    }

56
    return (false);
57 58 59
}

bool
60 61
MessageCache::update(const Message& msg) {
    QuestionIterator iter = msg.beginQuestion();
zhanglikun's avatar
zhanglikun committed
62
    std::string entry_name = genCacheEntryName((*iter)->getName(), (*iter)->getType());
63
    std::cout << "[XX] MESSAGECACHE UDPATE: " << entry_name << std::endl;
zhanglikun's avatar
zhanglikun committed
64
    HashKey entry_key = HashKey(entry_name, RRClass(message_class_));
zhanglikun's avatar
zhanglikun committed
65

66 67 68
    // The simplest way to update is removing the old message entry directly.
    // We have find the existed message entry, since we need to delete it
    // from lru list too.
zhanglikun's avatar
zhanglikun committed
69 70
    // TODO, but there should be a better way, since we here have to remove and
    // add the message entry, maybe there is one way to touch it once.
71 72 73 74 75
    MessageEntryPtr old_msg_entry = message_table_.get(entry_key);
    if (old_msg_entry) {
        message_lru_.remove(old_msg_entry);
    }

76
    MessageEntryPtr msg_entry(new MessageEntry(msg, rrset_cache_));
zhanglikun's avatar
zhanglikun committed
77
    message_lru_.add(msg_entry);
78
    return (message_table_.add(msg_entry, entry_key, true));
79 80 81 82
}

void
MessageCache::dump(const std::string&) {
83
    //TODO
84 85 86 87
}

void
MessageCache::load(const std::string&) {
88
    //TODO
89 90 91 92
}

bool
MessageCache::resize(uint32_t) {
93
    //TODO
94
    return (true);
95 96 97 98 99
}

} // namespace cache
} // namespace isc