2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 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.
22 * @file ats-tool/gnunet-ats.c
23 * @brief ATS command line tool
24 * @author Matthias Wachs
27 #include "gnunet_util_lib.h"
28 #include "gnunet_ats_service.h"
29 #include "gnunet_transport_service.h"
31 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5)
38 static int resolve_addresses_numeric;
41 static struct GNUNET_ATS_PerformanceHandle *ph;
43 static struct GNUNET_CONFIGURATION_Handle *cfg;
45 GNUNET_SCHEDULER_TaskIdentifier end_task;
47 struct PendingResolutions
49 struct PendingResolutions *next;
50 struct PendingResolutions *prev;
52 struct GNUNET_HELLO_Address *address;
53 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
54 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
56 struct GNUNET_TRANSPORT_AddressToStringContext * tats_ctx;
59 struct PendingResolutions *head;
60 struct PendingResolutions *tail;
62 void transport_addr_to_str_cb (void *cls, const char *address)
64 struct PendingResolutions * pr = cls;
67 fprintf (stderr, _("Peer `%s' plugin `%s', address `%s', bandwidth out: %u Bytes/s, bandwidth in %u Bytes/s\n"),
68 GNUNET_i2s (&pr->address->peer), pr->address->transport_name, address,
69 ntohl (pr->bandwidth_out.value__), ntohl (pr->bandwidth_in.value__));
74 GNUNET_CONTAINER_DLL_remove (head, tail, pr);
75 GNUNET_free (pr->address);
81 void ats_perf_cb (void *cls,
83 GNUNET_HELLO_Address *
86 GNUNET_BANDWIDTH_Value32NBO
89 GNUNET_BANDWIDTH_Value32NBO
92 GNUNET_ATS_Information *
93 ats, uint32_t ats_count)
95 struct PendingResolutions * pr;
97 pr = GNUNET_malloc (sizeof (struct PendingResolutions));
98 pr->address = GNUNET_HELLO_address_copy (address);
99 pr->bandwidth_in = bandwidth_in;
100 pr->bandwidth_out = bandwidth_out;
101 pr->tats_ctx = GNUNET_TRANSPORT_address_to_string(cfg, address,
102 resolve_addresses_numeric, GNUNET_TIME_UNIT_FOREVER_REL, transport_addr_to_str_cb, pr);
103 GNUNET_CONTAINER_DLL_insert (head, tail, pr);
108 const struct GNUNET_SCHEDULER_TaskContext *tc)
110 struct PendingResolutions * pr;
111 struct PendingResolutions * next;
112 unsigned int pending;
114 GNUNET_ATS_performance_done (ph);
119 while (NULL != (pr = next))
122 GNUNET_CONTAINER_DLL_remove (head, tail, pr);
123 GNUNET_TRANSPORT_address_to_string_cancel (pr->tats_ctx);
124 GNUNET_free (pr->address);
129 fprintf (stderr, _("%u address resolutions had a timeout\n"), pending);
131 fprintf (stderr, _("ATS returned results for %u addresses\n"), results);
135 void testservice_ats (void *cls,
136 const struct GNUNET_SCHEDULER_TaskContext *tc)
138 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
140 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
142 FPRINTF (stderr, _("Service `%s' is not running\n"), "ats");
147 ph = GNUNET_ATS_performance_init (cfg, ats_perf_cb, NULL);
149 fprintf (stderr, _("Cannot connect to ATS service, exiting...\n"));
151 if (GNUNET_NO == monitor)
152 end_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end, NULL);
154 end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end, NULL);
159 * Main function that will be run by the scheduler.
162 * @param args remaining command-line arguments
163 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
164 * @param my_cfg configuration
167 run (void *cls, char *const *args, const char *cfgfile,
168 const struct GNUNET_CONFIGURATION_Handle *my_cfg)
170 cfg = (struct GNUNET_CONFIGURATION_Handle *) my_cfg;
171 GNUNET_CLIENT_service_test ("ats", cfg,
181 * @param argc number of arguments from the command line
182 * @param argv command line arguments
183 * @return 0 ok, 1 on error
186 main (int argc, char *const *argv)
189 resolve_addresses_numeric = GNUNET_NO;
192 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
193 {'n', "numeric", NULL,
194 gettext_noop ("do not resolve hostnames"),
195 0, &GNUNET_GETOPT_set_one, &resolve_addresses_numeric},
196 {'m', "monitor", NULL,
197 gettext_noop ("monitor mode"),
198 0, &GNUNET_GETOPT_set_one, &monitor},
199 GNUNET_GETOPT_OPTION_END
202 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
205 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-ats",
206 gettext_noop ("Print information about ATS state"), options, &run,
208 GNUNET_free ((void *) argv);
210 if (GNUNET_OK == res)
217 /* end of gnunet-ats.c */