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