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