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)
99 for (i = 0; i < n; i++)
101 fprintf (stdout, "%s\n",
102 GNUNET_i2s_full (&recv_peers[i]));
106 GNUNET_SCHEDULER_shutdown ();
111 * Callback called on receipt view update.
114 * @param n number of peers
115 * @param recv_peers the received peers
118 view_update_handle (void *cls,
120 const struct GNUNET_PeerIdentity *recv_peers)
128 fprintf (stdout, "Empty view\n");
131 for (i = 0; i < n; i++)
133 fprintf (stdout, "%s\n",
134 GNUNET_i2s_full (&recv_peers[i]));
137 if (1 == num_view_updates)
140 GNUNET_SCHEDULER_shutdown ();
142 else if (1 < num_view_updates)
150 * Callback called on receipt of peer from biased stream
152 * @param n number of peers
153 * @param recv_peers the received peers
156 stream_input_handle (void *cls,
158 const struct GNUNET_PeerIdentity *recv_peers)
166 fprintf (stdout, "No peer was returned\n");
169 for (i = 0; i < num_peers; i++)
171 fprintf (stdout, "%s\n",
172 GNUNET_i2s_full (&recv_peers[i]));
178 * Main function that will be run by the scheduler.
181 * @param args remaining command-line arguments
182 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
183 * @param cfg configuration
189 const struct GNUNET_CONFIGURATION_Handle *cfg)
191 static uint64_t num_peers;
192 static struct GNUNET_PeerIdentity zero_pid;
197 rps_handle = GNUNET_RPS_connect (cfg);
198 if (NULL == rps_handle)
200 fprintf (stderr, "Failed to connect to the rps service\n");
204 if ((0 == memcmp (&zero_pid, &peer_id, sizeof(peer_id))) &&
207 { /* Request n PeerIDs */
208 /* If number was specified use it, else request single peer. */
209 if ((NULL == args[0]) ||
210 (0 == sscanf (args[0], "%lu", &num_peers)) )
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215 "Requesting %" PRIu64 " PeerIDs\n", num_peers);
216 req_handle = GNUNET_RPS_request_peers (rps_handle, num_peers, reply_handle,
218 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
220 else if (view_update)
222 /* Get updates of view */
223 if ((NULL == args[0]) ||
224 (0 == sscanf (args[0], "%lu", &num_view_updates)) )
226 num_view_updates = 0;
228 GNUNET_RPS_view_request (rps_handle, num_view_updates, view_update_handle,
230 if (0 != num_view_updates)
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
232 "Requesting %" PRIu64 " view updates\n", num_view_updates);
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
235 "Requesting continuous view updates\n");
236 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
238 else if (stream_input)
240 /* Get updates of view */
241 GNUNET_RPS_stream_request (rps_handle, stream_input_handle, NULL);
242 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
246 GNUNET_RPS_seed_ids (rps_handle, 1, &peer_id);
247 fprintf (stdout, "Seeded PeerID %s\n", GNUNET_i2s_full (&peer_id));
249 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
255 * The main function to rps.
257 * @param argc number of arguments from the command line
258 * @param argv command line arguments
259 * @return 0 ok, 1 on error
262 main (int argc, char *const *argv)
264 const char helpstr[] =
265 "Get random GNUnet peers. If none is specified a single is requested.";
266 struct GNUNET_GETOPT_CommandLineOption options[] = {
267 GNUNET_GETOPT_option_base32_auto ('s',
270 gettext_noop ("Seed a PeerID"),
272 GNUNET_GETOPT_option_flag ('V',
275 "Get updates of view (0 for infinite updates)"),
277 GNUNET_GETOPT_option_flag ('S',
279 gettext_noop ("Get peers from biased stream"),
281 GNUNET_GETOPT_OPTION_END
285 GNUNET_PROGRAM_run (argc,
287 "gnunet-rps [NUMBER_OF_PEERS]",
290 options, &run, NULL)) ? ret : 1;
294 /* end of gnunet-rps.c */