GNUnet  0.10.x
Functions | Variables
gnunet-rps.c File Reference

random peer sampling More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_rps_service.h"
#include <inttypes.h>
Include dependency graph for gnunet-rps.c:

Go to the source code of this file.

Functions

static void do_shutdown (void *cls)
 Task run when user presses CTRL-C to abort. More...
 
static void reply_handle (void *cls, uint64_t n, const struct GNUNET_PeerIdentity *recv_peers)
 Callback called on receipt of reply. More...
 
static void view_update_handle (void *cls, uint64_t n, const struct GNUNET_PeerIdentity *recv_peers)
 Callback called on receipt view update. More...
 
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. More...
 
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. More...
 
int main (int argc, char *const *argv)
 The main function to rps. More...
 

Variables

static int ret
 
static struct GNUNET_RPS_Handlerps_handle
 RPS handle. More...
 
static struct GNUNET_RPS_Request_Handlereq_handle
 Request handle. More...
 
static struct GNUNET_PeerIdentity peer_id
 PeerID (Option –seed) More...
 
static int view_update
 Do we want to receive updates of the view? (Option –view) More...
 
static int stream_input
 Do we want to receive updates of the view? (Option –view) More...
 
static uint64_t num_view_updates
 Number of updates we want to receive. More...
 

Detailed Description

random peer sampling

Author
Julius Bünger

Definition in file gnunet-rps.c.

Function Documentation

◆ do_shutdown()

static void do_shutdown ( void *  cls)
static

Task run when user presses CTRL-C to abort.

Cancels pending request and disconnects.

Parameters
clsNULL

Definition at line 71 of file gnunet-rps.c.

References GNUNET_RPS_disconnect(), and GNUNET_RPS_request_cancel().

Referenced by run().

72 {
73  (void) cls;
74 
75  if (NULL != req_handle)
78 }
static struct GNUNET_RPS_Handle * rps_handle
RPS handle.
Definition: gnunet-rps.c:36
void GNUNET_RPS_disconnect(struct GNUNET_RPS_Handle *h)
Disconnect from the rps service.
Definition: rps_api.c:1248
static struct GNUNET_RPS_Request_Handle * req_handle
Request handle.
Definition: gnunet-rps.c:41
void GNUNET_RPS_request_cancel(struct GNUNET_RPS_Request_Handle *rh)
Cancle an issued request.
Definition: rps_api.c:1187
Here is the call graph for this function:
Here is the caller graph for this function:

◆ reply_handle()

static void reply_handle ( void *  cls,
uint64_t  n,
const struct GNUNET_PeerIdentity recv_peers 
)
static

Callback called on receipt of reply.

Prints replied PeerIDs.

Parameters
clsclosure
nnumber of peers
recv_peersthe received peers

Definition at line 90 of file gnunet-rps.c.

References FPRINTF, GNUNET_i2s_full(), GNUNET_SCHEDULER_shutdown(), and ret.

Referenced by run().

93 {
94  uint64_t i;
95  (void) cls;
96 
97  req_handle = NULL;
98  for (i = 0; i < n; i++)
99  {
100  FPRINTF (stdout, "%s\n",
101  GNUNET_i2s_full (&recv_peers[i]));
102  }
103  ret = 0;
104 
106 }
#define FPRINTF
Definition: plibc.h:683
const char * GNUNET_i2s_full(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:524
static struct GNUNET_RPS_Request_Handle * req_handle
Request handle.
Definition: gnunet-rps.c:41
static int ret
Definition: gnunet-rps.c:31
Here is the call graph for this function:
Here is the caller graph for this function:

◆ view_update_handle()

static void view_update_handle ( void *  cls,
uint64_t  n,
const struct GNUNET_PeerIdentity recv_peers 
)
static

Callback called on receipt view update.

Prints view.

Parameters
nnumber of peers
recv_peersthe received peers

Definition at line 116 of file gnunet-rps.c.

References FPRINTF, GNUNET_i2s_full(), GNUNET_SCHEDULER_shutdown(), num_view_updates, and ret.

Referenced by run().

119 {
120  uint64_t i;
121  (void) cls;
122 
123  if (0 == n)
124  {
125  FPRINTF (stdout, "Empty view\n");
126  }
127  req_handle = NULL;
128  for (i = 0; i < n; i++)
129  {
130  FPRINTF (stdout, "%s\n",
131  GNUNET_i2s_full (&recv_peers[i]));
132  }
133 
134  if (1 == num_view_updates)
135  {
136  ret = 0;
138  }
139  else if (1 < num_view_updates)
140  {
142  }
143 }
static uint64_t num_view_updates
Number of updates we want to receive.
Definition: gnunet-rps.c:61
#define FPRINTF
Definition: plibc.h:683
const char * GNUNET_i2s_full(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:524
static struct GNUNET_RPS_Request_Handle * req_handle
Request handle.
Definition: gnunet-rps.c:41
static int ret
Definition: gnunet-rps.c:31
Here is the call graph for this function:
Here is the caller graph for this function:

◆ stream_input_handle()

static void stream_input_handle ( void *  cls,
uint64_t  num_peers,
const struct GNUNET_PeerIdentity recv_peers 
)
static

Callback called on receipt of peer from biased stream.

Parameters
nnumber of peers
recv_peersthe received peers

Definition at line 153 of file gnunet-rps.c.

References FPRINTF, GNUNET_i2s_full(), and num_peers.

Referenced by run().

156 {
157  uint64_t i;
158  (void) cls;
159 
160  if (0 == num_peers)
161  {
162  FPRINTF (stdout, "No peer was returned\n");
163  }
164  req_handle = NULL;
165  for (i = 0; i < num_peers; i++)
166  {
167  FPRINTF (stdout, "%s\n",
168  GNUNET_i2s_full (&recv_peers[i]));
169  }
170 }
#define FPRINTF
Definition: plibc.h:683
const char * GNUNET_i2s_full(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
static struct GNUNET_RPS_Request_Handle * req_handle
Request handle.
Definition: gnunet-rps.c:41
static unsigned int num_peers
Here is the call graph for this function:
Here is the caller graph for this function:

◆ run()

static void run ( void *  cls,
char *const *  args,
const char *  cfgfile,
const struct GNUNET_CONFIGURATION_Handle cfg 
)
static

Main function that will be run by the scheduler.

Parameters
clsclosure
argsremaining command-line arguments
cfgfilename of the configuration file used (for saving, can be NULL!)
cfgconfiguration

Definition at line 182 of file gnunet-rps.c.

References do_shutdown(), FPRINTF, GNUNET_ERROR_TYPE_DEBUG, GNUNET_i2s_full(), GNUNET_log, GNUNET_RPS_connect(), GNUNET_RPS_request_peers(), GNUNET_RPS_seed_ids(), GNUNET_RPS_stream_request(), GNUNET_RPS_view_request(), GNUNET_SCHEDULER_add_now(), GNUNET_SCHEDULER_add_shutdown(), num_peers, num_view_updates, peer_id, reply_handle(), ret, stream_input, stream_input_handle(), view_update, and view_update_handle().

Referenced by main().

186 {
187  static uint64_t num_peers;
188  static struct GNUNET_PeerIdentity zero_pid;
189  (void) cls;
190  (void) cfgfile;
191 
193  if (NULL == rps_handle)
194  {
195  FPRINTF (stderr, "Failed to connect to the rps service\n");
196  return;
197  }
198 
199  if ((0 == memcmp (&zero_pid, &peer_id, sizeof (peer_id))) &&
200  (!view_update) &&
201  (!stream_input))
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))
206  {
207  num_peers = 1;
208  }
210  "Requesting %" PRIu64 " PeerIDs\n", num_peers);
211  req_handle = GNUNET_RPS_request_peers (rps_handle, num_peers, reply_handle, NULL);
213  } else if (view_update)
214  {
215  /* Get updates of view */
216  if (NULL == args[0] ||
217  0 == sscanf (args[0], "%lu", &num_view_updates))
218  {
219  num_view_updates = 0;
220  }
222  if (0 != num_view_updates)
224  "Requesting %" PRIu64 " view updates\n", num_view_updates);
225  else
227  "Requesting continuous view updates\n");
229  } else if (stream_input)
230  {
231  /* Get updates of view */
234  }
235  else
236  { /* Seed PeerID */
237  GNUNET_RPS_seed_ids (rps_handle, 1, &peer_id);
238  FPRINTF (stdout, "Seeded PeerID %s\n", GNUNET_i2s_full (&peer_id));
239  ret = 0;
241  }
242 }
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:943
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:1029
#define FPRINTF
Definition: plibc.h:683
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:1293
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:461
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:116
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:153
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).
struct GNUNET_RPS_Handle * GNUNET_RPS_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the rps service.
Definition: rps_api.c:836
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:1273
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 unsigned int num_peers
static int ret
Definition: gnunet-rps.c:31
The identity of the host (wraps the signing key of the peer).
#define GNUNET_log(kind,...)
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:503
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *const *  argv 
)

The main function to rps.

Parameters
argcnumber of arguments from the command line
argvcommand line arguments
Returns
0 ok, 1 on error

Definition at line 252 of file gnunet-rps.c.

References gettext_noop, GNUNET_GETOPT_option_base32_auto, GNUNET_GETOPT_OPTION_END, GNUNET_GETOPT_option_flag(), GNUNET_OK, GNUNET_PROGRAM_run(), peer_id, ret, run(), stream_input, and view_update.

253 {
254  const char helpstr[] =
255  "Get random GNUnet peers. If none is specified a single is requested.";
258  "seed",
259  "PEER_ID",
260  gettext_noop ("Seed a PeerID"),
261  &peer_id),
263  "view",
264  gettext_noop ("Get updates of view (0 for infinite updates)"),
265  &view_update),
267  "stream",
268  gettext_noop ("Get peers from biased stream"),
269  &stream_input),
271  };
272  return (GNUNET_OK ==
273  GNUNET_PROGRAM_run (argc,
274  argv,
275  "gnunet-rps [NUMBER_OF_PEERS]",
277  (helpstr),
278  options, &run, NULL)) ? ret : 1;
279 }
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
static int stream_input
Do we want to receive updates of the view? (Option –view)
Definition: gnunet-rps.c:56
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
Definition of a command line option.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
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:182
static int view_update
Do we want to receive updates of the view? (Option –view)
Definition: gnunet-rps.c:51
static int ret
Definition: gnunet-rps.c:31
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_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...
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:361
static struct GNUNET_PeerIdentity peer_id
PeerID (Option –seed)
Definition: gnunet-rps.c:46
#define gettext_noop(String)
Definition: gettext.h:69
Here is the call graph for this function:

Variable Documentation

◆ ret

int ret
static

Definition at line 31 of file gnunet-rps.c.

Referenced by main(), reply_handle(), run(), and view_update_handle().

◆ rps_handle

struct GNUNET_RPS_Handle* rps_handle
static

RPS handle.

Definition at line 36 of file gnunet-rps.c.

Referenced by clean_peer().

◆ req_handle

struct GNUNET_RPS_Request_Handle* req_handle
static

◆ peer_id

struct GNUNET_PeerIdentity peer_id
static

PeerID (Option –seed)

Definition at line 46 of file gnunet-rps.c.

Referenced by main(), and run().

◆ view_update

int view_update
static

Do we want to receive updates of the view? (Option –view)

Definition at line 51 of file gnunet-rps.c.

Referenced by main(), reconnect(), and run().

◆ stream_input

int stream_input
static

Do we want to receive updates of the view? (Option –view)

Definition at line 56 of file gnunet-rps.c.

Referenced by main(), reconnect(), and run().

◆ num_view_updates

uint64_t num_view_updates
static

Number of updates we want to receive.

Definition at line 61 of file gnunet-rps.c.

Referenced by run(), and view_update_handle().