2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file ats/gnunet-service-ats_performance.c
23 * @brief ats service, interaction with 'performance' API
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet-service-ats_performance.h"
29 #include "gnunet-service-ats_reservations.h"
34 * We keep clients that are interested in performance in a linked list.
36 struct PerformanceClient
39 * Next in doubly-linked list.
41 struct PerformanceClient * next;
44 * Previous in doubly-linked list.
46 struct PerformanceClient * prev;
49 * Actual handle to the client.
51 struct GNUNET_SERVER_Client *client;
54 * Options for the client.
62 * Head of linked list of all clients to this service.
64 static struct PerformanceClient *pc_head;
67 * Tail of linked list of all clients to this service.
69 static struct PerformanceClient *pc_tail;
72 * Context for sending messages to performance clients.
74 static struct GNUNET_SERVER_NotificationContext *nc;
78 * Find the performance client associated with the given handle.
80 * @param client server handle
81 * @return internal handle
83 static struct PerformanceClient *
84 find_client (struct GNUNET_SERVER_Client *client)
86 struct PerformanceClient * pc;
88 for (pc = pc_head; pc != NULL; pc = pc->next)
89 if (pc->client == client)
96 * Register a new performance client.
98 * @param client handle of the new client
101 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
104 struct PerformanceClient * pc;
106 GNUNET_break (NULL == find_client (client));
107 pc = GNUNET_malloc (sizeof (struct PerformanceClient));
110 GNUNET_SERVER_notification_context_add (nc, client);
111 GNUNET_SERVER_client_keep (client);
112 GNUNET_CONTAINER_DLL_insert(pc_head, pc_tail, pc);
117 * Unregister a client (which may have been a performance client,
118 * but this is not assured).
120 * @param client handle of the (now dead) client
123 GAS_performance_remove_client (struct GNUNET_SERVER_Client *client)
125 struct PerformanceClient * pc;
127 pc = find_client (client);
130 GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
131 GNUNET_SERVER_client_drop (client);
137 * Handle 'reservation request' messages from clients.
139 * @param cls unused, NULL
140 * @param client client that sent the request
141 * @param message the request message
144 GAS_handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
145 const struct GNUNET_MessageHeader *message)
147 const struct ReservationRequestMessage * msg = (const struct ReservationRequestMessage *) message;
148 struct ReservationResultMessage result;
150 struct GNUNET_TIME_Relative res_delay;
152 if (NULL == find_client (client))
154 /* missing start message! */
156 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
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,
165 if (res_delay.rel_value > 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,
175 GNUNET_SERVER_receive_done (client, GNUNET_OK);
180 * Handle 'preference change' messages from clients.
182 * @param cls unused, NULL
183 * @param client client that sent the request
184 * @param message the request message
187 GAS_handle_preference_change (void *cls, struct GNUNET_SERVER_Client *client,
188 const struct GNUNET_MessageHeader *message)
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);
198 * Initialize performance subsystem.
200 * @param server handle to our server
203 GAS_performance_init (struct GNUNET_SERVER_Handle *server)
205 nc = GNUNET_SERVER_notification_context_create (server, 128);
210 * Shutdown performance subsystem.
213 GAS_performance_done ()
215 GNUNET_SERVER_notification_context_destroy (nc);
219 /* end of gnunet-service-ats_performance.c */