2 This file is part of GNUnet.
3 Copyright (C) 2008--2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file testbed/gnunet-service-testbed_cache.c
21 * @brief testbed cache implementation
22 * @author Sree Harsha Totakura
24 #include "gnunet-service-testbed.h"
27 * Redefine LOG with a changed log component string
32 #define LOG(kind,...) \
33 GNUNET_log_from (kind, "testbed-cache", __VA_ARGS__)
42 * DLL next ptr for least recently used cache entries
44 struct CacheEntry *next;
47 * DLL prev ptr for least recently used cache entries
49 struct CacheEntry *prev;
54 struct GNUNET_MessageHeader *hello;
57 * The id of the peer this entry corresponds to
64 * Hashmap to maintain cache
66 static struct GNUNET_CONTAINER_MultiHashMap32 *cache;
69 * DLL head for least recently used cache entries; least recently used
70 * cache items are at the head. The cache enties are added to this queue when
71 * their demand becomes zero. They are removed from the queue when they are
72 * needed by any operation.
74 static struct CacheEntry *cache_head;
77 * DLL tail for least recently used cache entries; recently used cache
78 * items are at the tail.The cache enties are added to this queue when
79 * their demand becomes zero. They are removed from the queue when they are
80 * needed by any operation.
82 static struct CacheEntry *cache_tail;
85 * Maximum number of elements to cache
87 static unsigned int cache_size;
91 * Looks up in the cache and returns the entry
93 * @param peer_id the peer identity of the peer whose corresponding entry has to
95 * @return the HELLO message; NULL if not found
97 static struct CacheEntry *
98 cache_lookup (unsigned int peer_id)
100 struct CacheEntry *entry;
102 GNUNET_assert (NULL != cache);
103 entry = GNUNET_CONTAINER_multihashmap32_get (cache, peer_id);
106 GNUNET_CONTAINER_DLL_remove (cache_head, cache_tail, entry);
107 GNUNET_CONTAINER_DLL_insert_tail (cache_head, cache_tail, entry);
113 * Free the resources occupied by a cache entry
115 * @param entry the cache entry to free
118 free_entry (struct CacheEntry *entry)
120 GNUNET_CONTAINER_DLL_remove (cache_head, cache_tail, entry);
121 GNUNET_free_non_null (entry->hello);
127 * Creates a new cache entry and then puts it into the cache's hashtable.
129 * @param peer_id the index of the peer to tag the newly created entry
130 * @return the newly created entry
132 static struct CacheEntry *
133 add_entry (unsigned int peer_id)
135 struct CacheEntry *entry;
137 GNUNET_assert (NULL != cache);
138 if (cache_size == GNUNET_CONTAINER_multihashmap32_size (cache))
140 /* remove the LRU head */
142 GNUNET_assert (GNUNET_OK ==
143 GNUNET_CONTAINER_multihashmap32_remove (cache, (uint32_t)
148 entry = GNUNET_new (struct CacheEntry);
149 entry->peer_id = peer_id;
150 GNUNET_assert (GNUNET_OK ==
151 GNUNET_CONTAINER_multihashmap32_put (cache,
154 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
155 GNUNET_CONTAINER_DLL_insert_tail (cache_head, cache_tail, entry);
161 * Iterator over hash map entries.
164 * @param key current key
165 * @param value value in the hash map
166 * @return GNUNET_YES if we should continue to
171 cache_clear_iterator (void *cls, uint32_t key, void *value)
173 struct CacheEntry *entry = value;
175 GNUNET_assert (NULL != entry);
176 GNUNET_assert (GNUNET_YES ==
177 GNUNET_CONTAINER_multihashmap32_remove (cache, key, value));
191 GNUNET_CONTAINER_multihashmap32_iterate (cache, &cache_clear_iterator, NULL);
192 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (cache));
193 GNUNET_CONTAINER_multihashmap32_destroy (cache);
203 * Initializes the cache
205 * @param size the size of the cache
208 GST_cache_init (unsigned int size)
213 cache = GNUNET_CONTAINER_multihashmap32_create (cache_size);
218 * Looks up in the hello cache and returns the HELLO of the given peer
220 * @param peer_id the index of the peer whose HELLO has to be looked up
221 * @return the HELLO message; NULL if not found
223 const struct GNUNET_MessageHeader *
224 GST_cache_lookup_hello (const unsigned int peer_id)
226 struct CacheEntry *entry;
228 LOG_DEBUG ("Looking up HELLO for peer %u\n", peer_id);
231 LOG_DEBUG ("Caching disabled\n");
234 entry = cache_lookup (peer_id);
237 if (NULL != entry->hello)
238 LOG_DEBUG ("HELLO found for peer %u\n", peer_id);
244 * Caches the HELLO of the given peer. Updates the HELLO if it was already
247 * @param peer_id the peer identity of the peer whose HELLO has to be cached
248 * @param hello the HELLO message
251 GST_cache_add_hello (const unsigned int peer_id,
252 const struct GNUNET_MessageHeader *hello)
254 struct CacheEntry *entry;
258 entry = cache_lookup (peer_id);
260 entry = add_entry (peer_id);
261 GNUNET_free_non_null (entry->hello);
262 entry->hello = GNUNET_copy_message (hello);
265 /* end of gnunet-service-testbed_hc.c */