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