REST: nothing triggers rest
[oweals/gnunet.git] / src / ats / gnunet-service-ats-new.c
index c4ce4e0bbf9a0d868b38af81b8de9848484044c9..b451c8c677128cc96a5763de24e23a54d3d1d050 100644 (file)
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      Affero General Public License for more details.
-    
+
      You should have received a copy of the GNU Affero General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 /**
  * @file ats/gnunet-service-ats-new.c
  * @brief ats service
  * @author Matthias Wachs
  * @author Christian Grothoff
- *
- * TODO:
- * - implement unloading of ATS plugins
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
@@ -81,7 +80,7 @@ struct ClientPreference
    * Plugin's representation of the preference.
    */
   struct GNUNET_ATS_PreferenceHandle *ph;
-  
+
   /**
    * Details about the preference.
    */
@@ -96,7 +95,7 @@ struct GNUNET_ATS_Session
 {
 
   /**
-   * Session data exposed to the plugin. 
+   * Session data exposed to the plugin.
    */
   struct GNUNET_ATS_SessionData data;
 
@@ -109,12 +108,12 @@ struct GNUNET_ATS_Session
    * Session state in the plugin.
    */
   struct GNUNET_ATS_SessionHandle *sh;
-  
+
   /**
    * Unique ID for the session when talking with the client.
-   */ 
+   */
   uint32_t session_id;
-  
+
 };
 
 
@@ -149,12 +148,12 @@ struct Client
        * Head of DLL of preferences expressed by this client.
        */
       struct ClientPreference *cp_head;
-      
+
       /**
        * Tail of DLL of preferences expressed by this client.
        */
       struct ClientPreference *cp_tail;
-      
+
     } application;
 
     struct {
@@ -163,9 +162,9 @@ struct Client
        * Map from session IDs to `struct GNUNET_ATS_Session` objects.
        */
       struct GNUNET_CONTAINER_MultiHashMap32 *sessions;
-      
+
     } transport;
-    
+
   } details;
 
 };
@@ -199,7 +198,7 @@ static struct Client *transport_client;
  * @param cls closure, NULL
  * @param pid peer this is about
  * @param address address the transport should try
- */ 
+ */
 static void
 suggest_cb (void *cls,
            const struct GNUNET_PeerIdentity *pid,
@@ -208,12 +207,16 @@ suggest_cb (void *cls,
   struct GNUNET_MQ_Envelope *env;
   size_t slen = strlen (address) + 1;
   struct AddressSuggestionMessage *as;
-  
+
   if (NULL == transport_client)
   {
     // FIXME: stats!
     return;
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Suggesting address `%s' of peer `%s'\n",
+              address,
+              GNUNET_i2s (pid));
   env = GNUNET_MQ_msg_extra (as,
                             slen,
                             GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION);
@@ -254,6 +257,12 @@ allocate_cb (void *cls,
        losses of sessions (possibly of previous transport), ignore! */
     return;
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Allocating %u/%u bytes for %p of peer `%s'\n",
+              ntohl (bw_in.value__),
+              ntohl (bw_out.value__),
+              session,
+              GNUNET_i2s (peer));
   env = GNUNET_MQ_msg (sam,
                       GNUNET_MESSAGE_TYPE_ATS_SESSION_ALLOCATION);
   sam->session_id = session->session_id;
@@ -288,14 +297,14 @@ prop_ntoh (const struct PropertiesNBO *properties,
 
 
 /**
- * We have received a `struct ExpressPreferenceMessage` from an application client.  
+ * We have received a `struct ExpressPreferenceMessage` from an application client.
  *
  * @param cls handle to the client
  * @param msg the start message
  */
 static void
 handle_suggest (void *cls,
-               const struct ExpressPreferenceMessage *msg)
+                const struct ExpressPreferenceMessage *msg)
 {
   struct Client *c = cls;
   struct ClientPreference *cp;
@@ -308,6 +317,11 @@ handle_suggest (void *cls,
     GNUNET_SERVICE_client_drop (c->client);
     return;
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Client suggested we talk to %s with preference %d at rate %u\n",
+              GNUNET_i2s (&msg->peer),
+              (int) ntohl (msg->pk),
+              (int) ntohl (msg->bw.value__));
   cp = GNUNET_new (struct ClientPreference);
   cp->client = c;
   cp->pref.peer = msg->peer;
@@ -323,18 +337,18 @@ handle_suggest (void *cls,
 
 
 /**
- * We have received a `struct ExpressPreferenceMessage` from an application client.  
+ * We have received a `struct ExpressPreferenceMessage` from an application client.
  *
  * @param cls handle to the client
  * @param msg the start message
  */
 static void
 handle_suggest_cancel (void *cls,
-                      const struct ExpressPreferenceMessage *msg)
+                       const struct ExpressPreferenceMessage *msg)
 {
   struct Client *c = cls;
   struct ClientPreference *cp;
-  
+
   if (CT_NONE == c->type)
     c->type = CT_APPLICATION;
   if (CT_APPLICATION != c->type)
@@ -348,9 +362,8 @@ handle_suggest_cancel (void *cls,
        cp = cp->next)
     if ( (cp->pref.pk == (enum GNUNET_MQ_PreferenceKind) ntohl (msg->pk)) &&
         (cp->pref.bw.value__ == msg->bw.value__) &&
-        (0 == memcmp (&cp->pref.peer,
-                      &msg->peer,
-                      sizeof (struct GNUNET_PeerIdentity))) )
+        (0 == GNUNET_memcmp (&cp->pref.peer,
+                      &msg->peer)) )
       break;
   if (NULL == cp)
   {
@@ -401,7 +414,7 @@ handle_start (void *cls,
 
 
 /**
- * Check 'session_add' message is well-formed and comes from a 
+ * Check 'session_add' message is well-formed and comes from a
  * transport client.
  *
  * @param cls client that sent the request
@@ -436,7 +449,7 @@ handle_session_add (void *cls,
 {
   struct Client *c = cls;
   const char *address = (const char *) &message[1];
-  struct GNUNET_ATS_Session *session;  
+  struct GNUNET_ATS_Session *session;
   int inbound_only = (GNUNET_MESSAGE_TYPE_ATS_SESSION_ADD_INBOUND_ONLY ==
                      ntohs (message->header.type));
 
@@ -464,6 +477,11 @@ handle_session_add (void *cls,
   session->sh = plugin->session_add (plugin->cls,
                                     &session->data,
                                     address);
+  GNUNET_assert (NULL != session->sh);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Transport has new session %p to %s\n",
+              session,
+              GNUNET_i2s (&message->peer));
   GNUNET_SERVICE_client_continue (c->client);
 }
 
@@ -480,7 +498,7 @@ handle_session_update (void *cls,
 {
   struct Client *c = cls;
   struct GNUNET_ATS_Session *session;
-  
+
   if (CT_TRANSPORT != c->type)
   {
     GNUNET_break (0);
@@ -530,14 +548,20 @@ handle_session_del (void *cls,
     GNUNET_break (0);
     GNUNET_SERVICE_client_drop (c->client);
     return;
-  } 
+  }
+  GNUNET_assert (NULL != session->sh);
   plugin->session_del (plugin->cls,
                       session->sh,
                       &session->data);
+  session->sh = NULL;
   GNUNET_assert (GNUNET_YES ==
                 GNUNET_CONTAINER_multihashmap32_remove (c->details.transport.sessions,
                                                         session->session_id,
                                                         session));
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Transport lost session %p to %s\n",
+              session,
+              GNUNET_i2s (&session->data.peer));
   GNUNET_free (session);
   GNUNET_SERVICE_client_continue (c->client);
 }
@@ -583,9 +607,11 @@ free_session (void *cls,
 
   (void) key;
   GNUNET_assert (c == session->client);
+  GNUNET_assert (NULL != session->sh);
   plugin->session_del (plugin->cls,
                       session->sh,
                       &session->data);
+  session->sh = NULL;
   GNUNET_free (session);
   return GNUNET_OK;
 }
@@ -640,21 +666,26 @@ client_disconnect_cb (void *cls,
 
 
 /**
- * Task run during shutdown.
+ * Task run at the end during shutdown.
  *
  * @param cls unused
  */
 static void
-cleanup_task (void *cls)
+final_cleanup (void *cls)
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "ATS shutdown initiated\n");
+  (void) cls;
   if (NULL != stats)
   {
     GNUNET_STATISTICS_destroy (stats,
                               GNUNET_NO);
     stats = NULL;
   }
+  if (NULL != plugin)
+  {
+    GNUNET_PLUGIN_unload (plugin_name,
+                         plugin);
+    plugin = NULL;
+  }
   if (NULL != plugin_name)
   {
     GNUNET_free (plugin_name);
@@ -663,6 +694,22 @@ cleanup_task (void *cls)
 }
 
 
+/**
+ * Task run during shutdown.
+ *
+ * @param cls unused
+ */
+static void
+cleanup_task (void *cls)
+{
+  (void) cls;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "ATS shutdown initiated\n");
+  GNUNET_SCHEDULER_add_now (&final_cleanup,
+                            NULL);
+}
+
+
 /**
  * Process template requests.
  *
@@ -677,7 +724,7 @@ run (void *cls,
 {
   static struct GNUNET_ATS_PluginEnvironment env;
   char *solver;
-  
+
   stats = GNUNET_STATISTICS_create ("ats",
                                    cfg);
   if (GNUNET_SYSERR ==
@@ -708,7 +755,7 @@ run (void *cls,
                 _("Failed to initialize solver `%s'!\n"),
                 plugin_name);
     GNUNET_SCHEDULER_shutdown ();
-    return;   
+    return;
   }
 }
 
@@ -724,13 +771,13 @@ GNUNET_SERVICE_MAIN
  &client_disconnect_cb,
  NULL,
  GNUNET_MQ_hd_fixed_size (suggest,
-                         GNUNET_MESSAGE_TYPE_ATS_SUGGEST,
-                         struct ExpressPreferenceMessage,
-                         NULL),
+                          GNUNET_MESSAGE_TYPE_ATS_SUGGEST,
+                          struct ExpressPreferenceMessage,
+                          NULL),
  GNUNET_MQ_hd_fixed_size (suggest_cancel,
-                         GNUNET_MESSAGE_TYPE_ATS_SUGGEST_CANCEL,
-                         struct ExpressPreferenceMessage,
-                         NULL),
+                          GNUNET_MESSAGE_TYPE_ATS_SUGGEST_CANCEL,
+                          struct ExpressPreferenceMessage,
+                          NULL),
  GNUNET_MQ_hd_fixed_size (start,
                          GNUNET_MESSAGE_TYPE_ATS_START,
                          struct GNUNET_MessageHeader,
@@ -743,11 +790,11 @@ GNUNET_SERVICE_MAIN
                        GNUNET_MESSAGE_TYPE_ATS_SESSION_ADD_INBOUND_ONLY,
                        struct SessionAddMessage,
                        NULL),
- GNUNET_MQ_hd_fixed_size (session_update, 
+ GNUNET_MQ_hd_fixed_size (session_update,
                          GNUNET_MESSAGE_TYPE_ATS_SESSION_UPDATE,
                          struct SessionUpdateMessage,
                          NULL),
- GNUNET_MQ_hd_fixed_size (session_del, 
+ GNUNET_MQ_hd_fixed_size (session_del,
                          GNUNET_MESSAGE_TYPE_ATS_SESSION_DEL,
                          struct SessionDelMessage,
                          NULL),