#ifndef WWWI_MIND_RRCACHE_H
#define WWWI_MIND_RRCACHE_H
#include <map>
#include "labellist.h"
#include "request.h"
#include "response.h"
#include "rrlist.h"
#include "wwwi/mutex.h"
#include "wwwi/rwlock.h"
using std::map;
using WWWI::Mutex;
using WWWI::RWLock;
class CacheNode {
public:
CacheNode() { cspEntries = new CacheRRList; }
unsigned Expire(time_t i_tmNow, time_t &io_tmrNextExpire);
void Walk(ostream &io_smr, int iDepth) const;
~CacheNode();
friend class RRCache;
protected:
CacheRRListPtr cspEntries;
map<Label,CacheNode*> cmTree;
};
typedef CacheNode* CacheNodePtr;
typedef map<Label,CacheNode*> CacheMap;
typedef CacheMap *CacheMapPtr;
typedef const CacheMap *CacheMapConstPtr;
class RRCache {
public:
RRCache(void);
void Add(ResponseConstPtr ci_rsp);
bool Add(RRConstPtr ci_rrp);
void Add(RRListConstPtr ci_rlp);
bool AddRecord(const char *i_strName, RRType i_ty, RRClass i_cl, unsigned i_uTTL, const char *i_strRData);
bool AddRecord(LabelListConstPtr i_llpName, RRType i_ty, RRClass i_cl, unsigned i_uTTL, const char *ci_strRData);
void AddRootServer(const char *i_strName, const char *ci_strIP);
void Expire(void);
inline ResponsePtr Get(QuestionConstPtr ci_qnp);
inline ResponsePtr Get(const char *ci_strName, RRType i_ty, RRClass i_cl);
ResponsePtr Get(LabelListConstPtr ci_llpName, RRType i_ty, RRClass i_cl);
RRListPtr GetAR(RRListPtr i_rlp);
void GetAR(RRListPtr o_rlp, RRPtr i_rrp);
RRListPtr GetBestAuthority(LabelListConstPtr ci_llp) const;
RRListPtr GetNS(RRListPtr i_rlp) const;
inline RRListPtr GetSimple(const char *i_strName, RRType i_ty, RRClass i_cl);
RRListPtr GetSimple(LabelListConstPtr i_llpName, RRType i_ty, RRClass i_cl);
inline void SetMaxEntries(unsigned i_uMaxEntries);
inline void SetNextExpire(time_t i_tmWhenExpire);
inline bool ShouldExpire(void) const;
void Walk(ostream &io_smr) const;
~RRCache(void);
protected:
bool Add(CacheRRListPtr io_csp, CacheRRPtr &i_crpr);
CacheRRListPtr GetNameList(LabelListConstPtr i_llpName, bool i_bCreate);
CacheRRListConstPtr GetNameList(LabelListPtr o_llpMatch, LabelListConstPtr i_llpName, RRType i_ty, RRClass i_cl, bool i_bBest) const;
void GetNS(RRListPtr o_rlp, LabelListConstPtr i_llpName) const;
protected:
CacheNode m_cn;
mutable RWLock m_rw;
unsigned m_uEntries;
unsigned m_uMaxEntries;
time_t m_tmNextExpire;
mutable Mutex m_mxExpire;
};
typedef RRCache* RRCachePtr;
#include "rrcache.i"
#endif