blob: 61d6452d57ca714fec908d568e1ef7a3d32b6018 [file] [log] [blame]
Ilya Moiseenko6b583af2011-08-12 19:01:43 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Ilya Moiseenko <iliamo@cs.ucla.edu>
19 */
20
21#include "ndn_contentstore.h"
22
23namespace ns3
24{
25namespace NDNabstraction
26{
27
28ContentStore::ContentStore( int maxSize )
29: m_maxSize(maxSize) { }
30
31
32ContentStore::~ContentStore( )
33{ }
34
35//Find corresponding CS entry for the given content name
36CsEntry*
37ContentStore::Lookup(const char *prefix )
38{
39 CriticalSection section(m_csMutex);
40
41 struct CsEntry *result = NULL;
42
43 HASH_FIND_STR (m_contentStore, prefix, result);
44
45 if(result != NULL)
46 Promote (*result);
47
48 return result;
49}
50
51
52
53//Add entry to content store, if content store is full, use LRU replacement
54void
55ContentStore::Add( const char *contentName, int contentSize )
56{
57 CriticalSection section(m_csMutex);
58
59 // removing the old record
60 struct CsEntry *tmp = NULL;
61 HASH_FIND_STR (m_contentStore, contentName, tmp);
62 HASH_DELETE (hh, m_contentStore,tmp);
63 //free(tmp);
64
65 int size = (int)HASH_COUNT(m_contentStore);
66
67 if(size == m_maxSize )
68 {
69 CsEntry *entry = m_LRU.back();
70 HASH_DELETE (hh, m_contentStore,entry);//_cs.erase( entry->contentName );
71 m_LRU.pop_back( );
72 }
73
74 struct CsEntry *ce = (struct CsEntry*)malloc(sizeof(struct CsEntry));
75 ce->contentName = (char*)contentName;
76 ce->contentSize = contentSize;
77
78 //_cs[ contentName ] = ce;
79 HASH_ADD_KEYPTR (hh, m_contentStore, ce->contentName, strlen(ce->contentName), ce);
80
81 //CsEntry *ce_in_hash = &(_cs[ contentName ]);
82 struct CsEntry *ce_in_hash = NULL;
83 HASH_FIND_STR (m_contentStore, contentName, ce_in_hash);
84 m_LRU.push_front( ce_in_hash );
85 ce_in_hash->lruPosition = m_LRU.begin( );
86}
87
88
89//move the given CS entry to the head of the list
90void
91ContentStore::Promote(CsEntry &ce )
92{
93 // should not lock mutex. Otherwise deadlocks will be welcome
94 if( m_LRU.front() == &ce ) return;
95
96 //assert( *(ce.lruPosition)==&ce ); // should point to the same object
97
98 // swaping positions in _lru
99 m_LRU.erase( ce.lruPosition );
100 m_LRU.push_front( &ce );
101 ce.lruPosition = m_LRU.begin( );
102
103 //assert( *(ce.lruPosition)==&ce ); // should point to the same object
104}
105
106void
107ContentStore::Dump()
108{
109 CriticalSection section(m_csMutex);
110
111 struct CsEntry *s, *tmp;
112
113 HASH_ITER(hh, m_contentStore, s, tmp)
114 {
115 printf("-%s-", s->contentName);
116 /* ... it is safe to delete and free s here */
117 }
118
119 printf("\n");
120
121}
122}
123}