GNUnet  0.11.x
Data Structures | Functions
gnunet-service-rps_view.c File Reference

wrapper around the "local view" More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet-service-rps_view.h"
#include <inttypes.h>
Include dependency graph for gnunet-service-rps_view.c:

Go to the source code of this file.

Data Structures

struct  View
 

Functions

struct ViewView_create (uint32_t len)
 Create an empty view. More...
 
void View_change_len (struct View *view, uint32_t len)
 Change length of view. More...
 
const struct GNUNET_PeerIdentityView_get_as_array (const struct View *view)
 Get the view as an array. More...
 
unsigned int View_size (const struct View *view)
 Get the size of the view. More...
 
int View_put (struct View *view, const struct GNUNET_PeerIdentity *peer)
 Insert peer into the view. More...
 
int View_contains_peer (const struct View *view, const struct GNUNET_PeerIdentity *peer)
 Check whether view contains a peer. More...
 
int View_remove_peer (struct View *view, const struct GNUNET_PeerIdentity *peer)
 Remove peer from view. More...
 
const struct GNUNET_PeerIdentityView_get_peer_by_index (const struct View *view, uint32_t index)
 Get a peer by index. More...
 
void View_clear (struct View *view)
 Clear the view. More...
 
void View_destroy (struct View *view)
 Destroy view. More...
 

Detailed Description

wrapper around the "local view"

Author
Julius B√ľnger

Definition in file gnunet-service-rps_view.c.

Function Documentation

◆ View_create()

struct View* View_create ( uint32_t  len)

Create an empty view.

Parameters
lenthe maximum length for the view
Returns
The newly created view

Definition at line 57 of file gnunet-service-rps_view.c.

References View::array, GNUNET_CONTAINER_multipeermap_create(), GNUNET_new, GNUNET_new_array, GNUNET_NO, len, View::length, and View::mpm.

Referenced by new_sub().

58 {
59  struct View *view;
60 
61  view = GNUNET_new (struct View);
62  view->length = len;
64  view->mpm =
66  * set to _YES */
67  return view;
68 }
struct GNUNET_CONTAINER_MultiPeerMap * mpm
Multipeermap containing the peers.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_CONTAINER_MultiPeerMap * GNUNET_CONTAINER_multipeermap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
The identity of the host (wraps the signing key of the peer).
struct GNUNET_PeerIdentity * array
Array containing the peers.
uint32_t length
(Maximum) length of the view
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ View_change_len()

void View_change_len ( struct View view,
uint32_t  len 
)

Change length of view.

If size is decreased, peers with higher indices are removed.

Parameters
viewThe view that is changed
lenthe (maximum) length for the view

Definition at line 80 of file gnunet-service-rps_view.c.

References View::array, GNUNET_array_grow, GNUNET_assert, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST, GNUNET_CONTAINER_multipeermap_create(), GNUNET_CONTAINER_multipeermap_destroy(), GNUNET_CONTAINER_multipeermap_get(), GNUNET_CONTAINER_multipeermap_put(), GNUNET_CONTAINER_multipeermap_size(), GNUNET_free, GNUNET_new, GNUNET_NO, len, View::length, and View::mpm.

Referenced by adapt_sizes().

82 {
83  uint32_t i;
84  uint32_t *index;
85 
87  { /* Simply shrink */
88  /* We might simply clear and free the left over space */
89  GNUNET_array_grow (view->array, view->length, len);
90  }
91  else /* We have to remove elements */
92  {
93  /* TODO find a way to preserve indices */
94  for (i = 0; i < len; i++)
95  {
96  index = GNUNET_CONTAINER_multipeermap_get (view->mpm, &view->array[i]);
97  GNUNET_assert (NULL != index);
98  GNUNET_free (index);
99  }
100  GNUNET_array_grow (view->array, view->length, len);
103  for (i = 0; i < len; i++)
104  {
105  index = GNUNET_new (uint32_t);
106  *index = i;
107  GNUNET_CONTAINER_multipeermap_put (view->mpm, &view->array[i], index,
109  }
110  }
111  GNUNET_assert (view->length == len);
112 }
struct GNUNET_CONTAINER_MultiPeerMap * mpm
Multipeermap containing the peers.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_CONTAINER_MultiPeerMap * GNUNET_CONTAINER_multipeermap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).
void GNUNET_CONTAINER_multipeermap_destroy(struct GNUNET_CONTAINER_MultiPeerMap *map)
Destroy a hash map.
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
, &#39; bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_...
int GNUNET_CONTAINER_multipeermap_put(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
void * GNUNET_CONTAINER_multipeermap_get(const struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key)
Given a key find a value in the map matching the key.
unsigned int GNUNET_CONTAINER_multipeermap_size(const struct GNUNET_CONTAINER_MultiPeerMap *map)
Get the number of key-value pairs in the map.
struct GNUNET_PeerIdentity * array
Array containing the peers.
uint32_t length
(Maximum) length of the view
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ View_get_as_array()

const struct GNUNET_PeerIdentity* View_get_as_array ( const struct View view)

Get the view as an array.

Parameters
viewThe view of which the array representation is of interest
Returns
the view in array representation

Definition at line 122 of file gnunet-service-rps_view.c.

References View::array.

Referenced by clients_notify_view_update(), do_round(), handle_peer_pull_request(), and send_view().

123 {
124  return view->array;
125 }
struct GNUNET_PeerIdentity * array
Array containing the peers.
Here is the caller graph for this function:

◆ View_size()

unsigned int View_size ( const struct View view)

Get the size of the view.

Parameters
viewThe view of which the size should be returned
Returns
current number of actually contained peers

Definition at line 135 of file gnunet-service-rps_view.c.

References GNUNET_CONTAINER_multipeermap_size(), and View::mpm.

Referenced by clients_notify_view_update(), do_round(), handle_peer_pull_request(), insert_in_view(), send_view(), View_clear(), View_put(), and View_remove_peer().

136 {
138 }
struct GNUNET_CONTAINER_MultiPeerMap * mpm
Multipeermap containing the peers.
unsigned int GNUNET_CONTAINER_multipeermap_size(const struct GNUNET_CONTAINER_MultiPeerMap *map)
Get the number of key-value pairs in the map.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ View_put()

int View_put ( struct View view,
const struct GNUNET_PeerIdentity peer 
)

Insert peer into the view.

Parameters
viewThe view to put the peer into
peerthe peer to insert
Returns
GNUNET_OK if peer was actually inserted GNUNET_NO if peer was not inserted

Definition at line 151 of file gnunet-service-rps_view.c.

References View::array, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST, GNUNET_CONTAINER_multipeermap_put(), GNUNET_new, GNUNET_NO, GNUNET_OK, GNUNET_YES, View::length, View::mpm, peer, View_contains_peer(), and View_size().

Referenced by insert_in_view().

153 {
154  uint32_t *index;
155 
156  if ((view->length <= View_size (view)) || /* If array is 'full' */
157  (GNUNET_YES == View_contains_peer (view, peer)))
158  {
159  return GNUNET_NO;
160  }
161  else
162  {
163  index = GNUNET_new (uint32_t);
164  *index = (uint32_t) View_size (view);
165  view->array[*index] = *peer;
166  GNUNET_CONTAINER_multipeermap_put (view->mpm, peer, index,
168  return GNUNET_OK;
169  }
170 }
struct GNUNET_CONTAINER_MultiPeerMap * mpm
Multipeermap containing the peers.
int View_contains_peer(const struct View *view, const struct GNUNET_PeerIdentity *peer)
Check whether view contains a peer.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
, &#39; bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_...
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
int GNUNET_CONTAINER_multipeermap_put(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
unsigned int View_size(const struct View *view)
Get the size of the view.
#define GNUNET_YES
Definition: gnunet_common.h:77
struct GNUNET_PeerIdentity * array
Array containing the peers.
uint32_t length
(Maximum) length of the view
Here is the call graph for this function:
Here is the caller graph for this function:

◆ View_contains_peer()

int View_contains_peer ( const struct View view,
const struct GNUNET_PeerIdentity peer 
)

Check whether view contains a peer.

Parameters
viewThe which is checked for a peer
peerthe peer to check for
Returns
GNUNET_OK if view contains peer GNUNET_NO otherwise

Definition at line 183 of file gnunet-service-rps_view.c.

References GNUNET_CONTAINER_multipeermap_contains(), and View::mpm.

Referenced by check_sending_channel_needed(), clean_peer(), View_put(), and View_remove_peer().

185 {
186  return GNUNET_CONTAINER_multipeermap_contains (view->mpm, peer);
187 }
struct GNUNET_CONTAINER_MultiPeerMap * mpm
Multipeermap containing the peers.
int GNUNET_CONTAINER_multipeermap_contains(const struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key)
Check if the map contains any value under the given key (including values that are NULL)...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ View_remove_peer()

int View_remove_peer ( struct View view,
const struct GNUNET_PeerIdentity peer 
)

Remove peer from view.

Parameters
viewThe view of which to remove the peer
peerthe peer to remove
Returns
GNUNET_OK if view contained peer and removed it successfully GNUNET_NO if view does not contain peer

Definition at line 200 of file gnunet-service-rps_view.c.

References View::array, GNUNET_assert, GNUNET_CONTAINER_multipeermap_get(), GNUNET_CONTAINER_multipeermap_remove_all(), GNUNET_free, GNUNET_NO, GNUNET_OK, GNUNET_YES, View::mpm, View_contains_peer(), and View_size().

Referenced by remove_peer().

202 {
203  uint32_t *index;
204  uint32_t *swap_index;
205  uint32_t last_index;
206 
207  if (GNUNET_NO == View_contains_peer (view, peer))
208  {
209  return GNUNET_NO;
210  }
211  index = GNUNET_CONTAINER_multipeermap_get (view->mpm, peer);
212  GNUNET_assert (NULL != index);
213  last_index = View_size (view) - 1;
214  if (*index < last_index)
215  { /* Fill the 'gap' in the array with the last peer */
216  view->array[*index] = view->array[last_index];
218  &view->array[last_index]));
219  swap_index = GNUNET_CONTAINER_multipeermap_get (view->mpm,
220  &view->array[last_index]);
221  GNUNET_assert (NULL != swap_index);
222  *swap_index = *index;
223  GNUNET_free (index);
224  }
226  return GNUNET_OK;
227 }
struct GNUNET_CONTAINER_MultiPeerMap * mpm
Multipeermap containing the peers.
int GNUNET_CONTAINER_multipeermap_remove_all(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key)
Remove all entries for the given key from the map.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
int View_contains_peer(const struct View *view, const struct GNUNET_PeerIdentity *peer)
Check whether view contains a peer.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
void * GNUNET_CONTAINER_multipeermap_get(const struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key)
Given a key find a value in the map matching the key.
unsigned int View_size(const struct View *view)
Get the size of the view.
#define GNUNET_YES
Definition: gnunet_common.h:77
struct GNUNET_PeerIdentity * array
Array containing the peers.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ View_get_peer_by_index()

const struct GNUNET_PeerIdentity* View_get_peer_by_index ( const struct View view,
uint32_t  index 
)

Get a peer by index.

Parameters
viewthe view of which to get the peer
indexthe index of the peer to get
Returns
peer to the corresponding index. NULL if this index is not known

Definition at line 240 of file gnunet-service-rps_view.c.

References View::array, GNUNET_CONTAINER_multipeermap_size(), and View::mpm.

242 {
243  if (index < GNUNET_CONTAINER_multipeermap_size (view->mpm))
244  {
245  return &view->array[index];
246  }
247  else
248  {
249  return NULL;
250  }
251 }
struct GNUNET_CONTAINER_MultiPeerMap * mpm
Multipeermap containing the peers.
unsigned int GNUNET_CONTAINER_multipeermap_size(const struct GNUNET_CONTAINER_MultiPeerMap *map)
Get the number of key-value pairs in the map.
struct GNUNET_PeerIdentity * array
Array containing the peers.
Here is the call graph for this function:

◆ View_clear()

void View_clear ( struct View view)

Clear the view.

Parameters
viewThe view to clear

Definition at line 260 of file gnunet-service-rps_view.c.

References View::array, GNUNET_assert, GNUNET_CONTAINER_multipeermap_contains(), GNUNET_CONTAINER_multipeermap_get(), GNUNET_CONTAINER_multipeermap_remove_all(), GNUNET_free, GNUNET_YES, View::mpm, and View_size().

Referenced by do_round(), and View_destroy().

261 {
262  for (uint32_t i = 0; 0 < View_size (view); i++)
263  { /* Need to free indices stored at peers */
264  uint32_t *index;
265 
268  &view->array[i]));
269  index = GNUNET_CONTAINER_multipeermap_get (view->mpm, &view->array[i]);
270  GNUNET_assert (NULL != index);
271  GNUNET_free (index);
273  }
274  GNUNET_assert (0 == View_size (view));
275 }
struct GNUNET_CONTAINER_MultiPeerMap * mpm
Multipeermap containing the peers.
int GNUNET_CONTAINER_multipeermap_remove_all(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key)
Remove all entries for the given key from the map.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
void * GNUNET_CONTAINER_multipeermap_get(const struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key)
Given a key find a value in the map matching the key.
unsigned int View_size(const struct View *view)
Get the size of the view.
#define GNUNET_YES
Definition: gnunet_common.h:77
struct GNUNET_PeerIdentity * array
Array containing the peers.
int GNUNET_CONTAINER_multipeermap_contains(const struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key)
Check if the map contains any value under the given key (including values that are NULL)...
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ View_destroy()

void View_destroy ( struct View view)

Destroy view.

Parameters
viewthe view to destroy

Definition at line 284 of file gnunet-service-rps_view.c.

References View::array, GNUNET_CONTAINER_multipeermap_destroy(), GNUNET_free, View::mpm, and View_clear().

Referenced by destroy_sub().

285 {
286  View_clear (view);
287  GNUNET_free (view->array);
288  view->array = NULL;
290  GNUNET_free (view);
291 }
struct GNUNET_CONTAINER_MultiPeerMap * mpm
Multipeermap containing the peers.
void GNUNET_CONTAINER_multipeermap_destroy(struct GNUNET_CONTAINER_MultiPeerMap *map)
Destroy a hash map.
void View_clear(struct View *view)
Clear the view.
struct GNUNET_PeerIdentity * array
Array containing the peers.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function: