2 This file is part of GNUnet.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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"
33 * Array containing the peers
35 static struct GNUNET_PeerIdentity *array;
38 * (Maximum) length of the view
40 static uint32_t length;
43 * Multipeermap containing the peers
45 static struct GNUNET_CONTAINER_MultiPeerMap *mpm;
49 * Create an empty view.
51 * @param len the maximum length for the view
54 View_create (uint32_t len)
57 array = GNUNET_new_array (len, struct GNUNET_PeerIdentity);
58 mpm = GNUNET_CONTAINER_multipeermap_create (len, GNUNET_NO); /* might even be
63 * Change length of view
65 * @param len the (maximum) length for the view
68 View_change_len (uint32_t len)
73 if (GNUNET_CONTAINER_multipeermap_size (mpm) < len)
75 /* We might simply clear and free the left over space */
76 GNUNET_array_grow (array, length, len);
78 else /* We have to remove elements */
80 /* TODO find a way to preserve indices */
81 for (i = 0; i < len; i++)
83 index = GNUNET_CONTAINER_multipeermap_get (mpm, &array[i]);
84 GNUNET_assert (NULL != index);
87 GNUNET_array_grow (array, length, len);
88 GNUNET_CONTAINER_multipeermap_destroy (mpm);
89 mpm = GNUNET_CONTAINER_multipeermap_create (len, GNUNET_NO);
90 for (i = 0; i < len; i++)
92 index = GNUNET_new (uint32_t);
94 GNUNET_CONTAINER_multipeermap_put (mpm, &array[i], index,
95 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
98 GNUNET_assert (length == len);
102 * Get the view as an array
104 * @return the view in array representation
106 const struct GNUNET_PeerIdentity *
113 * Get the size of the view
115 * @return current number of actually contained peers
120 return GNUNET_CONTAINER_multipeermap_size (mpm);
124 * Insert peer into the view
126 * @param peer the peer to insert
128 * @return GNUNET_OK if peer was actually inserted
129 * GNUNET_NO if peer was not inserted
132 View_put (const struct GNUNET_PeerIdentity *peer)
136 if ((length <= View_size ()) || /* If array is 'full' */
137 (GNUNET_YES == View_contains_peer (peer)))
143 index = GNUNET_new (uint32_t);
144 *index = (uint32_t) View_size ();
145 array[*index] = *peer;
146 GNUNET_CONTAINER_multipeermap_put (mpm, peer, index,
147 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
153 * Check whether view contains a peer
155 * @param peer the peer to check for
157 * @return GNUNET_OK if view contains peer
158 * GNUNET_NO otherwise
161 View_contains_peer (const struct GNUNET_PeerIdentity *peer)
163 return GNUNET_CONTAINER_multipeermap_contains (mpm, peer);
167 * Remove peer from view
169 * @param peer the peer to remove
171 * @return GNUNET_OK if view contained peer and removed it successfully
172 * GNUNET_NO if view does not contain peer
175 View_remove_peer (const struct GNUNET_PeerIdentity *peer)
179 if (GNUNET_YES == View_contains_peer (peer))
181 index = GNUNET_CONTAINER_multipeermap_get (mpm, peer);
182 GNUNET_assert (NULL != index);
183 if (*index == GNUNET_CONTAINER_multipeermap_size (mpm) - 1)
184 { /* Last peer in array - simply remove */
187 { /* Fill the 'gap' in the array with the last peer */
188 array[*index] = array[View_size ()];
190 GNUNET_CONTAINER_multipeermap_remove_all (mpm, peer);
200 * Get a peer by index
202 * @param index the index of the peer to get
204 * @return peer to the corresponding index.
205 * NULL if this index is not known
207 const struct GNUNET_PeerIdentity *
208 View_get_peer_by_index (uint32_t index)
210 if (index < GNUNET_CONTAINER_multipeermap_size (mpm))
212 return &array[index];
221 * Clear the custom peer map
223 * @param c_peer_map the custom peer map to look in
225 * @return size of the map
233 for (i = 0; i < GNUNET_CONTAINER_multipeermap_size (mpm); i++)
234 { /* Need to free indices stored at peers */
235 index = GNUNET_CONTAINER_multipeermap_get (mpm, &array[i]);
236 GNUNET_assert (NULL != index);
238 GNUNET_CONTAINER_multipeermap_remove_all (mpm, &array[i]);
240 GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (mpm));
246 * @param c_peer_map the map to destroy
253 GNUNET_CONTAINER_multipeermap_destroy (mpm);
256 /* end of gnunet-service-rps_view.c */