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__)
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 * Creates a new cache entry and then puts it into the cache's hashtable.
117 * @param peer_id the index of the peer to tag the newly created entry
118 * @return the newly created entry
120 static struct CacheEntry *
121 add_entry (unsigned int peer_id)
123 struct CacheEntry *entry;
125 GNUNET_assert (NULL != cache);
126 if (cache_size == GNUNET_CONTAINER_multihashmap32_size (cache))
128 /* remove the LRU head */
130 GNUNET_assert (GNUNET_OK ==
131 GNUNET_CONTAINER_multihashmap32_remove (cache, (uint32_t)
135 entry = GNUNET_new (struct CacheEntry);
136 entry->peer_id = peer_id;
137 GNUNET_assert (GNUNET_OK ==
138 GNUNET_CONTAINER_multihashmap32_put (cache,
141 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
142 GNUNET_CONTAINER_DLL_insert_tail (cache_head, cache_tail, entry);
148 * Iterator over hash map entries.
151 * @param key current key
152 * @param value value in the hash map
153 * @return GNUNET_YES if we should continue to
158 cache_clear_iterator (void *cls, uint32_t key, void *value)
160 struct CacheEntry *entry = value;
162 GNUNET_assert (NULL != entry);
163 GNUNET_assert (GNUNET_YES ==
164 GNUNET_CONTAINER_multihashmap32_remove (cache, key, value));
165 GNUNET_free_non_null (entry->hello);
179 GNUNET_CONTAINER_multihashmap32_iterate (cache, &cache_clear_iterator, NULL);
180 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (cache));
181 GNUNET_CONTAINER_multihashmap32_destroy (cache);
191 * Initializes the cache
193 * @param size the size of the cache
196 GST_cache_init (unsigned int size)
201 cache = GNUNET_CONTAINER_multihashmap32_create (cache_size);
206 * Looks up in the hello cache and returns the HELLO of the given peer
208 * @param peer_id the index of the peer whose HELLO has to be looked up
209 * @return the HELLO message; NULL if not found
211 const struct GNUNET_MessageHeader *
212 GST_cache_lookup_hello (const unsigned int peer_id)
214 struct CacheEntry *entry;
216 LOG_DEBUG ("Looking up HELLO for peer %u\n", peer_id);
219 LOG_DEBUG ("Caching disabled\n");
222 entry = cache_lookup (peer_id);
225 if (NULL != entry->hello)
226 LOG_DEBUG ("HELLO found for peer %u\n", peer_id);
232 * Caches the HELLO of the given peer. Updates the HELLO if it was already
235 * @param peer_id the peer identity of the peer whose HELLO has to be cached
236 * @param hello the HELLO message
239 GST_cache_add_hello (const unsigned int peer_id,
240 const struct GNUNET_MessageHeader *hello)
242 struct CacheEntry *entry;
246 entry = cache_lookup (peer_id);
248 entry = add_entry (peer_id);
249 GNUNET_free_non_null (entry->hello);
250 entry->hello = GNUNET_copy_message (hello);
253 /* end of gnunet-service-testbed_hc.c */