GNUnet 0.21.1
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.

72{
73 (void) cls;
74
75 if (NULL != req_handle)
78}
static struct GNUNET_RPS_Request_Handle * req_handle
Request handle.
Definition: gnunet-rps.c:41
static struct GNUNET_RPS_Handle * rps_handle
RPS handle.
Definition: gnunet-rps.c:36
void GNUNET_RPS_request_cancel(struct GNUNET_RPS_Request_Handle *rh)
Cancel an issued request.
Definition: rps_api.c:1200
void GNUNET_RPS_disconnect(struct GNUNET_RPS_Handle *h)
Disconnect from the rps service.
Definition: rps_api.c:1263

References GNUNET_RPS_disconnect(), GNUNET_RPS_request_cancel(), req_handle, and rps_handle.

Referenced by run().

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.

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}
static int ret
Definition: gnunet-rps.c:31
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:562

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

Referenced by run().

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 118 of file gnunet-rps.c.

121{
122 uint64_t i;
123
124 (void) cls;
125
126 if (0 == n)
127 {
128 fprintf (stdout, "Empty view\n");
129 }
130 req_handle = NULL;
131 for (i = 0; i < n; i++)
132 {
133 fprintf (stdout, "%s\n",
134 GNUNET_i2s_full (&recv_peers[i]));
135 }
136
137 if (1 == num_view_updates)
138 {
139 ret = 0;
141 }
142 else if (1 < num_view_updates)
143 {
145 }
146}
static uint64_t num_view_updates
Number of updates we want to receive.
Definition: gnunet-rps.c:61

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

Referenced by run().

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
num_peersnumber of peers
recv_peersthe received peers

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

159{
160 uint64_t i;
161
162 (void) cls;
163
164 if (0 == num_peers)
165 {
166 fprintf (stdout, "No peer was returned\n");
167 }
168 req_handle = NULL;
169 for (i = 0; i < num_peers; i++)
170 {
171 fprintf (stdout, "%s\n",
172 GNUNET_i2s_full (&recv_peers[i]));
173 }
174}
static unsigned int num_peers
Number of peers.

References GNUNET_i2s_full(), num_peers, and req_handle.

Referenced by run().

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 186 of file gnunet-rps.c.

190{
191 static uint64_t num_peers;
192 static struct GNUNET_PeerIdentity zero_pid;
193
194 (void) cls;
195 (void) cfgfile;
196
198 if (NULL == rps_handle)
199 {
200 fprintf (stderr, "Failed to connect to the rps service\n");
201 return;
202 }
203
204 if ((0 == memcmp (&zero_pid, &peer_id, sizeof(peer_id))) &&
205 (! view_update) &&
206 (! stream_input))
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)) )
211 {
212 num_peers = 1;
213 }
215 "Requesting %" PRIu64 " PeerIDs\n", num_peers);
217 NULL);
219 }
220 else if (view_update)
221 {
222 /* Get updates of view */
223 if ((NULL == args[0]) ||
224 (0 == sscanf (args[0], "%lu", &num_view_updates)) )
225 {
227 }
229 NULL);
230 if (0 != num_view_updates)
232 "Requesting %" PRIu64 " view updates\n", num_view_updates);
233 else
235 "Requesting continuous view updates\n");
237 }
238 else if (stream_input)
239 {
240 /* Get updates of view */
243 }
244 else
245 { /* Seed PeerID */
247 fprintf (stdout, "Seeded PeerID %s\n", GNUNET_i2s_full (&peer_id));
248 ret = 0;
250 }
251}
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:109
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:156
static int view_update
Do we want to receive updates of the view? (Option –view)
Definition: gnunet-rps.c:51
static int stream_input
Do we want to receive updates of the view? (Option –view)
Definition: gnunet-rps.c:56
static void do_shutdown(void *cls)
Task run when user presses CTRL-C to abort.
Definition: gnunet-rps.c:71
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
static struct GNUNET_PeerIdentity peer_id
PeerID (Option –seed)
Definition: gnunet-rps.c:46
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:118
#define GNUNET_log(kind,...)
@ GNUNET_ERROR_TYPE_DEBUG
struct GNUNET_RPS_Handle * GNUNET_RPS_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the rps service.
Definition: rps_api.c:843
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
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
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:1036
struct GNUNET_RPS_Request_Handle * GNUNET_RPS_request_peers(struct GNUNET_RPS_Handle *rps_handle, uint32_t num_req_peers, GNUNET_RPS_NotifyReadyCB ready_cb, void *cls)
Request n random peers.
Definition: rps_api.c:950
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,...
Definition: scheduler.c:1334
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:1299
The identity of the host (wraps the signing key of the peer).

References consensus-simulation::args, cfg, do_shutdown(), 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(), req_handle, ret, rps_handle, stream_input, stream_input_handle(), view_update, and view_update_handle().

Referenced by main().

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 262 of file gnunet-rps.c.

263{
264 const char helpstr[] =
265 "Get random GNUnet peers. If none is specified a single is requested.";
268 "seed",
269 "PEER_ID",
270 gettext_noop ("Seed a PeerID"),
271 &peer_id),
273 "view",
275 "Get updates of view (0 for infinite updates)"),
276 &view_update),
278 "stream",
279 gettext_noop ("Get peers from biased stream"),
280 &stream_input),
282 };
283
284 return (GNUNET_OK ==
285 GNUNET_PROGRAM_run (argc,
286 argv,
287 "gnunet-rps [NUMBER_OF_PEERS]",
289 (helpstr),
290 options, &run, NULL)) ? ret : 1;
291}
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
#define gettext_noop(String)
Definition: gettext.h:70
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:186
#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_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.
@ GNUNET_OK
enum GNUNET_GenericReturnValue 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,...
Definition: program.c:400
Definition of a command line option.

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

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

◆ req_handle

◆ 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().