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