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