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