Merge remote-tracking branch 'origin/master' into identity_abe
[oweals/gnunet.git] / src / rps / gnunet-service-rps_view.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C)
4
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.
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      General Public License for more details.
14
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.
19 */
20
21 /**
22  * @file rps/gnunet-service-rps_view.c
23  * @brief wrapper around the "local view"
24  * @author Julius Bünger
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet-service-rps_view.h"
29 #include <inttypes.h>
30
31
32 /**
33  * Array containing the peers
34  */
35 static struct GNUNET_PeerIdentity *array;
36
37 /**
38  * (Maximum) length of the view
39  */
40 static uint32_t length;
41
42 /**
43  * Multipeermap containing the peers
44  */
45 static struct GNUNET_CONTAINER_MultiPeerMap *mpm;
46
47
48 /**
49  * Create an empty view.
50  *
51  * @param len the maximum length for the view
52  */
53 void
54 View_create (uint32_t len)
55 {
56   length = len;
57   array = GNUNET_new_array (len, struct GNUNET_PeerIdentity);
58   mpm = GNUNET_CONTAINER_multipeermap_create (len, GNUNET_NO); /* might even be
59                                                                 * set to _YES */
60 }
61
62 /**
63  * Change length of view
64  *
65  * If size is decreased, peers with higher indices are removed.
66  *
67  * @param len the (maximum) length for the view
68  */
69 void
70 View_change_len (uint32_t len)
71 {
72   uint32_t i;
73   uint32_t *index;
74
75   if (GNUNET_CONTAINER_multipeermap_size (mpm) < len)
76   { /* Simply shrink */
77     /* We might simply clear and free the left over space */
78     GNUNET_array_grow (array, length, len);
79   }
80   else /* We have to remove elements */
81   {
82     /* TODO find a way to preserve indices */
83     for (i = 0; i < len; i++)
84     {
85       index = GNUNET_CONTAINER_multipeermap_get (mpm, &array[i]);
86       GNUNET_assert (NULL != index);
87       GNUNET_free (index);
88     }
89     GNUNET_array_grow (array, length, len);
90     GNUNET_CONTAINER_multipeermap_destroy (mpm);
91     mpm = GNUNET_CONTAINER_multipeermap_create (len, GNUNET_NO);
92     for (i = 0; i < len; i++)
93     {
94       index = GNUNET_new (uint32_t);
95       *index = i;
96       GNUNET_CONTAINER_multipeermap_put (mpm, &array[i], index,
97           GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
98     }
99   }
100   GNUNET_assert (length == len);
101 }
102
103 /**
104  * Get the view as an array
105  *
106  * @return the view in array representation
107  */
108 const struct GNUNET_PeerIdentity *
109 View_get_as_array ()
110 {
111   return array;
112 }
113
114 /**
115  * Get the size of the view
116  *
117  * @return current number of actually contained peers
118  */
119 unsigned int
120 View_size ()
121 {
122   return GNUNET_CONTAINER_multipeermap_size (mpm);
123 }
124
125 /**
126  * Insert peer into the view
127  *
128  * @param peer the peer to insert
129  *
130  * @return GNUNET_OK if peer was actually inserted
131  *         GNUNET_NO if peer was not inserted
132  */
133 int
134 View_put (const struct GNUNET_PeerIdentity *peer)
135 {
136   uint32_t *index;
137
138   if ((length <= View_size ()) || /* If array is 'full' */
139       (GNUNET_YES == View_contains_peer (peer)))
140   {
141     return GNUNET_NO;
142   }
143   else
144   {
145     index = GNUNET_new (uint32_t);
146     *index = (uint32_t) View_size ();
147     array[*index] = *peer;
148     GNUNET_CONTAINER_multipeermap_put (mpm, peer, index,
149         GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
150     return GNUNET_OK;
151   }
152 }
153
154 /**
155  * Check whether view contains a peer
156  *
157  * @param peer the peer to check for
158  *
159  * @return GNUNET_OK if view contains peer
160  *         GNUNET_NO otherwise
161  */
162 int
163 View_contains_peer (const struct GNUNET_PeerIdentity *peer)
164 {
165   return GNUNET_CONTAINER_multipeermap_contains (mpm, peer);
166 }
167
168 /**
169  * Remove peer from view
170  *
171  * @param peer the peer to remove
172  *
173  * @return GNUNET_OK if view contained peer and removed it successfully
174  *         GNUNET_NO if view does not contain peer
175  */
176 int
177 View_remove_peer (const struct GNUNET_PeerIdentity *peer)
178 {
179   uint32_t *index;
180   uint32_t *swap_index;
181   uint32_t last_index;
182
183   if (GNUNET_NO == View_contains_peer (peer))
184   {
185     return GNUNET_NO;
186   }
187   index = GNUNET_CONTAINER_multipeermap_get (mpm, peer);
188   GNUNET_assert (NULL != index);
189   last_index = View_size () - 1;
190   if (*index < last_index)
191   { /* Fill the 'gap' in the array with the last peer */
192     array[*index] = array[last_index];
193     GNUNET_assert (GNUNET_YES == View_contains_peer (&array[last_index]));
194     swap_index = GNUNET_CONTAINER_multipeermap_get (mpm, &array[last_index]);
195     GNUNET_assert (NULL != swap_index);
196     *swap_index = *index;
197     GNUNET_free (index);
198   }
199   GNUNET_CONTAINER_multipeermap_remove_all (mpm, peer);
200   return GNUNET_OK;
201 }
202
203 /**
204  * Get a peer by index
205  *
206  * @param index the index of the peer to get
207  *
208  * @return peer to the corresponding index.
209  *         NULL if this index is not known
210  */
211 const struct GNUNET_PeerIdentity *
212 View_get_peer_by_index (uint32_t index)
213 {
214   if (index < GNUNET_CONTAINER_multipeermap_size (mpm))
215   {
216     return &array[index];
217   }
218   else
219   {
220     return NULL;
221   }
222 }
223
224 /**
225  * Clear the custom peer map
226  *
227  * @param c_peer_map the custom peer map to look in
228  *
229  * @return size of the map
230  */
231 void
232 View_clear ()
233 {
234   uint32_t i;
235   uint32_t *index;
236
237   for (i = 0; 0 < View_size (); i++)
238   { /* Need to free indices stored at peers */
239     GNUNET_assert (GNUNET_YES ==
240         GNUNET_CONTAINER_multipeermap_contains (mpm, &array[i]));
241     index = GNUNET_CONTAINER_multipeermap_get (mpm, &array[i]);
242     GNUNET_assert (NULL != index);
243     GNUNET_free (index);
244     GNUNET_CONTAINER_multipeermap_remove_all (mpm, &array[i]);
245   }
246   GNUNET_assert (0 == View_size ());
247 }
248
249 /**
250  * Destroy peermap.
251  *
252  * @param c_peer_map the map to destroy
253  */
254 void
255 View_destroy ()
256 {
257   View_clear ();
258   GNUNET_free (array);
259   GNUNET_CONTAINER_multipeermap_destroy (mpm);
260 }
261
262 /* end of gnunet-service-rps_view.c */