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_ats_service.h"
30 #include "gnunet-service-ats.h"
31 #include "gnunet-service-ats_addresses.h"
32 #include "gnunet-service-ats_performance.h"
33 #include "gnunet-service-ats_scheduling.h"
34 #include "gnunet-service-ats_reservations.h"
38 * Handle for statistics.
40 struct GNUNET_STATISTICS_Handle *GSA_stats;
43 * We have received a 'ClientStartMessage' from a client. Find out which
44 * type of client it is and notify the respective subsystem.
46 * @param client handle to the client
47 * @param message the start message
50 handle_ats_start (void *cls, struct GNUNET_SERVER_Client *client,
51 const struct GNUNET_MessageHeader *message)
53 const struct ClientStartMessage *msg =
54 (const struct ClientStartMessage *) message;
57 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ATS_START");
58 flag = ntohl (msg->start_flag);
61 case START_FLAG_SCHEDULING:
62 if (GNUNET_OK != GAS_scheduling_add_client (client))
64 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
68 case START_FLAG_PERFORMANCE_WITH_PIC:
69 GAS_performance_add_client (client, flag);
71 case START_FLAG_PERFORMANCE_NO_PIC:
72 GAS_performance_add_client (client, flag);
76 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
79 GNUNET_SERVER_receive_done (client, GNUNET_OK);
84 * A client disconnected from us. Tear down the local client
88 * @param client handle of the client
91 client_disconnect_handler (void *cls, struct GNUNET_SERVER_Client *client)
95 GAS_scheduling_remove_client (client);
96 GAS_performance_remove_client (client);
101 * Task run during shutdown.
107 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
109 GAS_addresses_done ();
110 GAS_scheduling_done ();
111 GAS_performance_done ();
112 GAS_reservations_done ();
113 if (NULL != GSA_stats)
115 GNUNET_STATISTICS_destroy (GSA_stats, GNUNET_NO);
122 * Process template requests.
125 * @param server the initialized server
126 * @param cfg configuration to use
129 run (void *cls, struct GNUNET_SERVER_Handle *server,
130 const struct GNUNET_CONFIGURATION_Handle *cfg)
132 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
133 {&handle_ats_start, NULL,
134 GNUNET_MESSAGE_TYPE_ATS_START, sizeof (struct ClientStartMessage)},
135 {&GAS_handle_request_address, NULL,
136 GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS,
137 sizeof (struct RequestAddressMessage)},
138 {&GAS_handle_address_update, NULL,
139 GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE, 0},
140 {&GAS_handle_address_in_use, NULL,
141 GNUNET_MESSAGE_TYPE_ATS_ADDRESS_IN_USE, 0},
142 {&GAS_handle_address_destroyed, NULL,
143 GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED, 0},
144 {&GAS_handle_reservation_request, NULL,
145 GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST,
146 sizeof (struct ReservationRequestMessage)},
147 {&GAS_handle_preference_change, NULL,
148 GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE, 0},
151 GSA_stats = GNUNET_STATISTICS_create ("ats", cfg);
152 GAS_reservations_init ();
153 GAS_performance_init (server);
154 GAS_scheduling_init (server);
155 GAS_addresses_init (cfg);
156 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_handler, NULL);
157 GNUNET_SERVER_add_handlers (server, handlers);
158 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
164 * The main function for the ats service.
166 * @param argc number of arguments from the command line
167 * @param argv command line arguments
168 * @return 0 ok, 1 on error
171 main (int argc, char *const *argv)
174 GNUNET_SERVICE_run (argc, argv, "ats", GNUNET_SERVICE_OPTION_NONE,
175 &run, NULL)) ? 0 : 1;
178 /* end of gnunet-service-ats.c */