GNUnet  0.10.x
gnunet-rps.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C)
4 
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.
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  Affero General Public License for more details.
14 
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/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_rps_service.h"
29 #include <inttypes.h>
30 
31 static int ret;
32 
37 
42 
47 
51 static int view_update;
52 
56 static int stream_input;
57 
61 static uint64_t num_view_updates;
62 
63 
70 static void
71 do_shutdown(void *cls)
72 {
73  (void)cls;
74 
75  if (NULL != req_handle)
76  GNUNET_RPS_request_cancel(req_handle);
77  GNUNET_RPS_disconnect(rps_handle);
78 }
79 
80 
89 static void
90 reply_handle(void *cls,
91  uint64_t n,
92  const struct GNUNET_PeerIdentity *recv_peers)
93 {
94  uint64_t i;
95 
96  (void)cls;
97 
98  req_handle = NULL;
99  for (i = 0; i < n; i++)
100  {
101  fprintf(stdout, "%s\n",
102  GNUNET_i2s_full(&recv_peers[i]));
103  }
104  ret = 0;
105 
107 }
108 
116 static void
118  uint64_t n,
119  const struct GNUNET_PeerIdentity *recv_peers)
120 {
121  uint64_t i;
122 
123  (void)cls;
124 
125  if (0 == n)
126  {
127  fprintf(stdout, "Empty view\n");
128  }
129  req_handle = NULL;
130  for (i = 0; i < n; i++)
131  {
132  fprintf(stdout, "%s\n",
133  GNUNET_i2s_full(&recv_peers[i]));
134  }
135 
136  if (1 == num_view_updates)
137  {
138  ret = 0;
140  }
141  else if (1 < num_view_updates)
142  {
144  }
145 }
146 
147 
154 static void
156  uint64_t num_peers,
157  const struct GNUNET_PeerIdentity *recv_peers)
158 {
159  uint64_t i;
160 
161  (void)cls;
162 
163  if (0 == num_peers)
164  {
165  fprintf(stdout, "No peer was returned\n");
166  }
167  req_handle = NULL;
168  for (i = 0; i < num_peers; i++)
169  {
170  fprintf(stdout, "%s\n",
171  GNUNET_i2s_full(&recv_peers[i]));
172  }
173 }
174 
175 
184 static void
185 run(void *cls,
186  char *const *args,
187  const char *cfgfile,
188  const struct GNUNET_CONFIGURATION_Handle *cfg)
189 {
190  static uint64_t num_peers;
191  static struct GNUNET_PeerIdentity zero_pid;
192 
193  (void)cls;
194  (void)cfgfile;
195 
196  rps_handle = GNUNET_RPS_connect(cfg);
197  if (NULL == rps_handle)
198  {
199  fprintf(stderr, "Failed to connect to the rps service\n");
200  return;
201  }
202 
203  if ((0 == memcmp(&zero_pid, &peer_id, sizeof(peer_id))) &&
204  (!view_update) &&
205  (!stream_input))
206  { /* Request n PeerIDs */
207  /* If number was specified use it, else request single peer. */
208  if (NULL == args[0] ||
209  0 == sscanf(args[0], "%lu", &num_peers))
210  {
211  num_peers = 1;
212  }
214  "Requesting %" PRIu64 " PeerIDs\n", num_peers);
215  req_handle = GNUNET_RPS_request_peers(rps_handle, num_peers, reply_handle, NULL);
217  }
218  else if (view_update)
219  {
220  /* Get updates of view */
221  if (NULL == args[0] ||
222  0 == sscanf(args[0], "%lu", &num_view_updates))
223  {
224  num_view_updates = 0;
225  }
227  if (0 != num_view_updates)
229  "Requesting %" PRIu64 " view updates\n", num_view_updates);
230  else
232  "Requesting continuous view updates\n");
234  }
235  else if (stream_input)
236  {
237  /* Get updates of view */
240  }
241  else
242  { /* Seed PeerID */
243  GNUNET_RPS_seed_ids(rps_handle, 1, &peer_id);
244  fprintf(stdout, "Seeded PeerID %s\n", GNUNET_i2s_full(&peer_id));
245  ret = 0;
247  }
248 }
249 
257 int
258 main(int argc, char *const *argv)
259 {
260  const char helpstr[] =
261  "Get random GNUnet peers. If none is specified a single is requested.";
262  struct GNUNET_GETOPT_CommandLineOption options[] = {
264  "seed",
265  "PEER_ID",
266  gettext_noop("Seed a PeerID"),
267  &peer_id),
269  "view",
270  gettext_noop("Get updates of view (0 for infinite updates)"),
271  &view_update),
273  "stream",
274  gettext_noop("Get peers from biased stream"),
275  &stream_input),
277  };
278 
279  return (GNUNET_OK ==
280  GNUNET_PROGRAM_run(argc,
281  argv,
282  "gnunet-rps [NUMBER_OF_PEERS]",
284  (helpstr),
285  options, &run, NULL)) ? ret : 1;
286 }
287 
288 /* end of gnunet-rps.c */
int main(int argc, char *const *argv)
The main function to rps.
Definition: gnunet-rps.c:258
struct GNUNET_RPS_Request_Handle * GNUNET_RPS_request_peers(struct GNUNET_RPS_Handle *h, uint32_t n, GNUNET_RPS_NotifyReadyCB ready_cb, void *cls)
Request n random peers.
Definition: rps_api.c:942
static uint64_t num_view_updates
Number of updates we want to receive.
Definition: gnunet-rps.c:61
void GNUNET_RPS_seed_ids(struct GNUNET_RPS_Handle *h, uint32_t n, const struct GNUNET_PeerIdentity *ids)
Seed rps service with peerIDs.
Definition: rps_api.c:1028
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1284
void GNUNET_RPS_view_request(struct GNUNET_RPS_Handle *rps_handle, uint32_t num_updates, GNUNET_RPS_NotifyReadyCB view_update_cb, void *cls)
Request updates of view.
Definition: rps_api.c:456
static void view_update_handle(void *cls, uint64_t n, const struct GNUNET_PeerIdentity *recv_peers)
Callback called on receipt view update.
Definition: gnunet-rps.c:117
static struct GNUNET_RPS_Handle * rps_handle
RPS handle.
Definition: gnunet-rps.c:36
static void stream_input_handle(void *cls, uint64_t num_peers, const struct GNUNET_PeerIdentity *recv_peers)
Callback called on receipt of peer from biased stream.
Definition: gnunet-rps.c:155
static int stream_input
Do we want to receive updates of the view? (Option –view)
Definition: gnunet-rps.c:56
const char * GNUNET_i2s_full(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
Definition of a command line option.
void GNUNET_RPS_disconnect(struct GNUNET_RPS_Handle *h)
Disconnect from the rps service.
Definition: rps_api.c:1249
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:517
Handler to handle requests from a client.
Definition: rps_api.c:77
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_RPS_Handle * GNUNET_RPS_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the rps service.
Definition: rps_api.c:835
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be run by the scheduler.
Definition: gnunet-rps.c:185
static struct GNUNET_RPS_Request_Handle * req_handle
Request handle.
Definition: gnunet-rps.c:41
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1264
static void do_shutdown(void *cls)
Task run when user presses CTRL-C to abort.
Definition: gnunet-rps.c:71
static int view_update
Do we want to receive updates of the view? (Option –view)
Definition: gnunet-rps.c:51
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
static unsigned int num_peers
static int ret
Definition: gnunet-rps.c:31
The identity of the host (wraps the signing key of the peer).
void GNUNET_RPS_request_cancel(struct GNUNET_RPS_Request_Handle *rh)
Cancle an issued request.
Definition: rps_api.c:1186
configuration data
Definition: configuration.c:83
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
#define GNUNET_log(kind,...)
#define GNUNET_GETOPT_option_base32_auto(shortName, name, argumentHelp, description, val)
Allow user to specify a binary value using Crockford Base32 encoding where the size of the binary val...
struct GNUNET_RPS_StreamRequestHandle * GNUNET_RPS_stream_request(struct GNUNET_RPS_Handle *rps_handle, GNUNET_RPS_NotifyReadyCB stream_input_cb, void *cls)
Request biased stream of peers that are being put into the sampler.
Definition: rps_api.c:498
int GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration, parse options).
Definition: program.c:367
Handler for a single request from a client.
Definition: rps_api.c:157
static struct GNUNET_PeerIdentity peer_id
PeerID (Option –seed)
Definition: gnunet-rps.c:46
static void reply_handle(void *cls, uint64_t n, const struct GNUNET_PeerIdentity *recv_peers)
Callback called on receipt of reply.
Definition: gnunet-rps.c:90
#define gettext_noop(String)
Definition: gettext.h:69