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