gnunet-service-ats_scheduling.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2011-2016 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file ats/gnunet-service-ats_scheduling.c
  18. * @brief ats service, interaction with 'scheduling' API
  19. * @author Matthias Wachs
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet-service-ats_addresses.h"
  24. #include "gnunet-service-ats_scheduling.h"
  25. #include "ats.h"
  26. /**
  27. * Actual handle to the client.
  28. */
  29. static struct GNUNET_SERVICE_Client *my_client;
  30. /**
  31. * Register a new scheduling client.
  32. *
  33. * @param client handle of the new client
  34. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  35. */
  36. int
  37. GAS_scheduling_add_client (struct GNUNET_SERVICE_Client *client)
  38. {
  39. if (NULL != my_client)
  40. {
  41. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  42. "This ATS already has a scheduling client, refusing new scheduling client for now.\n");
  43. return GNUNET_SYSERR;
  44. }
  45. my_client = client;
  46. return GNUNET_OK;
  47. }
  48. /**
  49. * Unregister a client (which may have been a scheduling client,
  50. * but this is not assured).
  51. *
  52. * @param client handle of the (now dead) client
  53. */
  54. void
  55. GAS_scheduling_remove_client (struct GNUNET_SERVICE_Client *client)
  56. {
  57. if (my_client != client)
  58. return;
  59. GAS_addresses_destroy_all ();
  60. my_client = NULL;
  61. }
  62. /**
  63. * Transmit the given address suggestion and bandwidth update to all scheduling
  64. * clients.
  65. *
  66. * @param peer peer for which this is an address suggestion
  67. * @param session_id session ID to use for the given client
  68. * @param bandwidth_out assigned outbound bandwidth
  69. * @param bandwidth_in assigned inbound bandwidth
  70. */
  71. void
  72. GAS_scheduling_transmit_address_suggestion (const struct
  73. GNUNET_PeerIdentity *peer,
  74. uint32_t session_id,
  75. struct GNUNET_BANDWIDTH_Value32NBO
  76. bandwidth_out,
  77. struct GNUNET_BANDWIDTH_Value32NBO
  78. bandwidth_in)
  79. {
  80. struct GNUNET_MQ_Envelope *env;
  81. struct AddressSuggestionMessage *msg;
  82. if (NULL == my_client)
  83. return;
  84. GNUNET_STATISTICS_update (GSA_stats,
  85. "# address suggestions made",
  86. 1,
  87. GNUNET_NO);
  88. env = GNUNET_MQ_msg (msg,
  89. GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION);
  90. msg->peer = *peer;
  91. msg->session_id = htonl (session_id);
  92. msg->bandwidth_out = bandwidth_out;
  93. msg->bandwidth_in = bandwidth_in;
  94. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  95. "ATS sends quota for peer `%s': (in/out) %u/%u\n",
  96. GNUNET_i2s (peer),
  97. (unsigned int) ntohl (bandwidth_in.value__),
  98. (unsigned int) ntohl (bandwidth_out.value__));
  99. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (my_client),
  100. env);
  101. }
  102. /**
  103. * Handle 'address add' messages from clients.
  104. *
  105. * @param m the request message
  106. */
  107. void
  108. GAS_handle_address_add (const struct AddressAddMessage *m)
  109. {
  110. const char *address;
  111. const char *plugin_name;
  112. uint16_t address_length;
  113. uint16_t plugin_name_length;
  114. struct GNUNET_ATS_Properties prop;
  115. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  116. "Received `%s' message\n",
  117. "ADDRESS_ADD");
  118. address_length = ntohs (m->address_length);
  119. plugin_name_length = ntohs (m->plugin_name_length);
  120. address = (const char *) &m[1];
  121. if (plugin_name_length != 0)
  122. plugin_name = &address[address_length];
  123. else
  124. plugin_name = "";
  125. GNUNET_STATISTICS_update (GSA_stats,
  126. "# addresses created",
  127. 1,
  128. GNUNET_NO);
  129. GNUNET_ATS_properties_ntoh (&prop,
  130. &m->properties);
  131. GNUNET_break (GNUNET_NT_UNSPECIFIED != prop.scope);
  132. GAS_addresses_add (&m->peer,
  133. plugin_name,
  134. address,
  135. address_length,
  136. ntohl (m->address_local_info),
  137. ntohl (m->session_id),
  138. &prop);
  139. }
  140. /**
  141. * Handle 'address update' messages from clients.
  142. *
  143. * @param m the request message
  144. */
  145. void
  146. GAS_handle_address_update (const struct AddressUpdateMessage *m)
  147. {
  148. struct GNUNET_ATS_Properties prop;
  149. GNUNET_STATISTICS_update (GSA_stats,
  150. "# address updates received",
  151. 1,
  152. GNUNET_NO);
  153. GNUNET_ATS_properties_ntoh (&prop,
  154. &m->properties);
  155. GAS_addresses_update (&m->peer,
  156. ntohl (m->session_id),
  157. &prop);
  158. }
  159. /**
  160. * Handle 'address destroyed' messages from clients.
  161. *
  162. * @param m the request message
  163. */
  164. void
  165. GAS_handle_address_destroyed (const struct AddressDestroyedMessage *m)
  166. {
  167. struct GNUNET_MQ_Envelope *env;
  168. struct GNUNET_ATS_SessionReleaseMessage *srm;
  169. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  170. "Received `%s' message\n",
  171. "ADDRESS_DESTROYED");
  172. GNUNET_STATISTICS_update (GSA_stats,
  173. "# addresses destroyed",
  174. 1,
  175. GNUNET_NO);
  176. GAS_addresses_destroy (&m->peer,
  177. ntohl (m->session_id));
  178. env = GNUNET_MQ_msg (srm,
  179. GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE);
  180. srm->session_id = m->session_id;
  181. srm->peer = m->peer;
  182. GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (my_client),
  183. env);
  184. }
  185. /* end of gnunet-service-ats_scheduling.c */