2 This file is part of GNUnet.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file rps/gnunet-service-rps_view.c
23 * @brief wrapper around the "local view"
24 * @author Julius Bünger
27 #include "gnunet_util_lib.h"
28 #include "gnunet-service-rps_view.h"
34 * Array containing the peers
36 struct GNUNET_PeerIdentity *array;
39 * (Maximum) length of the view
44 * Multipeermap containing the peers
46 struct GNUNET_CONTAINER_MultiPeerMap *mpm;
51 * Create an empty view.
53 * @param len the maximum length for the view
54 * @return The newly created view
57 View_create (uint32_t len)
61 view = GNUNET_new (struct View);
63 view->array = GNUNET_new_array (len, struct GNUNET_PeerIdentity);
65 GNUNET_CONTAINER_multipeermap_create (len, GNUNET_NO); /* might even be
72 * Change length of view
74 * If size is decreased, peers with higher indices are removed.
76 * @param view The view that is changed
77 * @param len the (maximum) length for the view
80 View_change_len (struct View *view,
86 if (GNUNET_CONTAINER_multipeermap_size (view->mpm) < len)
88 /* We might simply clear and free the left over space */
89 GNUNET_array_grow (view->array, view->length, len);
91 else /* We have to remove elements */
93 /* TODO find a way to preserve indices */
94 for (i = 0; i < len; i++)
96 index = GNUNET_CONTAINER_multipeermap_get (view->mpm, &view->array[i]);
97 GNUNET_assert (NULL != index);
100 GNUNET_array_grow (view->array, view->length, len);
101 GNUNET_CONTAINER_multipeermap_destroy (view->mpm);
102 view->mpm = GNUNET_CONTAINER_multipeermap_create (len, GNUNET_NO);
103 for (i = 0; i < len; i++)
105 index = GNUNET_new (uint32_t);
107 GNUNET_CONTAINER_multipeermap_put (view->mpm, &view->array[i], index,
108 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
111 GNUNET_assert (view->length == len);
116 * Get the view as an array
118 * @param view The view of which the array representation is of interest
119 * @return the view in array representation
121 const struct GNUNET_PeerIdentity *
122 View_get_as_array (const struct View *view)
129 * Get the size of the view
131 * @param view The view of which the size should be returned
132 * @return current number of actually contained peers
135 View_size (const struct View *view)
137 return GNUNET_CONTAINER_multipeermap_size (view->mpm);
142 * Insert peer into the view
144 * @param view The view to put the peer into
145 * @param peer the peer to insert
147 * @return GNUNET_OK if peer was actually inserted
148 * GNUNET_NO if peer was not inserted
151 View_put (struct View *view,
152 const struct GNUNET_PeerIdentity *peer)
156 if ((view->length <= View_size (view)) || /* If array is 'full' */
157 (GNUNET_YES == View_contains_peer (view, peer)))
163 index = GNUNET_new (uint32_t);
164 *index = (uint32_t) View_size (view);
165 view->array[*index] = *peer;
166 GNUNET_CONTAINER_multipeermap_put (view->mpm, peer, index,
167 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
174 * Check whether view contains a peer
176 * @param view The which is checked for a peer
177 * @param peer the peer to check for
179 * @return GNUNET_OK if view contains peer
180 * GNUNET_NO otherwise
183 View_contains_peer (const struct View *view,
184 const struct GNUNET_PeerIdentity *peer)
186 return GNUNET_CONTAINER_multipeermap_contains (view->mpm, peer);
191 * Remove peer from view
193 * @param view The view of which to remove the peer
194 * @param peer the peer to remove
196 * @return GNUNET_OK if view contained peer and removed it successfully
197 * GNUNET_NO if view does not contain peer
200 View_remove_peer (struct View *view,
201 const struct GNUNET_PeerIdentity *peer)
204 uint32_t *swap_index;
207 if (GNUNET_NO == View_contains_peer (view, peer))
211 index = GNUNET_CONTAINER_multipeermap_get (view->mpm, peer);
212 GNUNET_assert (NULL != index);
213 last_index = View_size (view) - 1;
214 if (*index < last_index)
215 { /* Fill the 'gap' in the array with the last peer */
216 view->array[*index] = view->array[last_index];
217 GNUNET_assert (GNUNET_YES == View_contains_peer (view,
218 &view->array[last_index]));
219 swap_index = GNUNET_CONTAINER_multipeermap_get (view->mpm,
220 &view->array[last_index]);
221 GNUNET_assert (NULL != swap_index);
222 *swap_index = *index;
225 GNUNET_CONTAINER_multipeermap_remove_all (view->mpm, peer);
231 * Get a peer by index
233 * @param view the view of which to get the peer
234 * @param index the index of the peer to get
236 * @return peer to the corresponding index.
237 * NULL if this index is not known
239 const struct GNUNET_PeerIdentity *
240 View_get_peer_by_index (const struct View *view,
243 if (index < GNUNET_CONTAINER_multipeermap_size (view->mpm))
245 return &view->array[index];
257 * @param view The view to clear
260 View_clear (struct View *view)
262 for (uint32_t i = 0; 0 < View_size (view); i++)
263 { /* Need to free indices stored at peers */
266 GNUNET_assert (GNUNET_YES ==
267 GNUNET_CONTAINER_multipeermap_contains (view->mpm, &view->array[i]));
268 index = GNUNET_CONTAINER_multipeermap_get (view->mpm, &view->array[i]);
269 GNUNET_assert (NULL != index);
271 GNUNET_CONTAINER_multipeermap_remove_all (view->mpm, &view->array[i]);
273 GNUNET_assert (0 == View_size (view));
280 * @param view the view to destroy
283 View_destroy (struct View *view)
286 GNUNET_free (view->array);
288 GNUNET_CONTAINER_multipeermap_destroy (view->mpm);
292 /* end of gnunet-service-rps_view.c */