aboutsummaryrefslogtreecommitdiff
path: root/src/transport/gnunet-transport-connect-running-peers.c
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2011-09-08 08:39:26 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2011-09-08 08:39:26 +0000
commit7967fed20086cb4d46292890b15ab8942fa959f0 (patch)
tree6e4dafa25130ccef4af081c1374a9f8fe96166ea /src/transport/gnunet-transport-connect-running-peers.c
parent395d367a4bd12c17a1a4f31b4e3f02b78bc3450d (diff)
downloadgnunet-7967fed20086cb4d46292890b15ab8942fa959f0.tar.gz
gnunet-7967fed20086cb4d46292890b15ab8942fa959f0.zip
renaming and fixing
Diffstat (limited to 'src/transport/gnunet-transport-connect-running-peers.c')
-rw-r--r--src/transport/gnunet-transport-connect-running-peers.c391
1 files changed, 391 insertions, 0 deletions
diff --git a/src/transport/gnunet-transport-connect-running-peers.c b/src/transport/gnunet-transport-connect-running-peers.c
new file mode 100644
index 000000000..ac4c9cf34
--- /dev/null
+++ b/src/transport/gnunet-transport-connect-running-peers.c
@@ -0,0 +1,391 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20/**
21 * @file transport/gnunet-transport-connect-running-peers.c
22 * @brief utility to connect running peers
23 *
24 * This utility connects to running peers with each other
25 * The peers have to be started before, for example in the debugger with
26 * breakpoints set
27 */
28#include "platform.h"
29#include "gnunet_common.h"
30#include "gnunet_hello_lib.h"
31#include "gnunet_getopt_lib.h"
32#include "gnunet_os_lib.h"
33#include "gnunet_program_lib.h"
34#include "gnunet_scheduler_lib.h"
35#include "gnunet_transport_service.h"
36#include "transport.h"
37#include "transport-testing.h"
38
39#define VERBOSE GNUNET_NO
40
41#define VERBOSE_ARM GNUNET_NO
42
43#define START_ARM GNUNET_YES
44
45/**
46 * How long until we give up on transmitting the message?
47 */
48#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
49
50/**
51 * How long until we give up on transmitting the message?
52 */
53#define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
54
55#define MTYPE 12345
56
57static int ok;
58
59static GNUNET_SCHEDULER_TaskIdentifier die_task;
60
61static GNUNET_SCHEDULER_TaskIdentifier send_task;
62
63struct PeerContext *p1;
64
65struct PeerContext *p2;
66
67static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
68
69struct GNUNET_TRANSPORT_TransmitHandle *th;
70
71char *cfg_file_p1;
72
73char *cfg_file_p2;
74
75#if VERBOSE
76#define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
77#else
78#define OKPP do { ok++; } while (0)
79#endif
80
81void
82disconnect_from_peer (struct PeerContext *p)
83{
84 GNUNET_assert (p != NULL);
85 if (p->th != NULL)
86 GNUNET_TRANSPORT_disconnect (p->th);
87
88 if (p->cfg != NULL)
89 GNUNET_CONFIGURATION_destroy (p->cfg);
90 GNUNET_free (p);
91 p = NULL;
92}
93
94static void
95end ()
96{
97 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
98
99 if (send_task != GNUNET_SCHEDULER_NO_TASK)
100 GNUNET_SCHEDULER_cancel (send_task);
101
102 if (die_task != GNUNET_SCHEDULER_NO_TASK)
103 GNUNET_SCHEDULER_cancel (die_task);
104
105 if (th != NULL)
106 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
107 th = NULL;
108
109 if (p1 != NULL)
110 disconnect_from_peer (p1);
111 if (p2 != NULL)
112 disconnect_from_peer (p2);
113}
114
115static void
116end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
117{
118 die_task = GNUNET_SCHEDULER_NO_TASK;
119
120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
121
122 if (send_task != GNUNET_SCHEDULER_NO_TASK)
123 GNUNET_SCHEDULER_cancel (send_task);
124
125 if (cc != NULL)
126 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
127
128 if (th != NULL)
129 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
130 th = NULL;
131
132 if (p1 != NULL)
133 disconnect_from_peer (p1);
134 if (p2 != NULL)
135 disconnect_from_peer (p2);
136
137 ok = GNUNET_SYSERR;
138}
139
140
141static void
142notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
143 const struct GNUNET_MessageHeader *message,
144 const struct GNUNET_TRANSPORT_ATS_Information *ats,
145 uint32_t ats_count)
146{
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148 "Received message of type %d from peer %s!\n",
149 ntohs (message->type), GNUNET_i2s (peer));
150
151 if ((MTYPE == ntohs (message->type)) &&
152 (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
153 {
154 ok = 0;
155 end ();
156 }
157 else
158 {
159 GNUNET_break (0);
160 ok = 1;
161 end ();
162 }
163}
164
165
166static size_t
167notify_ready (void *cls, size_t size, void *buf)
168{
169 struct PeerContext *p = cls;
170 struct GNUNET_MessageHeader *hdr;
171
172 th = NULL;
173
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175 "Transmitting message with %u bytes to peer %s\n",
176 sizeof (struct GNUNET_MessageHeader), GNUNET_i2s (&p->id));
177 GNUNET_assert (size >= 256);
178
179 if (buf != NULL)
180 {
181 hdr = buf;
182 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
183 hdr->type = htons (MTYPE);
184 }
185 return sizeof (struct GNUNET_MessageHeader);
186}
187
188
189static void
190notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
191 const struct GNUNET_TRANSPORT_ATS_Information *ats,
192 uint32_t ats_count)
193{
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
195 GNUNET_i2s (peer), cls);
196}
197
198
199static void
200notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
201{
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
203 GNUNET_i2s (peer), cls);
204}
205
206
207
208
209struct PeerContext *
210connect_to_peer (const char *cfgname, GNUNET_TRANSPORT_ReceiveCallback rec,
211 GNUNET_TRANSPORT_NotifyConnect nc,
212 GNUNET_TRANSPORT_NotifyDisconnect nd, void *cb_cls)
213{
214 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
215 {
216 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "File not found: `%s' \n", cfgname);
217 return NULL;
218 }
219
220 struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext));
221
222 p->cfg = GNUNET_CONFIGURATION_create ();
223
224 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
225 if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
226 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME",
227 &p->servicehome);
228 if (NULL != p->servicehome)
229 GNUNET_DISK_directory_remove (p->servicehome);
230 /*
231 * p->arm_proc =
232 * GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
233 * "gnunet-service-arm", "-c", cfgname,
234 * #if VERBOSE_PEERS
235 * "-L", "DEBUG",
236 * #else
237 * "-L", "ERROR",
238 * #endif
239 * NULL);
240 */
241 p->nc = nc;
242 p->nd = nd;
243 p->rec = rec;
244 if (cb_cls != NULL)
245 p->cb_cls = cb_cls;
246 else
247 p->cb_cls = p;
248
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service `%s'\n",
250 p->servicehome);
251 p->th =
252 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, &notify_receive,
253 &notify_connect, &notify_disconnect);
254 GNUNET_assert (p->th != NULL);
255 return p;
256}
257
258static void
259sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
260{
261 send_task = GNUNET_SCHEDULER_NO_TASK;
262
263 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
264 return;
265
266 th = GNUNET_TRANSPORT_notify_transmit_ready (p1->th, &p2->id, 256, 0, TIMEOUT,
267 &notify_ready, &p1);
268}
269
270static void
271testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
272{
273 cc = NULL;
274 char *p1_c = strdup (GNUNET_i2s (&p1->id));
275
276 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers connected: %s <-> %s\n", p1_c,
277 GNUNET_i2s (&p2->id));
278 GNUNET_free (p1_c);
279
280 // FIXME: THIS IS REQUIRED! SEEMS TO BE A BUG!
281 send_task =
282 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &sendtask, NULL);
283}
284
285static void
286run (void *cls, char *const *args, const char *cfgfile,
287 const struct GNUNET_CONFIGURATION_Handle *cfg)
288{
289 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
290
291 p1 = connect_to_peer (cfg_file_p1, &notify_receive, &notify_connect,
292 &notify_disconnect, NULL);
293 p2 = connect_to_peer (cfg_file_p2, &notify_receive, &notify_connect,
294 &notify_disconnect, NULL);
295
296 if ((p1 == NULL) || (p2 == NULL))
297 {
298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
299 if (die_task != GNUNET_SCHEDULER_NO_TASK)
300 GNUNET_SCHEDULER_cancel (die_task);
301 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
302 return;
303 }
304
305 cc = GNUNET_TRANSPORT_TESTING_connect_peers (p1, p2, &testing_connect_cb,
306 NULL);
307}
308
309
310static int
311check ()
312{
313 static char *const argv[] = { "test-transport-api",
314 "-c",
315 "test_transport_api_data.conf",
316#if VERBOSE
317 "-L", "DEBUG",
318#endif
319 NULL
320 };
321 static struct GNUNET_GETOPT_CommandLineOption options[] = {
322 GNUNET_GETOPT_OPTION_END
323 };
324
325#if WRITECONFIG
326 setTransportOptions ("test_transport_api_data.conf");
327#endif
328 send_task = GNUNET_SCHEDULER_NO_TASK;
329
330 ok = 1;
331 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
332 "util_connect_running_peers", "nohelp", options, &run,
333 &ok);
334
335 return ok;
336}
337
338int
339main (int argc, char *argv[])
340{
341 int ret;
342
343 GNUNET_log_setup ("gnunet-transport-connect-running-peers",
344#if VERBOSE
345 "DEBUG",
346#else
347 "WARNING",
348#endif
349 NULL);
350
351
352 if (argc < 3)
353 {
354 fprintf (stderr,
355 "usage gnunet-transport-connect-running-peers <cfg_peer1> <cfg_peer2>\n");
356 return -1;
357 }
358 else
359 {
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configuration file 1: `%s' \n",
361 argv[1]);
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configuration file 2: `%s'\n",
363 argv[2]);
364 }
365
366 if (GNUNET_DISK_file_test (argv[1]) == GNUNET_NO)
367 {
368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "File not found: `%s' \n", argv[1]);
369 return -1;
370 }
371 if (GNUNET_DISK_file_test (argv[2]) == GNUNET_NO)
372 {
373 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "File not found: `%s' \n", argv[2]);
374 return -1;
375 }
376
377 GNUNET_asprintf (&cfg_file_p1, argv[1]);
378 GNUNET_asprintf (&cfg_file_p2, argv[2]);
379
380 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
381 "Trying to connect peers, press control-d to stop... \n",
382 argv[1]);
383
384 ret = check ();
385
386 GNUNET_free (cfg_file_p1);
387 GNUNET_free (cfg_file_p2);
388 return ret;
389}
390
391/* end of gnunet-transport-connect-running-peers.c */