-fix compiler warning: do not return const double
[oweals/gnunet.git] / src / ats / gnunet-service-ats.c
index aca877d7b9c743172e629cf499bd850cadbcfc3c..68be19ba59202577f6907c3b319d27212d6b8258 100644 (file)
  * @file ats/gnunet-service-ats.c
  * @brief ats service
  * @author Matthias Wachs
+ * @author Christian Grothoff
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_ats_service.h"
+#include "gnunet-service-ats.h"
+#include "gnunet-service-ats_addresses.h"
 #include "gnunet-service-ats_performance.h"
 #include "gnunet-service-ats_scheduling.h"
-// #include "gnunet-service-ats_performance.h"
+#include "gnunet-service-ats_reservations.h"
 #include "ats.h"
 
-struct ATS_Address
-{
-  struct GNUNET_PeerIdentity peer;
-
-  size_t addr_len;
-
-  uint32_t session_id;
-
-  uint32_t ats_count;
-
-  void * addr;
-
-  char * plugin;
-
-  struct GNUNET_TRANSPORT_ATS_Information * ats;
-};
-
-static struct GNUNET_CONTAINER_MultiHashMap * addresses;
+/**
+ * Handle for statistics.
+ */
+struct GNUNET_STATISTICS_Handle *GSA_stats;
 
+static struct GNUNET_SERVER_Handle *GSA_server;
 
+struct GAS_Addresses_Handle *GSA_addresses;
 
+/**
+ * We have received a 'ClientStartMessage' from a client.  Find out which
+ * type of client it is and notify the respective subsystem.
+ *
+ * @param cls closure, unused
+ * @param client handle to the client
+ * @param message the start message
+ */
 static void
 handle_ats_start (void *cls, struct GNUNET_SERVER_Client *client,
-                 const struct GNUNET_MessageHeader *message)
+                  const struct GNUNET_MessageHeader *message)
 {
-  const struct ClientStartMessage * msg = (const struct ClientStartMessage *) message;
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
-             "Received `%s' message\n",
-             "ATS_START");
-  switch (ntohl (msg->start_flag))
+  const struct ClientStartMessage *msg =
+      (const struct ClientStartMessage *) message;
+  enum StartFlag flag;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ATS_START");
+  flag = ntohl (msg->start_flag);
+  switch (flag)
   {
   case START_FLAG_SCHEDULING:
-    GAS_add_scheduling_client (client);
+    if (GNUNET_OK != GAS_scheduling_add_client (client))
+    {
+      GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+      return;
+    }
     break;
   case START_FLAG_PERFORMANCE_WITH_PIC:
-    GAS_add_performance_client (client);
+    GAS_performance_add_client (client, flag);
     break;
   case START_FLAG_PERFORMANCE_NO_PIC:
+    GAS_performance_add_client (client, flag);
     break;
   default:
     GNUNET_break (0);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);    
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
 
-struct CompareAddressContext
-{
-  struct ATS_Address * search;
-  struct ATS_Address * result;
-};
-
-int compare_address_it (void *cls,
-               const GNUNET_HashCode * key,
-               void *value)
-{
-  struct CompareAddressContext * cac = cls;
-  struct ATS_Address * aa = (struct ATS_Address *) value;
-  if (0 == strcmp(aa->plugin, cac->search->plugin))
-  {
-    if ((aa->addr_len == cac->search->addr_len) &&
-        (0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)))
-      cac->result = aa;
-    return GNUNET_NO;
-  }
-  return GNUNET_YES;
-}
-
-
-static int 
-free_address_it (void *cls,
-                const GNUNET_HashCode * key,
-                void *value)
+/**
+ * A client disconnected from us.  Tear down the local client
+ * record.
+ *
+ * @param cls unused
+ * @param client handle of the client
+ */
+static void
+client_disconnect_handler (void *cls, struct GNUNET_SERVER_Client *client)
 {
-  struct ATS_Address * aa = cls;
-  GNUNET_free (aa);
-  return GNUNET_OK;
+  if (NULL == client)
+    return;
+  GAS_scheduling_remove_client (client);
+  GAS_performance_remove_client (client);
 }
 
 
-
-
-
 /**
  * Task run during shutdown.
  *
@@ -126,8 +110,16 @@ free_address_it (void *cls,
 static void
 cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  GNUNET_CONTAINER_multihashmap_iterate (addresses, &free_address_it, NULL);
-  GNUNET_CONTAINER_multihashmap_destroy (addresses);
+  GAS_addresses_done (GSA_addresses);
+  GAS_scheduling_done ();
+  GAS_performance_done ();
+  GAS_reservations_done ();
+  GNUNET_SERVER_disconnect_notify_cancel (GSA_server, &client_disconnect_handler, NULL);
+  if (NULL != GSA_stats)
+  {
+    GNUNET_STATISTICS_destroy (GSA_stats, GNUNET_NO);
+    GSA_stats = NULL;
+  }
 }
 
 
@@ -143,21 +135,45 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
      const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
-    { &handle_ats_start, NULL, 
-      GNUNET_MESSAGE_TYPE_ATS_START, sizeof (struct ClientStartMessage)},
-    { &GAS_handle_request_address, NULL,
-      GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS, sizeof (struct RequestAddressMessage)},
-    { &GAS_handle_address_update, NULL, 
-      GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE, 0},
-    { &GAS_handle_address_destroyed, NULL, 
-      GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED, 0},
-    { &GAS_handle_reservation_request, NULL, 
-      GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST, sizeof (struct ReservationRequestMessage)},
-    { &GAS_handle_preference_change, NULL, 
-      GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE, 0},
+    {&handle_ats_start, NULL,
+     GNUNET_MESSAGE_TYPE_ATS_START, sizeof (struct ClientStartMessage)},
+    {&GAS_handle_request_address, NULL,
+     GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS,
+     sizeof (struct RequestAddressMessage)},
+    {&GAS_handle_request_address_cancel, NULL,
+     GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL,
+     sizeof (struct RequestAddressMessage)},
+    {&GAS_handle_request_address_list, NULL,
+     GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_REQUEST,
+     sizeof (struct AddressListRequestMessage)},
+    {&GAS_handle_address_add, NULL,
+     GNUNET_MESSAGE_TYPE_ATS_ADDRESS_ADD, 0},
+    {&GAS_handle_address_update, NULL,
+     GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE, 0},
+    {&GAS_handle_address_in_use, NULL,
+     GNUNET_MESSAGE_TYPE_ATS_ADDRESS_IN_USE, 0},
+    {&GAS_handle_address_destroyed, NULL,
+     GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED, 0},
+    {&GAS_handle_reservation_request, NULL,
+     GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST,
+     sizeof (struct ReservationRequestMessage)},
+    {&GAS_handle_preference_change, NULL,
+     GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_CHANGE, 0},
+    {&GAS_handle_preference_feedback, NULL,
+     GNUNET_MESSAGE_TYPE_ATS_PREFERENCE_FEEDBACK, 0},
+    {&GAS_handle_reset_backoff, NULL,
+     GNUNET_MESSAGE_TYPE_ATS_RESET_BACKOFF,
+     sizeof (struct ResetBackoffMessage)},
     {NULL, NULL, 0, 0}
   };
-  addresses = GNUNET_CONTAINER_multihashmap_create(128);
+  GSA_server = server;
+  GSA_stats = GNUNET_STATISTICS_create ("ats", cfg);
+  GAS_reservations_init ();
+  GSA_addresses = GAS_addresses_init (cfg, GSA_stats);
+  GAS_performance_init (server, GSA_addresses);
+  GAS_scheduling_init (server, GSA_addresses);
+
+  GNUNET_SERVER_disconnect_notify (server, &client_disconnect_handler, NULL);
   GNUNET_SERVER_add_handlers (server, handlers);
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
                                 NULL);
@@ -175,8 +191,8 @@ int
 main (int argc, char *const *argv)
 {
   return (GNUNET_OK ==
-          GNUNET_SERVICE_run (argc, argv, "ats",
-                              GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
+          GNUNET_SERVICE_run (argc, argv, "ats", GNUNET_SERVICE_OPTION_NONE,
+                              &run, NULL)) ? 0 : 1;
 }
 
 /* end of gnunet-service-ats.c */