-improve UDP logging
[oweals/gnunet.git] / src / rps / gnunet-service-rps_sampler.h
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file rps/gnunet-service-rps_sampler.h
23  * @brief sampler implementation
24  * @author Julius Bünger
25  */
26
27 #ifndef RPS_SAMPLER_H
28 #define RPS_SAMPLER_H
29 #include <inttypes.h>
30
31
32 /**
33  * A sampler sampling a stream of PeerIDs.
34  */
35 struct RPS_Sampler;
36
37
38 /**
39  * Callback that is called when a new PeerID is inserted into a sampler.
40  *
41  * @param cls the closure given alongside this function.
42  * @param id the PeerID that is inserted
43  */
44 typedef void
45 (*RPS_sampler_insert_cb) (void *cls,
46     struct RPS_Sampler *sampler,
47     const struct GNUNET_PeerIdentity *id);
48
49 /**
50  * Callback that is called when a new PeerID is removed from a sampler.
51  *
52  * @param cls the closure given alongside this function.
53  * @param id the PeerID that is removed
54  */
55 typedef void
56 (*RPS_sampler_remove_cb) (void *cls,
57     struct RPS_Sampler *sampler,
58     const struct GNUNET_PeerIdentity *id);
59
60 /**
61  * Callback that is called from _get_n_rand_peers() when the PeerIDs are ready.
62  *
63  * @param cls the closure given alongside this function.
64  * @param ids the PeerIDs that were returned
65  *        to be freed
66  */
67   typedef void
68 (*RPS_sampler_n_rand_peers_ready_cb) (void *cls,
69     struct GNUNET_PeerIdentity *ids, uint32_t num_peers);
70
71
72 /**
73  * Get the size of the sampler.
74  *
75  * @param sampler the sampler to return the size of.
76  * @return the size of the sampler
77  */
78 unsigned int
79 RPS_sampler_get_size (struct RPS_Sampler *sampler);
80
81
82 /**
83  * Grow or shrink the size of the sampler.
84  *
85  * @param sampler the sampler to resize.
86  * @param new_size the new size of the sampler (not 0)
87  */
88 void
89 RPS_sampler_resize (struct RPS_Sampler *sampler, unsigned int new_size);
90
91
92 /**
93  * Initialise a tuple of samplers.
94  *
95  * @param init_size the size the sampler is initialised with
96  * @param id with which all newly created sampler elements are initialised
97  * @param ins_cb the callback that will be called on every PeerID that is
98  *               newly inserted into a sampler element
99  * @param ins_cls the closure given to #ins_cb
100  * @param rem_cb the callback that will be called on every PeerID that is
101  *               removed from a sampler element
102  * @param rem_cls the closure given to #rem_cb
103  * @return a handle to a sampler that consists of sampler elements.
104  */
105 struct RPS_Sampler *
106 RPS_sampler_init (size_t init_size,
107     struct GNUNET_TIME_Relative max_round_interval,
108     RPS_sampler_insert_cb ins_cb, void *ins_cls,
109     RPS_sampler_remove_cb rem_cb, void *rem_cls);
110
111
112 /**
113  * A fuction to update every sampler in the given list
114  *
115  * @param sampler the sampler to update.
116  * @param id the PeerID that is put in the sampler
117  */
118   void
119 RPS_sampler_update (struct RPS_Sampler *sampler,
120                     const struct GNUNET_PeerIdentity *id);
121
122
123 /**
124  * Reinitialise all previously initialised sampler elements with the given
125  * value.
126  *
127  * Used to get rid of a PeerID.
128  *
129  * @param sampler the sampler to reinitialise a sampler in.
130  * @param id the id of the samplers to update.
131  */
132   void
133 RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
134                                    const struct GNUNET_PeerIdentity *id);
135
136
137 /**
138  * Get n random peers out of the sampled peers.
139  *
140  * We might want to reinitialise this sampler after giving the
141  * corrsponding peer to the client.
142  * Random with or without consumption?
143  *
144  * @param sampler the sampler to get peers from.
145  * @param cb callback that will be called once the ids are ready.
146  * @param cls closure given to @a cb
147  * @param for_client #GNUNET_YES if result is used for client,
148  *                   #GNUNET_NO if used internally
149  * @param num_peers the number of peers requested
150  */
151     void
152 RPS_sampler_get_n_rand_peers (struct RPS_Sampler *sampler,
153                               RPS_sampler_n_rand_peers_ready_cb cb,
154                               void *cls, uint32_t num_peers, int for_client);
155
156
157 /**
158  * Counts how many Samplers currently hold a given PeerID.
159  *
160  * @param sampler the sampler to cound ids in.
161  * @param id the PeerID to count.
162  *
163  * @return the number of occurrences of id.
164  */
165   uint32_t
166 RPS_sampler_count_id (struct RPS_Sampler *sampler,
167                       const struct GNUNET_PeerIdentity *id);
168
169
170 /**
171  * Cleans the samplers.
172  *
173  * @param sampler the sampler to destroy.
174  */
175   void
176 RPS_sampler_destroy (struct RPS_Sampler *sampler);
177
178 #endif
179 /* end of gnunet-service-rps.c */