2 This file is part of GNUnet.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file rps/gnunet-rps.c
23 * @brief random peer sampling
24 * @author Julius Bünger
27 #include "gnunet_util_lib.h"
28 #include "gnunet_rps_service.h"
36 static struct GNUNET_RPS_Handle *rps_handle;
41 static struct GNUNET_RPS_Request_Handle *req_handle;
44 * PeerID (Option --seed)
46 static struct GNUNET_PeerIdentity peer_id;
49 * @brief Do we want to receive updates of the view? (Option --view)
51 static int view_update;
54 * @brief Do we want to receive updates of the view? (Option --view)
56 static int stream_input;
59 * @brief Number of updates we want to receive
61 static uint64_t num_view_updates;
65 * Task run when user presses CTRL-C to abort.
66 * Cancels pending request and disconnects.
71 do_shutdown (void *cls)
75 if (NULL != req_handle)
76 GNUNET_RPS_request_cancel (req_handle);
77 GNUNET_RPS_disconnect (rps_handle);
82 * Callback called on receipt of reply.
83 * Prints replied PeerIDs.
86 * @param n number of peers
87 * @param recv_peers the received peers
90 reply_handle (void *cls,
92 const struct GNUNET_PeerIdentity *recv_peers)
98 for (i = 0; i < n; i++)
100 FPRINTF (stdout, "%s\n",
101 GNUNET_i2s_full (&recv_peers[i]));
105 GNUNET_SCHEDULER_shutdown ();
109 * Callback called on receipt view update.
112 * @param n number of peers
113 * @param recv_peers the received peers
116 view_update_handle (void *cls,
118 const struct GNUNET_PeerIdentity *recv_peers)
125 FPRINTF (stdout, "Empty view\n");
128 for (i = 0; i < n; i++)
130 FPRINTF (stdout, "%s\n",
131 GNUNET_i2s_full (&recv_peers[i]));
134 if (1 == num_view_updates)
137 GNUNET_SCHEDULER_shutdown ();
139 else if (1 < num_view_updates)
147 * Callback called on receipt of peer from biased stream
149 * @param n number of peers
150 * @param recv_peers the received peers
153 stream_input_handle (void *cls,
155 const struct GNUNET_PeerIdentity *recv_peers)
162 FPRINTF (stdout, "No peer was returned\n");
165 for (i = 0; i < num_peers; i++)
167 FPRINTF (stdout, "%s\n",
168 GNUNET_i2s_full (&recv_peers[i]));
174 * Main function that will be run by the scheduler.
177 * @param args remaining command-line arguments
178 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
179 * @param cfg configuration
185 const struct GNUNET_CONFIGURATION_Handle *cfg)
187 static uint64_t num_peers;
188 static struct GNUNET_PeerIdentity zero_pid;
192 rps_handle = GNUNET_RPS_connect (cfg);
193 if (NULL == rps_handle)
195 FPRINTF (stderr, "Failed to connect to the rps service\n");
199 if ((0 == memcmp (&zero_pid, &peer_id, sizeof (peer_id))) &&
202 { /* Request n PeerIDs */
203 /* If number was specified use it, else request single peer. */
204 if (NULL == args[0] ||
205 0 == sscanf (args[0], "%lu", &num_peers))
209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210 "Requesting %" PRIu64 " PeerIDs\n", num_peers);
211 req_handle = GNUNET_RPS_request_peers (rps_handle, num_peers, reply_handle, NULL);
212 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
213 } else if (view_update)
215 /* Get updates of view */
216 if (NULL == args[0] ||
217 0 == sscanf (args[0], "%lu", &num_view_updates))
219 num_view_updates = 0;
221 GNUNET_RPS_view_request (rps_handle, num_view_updates, view_update_handle, NULL);
222 if (0 != num_view_updates)
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224 "Requesting %" PRIu64 " view updates\n", num_view_updates);
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
227 "Requesting continuous view updates\n");
228 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
229 } else if (stream_input)
231 /* Get updates of view */
232 GNUNET_RPS_stream_request (rps_handle, stream_input_handle, NULL);
233 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
237 GNUNET_RPS_seed_ids (rps_handle, 1, &peer_id);
238 FPRINTF (stdout, "Seeded PeerID %s\n", GNUNET_i2s_full (&peer_id));
240 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
245 * The main function to rps.
247 * @param argc number of arguments from the command line
248 * @param argv command line arguments
249 * @return 0 ok, 1 on error
252 main (int argc, char *const *argv)
254 const char helpstr[] =
255 "Get random GNUnet peers. If none is specified a single is requested.";
256 struct GNUNET_GETOPT_CommandLineOption options[] = {
257 GNUNET_GETOPT_option_base32_auto ('s',
260 gettext_noop ("Seed a PeerID"),
262 GNUNET_GETOPT_option_flag ('V',
264 gettext_noop ("Get updates of view (0 for infinite updates)"),
266 GNUNET_GETOPT_option_flag ('S',
268 gettext_noop ("Get peers from biased stream"),
270 GNUNET_GETOPT_OPTION_END
273 GNUNET_PROGRAM_run (argc,
275 "gnunet-rps [NUMBER_OF_PEERS]",
278 options, &run, NULL)) ? ret : 1;
281 /* end of gnunet-rps.c */