2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file ats/gnunet-service-ats.c
24 * @author Matthias Wachs
25 * @author Christian Grothoff
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"
37 * Handle for statistics.
39 struct GNUNET_STATISTICS_Handle *GSA_stats;
42 * Handle to the ATS server.
44 static struct GNUNET_SERVER_Handle *GSA_server;
47 * Handle to the address state.
49 static struct GAS_Addresses_Handle *GSA_addresses;
53 * We have received a `struct ClientStartMessage` from a client. Find out which
54 * type of client it is and notify the respective subsystem.
56 * @param cls closure, unused
57 * @param client handle to the client
58 * @param message the start message
61 handle_ats_start (void *cls, struct GNUNET_SERVER_Client *client,
62 const struct GNUNET_MessageHeader *message)
64 const struct ClientStartMessage *msg =
65 (const struct ClientStartMessage *) message;
68 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
69 "Received `%s' message\n",
71 flag = ntohl (msg->start_flag);
74 case START_FLAG_SCHEDULING:
75 if (GNUNET_OK != GAS_scheduling_add_client (client))
77 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
81 case START_FLAG_PERFORMANCE_WITH_PIC:
82 GAS_performance_add_client (client, flag);
84 case START_FLAG_PERFORMANCE_NO_PIC:
85 GAS_performance_add_client (client, flag);
89 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
92 GNUNET_SERVER_receive_done (client, GNUNET_OK);
97 * A client disconnected from us. Tear down the local client
101 * @param client handle of the client
104 client_disconnect_handler (void *cls,
105 struct GNUNET_SERVER_Client *client)
109 GAS_scheduling_remove_client (client);
110 GAS_performance_remove_client (client);
115 * Task run during shutdown.
121 cleanup_task (void *cls,
122 const struct GNUNET_SCHEDULER_TaskContext *tc)
124 GAS_addresses_done (GSA_addresses);
125 GAS_scheduling_done ();
126 GAS_performance_done ();
127 GAS_reservations_done ();
128 GNUNET_SERVER_disconnect_notify_cancel (GSA_server,
129 &client_disconnect_handler,
131 if (NULL != GSA_stats)
133 GNUNET_STATISTICS_destroy (GSA_stats, GNUNET_NO);
140 * Process template requests.
143 * @param server the initialized server
144 * @param cfg configuration to use
148 struct GNUNET_SERVER_Handle *server,
149 const struct GNUNET_CONFIGURATION_Handle *cfg)
151 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
152 {&handle_ats_start, NULL,
153 GNUNET_MESSAGE_TYPE_ATS_START, sizeof (struct ClientStartMessage)},
154 {&GAS_handle_request_address, NULL,
155 GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS,
156 sizeof (struct RequestAddressMessage)},
157 {&GAS_handle_request_address_cancel, NULL,
158 GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL,
159 sizeof (struct RequestAddressMessage)},
160 {&GAS_handle_request_address_list, NULL,
161 GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_REQUEST,
162 sizeof (struct AddressListRequestMessage)},
163 {&GAS_handle_address_add, NULL,
164 GNUNET_MESSAGE_TYPE_ATS_ADDRESS_ADD, 0},
165 {&GAS_handle_address_update, NULL,
166 GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE, 0},
167 {&GAS_handle_address_in_use, NULL,
168 GNUNET_MESSAGE_TYPE_ATS_ADDRESS_IN_USE, 0},
169 {&GAS_handle_address_destroyed, NULL,
170 GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED, 0},
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 {&GAS_handle_reset_backoff, NULL,
179 GNUNET_MESSAGE_TYPE_ATS_RESET_BACKOFF,
180 sizeof (struct ResetBackoffMessage)},
184 GSA_stats = GNUNET_STATISTICS_create ("ats", cfg);
185 GAS_reservations_init ();
186 GSA_addresses = GAS_addresses_init (cfg, GSA_stats);
187 GAS_performance_init (server, GSA_addresses);
188 GAS_scheduling_init (server, GSA_addresses);
190 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_handler, NULL);
191 GNUNET_SERVER_add_handlers (server, handlers);
192 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
198 * The main function for the ats service.
200 * @param argc number of arguments from the command line
201 * @param argv command line arguments
202 * @return 0 ok, 1 on error
205 main (int argc, char *const *argv)
208 GNUNET_SERVICE_run (argc, argv, "ats",
209 GNUNET_SERVICE_OPTION_NONE,
210 &run, NULL)) ? 0 : 1;
213 /* end of gnunet-service-ats.c */