2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file transport/gnunet-transport-connect-running-peers.c
22 * @brief utility to connect running peers
24 * This utility connects to running peers with each other
25 * The peers have to be started before, for example in the debugger with
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"
39 #define VERBOSE GNUNET_NO
41 #define VERBOSE_ARM GNUNET_NO
43 #define START_ARM GNUNET_YES
46 * How long until we give up on transmitting the message?
48 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
51 * How long until we give up on transmitting the message?
53 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
59 static GNUNET_SCHEDULER_TaskIdentifier die_task;
61 static GNUNET_SCHEDULER_TaskIdentifier send_task;
63 struct PeerContext *p1;
65 struct PeerContext *p2;
67 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
69 struct GNUNET_TRANSPORT_TransmitHandle *th;
76 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
78 #define OKPP do { ok++; } while (0)
82 disconnect_from_peer (struct PeerContext *p)
84 GNUNET_assert (p != NULL);
86 GNUNET_TRANSPORT_disconnect (p->th);
89 GNUNET_CONFIGURATION_destroy (p->cfg);
95 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
97 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
99 if (send_task != GNUNET_SCHEDULER_NO_TASK)
100 GNUNET_SCHEDULER_cancel (send_task);
102 if (die_task != GNUNET_SCHEDULER_NO_TASK)
103 GNUNET_SCHEDULER_cancel (die_task);
106 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
110 disconnect_from_peer (p1);
112 disconnect_from_peer (p2);
116 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
118 die_task = GNUNET_SCHEDULER_NO_TASK;
120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
122 if (send_task != GNUNET_SCHEDULER_NO_TASK)
123 GNUNET_SCHEDULER_cancel (send_task);
126 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
129 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
133 disconnect_from_peer (p1);
135 disconnect_from_peer (p2);
142 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
143 const struct GNUNET_MessageHeader *message,
144 const struct GNUNET_TRANSPORT_ATS_Information *ats,
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148 "Received message of type %d from peer %s!\n",
149 ntohs (message->type), GNUNET_i2s (peer));
151 if ((MTYPE == ntohs (message->type)) &&
152 (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
167 notify_ready (void *cls, size_t size, void *buf)
169 struct PeerContext *p = cls;
170 struct GNUNET_MessageHeader *hdr;
174 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
175 "Press <q> to quit or any key to transmit a message\n");
180 GNUNET_SCHEDULER_add_now(&end, NULL);
184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
185 "Transmitting message with %u bytes to peer %s\n",
186 sizeof (struct GNUNET_MessageHeader), GNUNET_i2s (&p->id));
187 GNUNET_assert (size >= 256);
192 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
193 hdr->type = htons (MTYPE);
196 th = GNUNET_TRANSPORT_notify_transmit_ready (p1->th, &p2->id, 256, 0, TIMEOUT,
199 return sizeof (struct GNUNET_MessageHeader);
204 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
205 const struct GNUNET_TRANSPORT_ATS_Information *ats,
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
209 GNUNET_i2s (peer), cls);
214 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
217 GNUNET_i2s (peer), cls);
224 connect_to_peer (const char *cfgname, GNUNET_TRANSPORT_ReceiveCallback rec,
225 GNUNET_TRANSPORT_NotifyConnect nc,
226 GNUNET_TRANSPORT_NotifyDisconnect nd, void *cb_cls)
228 if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "File not found: `%s' \n", cfgname);
234 struct PeerContext *p = GNUNET_malloc (sizeof (struct PeerContext));
236 p->cfg = GNUNET_CONFIGURATION_create ();
238 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
239 if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
240 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME",
242 if (NULL != p->servicehome)
243 GNUNET_DISK_directory_remove (p->servicehome);
246 * GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
247 * "gnunet-service-arm", "-c", cfgname,
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service `%s'\n",
266 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive,
267 ¬ify_connect, ¬ify_disconnect);
268 GNUNET_assert (p->th != NULL);
273 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
275 send_task = GNUNET_SCHEDULER_NO_TASK;
277 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
280 th = GNUNET_TRANSPORT_notify_transmit_ready (p1->th, &p2->id, 256, 0, TIMEOUT,
285 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
288 char *p1_c = strdup (GNUNET_i2s (&p1->id));
290 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers connected: %s <-> %s\n", p1_c,
291 GNUNET_i2s (&p2->id));
294 // FIXME: THIS IS REQUIRED! SEEMS TO BE A BUG!
297 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &sendtask, NULL);
301 run (void *cls, char *const *args, const char *cfgfile,
302 const struct GNUNET_CONFIGURATION_Handle *cfg)
304 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
306 p1 = connect_to_peer (cfg_file_p1, ¬ify_receive, ¬ify_connect,
307 ¬ify_disconnect, NULL);
308 p2 = connect_to_peer (cfg_file_p2, ¬ify_receive, ¬ify_connect,
309 ¬ify_disconnect, NULL);
311 if ((p1 == NULL) || (p2 == NULL))
313 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
314 if (die_task != GNUNET_SCHEDULER_NO_TASK)
315 GNUNET_SCHEDULER_cancel (die_task);
316 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
320 cc = GNUNET_TRANSPORT_TESTING_connect_peers (p1, p2, &testing_connect_cb,
328 static char *const argv[] = { "test-transport-api",
330 "test_transport_api_data.conf",
336 static struct GNUNET_GETOPT_CommandLineOption options[] = {
337 GNUNET_GETOPT_OPTION_END
341 setTransportOptions ("test_transport_api_data.conf");
343 send_task = GNUNET_SCHEDULER_NO_TASK;
346 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
347 "util_connect_running_peers", "nohelp", options, &run,
354 main (int argc, char *argv[])
358 GNUNET_log_setup ("gnunet-transport-connect-running-peers",
370 "usage gnunet-transport-connect-running-peers <cfg_peer1> <cfg_peer2>\n");
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configuration file 1: `%s' \n",
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configuration file 2: `%s'\n",
381 if (GNUNET_DISK_file_test (argv[1]) == GNUNET_NO)
383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "File not found: `%s' \n", argv[1]);
386 if (GNUNET_DISK_file_test (argv[2]) == GNUNET_NO)
388 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "File not found: `%s' \n", argv[2]);
392 GNUNET_asprintf (&cfg_file_p1, argv[1]);
393 GNUNET_asprintf (&cfg_file_p2, argv[2]);
395 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
396 "Trying to connect peers, press control-d to stop... \n",
401 GNUNET_free (cfg_file_p1);
402 GNUNET_free (cfg_file_p2);
406 /* end of gnunet-transport-connect-running-peers.c */