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