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.
17 * @file rps/gnunet-service-rps_view.c
18 * @brief wrapper around the "local view"
19 * @author Julius Bünger
22 #include "gnunet_util_lib.h"
23 #include "gnunet-service-rps_view.h"
28 * Array containing the peers
30 static struct GNUNET_PeerIdentity *array;
33 * (Maximum) length of the view
35 static uint32_t length;
38 * Multipeermap containing the peers
40 static struct GNUNET_CONTAINER_MultiPeerMap *mpm;
44 * Create an empty view.
46 * @param len the maximum length for the view
49 View_create (uint32_t len)
52 array = GNUNET_new_array (len, struct GNUNET_PeerIdentity);
53 mpm = GNUNET_CONTAINER_multipeermap_create (len, GNUNET_NO); /* might even be
58 * Change length of view
60 * If size is decreased, peers with higher indices are removed.
62 * @param len the (maximum) length for the view
65 View_change_len (uint32_t len)
70 if (GNUNET_CONTAINER_multipeermap_size (mpm) < len)
72 /* We might simply clear and free the left over space */
73 GNUNET_array_grow (array, length, len);
75 else /* We have to remove elements */
77 /* TODO find a way to preserve indices */
78 for (i = 0; i < len; i++)
80 index = GNUNET_CONTAINER_multipeermap_get (mpm, &array[i]);
81 GNUNET_assert (NULL != index);
84 GNUNET_array_grow (array, length, len);
85 GNUNET_CONTAINER_multipeermap_destroy (mpm);
86 mpm = GNUNET_CONTAINER_multipeermap_create (len, GNUNET_NO);
87 for (i = 0; i < len; i++)
89 index = GNUNET_new (uint32_t);
91 GNUNET_CONTAINER_multipeermap_put (mpm, &array[i], index,
92 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
95 GNUNET_assert (length == len);
99 * Get the view as an array
101 * @return the view in array representation
103 const struct GNUNET_PeerIdentity *
110 * Get the size of the view
112 * @return current number of actually contained peers
117 return GNUNET_CONTAINER_multipeermap_size (mpm);
121 * Insert peer into the view
123 * @param peer the peer to insert
125 * @return GNUNET_OK if peer was actually inserted
126 * GNUNET_NO if peer was not inserted
129 View_put (const struct GNUNET_PeerIdentity *peer)
133 if ((length <= View_size ()) || /* If array is 'full' */
134 (GNUNET_YES == View_contains_peer (peer)))
140 index = GNUNET_new (uint32_t);
141 *index = (uint32_t) View_size ();
142 array[*index] = *peer;
143 GNUNET_CONTAINER_multipeermap_put (mpm, peer, index,
144 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
150 * Check whether view contains a peer
152 * @param peer the peer to check for
154 * @return GNUNET_OK if view contains peer
155 * GNUNET_NO otherwise
158 View_contains_peer (const struct GNUNET_PeerIdentity *peer)
160 return GNUNET_CONTAINER_multipeermap_contains (mpm, peer);
164 * Remove peer from view
166 * @param peer the peer to remove
168 * @return GNUNET_OK if view contained peer and removed it successfully
169 * GNUNET_NO if view does not contain peer
172 View_remove_peer (const struct GNUNET_PeerIdentity *peer)
175 uint32_t *swap_index;
178 if (GNUNET_NO == View_contains_peer (peer))
182 index = GNUNET_CONTAINER_multipeermap_get (mpm, peer);
183 GNUNET_assert (NULL != index);
184 last_index = View_size () - 1;
185 if (*index < last_index)
186 { /* Fill the 'gap' in the array with the last peer */
187 array[*index] = array[last_index];
188 GNUNET_assert (GNUNET_YES == View_contains_peer (&array[last_index]));
189 swap_index = GNUNET_CONTAINER_multipeermap_get (mpm, &array[last_index]);
190 GNUNET_assert (NULL != swap_index);
191 *swap_index = *index;
194 GNUNET_CONTAINER_multipeermap_remove_all (mpm, peer);
199 * Get a peer by index
201 * @param index the index of the peer to get
203 * @return peer to the corresponding index.
204 * NULL if this index is not known
206 const struct GNUNET_PeerIdentity *
207 View_get_peer_by_index (uint32_t index)
209 if (index < GNUNET_CONTAINER_multipeermap_size (mpm))
211 return &array[index];
220 * Clear the custom peer map
222 * @param c_peer_map the custom peer map to look in
224 * @return size of the map
232 for (i = 0; 0 < View_size (); i++)
233 { /* Need to free indices stored at peers */
234 GNUNET_assert (GNUNET_YES ==
235 GNUNET_CONTAINER_multipeermap_contains (mpm, &array[i]));
236 index = GNUNET_CONTAINER_multipeermap_get (mpm, &array[i]);
237 GNUNET_assert (NULL != index);
239 GNUNET_CONTAINER_multipeermap_remove_all (mpm, &array[i]);
241 GNUNET_assert (0 == View_size ());
247 * @param c_peer_map the map to destroy
254 GNUNET_CONTAINER_multipeermap_destroy (mpm);
257 /* end of gnunet-service-rps_view.c */