GNUnet  0.20.0
gnunet-service-testbed_cache.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2008--2013 GNUnet e.V.
4 
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.
9 
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.
14 
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/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
26 #include "platform.h"
27 #include "gnunet-service-testbed.h"
28 
32 #ifdef LOG
33 #undef LOG
34 #endif
35 #define LOG(kind, ...) \
36  GNUNET_log_from (kind, "testbed-cache", __VA_ARGS__)
37 
38 
42 struct CacheEntry
43 {
47  struct CacheEntry *next;
48 
52  struct CacheEntry *prev;
53 
58 
62  unsigned int peer_id;
63 };
64 
65 
70 
77 static struct CacheEntry *cache_head;
78 
85 static struct CacheEntry *cache_tail;
86 
90 static unsigned int cache_size;
91 
92 
100 static struct CacheEntry *
101 cache_lookup (unsigned int peer_id)
102 {
103  struct CacheEntry *entry;
104 
105  GNUNET_assert (NULL != cache);
107  if (NULL == entry)
108  return NULL;
111  return entry;
112 }
113 
114 
120 static void
121 free_entry (struct CacheEntry *entry)
122 {
124  GNUNET_free (entry->hello);
125  GNUNET_free (entry);
126 }
127 
128 
135 static struct CacheEntry *
136 add_entry (unsigned int peer_id)
137 {
138  struct CacheEntry *entry;
139 
140  GNUNET_assert (NULL != cache);
142  {
143  /* remove the LRU head */
144  entry = cache_head;
147  entry->peer_id,
148  entry));
149  free_entry (entry);
150  }
151  entry = GNUNET_new (struct CacheEntry);
152  entry->peer_id = peer_id;
155  (uint32_t) peer_id,
156  entry,
159  return entry;
160 }
161 
162 
173 static int
174 cache_clear_iterator (void *cls, uint32_t key, void *value)
175 {
176  struct CacheEntry *entry = value;
177 
178  GNUNET_assert (NULL != entry);
181  free_entry (entry);
182  return GNUNET_YES;
183 }
184 
185 
189 void
191 {
192  if (NULL != cache)
193  {
195  NULL);
198  cache = NULL;
199  }
200  cache_size = 0;
201  cache_head = NULL;
202  cache_tail = NULL;
203 }
204 
205 
211 void
212 GST_cache_init (unsigned int size)
213 {
214  if (0 == size)
215  return;
216  cache_size = size;
218 }
219 
220 
227 const struct GNUNET_MessageHeader *
228 GST_cache_lookup_hello (const unsigned int peer_id)
229 {
230  struct CacheEntry *entry;
231 
232  LOG_DEBUG ("Looking up HELLO for peer %u\n", peer_id);
233  if (NULL == cache)
234  {
235  LOG_DEBUG ("Caching disabled\n");
236  return NULL;
237  }
238  entry = cache_lookup (peer_id);
239  if (NULL == entry)
240  return NULL;
241  if (NULL != entry->hello)
242  LOG_DEBUG ("HELLO found for peer %u\n", peer_id);
243  return entry->hello;
244 }
245 
246 
254 void
255 GST_cache_add_hello (const unsigned int peer_id,
256  const struct GNUNET_MessageHeader *hello)
257 {
258  struct CacheEntry *entry;
259 
260  if (NULL == cache)
261  return;
262  entry = cache_lookup (peer_id);
263  if (NULL == entry)
264  entry = add_entry (peer_id);
265  GNUNET_free (entry->hello);
266  entry->hello = GNUNET_copy_message (hello);
267 }
268 
269 
270 /* end of gnunet-service-testbed_hc.c */
static char * peer_id
Option –peer.
Definition: gnunet-cadet.c:42
struct GNUNET_HashCode key
The key used in the DHT.
static char * value
Value of the record to add/remove.
#define LOG_DEBUG(...)
Debug logging shorthand.
data structures shared amongst components of TESTBED service
const struct GNUNET_MessageHeader * GST_cache_lookup_hello(const unsigned int peer_id)
Looks up in the hello cache and returns the HELLO of the given peer.
static int cache_clear_iterator(void *cls, uint32_t key, void *value)
Iterator over hash map entries.
void GST_cache_clear()
Clear cache.
static unsigned int cache_size
Maximum number of elements to cache.
static struct CacheEntry * cache_lookup(unsigned int peer_id)
Looks up in the cache and returns the entry.
static void free_entry(struct CacheEntry *entry)
Free the resources occupied by a cache entry.
static struct CacheEntry * cache_tail
DLL tail for least recently used cache entries; recently used cache items are at the tail....
static struct CacheEntry * add_entry(unsigned int peer_id)
Creates a new cache entry and then puts it into the cache's hashtable.
static struct GNUNET_CONTAINER_MultiHashMap32 * cache
Hashmap to maintain cache.
void GST_cache_init(unsigned int size)
Initializes the cache.
static struct CacheEntry * cache_head
DLL head for least recently used cache entries; least recently used cache items are at the head.
void GST_cache_add_hello(const unsigned int peer_id, const struct GNUNET_MessageHeader *hello)
Caches the HELLO of the given peer.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap32_put(struct GNUNET_CONTAINER_MultiHashMap32 *map, uint32_t key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap32_remove(struct GNUNET_CONTAINER_MultiHashMap32 *map, uint32_t key, const void *value)
Remove the given key-value pair from the map.
unsigned int GNUNET_CONTAINER_multihashmap32_size(const struct GNUNET_CONTAINER_MultiHashMap32 *map)
Get the number of key-value pairs in the map.
struct GNUNET_CONTAINER_MultiHashMap32 * GNUNET_CONTAINER_multihashmap32_create(unsigned int len)
Create a 32-bit key multi hash map.
void * GNUNET_CONTAINER_multihashmap32_get(const struct GNUNET_CONTAINER_MultiHashMap32 *map, uint32_t key)
Given a key find a value in the map matching the key.
void GNUNET_CONTAINER_multihashmap32_destroy(struct GNUNET_CONTAINER_MultiHashMap32 *map)
Destroy a 32-bit key hash map.
int GNUNET_CONTAINER_multihashmap32_iterate(struct GNUNET_CONTAINER_MultiHashMap32 *map, GNUNET_CONTAINER_MultiHashMapIterator32Callback it, void *it_cls)
Iterate over all entries in the map.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
, ' bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE...
@ GNUNET_OK
@ GNUNET_YES
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_MessageHeader * GNUNET_copy_message(const struct GNUNET_MessageHeader *msg)
Create a copy of the given message.
#define GNUNET_free(ptr)
Wrapper around free.
static unsigned int size
Size of the "table".
Definition: peer.c:68
unsigned int peer_id
The id of the peer this entry corresponds to.
struct CacheEntry * prev
DLL prev ptr for least recently used cache entries.
struct GNUNET_MessageHeader * hello
The HELLO message.
struct CacheEntry * next
DLL next ptr for least recently used cache entries.
Internal representation of the hash map.
Header for all communications.