doxygen
[oweals/gnunet.git] / src / ats-tool / gnunet-ats.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 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 /**
22  * @file ats-tool/gnunet-ats.c
23  * @brief ATS command line tool
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_ats_service.h"
29 #include "gnunet_transport_service.h"
30
31 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5)
32
33 /**
34  * Final status code.
35  */
36 static int ret;
37 static int results;
38 static int resolve_addresses_numeric;
39 static int monitor;
40
41 static struct GNUNET_ATS_PerformanceHandle *ph;
42
43 static struct GNUNET_CONFIGURATION_Handle *cfg;
44
45 GNUNET_SCHEDULER_TaskIdentifier end_task;
46
47 struct PendingResolutions
48 {
49   struct PendingResolutions *next;
50   struct PendingResolutions *prev;
51
52   struct GNUNET_HELLO_Address *address;
53   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
54   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
55
56   struct GNUNET_TRANSPORT_AddressToStringContext * tats_ctx;
57 };
58
59 struct PendingResolutions *head;
60 struct PendingResolutions *tail;
61
62 void transport_addr_to_str_cb (void *cls, const char *address)
63 {
64   struct PendingResolutions * pr = cls;
65   if (NULL != address)
66   {
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__));
70   }
71   else if (NULL != pr)
72   {
73       /* We're done */
74       GNUNET_CONTAINER_DLL_remove (head, tail, pr);
75       GNUNET_free (pr->address);
76       GNUNET_free (pr);
77   }
78
79 }
80
81 void ats_perf_cb (void *cls,
82                   const struct
83                   GNUNET_HELLO_Address *
84                   address,
85                   struct
86                   GNUNET_BANDWIDTH_Value32NBO
87                   bandwidth_out,
88                   struct
89                   GNUNET_BANDWIDTH_Value32NBO
90                   bandwidth_in,
91                   const struct
92                   GNUNET_ATS_Information *
93                   ats, uint32_t ats_count)
94 {
95   struct PendingResolutions * pr;
96
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);
104   results++;
105 }
106
107 void end (void *cls,
108           const struct GNUNET_SCHEDULER_TaskContext *tc)
109 {
110   struct PendingResolutions * pr;
111   struct PendingResolutions * next;
112   unsigned int pending;
113
114   GNUNET_ATS_performance_done (ph);
115   ph = NULL;
116
117   pending = 0;
118   next = head;
119   while (NULL != (pr = next))
120   {
121       next = 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);
125       GNUNET_free (pr);
126       pending ++;
127   }
128   if (0 < pending)
129     fprintf (stderr, _("%u address resolutions had a timeout\n"), pending);
130
131   fprintf (stderr, _("ATS returned results for %u addresses\n"), results);
132   ret = 0;
133 }
134
135 void testservice_ats (void *cls,
136                const struct GNUNET_SCHEDULER_TaskContext *tc)
137 {
138   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
139
140   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
141   {
142       FPRINTF (stderr, _("Service `%s' is not running\n"), "ats");
143       return;
144   }
145
146   results = 0;
147   ph = GNUNET_ATS_performance_init (cfg, ats_perf_cb, NULL);
148   if (NULL == ph)
149     fprintf (stderr, _("Cannot connect to ATS service, exiting...\n"));
150
151   if (GNUNET_NO == monitor)
152     end_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end, NULL);
153   else
154     end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end, NULL);
155   ret = 1;
156 }
157
158 /**
159  * Main function that will be run by the scheduler.
160  *
161  * @param cls closure
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
165  */
166 static void
167 run (void *cls, char *const *args, const char *cfgfile,
168      const struct GNUNET_CONFIGURATION_Handle *my_cfg)
169 {
170   cfg = (struct GNUNET_CONFIGURATION_Handle *) my_cfg;
171   GNUNET_CLIENT_service_test ("ats", cfg,
172                               TIMEOUT,
173                               &testservice_ats,
174                               (void *) cfg);
175 }
176
177
178 /**
179  * The main function.
180  *
181  * @param argc number of arguments from the command line
182  * @param argv command line arguments
183  * @return 0 ok, 1 on error
184  */
185 int
186 main (int argc, char *const *argv)
187 {
188   int res;
189   resolve_addresses_numeric = GNUNET_NO;
190   monitor = GNUNET_NO;
191
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
200   };
201
202   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
203     return 2;
204
205   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-ats",
206                               gettext_noop ("Print information about ATS state"), options, &run,
207                               NULL);
208   GNUNET_free ((void *) argv);
209
210   if (GNUNET_OK == res)
211     return ret;
212   else
213     return 1;
214
215 }
216
217 /* end of gnunet-ats.c */