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