f68b1b8aeb5b73630cbbecf6c6517a2dd7a17a1d
[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 "ats.h"
30
31
32 /**
33  * We keep clients that are interested in performance notifications in a linked list.
34  * Note that not ALL clients that are handeled by this module also register for
35  * notifications.  Only those clients that are in this list are managed by the
36  * notification context.
37  */
38 struct PerformanceClient
39 {
40   /**
41    * Next in doubly-linked list.
42    */
43   struct PerformanceClient * next;
44
45   /**
46    * Previous in doubly-linked list.
47    */
48   struct PerformanceClient * prev;
49   
50   /**
51    * Actual handle to the client.
52    */
53   struct GNUNET_SERVER_Client *client;
54
55 };
56
57
58 /**
59  * Head of linked list of all clients to this service.
60  */
61 static struct PerformanceClient *pc_head;
62
63 /**
64  * Tail of linked list of all clients to this service.
65  */
66 static struct PerformanceClient *pc_tail;
67  
68 /**
69  * Context for sending messages to performance clients.
70  */
71 static struct GNUNET_SERVER_NotificationContext *nc;
72
73
74 /**
75  * Find the performance client associated with the given handle.
76  *
77  * @param client server handle
78  * @return internal handle
79  */
80 static struct PerformanceClient * 
81 find_client (struct GNUNET_SERVER_Client *client)
82 {
83   struct PerformanceClient * pc;
84
85   for (pc = pc_head; pc != NULL; pc = pc->next)
86     if (pc->client == client)
87       return pc;
88   return NULL;
89 }
90
91
92 /**
93  * Register a new performance client.
94  *
95  * @param client handle of the new client
96  */
97 void
98 GAS_performance_add_client (struct GNUNET_SERVER_Client *client)
99 {
100   struct PerformanceClient * pc;
101
102   GNUNET_break (NULL == find_client (client));
103   pc = GNUNET_malloc (sizeof (struct PerformanceClient));
104   pc->client = client;
105   GNUNET_SERVER_notification_context_add (nc, client);
106   GNUNET_SERVER_client_keep (client);
107   GNUNET_CONTAINER_DLL_insert(pc_head, pc_tail, pc);
108 }
109
110
111 /**
112  * Unregister a client (which may have been a performance client,
113  * but this is not assured).
114  *
115  * @param client handle of the (now dead) client
116  */
117 void
118 GAS_performance_remove_client (struct GNUNET_SERVER_Client *client)
119 {
120   struct PerformanceClient * pc;
121
122   pc = find_client (client);
123   if (NULL == pc)
124     return;
125   GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
126   GNUNET_SERVER_client_drop (client);
127   GNUNET_free (pc);
128 }
129
130
131 void
132 GAS_handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
133                                 const struct GNUNET_MessageHeader *message)
134 {
135   // const struct ReservationRequestMessage * msg = (const struct ReservationRequestMessage *) message;
136   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "RESERVATION_REQUEST");
137
138 }
139
140
141 void
142 GAS_handle_preference_change (void *cls, struct GNUNET_SERVER_Client *client,
143                               const struct GNUNET_MessageHeader *message)
144
145 {
146   // const struct ChangePreferenceMessage * msg = (const struct ChangePreferenceMessage *) message;
147   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "PREFERENCE_CHANGE");
148   // FIXME: implement later (we can safely ignore these for now)
149   GNUNET_SERVER_receive_done (client, GNUNET_OK);
150 }
151
152
153 /**
154  * Initialize performance subsystem.
155  *
156  * @param server handle to our server
157  */
158 void
159 GAS_performance_init (struct GNUNET_SERVER_Handle *server)
160 {
161   nc = GNUNET_SERVER_notification_context_create (server, 128);
162 }
163
164
165 /**
166  * Shutdown performance subsystem.
167  */
168 void
169 GAS_performance_done ()
170 {
171   GNUNET_SERVER_notification_context_destroy (nc);
172   nc = NULL;
173 }
174
175 /* end of gnunet-service-ats_performance.c */