-improve UDP logging
[oweals/gnunet.git] / src / ats / gnunet-service-ats.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011 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 ats/gnunet-service-ats.c
22  * @brief ats service
23  * @author Matthias Wachs
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet-service-ats.h"
29 #include "gnunet-service-ats_addresses.h"
30 #include "gnunet-service-ats_connectivity.h"
31 #include "gnunet-service-ats_feedback.h"
32 #include "gnunet-service-ats_normalization.h"
33 #include "gnunet-service-ats_performance.h"
34 #include "gnunet-service-ats_preferences.h"
35 #include "gnunet-service-ats_scheduling.h"
36 #include "gnunet-service-ats_reservations.h"
37 #include "gnunet-service-ats_plugins.h"
38 #include "ats.h"
39
40 /**
41  * Handle for statistics.
42  */
43 struct GNUNET_STATISTICS_Handle *GSA_stats;
44
45 /**
46  * Handle to the ATS server.
47  */
48 static struct GNUNET_SERVER_Handle *GSA_server;
49
50
51 /**
52  * We have received a `struct ClientStartMessage` from a client.  Find
53  * out which type of client it is and notify the respective subsystem.
54  *
55  * @param cls closure, unused
56  * @param client handle to the client
57  * @param message the start message
58  */
59 static void
60 handle_ats_start (void *cls,
61                   struct GNUNET_SERVER_Client *client,
62                   const struct GNUNET_MessageHeader *message)
63 {
64   const struct ClientStartMessage *msg =
65       (const struct ClientStartMessage *) message;
66   enum StartFlag flag;
67
68   flag = ntohl (msg->start_flag);
69   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
70               "Received ATS_START (%d) message\n",
71               (int) flag);
72   switch (flag)
73   {
74   case START_FLAG_SCHEDULING:
75     if (GNUNET_OK != GAS_scheduling_add_client (client))
76     {
77       GNUNET_SERVER_receive_done (client,
78                                   GNUNET_SYSERR);
79       return;
80     }
81     break;
82   case START_FLAG_PERFORMANCE_WITH_PIC:
83     GAS_performance_add_client (client,
84                                 flag);
85     break;
86   case START_FLAG_PERFORMANCE_NO_PIC:
87     GAS_performance_add_client (client,
88                                 flag);
89     break;
90   case START_FLAG_CONNECTION_SUGGESTION:
91     /* This client won't receive messages from us, no need to 'add' */
92     break;
93   default:
94     GNUNET_break (0);
95     GNUNET_SERVER_receive_done (client,
96                                 GNUNET_SYSERR);
97     return;
98   }
99   GNUNET_SERVER_receive_done (client,
100                               GNUNET_OK);
101 }
102
103
104 /**
105  * A client disconnected from us.  Tear down the local client
106  * record.
107  *
108  * @param cls unused
109  * @param client handle of the client
110  */
111 static void
112 client_disconnect_handler (void *cls,
113                            struct GNUNET_SERVER_Client *client)
114 {
115   if (NULL == client)
116     return;
117   GAS_scheduling_remove_client (client);
118   GAS_connectivity_remove_client (client);
119   GAS_preference_client_disconnect (client);
120 }
121
122
123 /**
124  * Task run during shutdown.
125  *
126  * @param cls unused
127  * @param tc unused
128  */
129 static void
130 cleanup_task (void *cls,
131               const struct GNUNET_SCHEDULER_TaskContext *tc)
132 {
133   GAS_connectivity_done ();
134   GAS_addresses_done ();
135   GAS_plugin_done ();
136   GAS_normalization_stop ();
137   GAS_scheduling_done ();
138   GAS_performance_done ();
139   GAS_preference_done ();
140   GAS_reservations_done ();
141   GNUNET_SERVER_disconnect_notify_cancel (GSA_server,
142                                           &client_disconnect_handler,
143                                           NULL);
144   if (NULL != GSA_stats)
145   {
146     GNUNET_STATISTICS_destroy (GSA_stats, GNUNET_NO);
147     GSA_stats = NULL;
148   }
149 }
150
151
152 /**
153  * Process template requests.
154  *
155  * @param cls closure
156  * @param server the initialized server
157  * @param cfg configuration to use
158  */
159 static void
160 run (void *cls,
161      struct GNUNET_SERVER_Handle *server,
162      const struct GNUNET_CONFIGURATION_Handle *cfg)
163 {
164   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
165     {&handle_ats_start, NULL,
166      GNUNET_MESSAGE_TYPE_ATS_START,
167      sizeof (struct ClientStartMessage)},
168     {&GAS_handle_request_address, NULL,
169      GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS,
170      sizeof (struct RequestAddressMessage)},
171     {&GAS_handle_request_address_cancel, NULL,
172      GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL,
173      sizeof (struct RequestAddressMessage)},
174     {&GAS_handle_request_address_list, NULL,
175      GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_REQUEST,
176      sizeof (struct AddressListRequestMessage)},
177     {&GAS_handle_address_add, NULL,
178      GNUNET_MESSAGE_TYPE_ATS_ADDRESS_ADD, 0},
179     {&GAS_handle_address_update, NULL,
180      GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE, 0},
181     {&GAS_handle_address_destroyed, NULL,
182      GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED,
183      sizeof (struct AddressDestroyedMessage) },
184     {&GAS_handle_reservation_request, NULL,
185      GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST,
186      sizeof (struct ReservationRequestMessage)},
187     {&GAS_handle_preference_change, NULL,
188      GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE, 0},
189     {&GAS_handle_feedback, NULL,
190      GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_FEEDBACK, 0},
191     {NULL, NULL, 0, 0}
192   };
193   GSA_server = server;
194   GSA_stats = GNUNET_STATISTICS_create ("ats", cfg);
195   GAS_reservations_init (server);
196   GAS_connectivity_init ();
197   GAS_preference_init ();
198   GAS_normalization_start ();
199   GAS_addresses_init (server);
200   if (GNUNET_OK !=
201       GAS_plugin_init (cfg))
202   {
203     GNUNET_break (0);
204     GAS_addresses_done ();
205     GAS_normalization_stop ();
206     GAS_reservations_done ();
207     GAS_connectivity_done ();
208     GAS_preference_done ();
209     if (NULL != GSA_stats)
210     {
211       GNUNET_STATISTICS_destroy (GSA_stats, GNUNET_NO);
212       GSA_stats = NULL;
213     }
214     return;
215   }
216   GAS_performance_init (server);
217   GAS_scheduling_init (server);
218
219   GNUNET_SERVER_disconnect_notify (server,
220                                    &client_disconnect_handler,
221                                    NULL);
222   GNUNET_SERVER_add_handlers (server, handlers);
223   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
224                                 &cleanup_task,
225                                 NULL);
226 }
227
228
229 /**
230  * The main function for the ats service.
231  *
232  * @param argc number of arguments from the command line
233  * @param argv command line arguments
234  * @return 0 ok, 1 on error
235  */
236 int
237 main (int argc, char *const *argv)
238 {
239   return (GNUNET_OK ==
240           GNUNET_SERVICE_run (argc, argv, "ats",
241                               GNUNET_SERVICE_OPTION_NONE,
242                               &run, NULL)) ? 0 : 1;
243 }
244
245 /* end of gnunet-service-ats.c */