gnunet-service-rps_view.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C)
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file rps/gnunet-service-rps_view.c
  18. * @brief wrapper around the "local view"
  19. * @author Julius Bünger
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet-service-rps_view.h"
  24. #include <inttypes.h>
  25. struct View
  26. {
  27. /**
  28. * Array containing the peers
  29. */
  30. struct GNUNET_PeerIdentity *array;
  31. /**
  32. * (Maximum) length of the view
  33. */
  34. uint32_t length;
  35. /**
  36. * Multipeermap containing the peers
  37. */
  38. struct GNUNET_CONTAINER_MultiPeerMap *mpm;
  39. };
  40. /**
  41. * Create an empty view.
  42. *
  43. * @param len the maximum length for the view
  44. * @return The newly created view
  45. */
  46. struct View *
  47. View_create (uint32_t len)
  48. {
  49. struct View *view;
  50. view = GNUNET_new (struct View);
  51. view->length = len;
  52. view->array = GNUNET_new_array (len, struct GNUNET_PeerIdentity);
  53. view->mpm =
  54. GNUNET_CONTAINER_multipeermap_create (len, GNUNET_NO); /* might even be
  55. * set to _YES */
  56. return view;
  57. }
  58. /**
  59. * Change length of view
  60. *
  61. * If size is decreased, peers with higher indices are removed.
  62. *
  63. * @param view The view that is changed
  64. * @param len the (maximum) length for the view
  65. */
  66. void
  67. View_change_len (struct View *view,
  68. uint32_t len)
  69. {
  70. uint32_t i;
  71. uint32_t *index;
  72. if (GNUNET_CONTAINER_multipeermap_size (view->mpm) < len)
  73. { /* Simply shrink */
  74. /* We might simply clear and free the left over space */
  75. GNUNET_array_grow (view->array, view->length, len);
  76. }
  77. else /* We have to remove elements */
  78. {
  79. /* TODO find a way to preserve indices */
  80. for (i = 0; i < len; i++)
  81. {
  82. index = GNUNET_CONTAINER_multipeermap_get (view->mpm, &view->array[i]);
  83. GNUNET_assert (NULL != index);
  84. GNUNET_free (index);
  85. }
  86. GNUNET_array_grow (view->array, view->length, len);
  87. GNUNET_CONTAINER_multipeermap_destroy (view->mpm);
  88. view->mpm = GNUNET_CONTAINER_multipeermap_create (len, GNUNET_NO);
  89. for (i = 0; i < len; i++)
  90. {
  91. index = GNUNET_new (uint32_t);
  92. *index = i;
  93. GNUNET_CONTAINER_multipeermap_put (view->mpm, &view->array[i], index,
  94. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
  95. }
  96. }
  97. GNUNET_assert (view->length == len);
  98. }
  99. /**
  100. * Get the view as an array
  101. *
  102. * @param view The view of which the array representation is of interest
  103. * @return the view in array representation
  104. */
  105. const struct GNUNET_PeerIdentity *
  106. View_get_as_array (const struct View *view)
  107. {
  108. return view->array;
  109. }
  110. /**
  111. * Get the size of the view
  112. *
  113. * @param view The view of which the size should be returned
  114. * @return current number of actually contained peers
  115. */
  116. unsigned int
  117. View_size (const struct View *view)
  118. {
  119. return GNUNET_CONTAINER_multipeermap_size (view->mpm);
  120. }
  121. /**
  122. * Insert peer into the view
  123. *
  124. * @param view The view to put the peer into
  125. * @param peer the peer to insert
  126. *
  127. * @return GNUNET_OK if peer was actually inserted
  128. * GNUNET_NO if peer was not inserted
  129. */
  130. int
  131. View_put (struct View *view,
  132. const struct GNUNET_PeerIdentity *peer)
  133. {
  134. uint32_t *index;
  135. if ((view->length <= View_size (view)) || /* If array is 'full' */
  136. (GNUNET_YES == View_contains_peer (view, peer)))
  137. {
  138. return GNUNET_NO;
  139. }
  140. else
  141. {
  142. index = GNUNET_new (uint32_t);
  143. *index = (uint32_t) View_size (view);
  144. view->array[*index] = *peer;
  145. GNUNET_CONTAINER_multipeermap_put (view->mpm, peer, index,
  146. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
  147. return GNUNET_OK;
  148. }
  149. }
  150. /**
  151. * Check whether view contains a peer
  152. *
  153. * @param view The which is checked for a peer
  154. * @param peer the peer to check for
  155. *
  156. * @return GNUNET_OK if view contains peer
  157. * GNUNET_NO otherwise
  158. */
  159. int
  160. View_contains_peer (const struct View *view,
  161. const struct GNUNET_PeerIdentity *peer)
  162. {
  163. return GNUNET_CONTAINER_multipeermap_contains (view->mpm, peer);
  164. }
  165. /**
  166. * Remove peer from view
  167. *
  168. * @param view The view of which to remove the peer
  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 (struct View *view,
  176. const struct GNUNET_PeerIdentity *peer)
  177. {
  178. uint32_t *index;
  179. uint32_t *swap_index;
  180. uint32_t last_index;
  181. if (GNUNET_NO == View_contains_peer (view, peer))
  182. {
  183. return GNUNET_NO;
  184. }
  185. index = GNUNET_CONTAINER_multipeermap_get (view->mpm, peer);
  186. GNUNET_assert (NULL != index);
  187. last_index = View_size (view) - 1;
  188. if (*index < last_index)
  189. { /* Fill the 'gap' in the array with the last peer */
  190. view->array[*index] = view->array[last_index];
  191. GNUNET_assert (GNUNET_YES == View_contains_peer (view,
  192. &view->array[last_index]));
  193. swap_index = GNUNET_CONTAINER_multipeermap_get (view->mpm,
  194. &view->array[last_index]);
  195. GNUNET_assert (NULL != swap_index);
  196. *swap_index = *index;
  197. GNUNET_free (index);
  198. }
  199. GNUNET_CONTAINER_multipeermap_remove_all (view->mpm, peer);
  200. return GNUNET_OK;
  201. }
  202. /**
  203. * Get a peer by index
  204. *
  205. * @param view the view of which to get the peer
  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 (const struct View *view,
  213. uint32_t index)
  214. {
  215. if (index < GNUNET_CONTAINER_multipeermap_size (view->mpm))
  216. {
  217. return &view->array[index];
  218. }
  219. else
  220. {
  221. return NULL;
  222. }
  223. }
  224. /**
  225. * Clear the view
  226. *
  227. * @param view The view to clear
  228. */
  229. void
  230. View_clear (struct View *view)
  231. {
  232. for (uint32_t i = 0; 0 < View_size (view); i++)
  233. { /* Need to free indices stored at peers */
  234. uint32_t *index;
  235. GNUNET_assert (GNUNET_YES ==
  236. GNUNET_CONTAINER_multipeermap_contains (view->mpm, &view->array[i]));
  237. index = GNUNET_CONTAINER_multipeermap_get (view->mpm, &view->array[i]);
  238. GNUNET_assert (NULL != index);
  239. GNUNET_free (index);
  240. GNUNET_CONTAINER_multipeermap_remove_all (view->mpm, &view->array[i]);
  241. }
  242. GNUNET_assert (0 == View_size (view));
  243. }
  244. /**
  245. * Destroy view.
  246. *
  247. * @param view the view to destroy
  248. */
  249. void
  250. View_destroy (struct View *view)
  251. {
  252. View_clear (view);
  253. GNUNET_free (view->array);
  254. view->array = NULL;
  255. GNUNET_CONTAINER_multipeermap_destroy (view->mpm);
  256. GNUNET_free (view);
  257. }
  258. /* end of gnunet-service-rps_view.c */