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
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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__)
44 * DLL next ptr for least recently used cache entries
46 struct CacheEntry *next;
49 * DLL prev ptr for least recently used cache entries
51 struct CacheEntry *prev;
56 struct GNUNET_MessageHeader *hello;
59 * The id of the peer this entry corresponds to
66 * Hashmap to maintain cache
68 static struct GNUNET_CONTAINER_MultiHashMap32 *cache;
71 * DLL head for least recently used cache entries; least recently used
72 * cache items are at the head. The cache enties are added to this queue when
73 * their demand becomes zero. They are removed from the queue when they are
74 * needed by any operation.
76 static struct CacheEntry *cache_head;
79 * DLL tail for least recently used cache entries; recently used cache
80 * items are at the tail.The cache enties are added to this queue when
81 * their demand becomes zero. They are removed from the queue when they are
82 * needed by any operation.
84 static struct CacheEntry *cache_tail;
87 * Maximum number of elements to cache
89 static unsigned int cache_size;
93 * Looks up in the cache and returns the entry
95 * @param peer_id the peer identity of the peer whose corresponding entry has to
97 * @return the HELLO message; NULL if not found
99 static struct CacheEntry *
100 cache_lookup (unsigned int peer_id)
102 struct CacheEntry *entry;
104 GNUNET_assert (NULL != cache);
105 entry = GNUNET_CONTAINER_multihashmap32_get (cache, peer_id);
108 GNUNET_CONTAINER_DLL_remove (cache_head, cache_tail, entry);
109 GNUNET_CONTAINER_DLL_insert_tail (cache_head, cache_tail, entry);
115 * Free the resources occupied by a cache entry
117 * @param entry the cache entry to free
120 free_entry (struct CacheEntry *entry)
122 GNUNET_CONTAINER_DLL_remove (cache_head, cache_tail, entry);
123 GNUNET_free_non_null (entry->hello);
129 * Creates a new cache entry and then puts it into the cache's hashtable.
131 * @param peer_id the index of the peer to tag the newly created entry
132 * @return the newly created entry
134 static struct CacheEntry *
135 add_entry (unsigned int peer_id)
137 struct CacheEntry *entry;
139 GNUNET_assert (NULL != cache);
140 if (cache_size == GNUNET_CONTAINER_multihashmap32_size (cache))
142 /* remove the LRU head */
144 GNUNET_assert (GNUNET_OK ==
145 GNUNET_CONTAINER_multihashmap32_remove (cache, (uint32_t)
150 entry = GNUNET_new (struct CacheEntry);
151 entry->peer_id = peer_id;
152 GNUNET_assert (GNUNET_OK ==
153 GNUNET_CONTAINER_multihashmap32_put (cache,
156 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
157 GNUNET_CONTAINER_DLL_insert_tail (cache_head, cache_tail, entry);
163 * Iterator over hash map entries.
166 * @param key current key
167 * @param value value in the hash map
168 * @return GNUNET_YES if we should continue to
173 cache_clear_iterator (void *cls, uint32_t key, void *value)
175 struct CacheEntry *entry = value;
177 GNUNET_assert (NULL != entry);
178 GNUNET_assert (GNUNET_YES ==
179 GNUNET_CONTAINER_multihashmap32_remove (cache, key, value));
193 GNUNET_CONTAINER_multihashmap32_iterate (cache, &cache_clear_iterator, NULL);
194 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (cache));
195 GNUNET_CONTAINER_multihashmap32_destroy (cache);
205 * Initializes the cache
207 * @param size the size of the cache
210 GST_cache_init (unsigned int size)
215 cache = GNUNET_CONTAINER_multihashmap32_create (cache_size);
220 * Looks up in the hello cache and returns the HELLO of the given peer
222 * @param peer_id the index of the peer whose HELLO has to be looked up
223 * @return the HELLO message; NULL if not found
225 const struct GNUNET_MessageHeader *
226 GST_cache_lookup_hello (const unsigned int peer_id)
228 struct CacheEntry *entry;
230 LOG_DEBUG ("Looking up HELLO for peer %u\n", peer_id);
233 LOG_DEBUG ("Caching disabled\n");
236 entry = cache_lookup (peer_id);
239 if (NULL != entry->hello)
240 LOG_DEBUG ("HELLO found for peer %u\n", peer_id);
246 * Caches the HELLO of the given peer. Updates the HELLO if it was already
249 * @param peer_id the peer identity of the peer whose HELLO has to be cached
250 * @param hello the HELLO message
253 GST_cache_add_hello (const unsigned int peer_id,
254 const struct GNUNET_MessageHeader *hello)
256 struct CacheEntry *entry;
260 entry = cache_lookup (peer_id);
262 entry = add_entry (peer_id);
263 GNUNET_free_non_null (entry->hello);
264 entry->hello = GNUNET_copy_message (hello);
267 /* end of gnunet-service-testbed_hc.c */