use NULL value in load_path_suffix to NOT load any files
[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 it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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
85                                             GNUNET_PeerIdentity *peer,
86                                             uint32_t session_id,
87                                             struct GNUNET_BANDWIDTH_Value32NBO
88                                             bandwidth_out,
89                                             struct GNUNET_BANDWIDTH_Value32NBO
90                                             bandwidth_in)
91 {
92   struct GNUNET_MQ_Envelope *env;
93   struct AddressSuggestionMessage *msg;
94
95   if (NULL == my_client)
96     return;
97   GNUNET_STATISTICS_update (GSA_stats,
98                             "# address suggestions made",
99                             1,
100                             GNUNET_NO);
101   env = GNUNET_MQ_msg (msg,
102                        GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION);
103   msg->peer = *peer;
104   msg->session_id = htonl (session_id);
105   msg->bandwidth_out = bandwidth_out;
106   msg->bandwidth_in = bandwidth_in;
107   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
108               "ATS sends quota for peer `%s': (in/out) %u/%u\n",
109               GNUNET_i2s (peer),
110               (unsigned int) ntohl (bandwidth_in.value__),
111               (unsigned int) ntohl (bandwidth_out.value__));
112   GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (my_client),
113                   env);
114 }
115
116
117 /**
118  * Handle 'address add' messages from clients.
119  *
120  * @param m the request message
121  */
122 void
123 GAS_handle_address_add (const struct AddressAddMessage *m)
124 {
125   const char *address;
126   const char *plugin_name;
127   uint16_t address_length;
128   uint16_t plugin_name_length;
129   struct GNUNET_ATS_Properties prop;
130
131   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
132               "Received `%s' message\n",
133               "ADDRESS_ADD");
134   address_length = ntohs (m->address_length);
135   plugin_name_length = ntohs (m->plugin_name_length);
136   address = (const char *) &m[1];
137   if (plugin_name_length != 0)
138     plugin_name = &address[address_length];
139   else
140     plugin_name = "";
141   GNUNET_STATISTICS_update (GSA_stats,
142                             "# addresses created",
143                             1,
144                             GNUNET_NO);
145   GNUNET_ATS_properties_ntoh (&prop,
146                               &m->properties);
147   GNUNET_break (GNUNET_NT_UNSPECIFIED != prop.scope);
148   GAS_addresses_add (&m->peer,
149                      plugin_name,
150                      address,
151                      address_length,
152                      ntohl (m->address_local_info),
153                      ntohl (m->session_id),
154                      &prop);
155 }
156
157
158 /**
159  * Handle 'address update' messages from clients.
160  *
161  * @param m the request message
162  */
163 void
164 GAS_handle_address_update (const struct AddressUpdateMessage *m)
165 {
166   struct GNUNET_ATS_Properties prop;
167
168   GNUNET_STATISTICS_update (GSA_stats,
169                             "# address updates received",
170                             1,
171                             GNUNET_NO);
172   GNUNET_ATS_properties_ntoh (&prop,
173                               &m->properties);
174   GAS_addresses_update (&m->peer,
175                         ntohl (m->session_id),
176                         &prop);
177 }
178
179
180 /**
181  * Handle 'address destroyed' messages from clients.
182  *
183  * @param m the request message
184  */
185 void
186 GAS_handle_address_destroyed (const struct AddressDestroyedMessage *m)
187 {
188   struct GNUNET_MQ_Envelope *env;
189   struct GNUNET_ATS_SessionReleaseMessage *srm;
190
191   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192               "Received `%s' message\n",
193               "ADDRESS_DESTROYED");
194   GNUNET_STATISTICS_update (GSA_stats,
195                             "# addresses destroyed",
196                             1,
197                             GNUNET_NO);
198   GAS_addresses_destroy (&m->peer,
199                          ntohl (m->session_id));
200   env = GNUNET_MQ_msg (srm,
201                        GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE);
202   srm->session_id = m->session_id;
203   srm->peer = m->peer;
204   GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (my_client),
205                   env);
206 }
207
208
209 /* end of gnunet-service-ats_scheduling.c */