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