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 Do we want to receive updates of the view? (Option --view)
54 static int stream_input;
57 * @brief Number of updates we want to receive
59 static uint64_t num_view_updates;
63 * Task run when user presses CTRL-C to abort.
64 * Cancels pending request and disconnects.
69 do_shutdown (void *cls)
73 if (NULL != req_handle)
74 GNUNET_RPS_request_cancel (req_handle);
75 GNUNET_RPS_disconnect (rps_handle);
80 * Callback called on receipt of reply.
81 * Prints replied PeerIDs.
84 * @param n number of peers
85 * @param recv_peers the received peers
88 reply_handle (void *cls,
90 const struct GNUNET_PeerIdentity *recv_peers)
96 for (i = 0; i < n; i++)
98 FPRINTF (stdout, "%s\n",
99 GNUNET_i2s_full (&recv_peers[i]));
103 GNUNET_SCHEDULER_shutdown ();
107 * Callback called on receipt view update.
110 * @param n number of peers
111 * @param recv_peers the received peers
114 view_update_handle (void *cls,
116 const struct GNUNET_PeerIdentity *recv_peers)
123 FPRINTF (stdout, "Empty view\n");
126 for (i = 0; i < n; i++)
128 FPRINTF (stdout, "%s\n",
129 GNUNET_i2s_full (&recv_peers[i]));
132 if (1 == num_view_updates)
135 GNUNET_SCHEDULER_shutdown ();
137 else if (1 < num_view_updates)
145 * Callback called on receipt of peer from biased stream
147 * @param n number of peers
148 * @param recv_peers the received peers
151 stream_input_handle (void *cls,
153 const struct GNUNET_PeerIdentity *recv_peers)
160 FPRINTF (stdout, "No peer was returned\n");
163 for (i = 0; i < num_peers; i++)
165 FPRINTF (stdout, "%s\n",
166 GNUNET_i2s_full (&recv_peers[i]));
172 * Main function that will be run by the scheduler.
175 * @param args remaining command-line arguments
176 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
177 * @param cfg configuration
183 const struct GNUNET_CONFIGURATION_Handle *cfg)
185 static uint64_t num_peers;
186 static struct GNUNET_PeerIdentity zero_pid;
190 rps_handle = GNUNET_RPS_connect (cfg);
191 if (NULL == rps_handle)
193 FPRINTF (stderr, "Failed to connect to the rps service\n");
197 if ((0 == memcmp (&zero_pid, &peer_id, sizeof (peer_id))) &&
200 { /* Request n PeerIDs */
201 /* If number was specified use it, else request single peer. */
202 if (NULL == args[0] ||
203 0 == sscanf (args[0], "%lu", &num_peers))
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208 "Requesting %" PRIu64 " PeerIDs\n", num_peers);
209 req_handle = GNUNET_RPS_request_peers (rps_handle, num_peers, reply_handle, NULL);
210 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
211 } else if (view_update)
213 /* Get updates of view */
214 if (NULL == args[0] ||
215 0 == sscanf (args[0], "%lu", &num_view_updates))
217 num_view_updates = 0;
219 GNUNET_RPS_view_request (rps_handle, num_view_updates, view_update_handle, NULL);
220 if (0 != num_view_updates)
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222 "Requesting %" PRIu64 " view updates\n", num_view_updates);
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225 "Requesting continuous view updates\n");
226 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
227 } else if (stream_input)
229 /* Get updates of view */
230 GNUNET_RPS_stream_request (rps_handle, stream_input_handle, NULL);
231 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
235 GNUNET_RPS_seed_ids (rps_handle, 1, &peer_id);
236 FPRINTF (stdout, "Seeded PeerID %s\n", GNUNET_i2s_full (&peer_id));
238 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
243 * The main function to rps.
245 * @param argc number of arguments from the command line
246 * @param argv command line arguments
247 * @return 0 ok, 1 on error
250 main (int argc, char *const *argv)
252 const char helpstr[] =
253 "Get random GNUnet peers. If none is specified a single is requested.";
254 struct GNUNET_GETOPT_CommandLineOption options[] = {
255 GNUNET_GETOPT_option_base32_auto ('s',
258 gettext_noop ("Seed a PeerID"),
260 GNUNET_GETOPT_option_flag ('V',
262 gettext_noop ("Get updates of view (0 for infinite updates)"),
264 GNUNET_GETOPT_option_flag ('S',
266 gettext_noop ("Get peers from biased stream"),
268 GNUNET_GETOPT_OPTION_END
271 GNUNET_PROGRAM_run (argc,
273 "gnunet-rps [NUMBER_OF_PEERS]",
276 options, &run, NULL)) ? ret : 1;
279 /* end of gnunet-rps.c */