diff options
Diffstat (limited to 'src/testbed/gnunet-service-testbed_cache.c')
-rw-r--r-- | src/testbed/gnunet-service-testbed_cache.c | 270 |
1 files changed, 0 insertions, 270 deletions
diff --git a/src/testbed/gnunet-service-testbed_cache.c b/src/testbed/gnunet-service-testbed_cache.c deleted file mode 100644 index 5d5c2e297..000000000 --- a/src/testbed/gnunet-service-testbed_cache.c +++ /dev/null | |||
@@ -1,270 +0,0 @@ | |||
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 | |||
21 | /** | ||
22 | * @file testbed/gnunet-service-testbed_cache.c | ||
23 | * @brief testbed cache implementation | ||
24 | * @author Sree Harsha Totakura | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet-service-testbed.h" | ||
28 | |||
29 | /** | ||
30 | * Redefine LOG with a changed log component string | ||
31 | */ | ||
32 | #ifdef LOG | ||
33 | #undef LOG | ||
34 | #endif | ||
35 | #define LOG(kind, ...) \ | ||
36 | GNUNET_log_from (kind, "testbed-cache", __VA_ARGS__) | ||
37 | |||
38 | |||
39 | /** | ||
40 | * Cache entry | ||
41 | */ | ||
42 | struct CacheEntry | ||
43 | { | ||
44 | /** | ||
45 | * DLL next ptr for least recently used cache entries | ||
46 | */ | ||
47 | struct CacheEntry *next; | ||
48 | |||
49 | /** | ||
50 | * DLL prev ptr for least recently used cache entries | ||
51 | */ | ||
52 | struct CacheEntry *prev; | ||
53 | |||
54 | /** | ||
55 | * The HELLO message | ||
56 | */ | ||
57 | struct GNUNET_MessageHeader *hello; | ||
58 | |||
59 | /** | ||
60 | * The id of the peer this entry corresponds to | ||
61 | */ | ||
62 | unsigned int peer_id; | ||
63 | }; | ||
64 | |||
65 | |||
66 | /** | ||
67 | * Hashmap to maintain cache | ||
68 | */ | ||
69 | static struct GNUNET_CONTAINER_MultiHashMap32 *cache; | ||
70 | |||
71 | /** | ||
72 | * DLL head for least recently used cache entries; least recently used | ||
73 | * cache items are at the head. The cache enties are added to this queue when | ||
74 | * their demand becomes zero. They are removed from the queue when they are | ||
75 | * needed by any operation. | ||
76 | */ | ||
77 | static struct CacheEntry *cache_head; | ||
78 | |||
79 | /** | ||
80 | * DLL tail for least recently used cache entries; recently used cache | ||
81 | * items are at the tail.The cache enties are added to this queue when | ||
82 | * their demand becomes zero. They are removed from the queue when they are | ||
83 | * needed by any operation. | ||
84 | */ | ||
85 | static struct CacheEntry *cache_tail; | ||
86 | |||
87 | /** | ||
88 | * Maximum number of elements to cache | ||
89 | */ | ||
90 | static unsigned int cache_size; | ||
91 | |||
92 | |||
93 | /** | ||
94 | * Looks up in the cache and returns the entry | ||
95 | * | ||
96 | * @param peer_id the peer identity of the peer whose corresponding entry has to | ||
97 | * be looked up | ||
98 | * @return the HELLO message; NULL if not found | ||
99 | */ | ||
100 | static struct CacheEntry * | ||
101 | cache_lookup (unsigned int peer_id) | ||
102 | { | ||
103 | struct CacheEntry *entry; | ||
104 | |||
105 | GNUNET_assert (NULL != cache); | ||
106 | entry = GNUNET_CONTAINER_multihashmap32_get (cache, peer_id); | ||
107 | if (NULL == entry) | ||
108 | return NULL; | ||
109 | GNUNET_CONTAINER_DLL_remove (cache_head, cache_tail, entry); | ||
110 | GNUNET_CONTAINER_DLL_insert_tail (cache_head, cache_tail, entry); | ||
111 | return entry; | ||
112 | } | ||
113 | |||
114 | |||
115 | /** | ||
116 | * Free the resources occupied by a cache entry | ||
117 | * | ||
118 | * @param entry the cache entry to free | ||
119 | */ | ||
120 | static void | ||
121 | free_entry (struct CacheEntry *entry) | ||
122 | { | ||
123 | GNUNET_CONTAINER_DLL_remove (cache_head, cache_tail, entry); | ||
124 | GNUNET_free (entry->hello); | ||
125 | GNUNET_free (entry); | ||
126 | } | ||
127 | |||
128 | |||
129 | /** | ||
130 | * Creates a new cache entry and then puts it into the cache's hashtable. | ||
131 | * | ||
132 | * @param peer_id the index of the peer to tag the newly created entry | ||
133 | * @return the newly created entry | ||
134 | */ | ||
135 | static struct CacheEntry * | ||
136 | add_entry (unsigned int peer_id) | ||
137 | { | ||
138 | struct CacheEntry *entry; | ||
139 | |||
140 | GNUNET_assert (NULL != cache); | ||
141 | if (cache_size == GNUNET_CONTAINER_multihashmap32_size (cache)) | ||
142 | { | ||
143 | /* remove the LRU head */ | ||
144 | entry = cache_head; | ||
145 | GNUNET_assert (GNUNET_OK == | ||
146 | GNUNET_CONTAINER_multihashmap32_remove (cache, (uint32_t) | ||
147 | entry->peer_id, | ||
148 | entry)); | ||
149 | free_entry (entry); | ||
150 | } | ||
151 | entry = GNUNET_new (struct CacheEntry); | ||
152 | entry->peer_id = peer_id; | ||
153 | GNUNET_assert (GNUNET_OK == | ||
154 | GNUNET_CONTAINER_multihashmap32_put (cache, | ||
155 | (uint32_t) peer_id, | ||
156 | entry, | ||
157 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)); | ||
158 | GNUNET_CONTAINER_DLL_insert_tail (cache_head, cache_tail, entry); | ||
159 | return entry; | ||
160 | } | ||
161 | |||
162 | |||
163 | /** | ||
164 | * Iterator over hash map entries. | ||
165 | * | ||
166 | * @param cls closure | ||
167 | * @param key current key | ||
168 | * @param value value in the hash map | ||
169 | * @return GNUNET_YES if we should continue to | ||
170 | * iterate, | ||
171 | * GNUNET_NO if not. | ||
172 | */ | ||
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); | ||
179 | GNUNET_assert (GNUNET_YES == | ||
180 | GNUNET_CONTAINER_multihashmap32_remove (cache, key, value)); | ||
181 | free_entry (entry); | ||
182 | return GNUNET_YES; | ||
183 | } | ||
184 | |||
185 | |||
186 | /** | ||
187 | * Clear cache | ||
188 | */ | ||
189 | void | ||
190 | GST_cache_clear () | ||
191 | { | ||
192 | if (NULL != cache) | ||
193 | { | ||
194 | GNUNET_CONTAINER_multihashmap32_iterate (cache, &cache_clear_iterator, | ||
195 | NULL); | ||
196 | GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (cache)); | ||
197 | GNUNET_CONTAINER_multihashmap32_destroy (cache); | ||
198 | cache = NULL; | ||
199 | } | ||
200 | cache_size = 0; | ||
201 | cache_head = NULL; | ||
202 | cache_tail = NULL; | ||
203 | } | ||
204 | |||
205 | |||
206 | /** | ||
207 | * Initializes the cache | ||
208 | * | ||
209 | * @param size the size of the cache | ||
210 | */ | ||
211 | void | ||
212 | GST_cache_init (unsigned int size) | ||
213 | { | ||
214 | if (0 == size) | ||
215 | return; | ||
216 | cache_size = size; | ||
217 | cache = GNUNET_CONTAINER_multihashmap32_create (cache_size); | ||
218 | } | ||
219 | |||
220 | |||
221 | /** | ||
222 | * Looks up in the hello cache and returns the HELLO of the given peer | ||
223 | * | ||
224 | * @param peer_id the index of the peer whose HELLO has to be looked up | ||
225 | * @return the HELLO message; NULL if not found | ||
226 | */ | ||
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 | |||
247 | /** | ||
248 | * Caches the HELLO of the given peer. Updates the HELLO if it was already | ||
249 | * cached before | ||
250 | * | ||
251 | * @param peer_id the peer identity of the peer whose HELLO has to be cached | ||
252 | * @param hello the HELLO message | ||
253 | */ | ||
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 */ | ||