cleanup
[oweals/gnunet.git] / src / ats / gnunet-service-ats_performance.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_performance.c
23  * @brief ats service, interaction with 'performance' API
24  * @author Matthias Wachs
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet-service-ats_performance.h"
29 #include "gnunet-service-ats_reservations.h"
30 #include "ats.h"
31
32
33 /**
34  * We keep clients that are interested in performance in a linked list.
35  */
36 struct PerformanceClient
37 {
38   /**
39    * Next in doubly-linked list.
40    */
41   struct PerformanceClient * next;
42
43   /**
44    * Previous in doubly-linked list.
45    */
46   struct PerformanceClient * prev;
47   
48   /**
49    * Actual handle to the client.
50    */
51   struct GNUNET_SERVER_Client *client;
52
53   /**
54    * Options for the client.
55    */
56   enum StartFlag flag;
57
58 };
59
60
61 /**
62  * Head of linked list of all clients to this service.
63  */
64 static struct PerformanceClient *pc_head;
65
66 /**
67  * Tail of linked list of all clients to this service.
68  */
69 static struct PerformanceClient *pc_tail;
70  
71 /**
72  * Context for sending messages to performance clients.
73  */
74 static struct GNUNET_SERVER_NotificationContext *nc;
75
76
77 /**
78  * Find the performance client associated with the given handle.
79  *
80  * @param client server handle
81  * @return internal handle
82  */
83 static struct PerformanceClient * 
84 find_client (struct GNUNET_SERVER_Client *client)
85 {
86   struct PerformanceClient * pc;
87
88   for (pc = pc_head; pc != NULL; pc = pc->next)
89     if (pc->client == client)
90       return pc;
91   return NULL;
92 }
93
94
95 /**
96  * Register a new performance client.
97  *
98  * @param client handle of the new client
99  */
100 void
101 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
102                             enum StartFlag flag)
103 {
104   struct PerformanceClient * pc;
105
106   GNUNET_break (NULL == find_client (client));
107   pc = GNUNET_malloc (sizeof (struct PerformanceClient));
108   pc->client = client;
109   pc->flag = flag;
110   GNUNET_SERVER_notification_context_add (nc, client);
111   GNUNET_SERVER_client_keep (client);
112   GNUNET_CONTAINER_DLL_insert(pc_head, pc_tail, pc);
113 }
114
115
116 /**
117  * Unregister a client (which may have been a performance client,
118  * but this is not assured).
119  *
120  * @param client handle of the (now dead) client
121  */
122 void
123 GAS_performance_remove_client (struct GNUNET_SERVER_Client *client)
124 {
125   struct PerformanceClient * pc;
126
127   pc = find_client (client);
128   if (NULL == pc)
129     return;
130   GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
131   GNUNET_SERVER_client_drop (client);
132   GNUNET_free (pc);
133 }
134
135
136 /**
137  * Handle 'reservation request' messages from clients.
138  *
139  * @param cls unused, NULL
140  * @param client client that sent the request
141  * @param message the request message
142  */
143 void
144 GAS_handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
145                                 const struct GNUNET_MessageHeader *message)
146 {
147   const struct ReservationRequestMessage * msg = (const struct ReservationRequestMessage *) message;
148   struct ReservationResultMessage result;
149   int32_t amount;
150   struct GNUNET_TIME_Relative res_delay;
151
152   if (NULL == find_client (client))
153   {
154     /* missing start message! */
155     GNUNET_break (0);
156     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
157     return;
158   }
159   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160               "Received `%s' message\n",
161               "RESERVATION_REQUEST");
162   amount = (int32_t) ntohl (msg->amount);
163   res_delay = GAS_reservations_reserve (&msg->peer,
164                                         amount);
165   if (res_delay.rel_value > 0)
166     amount = 0;
167   result.header.size = htons (sizeof (struct ReservationResultMessage));
168   result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
169   result.amount = htonl (amount);
170   result.res_delay = GNUNET_TIME_relative_hton (res_delay);
171   GNUNET_SERVER_notification_context_unicast (nc,
172                                               client,
173                                               &result.header,
174                                               GNUNET_NO);
175   GNUNET_SERVER_receive_done (client, GNUNET_OK);
176 }
177
178
179 /**
180  * Handle 'preference change' messages from clients.
181  *
182  * @param cls unused, NULL
183  * @param client client that sent the request
184  * @param message the request message
185  */
186 void
187 GAS_handle_preference_change (void *cls, struct GNUNET_SERVER_Client *client,
188                               const struct GNUNET_MessageHeader *message)
189 {
190   // const struct ChangePreferenceMessage * msg = (const struct ChangePreferenceMessage *) message;
191   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "PREFERENCE_CHANGE");
192   // FIXME: implement later (we can safely ignore these for now)
193   GNUNET_SERVER_receive_done (client, GNUNET_OK);
194 }
195
196
197 /**
198  * Initialize performance subsystem.
199  *
200  * @param server handle to our server
201  */
202 void
203 GAS_performance_init (struct GNUNET_SERVER_Handle *server)
204 {
205   nc = GNUNET_SERVER_notification_context_create (server, 128);
206 }
207
208
209 /**
210  * Shutdown performance subsystem.
211  */
212 void
213 GAS_performance_done ()
214 {
215   GNUNET_SERVER_notification_context_destroy (nc);
216   nc = NULL;
217 }
218
219 /* end of gnunet-service-ats_performance.c */