-remove dead include
[oweals/gnunet.git] / src / ats / gnunet-service-ats.c
index 36df715f1fdf31d6a538057cc78df58af5128b1e..729f1952b3e7bb1f4dd1e91805051c44cf2091dd 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009 Christian Grothoff (and other contributing authors)
+     (C) 2011 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
  * @file ats/gnunet-service-ats.c
  * @brief ats service
  * @author Matthias Wachs
+ * @author Christian Grothoff
  */
 #include "platform.h"
-#include "gnunet_getopt_lib.h"
-#include "gnunet_service_lib.h"
-#include "gnunet_container_lib.h"
-#include "gnunet_ats_service.h"
+#include "gnunet_util_lib.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_reservations.h"
 #include "ats.h"
 
-struct ATS_Clients
-{
-  struct ATS_Clients * next;
-
-  struct ATS_Clients * prev;
-
-  struct GNUNET_SERVER_Client *client;
-
-  uint32_t flags;
-};
-
-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;
-};
-
 /**
- * Head of linked list of all clients to this service.
+ * Handle for statistics.
  */
-static struct ATS_Clients *ac_head;
+struct GNUNET_STATISTICS_Handle *GSA_stats;
 
 /**
- * Tail of linked list of all clients to this service.
+ * Handle to the ATS server.
  */
-static struct ATS_Clients *ac_tail;
+static struct GNUNET_SERVER_Handle *GSA_server;
 
-static struct GNUNET_CONTAINER_MultiHashMap * addresses;
+/**
+ * Handle to the address state.
+ */
+static struct GAS_Addresses_Handle *GSA_addresses;
 
-int address_it (void *cls,
-               const GNUNET_HashCode * key,
-               void *value)
-{
-  struct ATS_Address * aa = cls;
-  GNUNET_free (aa);
-  return GNUNET_OK;
-}
 
 /**
- * Task run during shutdown.
+ * We have received a `struct ClientStartMessage` from a client.  Find out which
+ * type of client it is and notify the respective subsystem.
  *
- * @param cls unused
- * @param tc unused
+ * @param cls closure, unused
+ * @param client handle to the client
+ * @param message the start message
  */
 static void
-cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct ATS_Clients * t;
-
-  while (ac_head != NULL)
-  {
-    t = ac_head;
-    GNUNET_CONTAINER_DLL_remove(ac_head,ac_tail, t);
-    GNUNET_free (t);
-  }
-
-  GNUNET_CONTAINER_multihashmap_iterate (addresses, address_it, NULL);
-
-  GNUNET_CONTAINER_multihashmap_destroy (addresses);
-}
-
-static struct ATS_Clients * find_client (struct GNUNET_SERVER_Client *client)
+handle_ats_start (void *cls, struct GNUNET_SERVER_Client *client,
+                  const struct GNUNET_MessageHeader *message)
 {
-  struct ATS_Clients * ac = ac_head;
-  while (ac != NULL)
+  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)
   {
-  if (ac->client == client)
+  case START_FLAG_SCHEDULING:
+    if (GNUNET_OK != GAS_scheduling_add_client (client))
+    {
+      GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+      return;
+    }
+    break;
+  case START_FLAG_PERFORMANCE_WITH_PIC:
+    GAS_performance_add_client (client, flag);
+    break;
+  case START_FLAG_PERFORMANCE_NO_PIC:
+    GAS_performance_add_client (client, flag);
     break;
-  ac = ac->next;
+  default:
+    GNUNET_break (0);
+    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
   }
-  return ac;
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
-static void
-handle_ats_start (void *cls, struct GNUNET_SERVER_Client *client,
-                      const struct GNUNET_MessageHeader *message)
-
-{
-  struct ClientStartMessage * msg = (struct ClientStartMessage *) message;
-  struct ATS_Clients * ac = NULL;
-
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ATS_START");
-
-  GNUNET_assert (find_client(client) == NULL);
-
-  ac = GNUNET_malloc (sizeof (struct ATS_Clients));
-  ac->client = client;
-  ac->flags = ntohl (msg->start_flag);
-
-  GNUNET_CONTAINER_DLL_insert(ac_head, ac_tail, ac);
-}
-
-static void
-handle_request_address (void *cls, struct GNUNET_SERVER_Client *client,
-                      const struct GNUNET_MessageHeader *message)
-
-{
-  // struct RequestAddressMessage * msg = (struct RequestAddressMessage *) message;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "REQUEST_ADDRESS");
-
-}
 
+/**
+ * A client disconnected from us.  Tear down the local client
+ * record.
+ *
+ * @param cls unused
+ * @param client handle of the client
+ */
 static void
-handle_address_update (void *cls, struct GNUNET_SERVER_Client *client,
-                      const struct GNUNET_MessageHeader *message)
-
+client_disconnect_handler (void *cls,
+                           struct GNUNET_SERVER_Client *client)
 {
-  struct AddressUpdateMessage * msg = (struct AddressUpdateMessage *) message;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ADDRESS_UPDATE");
-
-  struct GNUNET_TRANSPORT_ATS_Information *am;
-  char *pm;
-
-  size_t size = ntohs (msg->header.size);
-  if (size <= sizeof (struct AddressUpdateMessage))
-      GNUNET_break (0);
-
-  size_t ats_count = ntohs (msg->ats_count);
-  size_t addr_len = ntohs (msg->address_length);
-  size_t plugin_len = ntohs (msg->plugin_name_length) + 1 ;
-
-  struct ATS_Address * aa = GNUNET_malloc (sizeof (struct ATS_Address) +
-                                           ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) +
-                                           addr_len +
-                                           plugin_len);
-
-
-
-  memcpy (&aa->peer, &msg->peer, sizeof (struct GNUNET_PeerIdentity));
-  aa->addr_len = addr_len;
-  aa->ats_count = ats_count;
-  aa->ats = (struct GNUNET_TRANSPORT_ATS_Information *) &aa[1];
-
-  am = (struct GNUNET_TRANSPORT_ATS_Information*) &msg[1];
-  memcpy (&aa->ats, am, ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
-  pm = (char *) &am[ats_count];
-  memcpy (aa->addr, pm, addr_len);
-  memcpy (aa->plugin, &pm[plugin_len], plugin_len);
-  aa->session_id = ntohl(msg->session_id);
-
-  GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put(addresses, &aa->peer.hashPubKey, aa, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
+  if (NULL == client)
+    return;
+  GAS_scheduling_remove_client (client);
+  GAS_performance_remove_client (client);
 }
 
-static void
-handle_address_destroyed (void *cls, struct GNUNET_SERVER_Client *client,
-                      const struct GNUNET_MessageHeader *message)
-
-{
-  // struct AddressDestroyedMessage * msg = (struct AddressDestroyedMessage *) message;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "ADDRESS_DESTROYED");
-
-}
 
+/**
+ * Task run during shutdown.
+ *
+ * @param cls unused
+ * @param tc unused
+ */
 static void
-handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
-                      const struct GNUNET_MessageHeader *message)
-
+cleanup_task (void *cls,
+              const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  // struct AddressUpdateMessage * msg = (struct AddressUpdateMessage *) message;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "RESERVATION_REQUEST");
+  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;
+  }
 }
 
-static void
-handle_preference_change (void *cls, struct GNUNET_SERVER_Client *client,
-                      const struct GNUNET_MessageHeader *message)
-
-{
-  // struct ChangePreferenceMessage * msg = (struct ChangePreferenceMessage *) message;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "PREFERENCE_CHANGE");
-}
 
 /**
  * Process template requests.
@@ -225,17 +148,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)},
-      {&handle_request_address, NULL, GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS, sizeof (struct RequestAddressMessage)},
-      {&handle_address_update, NULL, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE, 0},
-      {&handle_address_destroyed, NULL, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED, 0},
-      {&handle_reservation_request, NULL, GNUNET_MESSAGE_TYPE_ATS_RESERVATION_REQUEST, sizeof (struct ReservationRequestMessage)},
-      {&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(100);
-
+  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);
@@ -254,7 +205,8 @@ 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_OPTION_NONE,
+                              &run, NULL)) ? 0 : 1;
 }
 
 /* end of gnunet-service-ats.c */