convert conversation_api_call.c
[oweals/gnunet.git] / src / ats / ats_api_performance.c
index d294946053b9e2bd61e93e37572606d82377ed9a..48bb2ebf49a97ac7315de8a0376765a9da777049 100644 (file)
@@ -1,6 +1,6 @@
 /*
  This file is part of GNUnet.
- Copyright (C) 2010,2011 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2010,2011 GNUnet e.V.
 
  GNUnet is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
  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 ats/ats_api_performance.c
@@ -241,6 +241,11 @@ struct GNUNET_ATS_PerformanceHandle
    * Request multiplexing
    */
   uint32_t id;
+
+  /**
+   * Is the receive loop active?
+   */
+  int in_receive;
 };
 
 /**
@@ -256,11 +261,9 @@ reconnect (struct GNUNET_ATS_PerformanceHandle *ph);
  * Re-establish the connection to the ATS service.
  *
  * @param cls handle to use to re-connect.
- * @param tc scheduler context
  */
 static void
-reconnect_task (void *cls,
-                const struct GNUNET_SCHEDULER_TaskContext *tc)
+reconnect_task (void *cls)
 {
   struct GNUNET_ATS_PerformanceHandle *ph = cls;
 
@@ -323,6 +326,14 @@ transmit_message_to_ats (void *cls,
     GNUNET_free(p);
   }
   do_transmit (ph);
+  if (GNUNET_NO == ph->in_receive)
+  {
+    ph->in_receive = GNUNET_YES;
+    GNUNET_CLIENT_receive (ph->client,
+                           &process_ats_message,
+                           ph,
+                           GNUNET_TIME_UNIT_FOREVER_REL);
+  }
   return ret;
 }
 
@@ -396,6 +407,7 @@ process_pi_message (struct GNUNET_ATS_PerformanceHandle *ph,
     GNUNET_ATS_properties_ntoh (&prop,
                                 &pi->properties);
     address.peer = pi->peer;
+    address.local_info = (enum GNUNET_HELLO_AddressInfo) ntohl (pi->address_local_info);
     address.address = plugin_address;
     address.address_length = plugin_address_length;
     address.transport_name = plugin_name;
@@ -469,7 +481,7 @@ process_rr_message (struct GNUNET_ATS_PerformanceHandle *ph,
 
 
 /**
- * We received a reservation result message.  Validate and process it.
+ * We received a PeerInformationMessage.  Validate and process it.
  *
  * @param ph our context with the callback
  * @param msg the message
@@ -593,18 +605,27 @@ process_ats_message (void *cls,
   {
   case GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION:
     if (GNUNET_OK != process_pi_message (ph, msg))
+    {
+      GNUNET_break (0);
       goto reconnect;
+    }
     break;
   case GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT:
     if (GNUNET_OK != process_rr_message (ph, msg))
+    {
+      GNUNET_break (0);
       goto reconnect;
+    }
     break;
   case GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE:
     if (GNUNET_OK != process_ar_message (ph, msg))
+    {
+      GNUNET_break (0);
       goto reconnect;
+    }
     break;
   default:
-    GNUNET_break(0);
+    GNUNET_break (0);
     goto reconnect;
   }
   ph->backoff = GNUNET_TIME_UNIT_ZERO;
@@ -622,17 +643,21 @@ process_ats_message (void *cls,
     GNUNET_CLIENT_notify_transmit_ready_cancel (ph->th);
     ph->th = NULL;
   }
-  GNUNET_CLIENT_disconnect (ph->client);
-  ph->client = NULL;
-  if (NULL != ph->addr_info_cb)
+  if (NULL != ph->client)
   {
-    /* Indicate reconnect */
-    ph->addr_info_cb (ph->addr_info_cb_cls,
-                      NULL,
-                      GNUNET_NO,
-                      GNUNET_BANDWIDTH_value_init (0),
-                      GNUNET_BANDWIDTH_value_init (0),
-                      NULL);
+    GNUNET_CLIENT_disconnect (ph->client);
+    ph->client = NULL;
+    ph->in_receive = GNUNET_NO;
+    if (NULL != ph->addr_info_cb)
+    {
+      /* Indicate reconnect */
+      ph->addr_info_cb (ph->addr_info_cb_cls,
+                        NULL,
+                        GNUNET_NO,
+                        GNUNET_BANDWIDTH_value_init (0),
+                        GNUNET_BANDWIDTH_value_init (0),
+                        NULL);
+    }
   }
   ph->backoff = GNUNET_TIME_STD_BACKOFF (ph->backoff);
   ph->task = GNUNET_SCHEDULER_add_delayed (ph->backoff,
@@ -656,10 +681,6 @@ reconnect (struct GNUNET_ATS_PerformanceHandle *ph)
   ph->client = GNUNET_CLIENT_connect ("ats",
                                       ph->cfg);
   GNUNET_assert (NULL != ph->client);
-  GNUNET_CLIENT_receive (ph->client,
-                         &process_ats_message,
-                         ph,
-                         GNUNET_TIME_UNIT_FOREVER_REL);
   if ((NULL == (p = ph->pending_head)) || (GNUNET_YES != p->is_init))
   {
     p = GNUNET_malloc (sizeof (struct PendingMessage) +