2 This file is part of GNUnet.
3 (C) 2008--2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testbed/gnunet-service-testbed_cache.c
23 * @brief testbed cache implementation
24 * @author Sree Harsha Totakura
26 #include "gnunet-service-testbed.h"
29 * Redefine LOG with a changed log component string
34 #define LOG(kind,...) \
35 GNUNET_log_from (kind, "testbed-cache", __VA_ARGS__)
39 * Time to expire a cache entry
41 #define CACHE_EXPIRY \
42 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
51 * DLL next ptr for least recently used cache entries
53 struct CacheEntry *next;
56 * DLL prev ptr for least recently used cache entries
58 struct CacheEntry *prev;
61 * The peer identity of this peer. Will be set upon opening a connection to
62 * the peers CORE service. Will be NULL until then and after the CORE
63 * connection is closed
65 struct GNUNET_PeerIdentity *peer_identity;
68 * The key for this entry
70 struct GNUNET_HashCode key;
75 struct GNUNET_MessageHeader *hello;
78 * The id of the peer this entry corresponds to
83 * Is this entry in LRU cache queue?
90 * Hashmap to maintain cache
92 static struct GNUNET_CONTAINER_MultiHashMap *cache;
95 * DLL head for least recently used cache entries; least recently used
96 * cache items are at the head. The cache enties are added to this queue when
97 * their demand becomes zero. They are removed from the queue when they are
98 * needed by any operation.
100 static struct CacheEntry *lru_cache_head;
103 * DLL tail for least recently used cache entries; recently used cache
104 * items are at the tail.The cache enties are added to this queue when
105 * their demand becomes zero. They are removed from the queue when they are
106 * needed by any operation.
108 static struct CacheEntry *lru_cache_tail;
111 * the size of the LRU queue
113 static unsigned int lru_cache_size;
116 * the threshold size for the LRU queue
118 static unsigned int lru_cache_threshold_size;
121 * The total number of elements in cache
123 static unsigned int cache_size;
127 * Looks up in the cache and returns the entry
129 * @param key the peer identity of the peer whose corresponding entry has to be
131 * @return the HELLO message; NULL if not found
133 static struct CacheEntry *
134 cache_lookup (const struct GNUNET_HashCode *key)
136 struct CacheEntry *entry;
140 entry = GNUNET_CONTAINER_multihashmap_get (cache, key);
146 * Creates a new cache entry and then puts it into the cache's hashtable.
148 * @param key the hash code to use for inserting the newly created entry
149 * @param peer_id the index of the peer to tag the newly created entry
150 * @return the newly created entry
152 static struct CacheEntry *
153 add_entry (const struct GNUNET_HashCode *key, unsigned int peer_id)
155 struct CacheEntry *entry;
157 entry = GNUNET_malloc (sizeof (struct CacheEntry));
158 entry->peer_id = peer_id;
159 memcpy (&entry->key, key, sizeof (struct GNUNET_HashCode));
160 GNUNET_assert (GNUNET_OK ==
161 GNUNET_CONTAINER_multihashmap_put (cache, &entry->key, entry,
162 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
169 * Iterator over hash map entries.
172 * @param key current key code
173 * @param value value in the hash map
174 * @return GNUNET_YES if we should continue to
179 cache_clear_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
181 struct CacheEntry *entry = value;
182 static unsigned int ncleared;
184 GNUNET_assert (NULL != entry);
185 LOG_DEBUG ("Clearing entry %u of %u\n", ++ncleared, cache_size);
186 GNUNET_assert (GNUNET_YES ==
187 GNUNET_CONTAINER_multihashmap_remove (cache, key, value));
188 GNUNET_free_non_null (entry->hello);
200 GNUNET_CONTAINER_multihashmap_iterate (cache, &cache_clear_iterator, NULL);
201 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (cache));
202 GNUNET_CONTAINER_multihashmap_destroy (cache);
205 lru_cache_threshold_size = 0;
207 lru_cache_head = NULL;
208 lru_cache_tail = NULL;
213 * Initializes the cache
215 * @param size the size of the cache
218 GST_cache_init (unsigned int size)
222 lru_cache_threshold_size = size;
225 cache = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_YES);
230 * Looks up in the hello cache and returns the HELLO of the given peer
232 * @param peer_id the index of the peer whose HELLO has to be looked up
233 * @return the HELLO message; NULL if not found
235 const struct GNUNET_MessageHeader *
236 GST_cache_lookup_hello (const unsigned int peer_id)
238 struct CacheEntry *entry;
239 struct GNUNET_HashCode key;
241 LOG_DEBUG ("Looking up HELLO for peer %u\n", peer_id);
242 GNUNET_CRYPTO_hash (&peer_id, sizeof (peer_id), &key);
243 entry = cache_lookup (&key);
246 if (NULL != entry->hello)
247 LOG_DEBUG ("HELLO found for peer %u\n", peer_id);
253 * Caches the HELLO of the given peer. Updates the HELLO if it was already
256 * @param peer_id the peer identity of the peer whose HELLO has to be cached
257 * @param hello the HELLO message
260 GST_cache_add_hello (const unsigned int peer_id,
261 const struct GNUNET_MessageHeader *hello)
263 struct CacheEntry *entry;
264 struct GNUNET_HashCode key;
266 GNUNET_CRYPTO_hash (&peer_id, sizeof (peer_id), &key);
267 entry = GNUNET_CONTAINER_multihashmap_get (cache, &key);
269 entry = add_entry (&key, peer_id);
270 GNUNET_free_non_null (entry->hello);
271 entry->hello = GNUNET_copy_message (hello);
274 /* end of gnunet-service-testbed_hc.c */