diff options
author | Julius Bünger <buenger@mytum.de> | 2015-11-23 16:54:14 +0000 |
---|---|---|
committer | Julius Bünger <buenger@mytum.de> | 2015-11-23 16:54:14 +0000 |
commit | 424a1d3c046f8c9281cb38fcb937036d6c1671f2 (patch) | |
tree | 6814c9e8c5684e740f7d8e6a5b6df4f1d24ad4ee /src/rps/gnunet-rps.c | |
parent | 276363680b531de5b289401fa75e04149061c029 (diff) | |
download | gnunet-424a1d3c046f8c9281cb38fcb937036d6c1671f2.tar.gz gnunet-424a1d3c046f8c9281cb38fcb937036d6c1671f2.zip |
implemented CLI
Diffstat (limited to 'src/rps/gnunet-rps.c')
-rw-r--r-- | src/rps/gnunet-rps.c | 137 |
1 files changed, 133 insertions, 4 deletions
diff --git a/src/rps/gnunet-rps.c b/src/rps/gnunet-rps.c index 9ffed6a38..23516b626 100644 --- a/src/rps/gnunet-rps.c +++ b/src/rps/gnunet-rps.c | |||
@@ -20,7 +20,7 @@ | |||
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file rps/gnunet-rps.c | 22 | * @file rps/gnunet-rps.c |
23 | * @brief rps tool | 23 | * @brief random peer sampling |
24 | * @author Julius Bünger | 24 | * @author Julius Bünger |
25 | */ | 25 | */ |
26 | #include "platform.h" | 26 | #include "platform.h" |
@@ -30,6 +30,110 @@ | |||
30 | static int ret; | 30 | static int ret; |
31 | 31 | ||
32 | /** | 32 | /** |
33 | * RPS handle | ||
34 | */ | ||
35 | static struct GNUNET_RPS_Handle *rps_handle; | ||
36 | |||
37 | /** | ||
38 | * Request handle | ||
39 | */ | ||
40 | static struct GNUNET_RPS_Request_Handle *req_handle; | ||
41 | |||
42 | /** | ||
43 | * PeerID (Option --seed) | ||
44 | */ | ||
45 | static struct GNUNET_PeerIdentity *peer_id; | ||
46 | |||
47 | |||
48 | /** | ||
49 | * Shutdown task | ||
50 | */ | ||
51 | static struct GNUNET_SCHEDULER_Task *shutdown_task; | ||
52 | |||
53 | |||
54 | /** | ||
55 | * Set an option of type 'struct GNUNET_PeerIdentity *' from the command line. | ||
56 | * A pointer to this function should be passed as part of the | ||
57 | * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options | ||
58 | * of this type. It should be followed by a pointer to a value of | ||
59 | * type 'struct GNUNET_PeerIdentity *', which will be allocated with the requested string. | ||
60 | * | ||
61 | * @param ctx command line processing context | ||
62 | * @param scls additional closure (will point to the 'char *', | ||
63 | * which will be allocated) | ||
64 | * @param option name of the option | ||
65 | * @param value actual value of the option (a PeerID) | ||
66 | * @return #GNUNET_OK | ||
67 | */ | ||
68 | static int | ||
69 | GNUNET_GETOPT_set_peerid (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, | ||
70 | void *scls, const char *option, const char *value) | ||
71 | { | ||
72 | struct GNUNET_PeerIdentity **val = (struct GNUNET_PeerIdentity **) scls; | ||
73 | |||
74 | GNUNET_assert (NULL != value); | ||
75 | GNUNET_free_non_null (*val); | ||
76 | /* Not quite sure whether that is a sane way */ | ||
77 | *val = GNUNET_new (struct GNUNET_PeerIdentity); | ||
78 | if (GNUNET_OK != | ||
79 | GNUNET_CRYPTO_eddsa_public_key_from_string (value, | ||
80 | strlen (value), | ||
81 | &((*val)->public_key))) | ||
82 | { | ||
83 | FPRINTF (stderr, "Invalid peer ID %s\n", value); | ||
84 | return GNUNET_SYSERR; | ||
85 | } | ||
86 | return GNUNET_OK; | ||
87 | } | ||
88 | |||
89 | |||
90 | /** | ||
91 | * Task run when user presses CTRL-C to abort. | ||
92 | * Cancels pending request and disconnects. | ||
93 | * | ||
94 | * @param cls NULL | ||
95 | * @param tc scheduler context | ||
96 | */ | ||
97 | static void | ||
98 | do_shutdown (void *cls, | ||
99 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
100 | { | ||
101 | shutdown_task = NULL; | ||
102 | if (NULL != req_handle) | ||
103 | GNUNET_RPS_request_cancel (req_handle); | ||
104 | GNUNET_RPS_disconnect (rps_handle); | ||
105 | } | ||
106 | |||
107 | |||
108 | /** | ||
109 | * Callback called on receipt of reply. | ||
110 | * Prints replied PeerIDs. | ||
111 | * | ||
112 | * @param cls closure | ||
113 | * @param n number of peers | ||
114 | * @param recv_peers the received peers | ||
115 | */ | ||
116 | static void | ||
117 | reply_handle (void *cls, | ||
118 | uint64_t n, | ||
119 | const struct GNUNET_PeerIdentity *recv_peers) | ||
120 | { | ||
121 | uint64_t i; | ||
122 | |||
123 | req_handle = NULL; | ||
124 | for (i = 0; i < n; i++) | ||
125 | { | ||
126 | FPRINTF (stdout, "%s\n", | ||
127 | GNUNET_i2s_full (&recv_peers[i])); | ||
128 | } | ||
129 | ret = 0; | ||
130 | |||
131 | GNUNET_SCHEDULER_cancel (shutdown_task); | ||
132 | GNUNET_SCHEDULER_add_now (do_shutdown, NULL); | ||
133 | } | ||
134 | |||
135 | |||
136 | /** | ||
33 | * Main function that will be run by the scheduler. | 137 | * Main function that will be run by the scheduler. |
34 | * | 138 | * |
35 | * @param cls closure | 139 | * @param cls closure |
@@ -43,7 +147,27 @@ run (void *cls, | |||
43 | const char *cfgfile, | 147 | const char *cfgfile, |
44 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 148 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
45 | { | 149 | { |
46 | ret = 0; | 150 | static uint64_t num_peers; |
151 | |||
152 | rps_handle = GNUNET_RPS_connect (cfg); | ||
153 | |||
154 | if (NULL == peer_id) | ||
155 | { /* Request n PeerIDs */ | ||
156 | /* If number was specified use it, else request single peer. */ | ||
157 | num_peers = (NULL == args[0]) ? 1 : atoi (args[0]); | ||
158 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
159 | "Requesting %u PeerIDs\n", num_peers); | ||
160 | req_handle = GNUNET_RPS_request_peers (rps_handle, num_peers, reply_handle, NULL); | ||
161 | shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, | ||
162 | &do_shutdown, NULL); | ||
163 | } | ||
164 | else | ||
165 | { /* Seed PeerID */ | ||
166 | GNUNET_RPS_seed_ids (rps_handle, 1, peer_id); | ||
167 | FPRINTF (stdout, "Seeded PeerID %s\n", GNUNET_i2s_full (peer_id)); | ||
168 | ret = 0; | ||
169 | GNUNET_SCHEDULER_add_now (do_shutdown, NULL); | ||
170 | } | ||
47 | } | 171 | } |
48 | 172 | ||
49 | /** | 173 | /** |
@@ -56,15 +180,20 @@ run (void *cls, | |||
56 | int | 180 | int |
57 | main (int argc, char *const *argv) | 181 | main (int argc, char *const *argv) |
58 | { | 182 | { |
183 | const char helpstr[] = | ||
184 | "Get random GNUnet peers. If none is specified a single is requested."; | ||
59 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { | 185 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { |
186 | {'s', "seed", "PEER_ID", | ||
187 | gettext_noop ("Seed a PeerID"), | ||
188 | GNUNET_YES, &GNUNET_GETOPT_set_peerid, &peer_id}, | ||
60 | GNUNET_GETOPT_OPTION_END | 189 | GNUNET_GETOPT_OPTION_END |
61 | }; | 190 | }; |
62 | return (GNUNET_OK == | 191 | return (GNUNET_OK == |
63 | GNUNET_PROGRAM_run (argc, | 192 | GNUNET_PROGRAM_run (argc, |
64 | argv, | 193 | argv, |
65 | "gnunet-rps [options [value]]", | 194 | "gnunet-rps [NUMBER_OF_PEERS]", |
66 | gettext_noop | 195 | gettext_noop |
67 | ("rps"), | 196 | (helpstr), |
68 | options, &run, NULL)) ? ret : 1; | 197 | options, &run, NULL)) ? ret : 1; |
69 | } | 198 | } |
70 | 199 | ||