-improve indentation, reduce duplication of PIDs in core's neighbour map
[oweals/gnunet.git] / src / transport / transport_api_monitor_plugins.c
index 290092c22a2c238c12c6568893c2248ff7076f0e..1a8ec8b7c08a449bf4fb0bbcce89cd283e053177 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2014 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2014 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
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
  * @file transport/transport_api_monitor_plugins.c
  * @brief montoring api for transport plugin session status
+ * @author Christian Grothoff
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
@@ -71,7 +72,7 @@ struct GNUNET_TRANSPORT_PluginMonitor
   /**
    * Task ID for reconnect.
    */
-  GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
+  struct GNUNET_SCHEDULER_Task * reconnect_task;
 
 };
 
@@ -140,7 +141,7 @@ do_plugin_connect (void *cls,
 {
   struct GNUNET_TRANSPORT_PluginMonitor *pm = cls;
 
-  pm->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+  pm->reconnect_task = NULL;
   pm->client = GNUNET_CLIENT_connect ("transport", pm->cfg);
   GNUNET_assert (NULL != pm->client);
   send_plugin_mon_request (pm);
@@ -177,21 +178,6 @@ free_entry (void *cls,
 }
 
 
-/**
- * We got disconnected, remove all existing entries from
- * the map and notify client.
- *
- * @param pm montitor that got disconnected
- */
-static void
-clear_map (struct GNUNET_TRANSPORT_PluginMonitor *pm)
-{
-  GNUNET_CONTAINER_multihashmap32_iterate (pm->sessions,
-                                           &free_entry,
-                                           pm);
-}
-
-
 /**
  * Cut the existing connection and reconnect.
  *
@@ -202,7 +188,9 @@ reconnect_plugin_ctx (struct GNUNET_TRANSPORT_PluginMonitor *pm)
 {
   GNUNET_CLIENT_disconnect (pm->client);
   pm->client = NULL;
-  clear_map (pm);
+  GNUNET_CONTAINER_multihashmap32_iterate (pm->sessions,
+                                           &free_entry,
+                                           pm);
   pm->backoff = GNUNET_TIME_STD_BACKOFF (pm->backoff);
   pm->reconnect_task = GNUNET_SCHEDULER_add_delayed (pm->backoff,
                                                      &do_plugin_connect,
@@ -295,6 +283,21 @@ response_processor (void *cls,
     reconnect_plugin_ctx (pm);
     return;
   }
+  if ( (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_SYNC == ntohs (msg->type)) &&
+       (sizeof (struct GNUNET_MessageHeader) == ntohs (msg->size)) )
+  {
+    /* we are in sync */
+    pm->cb (pm->cb_cls,
+            NULL,
+            NULL,
+            NULL);
+    GNUNET_CLIENT_receive (pm->client,
+                           &response_processor,
+                           pm,
+                           GNUNET_TIME_UNIT_FOREVER_REL);
+    return;
+  }
+
   if ( (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_EVENT != ntohs (msg->type)) ||
        (sizeof (struct TransportPluginMonitorMessage) > ntohs (msg->size)) )
   {
@@ -371,6 +374,10 @@ response_processor (void *cls,
                                                            ps));
     GNUNET_free (ps);
   }
+  GNUNET_CLIENT_receive (pm->client,
+                         &response_processor,
+                         pm,
+                         GNUNET_TIME_UNIT_FOREVER_REL);
 }
 
 
@@ -391,7 +398,8 @@ GNUNET_TRANSPORT_monitor_plugins (const struct GNUNET_CONFIGURATION_Handle *cfg,
   struct GNUNET_TRANSPORT_PluginMonitor *pm;
   struct GNUNET_CLIENT_Connection *client;
 
-  client = GNUNET_CLIENT_connect ("transport", cfg);
+  client = GNUNET_CLIENT_connect ("transport",
+                                  cfg);
   if (NULL == client)
     return NULL;
   pm = GNUNET_new (struct GNUNET_TRANSPORT_PluginMonitor);
@@ -399,6 +407,7 @@ GNUNET_TRANSPORT_monitor_plugins (const struct GNUNET_CONFIGURATION_Handle *cfg,
   pm->cb_cls = cb_cls;
   pm->cfg = cfg;
   pm->client = client;
+  pm->sessions = GNUNET_CONTAINER_multihashmap32_create (128);
   send_plugin_mon_request (pm);
   return pm;
 }
@@ -420,12 +429,14 @@ GNUNET_TRANSPORT_monitor_plugins_cancel (struct GNUNET_TRANSPORT_PluginMonitor *
     GNUNET_CLIENT_disconnect (pm->client);
     pm->client = NULL;
   }
-  if (GNUNET_SCHEDULER_NO_TASK != pm->reconnect_task)
+  if (NULL != pm->reconnect_task)
   {
     GNUNET_SCHEDULER_cancel (pm->reconnect_task);
-    pm->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+    pm->reconnect_task = NULL;
   }
-  clear_map (pm);
+  GNUNET_CONTAINER_multihashmap32_iterate (pm->sessions,
+                                           &free_entry,
+                                           pm);
   GNUNET_CONTAINER_multihashmap32_destroy (pm->sessions);
   GNUNET_free (pm);
 }