-add tests
[oweals/gnunet.git] / src / ats / gnunet-service-ats_scheduling.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011-2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file ats/gnunet-service-ats_scheduling.c
23  * @brief ats service, interaction with 'scheduling' API
24  * @author Matthias Wachs
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet-service-ats_addresses.h"
29 #include "gnunet-service-ats_scheduling.h"
30 #include "ats.h"
31
32 /**
33  * Actual handle to the client.
34  */
35 static struct GNUNET_SERVICE_Client *my_client;
36
37
38 /**
39  * Register a new scheduling client.
40  *
41  * @param client handle of the new client
42  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
43  */
44 int
45 GAS_scheduling_add_client (struct GNUNET_SERVICE_Client *client)
46 {
47   if (NULL != my_client)
48   {
49     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
50                 "This ATS already has a scheduling client, refusing new scheduling client for now.\n");
51     return GNUNET_SYSERR;
52   }
53   my_client = client;
54   return GNUNET_OK;
55 }
56
57
58 /**
59  * Unregister a client (which may have been a scheduling client,
60  * but this is not assured).
61  *
62  * @param client handle of the (now dead) client
63  */
64 void
65 GAS_scheduling_remove_client (struct GNUNET_SERVICE_Client *client)
66 {
67   if (my_client != client)
68     return;
69   GAS_addresses_destroy_all ();
70   my_client = NULL;
71 }
72
73
74 /**
75  * Transmit the given address suggestion and bandwidth update to all scheduling
76  * clients.
77  *
78  * @param peer peer for which this is an address suggestion
79  * @param session_id session ID to use for the given client
80  * @param bandwidth_out assigned outbound bandwidth
81  * @param bandwidth_in assigned inbound bandwidth
82  */
83 void
84 GAS_scheduling_transmit_address_suggestion (const struct GNUNET_PeerIdentity *peer,
85                                             uint32_t session_id,
86                                             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
87                                             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
88 {
89   struct GNUNET_MQ_Envelope *env;
90   struct AddressSuggestionMessage *msg;
91
92   if (NULL == my_client)
93     return;
94   GNUNET_STATISTICS_update (GSA_stats,
95                             "# address suggestions made",
96                             1,
97                             GNUNET_NO);
98   env = GNUNET_MQ_msg (msg,
99                        GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION);
100   msg->peer = *peer;
101   msg->session_id = htonl (session_id);
102   msg->bandwidth_out = bandwidth_out;
103   msg->bandwidth_in = bandwidth_in;
104   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
105               "ATS sends quota for peer `%s': (in/out) %u/%u\n",
106               GNUNET_i2s (peer),
107               (unsigned int) ntohl (bandwidth_in.value__),
108               (unsigned int) ntohl (bandwidth_out.value__));
109   GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (my_client),
110                   env);
111 }
112
113
114 /**
115  * Handle 'address add' messages from clients.
116  *
117  * @param m the request message
118  */
119 void
120 GAS_handle_address_add (const struct AddressAddMessage *m)
121 {
122   const char *address;
123   const char *plugin_name;
124   uint16_t address_length;
125   uint16_t plugin_name_length;
126   struct GNUNET_ATS_Properties prop;
127
128   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
129               "Received `%s' message\n",
130               "ADDRESS_ADD");
131   address_length = ntohs (m->address_length);
132   plugin_name_length = ntohs (m->plugin_name_length);
133   address = (const char *) &m[1];
134   if (plugin_name_length != 0)
135     plugin_name = &address[address_length];
136   else
137     plugin_name = "";
138   GNUNET_STATISTICS_update (GSA_stats,
139                             "# addresses created",
140                             1,
141                             GNUNET_NO);
142   GNUNET_ATS_properties_ntoh (&prop,
143                               &m->properties);
144   GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != prop.scope);
145   GAS_addresses_add (&m->peer,
146                      plugin_name,
147                      address,
148                      address_length,
149                      ntohl (m->address_local_info),
150                      ntohl (m->session_id),
151                      &prop);
152 }
153
154
155 /**
156  * Handle 'address update' messages from clients.
157  *
158  * @param m the request message
159  */
160 void
161 GAS_handle_address_update (const struct AddressUpdateMessage *m)
162 {
163   struct GNUNET_ATS_Properties prop;
164
165   GNUNET_STATISTICS_update (GSA_stats,
166                             "# address updates received",
167                             1,
168                             GNUNET_NO);
169   GNUNET_ATS_properties_ntoh (&prop,
170                               &m->properties);
171   GAS_addresses_update (&m->peer,
172                         ntohl (m->session_id),
173                         &prop);
174 }
175
176
177 /**
178  * Handle 'address destroyed' messages from clients.
179  *
180  * @param m the request message
181  */
182 void
183 GAS_handle_address_destroyed (const struct AddressDestroyedMessage *m)
184 {
185   struct GNUNET_MQ_Envelope *env;
186   struct GNUNET_ATS_SessionReleaseMessage *srm;
187
188   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
189               "Received `%s' message\n",
190               "ADDRESS_DESTROYED");
191   GNUNET_STATISTICS_update (GSA_stats,
192                             "# addresses destroyed",
193                             1,
194                             GNUNET_NO);
195   GAS_addresses_destroy (&m->peer,
196                          ntohl (m->session_id));
197   env = GNUNET_MQ_msg (srm,
198                        GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE);
199   srm->session_id = m->session_id;
200   srm->peer = m->peer;
201   GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (my_client),
202                   env);
203 }
204
205
206 /* end of gnunet-service-ats_scheduling.c */