aca877d7b9c743172e629cf499bd850cadbcfc3c
[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_performance.h"
32 #include "ats.h"
33
34 struct ATS_Address
35 {
36   struct GNUNET_PeerIdentity peer;
37
38   size_t addr_len;
39
40   uint32_t session_id;
41
42   uint32_t ats_count;
43
44   void * addr;
45
46   char * plugin;
47
48   struct GNUNET_TRANSPORT_ATS_Information * ats;
49 };
50
51 static struct GNUNET_CONTAINER_MultiHashMap * addresses;
52
53
54
55 static void
56 handle_ats_start (void *cls, struct GNUNET_SERVER_Client *client,
57                   const struct GNUNET_MessageHeader *message)
58 {
59   const struct ClientStartMessage * msg = (const struct ClientStartMessage *) message;
60
61   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
62               "Received `%s' message\n",
63               "ATS_START");
64   switch (ntohl (msg->start_flag))
65   {
66   case START_FLAG_SCHEDULING:
67     GAS_add_scheduling_client (client);
68     break;
69   case START_FLAG_PERFORMANCE_WITH_PIC:
70     GAS_add_performance_client (client);
71     break;
72   case START_FLAG_PERFORMANCE_NO_PIC:
73     break;
74   default:
75     GNUNET_break (0);
76     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
77     return;
78   }
79   GNUNET_SERVER_receive_done (client, GNUNET_OK);    
80 }
81
82
83 struct CompareAddressContext
84 {
85   struct ATS_Address * search;
86   struct ATS_Address * result;
87 };
88
89 int compare_address_it (void *cls,
90                const GNUNET_HashCode * key,
91                void *value)
92 {
93   struct CompareAddressContext * cac = cls;
94   struct ATS_Address * aa = (struct ATS_Address *) value;
95   if (0 == strcmp(aa->plugin, cac->search->plugin))
96   {
97     if ((aa->addr_len == cac->search->addr_len) &&
98         (0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)))
99       cac->result = aa;
100     return GNUNET_NO;
101   }
102   return GNUNET_YES;
103 }
104
105
106 static int 
107 free_address_it (void *cls,
108                  const GNUNET_HashCode * key,
109                  void *value)
110 {
111   struct ATS_Address * aa = cls;
112   GNUNET_free (aa);
113   return GNUNET_OK;
114 }
115
116
117
118
119
120 /**
121  * Task run during shutdown.
122  *
123  * @param cls unused
124  * @param tc unused
125  */
126 static void
127 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
128 {
129   GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
130   GNUNET_CONTAINER_multihashmap_destroy (addresses);
131 }
132
133
134 /**
135  * Process template requests.
136  *
137  * @param cls closure
138  * @param server the initialized server
139  * @param cfg configuration to use
140  */
141 static void
142 run (void *cls, struct GNUNET_SERVER_Handle *server,
143      const struct GNUNET_CONFIGURATION_Handle *cfg)
144 {
145   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
146     { &handle_ats_start, NULL, 
147       GNUNET_MESSAGE_TYPE_ATS_START, sizeof (struct ClientStartMessage)},
148     { &GAS_handle_request_address, NULL,
149       GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS, sizeof (struct RequestAddressMessage)},
150     { &GAS_handle_address_update, NULL, 
151       GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE, 0},
152     { &GAS_handle_address_destroyed, NULL, 
153       GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED, 0},
154     { &GAS_handle_reservation_request, NULL, 
155       GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST, sizeof (struct ReservationRequestMessage)},
156     { &GAS_handle_preference_change, NULL, 
157       GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE, 0},
158     {NULL, NULL, 0, 0}
159   };
160   addresses = GNUNET_CONTAINER_multihashmap_create(128);
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",
179                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
180 }
181
182 /* end of gnunet-service-ats.c */