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/>.
20 * @file rps/gnunet-rps.c
21 * @brief random peer sampling
22 * @author Julius Bünger
25 #include "gnunet_util_lib.h"
26 #include "gnunet_rps_service.h"
34 static struct GNUNET_RPS_Handle *rps_handle;
39 static struct GNUNET_RPS_Request_Handle *req_handle;
42 * PeerID (Option --seed)
44 static struct GNUNET_PeerIdentity peer_id;
47 * @brief Do we want to receive updates of the view? (Option --view)
49 static int view_update;
52 * @brief Number of updates we want to receive
54 static uint64_t num_view_updates;
58 * Task run when user presses CTRL-C to abort.
59 * Cancels pending request and disconnects.
64 do_shutdown (void *cls)
66 if (NULL != req_handle)
67 GNUNET_RPS_request_cancel (req_handle);
68 GNUNET_RPS_disconnect (rps_handle);
73 * Callback called on receipt of reply.
74 * Prints replied PeerIDs.
77 * @param n number of peers
78 * @param recv_peers the received peers
81 reply_handle (void *cls,
83 const struct GNUNET_PeerIdentity *recv_peers)
88 for (i = 0; i < n; i++)
90 FPRINTF (stdout, "%s\n",
91 GNUNET_i2s_full (&recv_peers[i]));
95 GNUNET_SCHEDULER_shutdown ();
99 * Callback called on receipt view update.
102 * @param n number of peers
103 * @param recv_peers the received peers
106 view_update_handle (void *cls,
108 const struct GNUNET_PeerIdentity *recv_peers)
114 FPRINTF (stdout, "Empty view\n");
117 for (i = 0; i < n; i++)
119 FPRINTF (stdout, "%s\n",
120 GNUNET_i2s_full (&recv_peers[i]));
123 if (1 == num_view_updates)
126 GNUNET_SCHEDULER_shutdown ();
128 else if (1 < num_view_updates)
136 * Main function that will be run by the scheduler.
139 * @param args remaining command-line arguments
140 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
141 * @param cfg configuration
147 const struct GNUNET_CONFIGURATION_Handle *cfg)
149 static uint64_t num_peers;
150 static struct GNUNET_PeerIdentity zero_pid;
152 rps_handle = GNUNET_RPS_connect (cfg);
154 if ((0 == memcmp (&zero_pid, &peer_id, sizeof (peer_id))) &&
156 { /* Request n PeerIDs */
157 /* If number was specified use it, else request single peer. */
158 num_peers = (NULL == args[0]) ? 1 : atoi (args[0]);
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160 "Requesting %" PRIu64 " PeerIDs\n", num_peers);
161 req_handle = GNUNET_RPS_request_peers (rps_handle, num_peers, reply_handle, NULL);
162 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
163 } else if (view_update)
165 /* Get updates of view */
166 num_view_updates = (NULL == args[0]) ? 0 : atoi (args[0]);
167 GNUNET_RPS_view_request (rps_handle, num_view_updates, view_update_handle, NULL);
168 if (0 != num_view_updates)
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170 "Requesting %" PRIu64 " view updates\n", num_view_updates);
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173 "Requesting contiuous view updates\n");
174 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
178 GNUNET_RPS_seed_ids (rps_handle, 1, &peer_id);
179 FPRINTF (stdout, "Seeded PeerID %s\n", GNUNET_i2s_full (&peer_id));
181 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
186 * The main function to rps.
188 * @param argc number of arguments from the command line
189 * @param argv command line arguments
190 * @return 0 ok, 1 on error
193 main (int argc, char *const *argv)
195 const char helpstr[] =
196 "Get random GNUnet peers. If none is specified a single is requested.";
197 struct GNUNET_GETOPT_CommandLineOption options[] = {
198 GNUNET_GETOPT_option_base32_auto ('s',
201 gettext_noop ("Seed a PeerID"),
203 GNUNET_GETOPT_option_flag ('V',
205 gettext_noop ("Get updates of view (0 for infinite updates)"),
207 GNUNET_GETOPT_OPTION_END
210 GNUNET_PROGRAM_run (argc,
212 "gnunet-rps [NUMBER_OF_PEERS]",
215 options, &run, NULL)) ? ret : 1;
218 /* end of gnunet-rps.c */