aboutsummaryrefslogtreecommitdiff
path: root/src/rps/gnunet-rps.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rps/gnunet-rps.c')
-rw-r--r--src/rps/gnunet-rps.c227
1 files changed, 115 insertions, 112 deletions
diff --git a/src/rps/gnunet-rps.c b/src/rps/gnunet-rps.c
index 84043341b..6c1c96cc9 100644
--- a/src/rps/gnunet-rps.c
+++ b/src/rps/gnunet-rps.c
@@ -68,13 +68,13 @@ static uint64_t num_view_updates;
68 * @param cls NULL 68 * @param cls NULL
69 */ 69 */
70static void 70static void
71do_shutdown(void *cls) 71do_shutdown (void *cls)
72{ 72{
73 (void)cls; 73 (void) cls;
74 74
75 if (NULL != req_handle) 75 if (NULL != req_handle)
76 GNUNET_RPS_request_cancel(req_handle); 76 GNUNET_RPS_request_cancel (req_handle);
77 GNUNET_RPS_disconnect(rps_handle); 77 GNUNET_RPS_disconnect (rps_handle);
78} 78}
79 79
80 80
@@ -87,23 +87,23 @@ do_shutdown(void *cls)
87 * @param recv_peers the received peers 87 * @param recv_peers the received peers
88 */ 88 */
89static void 89static void
90reply_handle(void *cls, 90reply_handle (void *cls,
91 uint64_t n, 91 uint64_t n,
92 const struct GNUNET_PeerIdentity *recv_peers) 92 const struct GNUNET_PeerIdentity *recv_peers)
93{ 93{
94 uint64_t i; 94 uint64_t i;
95 95
96 (void)cls; 96 (void) cls;
97 97
98 req_handle = NULL; 98 req_handle = NULL;
99 for (i = 0; i < n; i++) 99 for (i = 0; i < n; i++)
100 { 100 {
101 fprintf(stdout, "%s\n", 101 fprintf (stdout, "%s\n",
102 GNUNET_i2s_full(&recv_peers[i])); 102 GNUNET_i2s_full (&recv_peers[i]));
103 } 103 }
104 ret = 0; 104 ret = 0;
105 105
106 GNUNET_SCHEDULER_shutdown(); 106 GNUNET_SCHEDULER_shutdown ();
107} 107}
108 108
109/** 109/**
@@ -114,34 +114,34 @@ reply_handle(void *cls,
114 * @param recv_peers the received peers 114 * @param recv_peers the received peers
115 */ 115 */
116static void 116static void
117view_update_handle(void *cls, 117view_update_handle (void *cls,
118 uint64_t n, 118 uint64_t n,
119 const struct GNUNET_PeerIdentity *recv_peers) 119 const struct GNUNET_PeerIdentity *recv_peers)
120{ 120{
121 uint64_t i; 121 uint64_t i;
122 122
123 (void)cls; 123 (void) cls;
124 124
125 if (0 == n) 125 if (0 == n)
126 { 126 {
127 fprintf(stdout, "Empty view\n"); 127 fprintf (stdout, "Empty view\n");
128 } 128 }
129 req_handle = NULL; 129 req_handle = NULL;
130 for (i = 0; i < n; i++) 130 for (i = 0; i < n; i++)
131 { 131 {
132 fprintf(stdout, "%s\n", 132 fprintf (stdout, "%s\n",
133 GNUNET_i2s_full(&recv_peers[i])); 133 GNUNET_i2s_full (&recv_peers[i]));
134 } 134 }
135 135
136 if (1 == num_view_updates) 136 if (1 == num_view_updates)
137 { 137 {
138 ret = 0; 138 ret = 0;
139 GNUNET_SCHEDULER_shutdown(); 139 GNUNET_SCHEDULER_shutdown ();
140 } 140 }
141 else if (1 < num_view_updates) 141 else if (1 < num_view_updates)
142 { 142 {
143 num_view_updates--; 143 num_view_updates--;
144 } 144 }
145} 145}
146 146
147 147
@@ -152,24 +152,24 @@ view_update_handle(void *cls,
152 * @param recv_peers the received peers 152 * @param recv_peers the received peers
153 */ 153 */
154static void 154static void
155stream_input_handle(void *cls, 155stream_input_handle (void *cls,
156 uint64_t num_peers, 156 uint64_t num_peers,
157 const struct GNUNET_PeerIdentity *recv_peers) 157 const struct GNUNET_PeerIdentity *recv_peers)
158{ 158{
159 uint64_t i; 159 uint64_t i;
160 160
161 (void)cls; 161 (void) cls;
162 162
163 if (0 == num_peers) 163 if (0 == num_peers)
164 { 164 {
165 fprintf(stdout, "No peer was returned\n"); 165 fprintf (stdout, "No peer was returned\n");
166 } 166 }
167 req_handle = NULL; 167 req_handle = NULL;
168 for (i = 0; i < num_peers; i++) 168 for (i = 0; i < num_peers; i++)
169 { 169 {
170 fprintf(stdout, "%s\n", 170 fprintf (stdout, "%s\n",
171 GNUNET_i2s_full(&recv_peers[i])); 171 GNUNET_i2s_full (&recv_peers[i]));
172 } 172 }
173} 173}
174 174
175 175
@@ -182,69 +182,71 @@ stream_input_handle(void *cls,
182 * @param cfg configuration 182 * @param cfg configuration
183 */ 183 */
184static void 184static void
185run(void *cls, 185run (void *cls,
186 char *const *args, 186 char *const *args,
187 const char *cfgfile, 187 const char *cfgfile,
188 const struct GNUNET_CONFIGURATION_Handle *cfg) 188 const struct GNUNET_CONFIGURATION_Handle *cfg)
189{ 189{
190 static uint64_t num_peers; 190 static uint64_t num_peers;
191 static struct GNUNET_PeerIdentity zero_pid; 191 static struct GNUNET_PeerIdentity zero_pid;
192 192
193 (void)cls; 193 (void) cls;
194 (void)cfgfile; 194 (void) cfgfile;
195 195
196 rps_handle = GNUNET_RPS_connect(cfg); 196 rps_handle = GNUNET_RPS_connect (cfg);
197 if (NULL == rps_handle) 197 if (NULL == rps_handle)
198 { 198 {
199 fprintf(stderr, "Failed to connect to the rps service\n"); 199 fprintf (stderr, "Failed to connect to the rps service\n");
200 return; 200 return;
201 } 201 }
202 202
203 if ((0 == memcmp(&zero_pid, &peer_id, sizeof(peer_id))) && 203 if ((0 == memcmp (&zero_pid, &peer_id, sizeof(peer_id))) &&
204 (!view_update) && 204 (! view_update) &&
205 (!stream_input)) 205 (! stream_input))
206 { /* Request n PeerIDs */ 206 { /* Request n PeerIDs */
207 /* If number was specified use it, else request single peer. */ 207 /* If number was specified use it, else request single peer. */
208 if (NULL == args[0] || 208 if ((NULL == args[0]) ||
209 0 == sscanf(args[0], "%lu", &num_peers)) 209 (0 == sscanf (args[0], "%lu", &num_peers)) )
210 { 210 {
211 num_peers = 1; 211 num_peers = 1;
212 }
213 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
214 "Requesting %" PRIu64 " PeerIDs\n", num_peers);
215 req_handle = GNUNET_RPS_request_peers(rps_handle, num_peers, reply_handle, NULL);
216 GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL);
217 } 212 }
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
214 "Requesting %" PRIu64 " PeerIDs\n", num_peers);
215 req_handle = GNUNET_RPS_request_peers (rps_handle, num_peers, reply_handle,
216 NULL);
217 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
218 }
218 else if (view_update) 219 else if (view_update)
220 {
221 /* Get updates of view */
222 if ((NULL == args[0]) ||
223 (0 == sscanf (args[0], "%lu", &num_view_updates)) )
219 { 224 {
220 /* Get updates of view */ 225 num_view_updates = 0;
221 if (NULL == args[0] ||
222 0 == sscanf(args[0], "%lu", &num_view_updates))
223 {
224 num_view_updates = 0;
225 }
226 GNUNET_RPS_view_request(rps_handle, num_view_updates, view_update_handle, NULL);
227 if (0 != num_view_updates)
228 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
229 "Requesting %" PRIu64 " view updates\n", num_view_updates);
230 else
231 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
232 "Requesting continuous view updates\n");
233 GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL);
234 } 226 }
227 GNUNET_RPS_view_request (rps_handle, num_view_updates, view_update_handle,
228 NULL);
229 if (0 != num_view_updates)
230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
231 "Requesting %" PRIu64 " view updates\n", num_view_updates);
232 else
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234 "Requesting continuous view updates\n");
235 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
236 }
235 else if (stream_input) 237 else if (stream_input)
236 { 238 {
237 /* Get updates of view */ 239 /* Get updates of view */
238 GNUNET_RPS_stream_request(rps_handle, stream_input_handle, NULL); 240 GNUNET_RPS_stream_request (rps_handle, stream_input_handle, NULL);
239 GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL); 241 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
240 } 242 }
241 else 243 else
242 { /* Seed PeerID */ 244 { /* Seed PeerID */
243 GNUNET_RPS_seed_ids(rps_handle, 1, &peer_id); 245 GNUNET_RPS_seed_ids (rps_handle, 1, &peer_id);
244 fprintf(stdout, "Seeded PeerID %s\n", GNUNET_i2s_full(&peer_id)); 246 fprintf (stdout, "Seeded PeerID %s\n", GNUNET_i2s_full (&peer_id));
245 ret = 0; 247 ret = 0;
246 GNUNET_SCHEDULER_add_now(&do_shutdown, NULL); 248 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
247 } 249 }
248} 250}
249 251
250/** 252/**
@@ -255,34 +257,35 @@ run(void *cls,
255 * @return 0 ok, 1 on error 257 * @return 0 ok, 1 on error
256 */ 258 */
257int 259int
258main(int argc, char *const *argv) 260main (int argc, char *const *argv)
259{ 261{
260 const char helpstr[] = 262 const char helpstr[] =
261 "Get random GNUnet peers. If none is specified a single is requested."; 263 "Get random GNUnet peers. If none is specified a single is requested.";
262 struct GNUNET_GETOPT_CommandLineOption options[] = { 264 struct GNUNET_GETOPT_CommandLineOption options[] = {
263 GNUNET_GETOPT_option_base32_auto('s', 265 GNUNET_GETOPT_option_base32_auto ('s',
264 "seed", 266 "seed",
265 "PEER_ID", 267 "PEER_ID",
266 gettext_noop("Seed a PeerID"), 268 gettext_noop ("Seed a PeerID"),
267 &peer_id), 269 &peer_id),
268 GNUNET_GETOPT_option_flag('V', 270 GNUNET_GETOPT_option_flag ('V',
269 "view", 271 "view",
270 gettext_noop("Get updates of view (0 for infinite updates)"), 272 gettext_noop (
271 &view_update), 273 "Get updates of view (0 for infinite updates)"),
272 GNUNET_GETOPT_option_flag('S', 274 &view_update),
273 "stream", 275 GNUNET_GETOPT_option_flag ('S',
274 gettext_noop("Get peers from biased stream"), 276 "stream",
275 &stream_input), 277 gettext_noop ("Get peers from biased stream"),
278 &stream_input),
276 GNUNET_GETOPT_OPTION_END 279 GNUNET_GETOPT_OPTION_END
277 }; 280 };
278 281
279 return (GNUNET_OK == 282 return (GNUNET_OK ==
280 GNUNET_PROGRAM_run(argc, 283 GNUNET_PROGRAM_run (argc,
281 argv, 284 argv,
282 "gnunet-rps [NUMBER_OF_PEERS]", 285 "gnunet-rps [NUMBER_OF_PEERS]",
283 gettext_noop 286 gettext_noop
284 (helpstr), 287 (helpstr),
285 options, &run, NULL)) ? ret : 1; 288 options, &run, NULL)) ? ret : 1;
286} 289}
287 290
288/* end of gnunet-rps.c */ 291/* end of gnunet-rps.c */