-cleaning up client api to test for service availability
[oweals/gnunet.git] / src / arm / arm_monitor_api.c
index dc690b904888da74449b3c09aaefa53525959bcb..f8d41e7908f0161c0519e36cfa222fbc2ed0321e 100644 (file)
@@ -88,7 +88,7 @@ struct GNUNET_ARM_MonitorHandle
 static void
 monitor_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg);
 
-static void
+static int
 reconnect_arm_monitor (struct GNUNET_ARM_MonitorHandle *h);
 
 /**
@@ -156,6 +156,7 @@ init_timeout_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Init message timed out\n");
 
+  h->init_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
   reconnect_arm_monitor_later (h);
 }
 
@@ -211,7 +212,7 @@ transmit_monitoring_init_message (void *cls, size_t size, void *buf)
 }
 
 
-static void
+static int
 reconnect_arm_monitor (struct GNUNET_ARM_MonitorHandle *h)
 {
   GNUNET_assert (NULL == h->monitor);
@@ -220,91 +221,83 @@ reconnect_arm_monitor (struct GNUNET_ARM_MonitorHandle *h)
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
           "arm_api, GNUNET_CLIENT_connect returned NULL\n");
-    GNUNET_CLIENT_disconnect (h->monitor);
-    h->monitor = NULL;
-    return;
+    if (NULL != h->service_status)
+      h->service_status (h->cls, NULL, GNUNET_ARM_SERVICE_STOPPED);
+    return GNUNET_SYSERR;
   }
   LOG (GNUNET_ERROR_TYPE_DEBUG,
         "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
   h->cth = GNUNET_CLIENT_notify_transmit_ready (h->monitor,
       sizeof (struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL,
       GNUNET_NO, &transmit_monitoring_init_message, h);
+  return GNUNET_OK;
 }
 
 
 /**
- * Setup a context for monitoring ARM.  Note that this
- * can be done even if the ARM service is not yet running.
- * Never fails.
+ * Setup a context for monitoring ARM, then
+ * start connecting to the ARM service for monitoring using that context.
  *
  * @param cfg configuration to use (needed to contact ARM;
  *        the ARM service may internally use a different
  *        configuration to determine how to start the service).
- * @return context to use for further ARM monitoring operations
- */
-struct GNUNET_ARM_MonitorHandle *
-GNUNET_ARM_monitor_alloc (const struct GNUNET_CONFIGURATION_Handle *cfg)
-{
-  struct GNUNET_ARM_MonitorHandle *ret;
-
-  ret = GNUNET_malloc (sizeof (struct GNUNET_ARM_MonitorHandle));
-  ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
-  ret->currently_down = GNUNET_YES;
-  ret->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
-  ret->init_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
-  return ret;
-}
-
-
-/**
- * Start connecting to the ARM service for monitoring using the context.
- *
- * @param h ARM monitor handle
  * @param cont callback to invoke on status updates
  * @param cont_cls closure
+ * @return context to use for further ARM monitor operations, NULL on error.
  */
-void
-GNUNET_ARM_monitor (struct GNUNET_ARM_MonitorHandle *h,
+struct GNUNET_ARM_MonitorHandle *
+GNUNET_ARM_monitor (const struct GNUNET_CONFIGURATION_Handle *cfg,
     GNUNET_ARM_ServiceStatusCallback cont, void *cont_cls)
 {
+  struct GNUNET_ARM_MonitorHandle *h;
+
+  h = GNUNET_malloc (sizeof (struct GNUNET_ARM_MonitorHandle));
+  h->cfg = GNUNET_CONFIGURATION_dup (cfg);
+  h->currently_down = GNUNET_YES;
+  h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+  h->init_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
   h->service_status = cont;
   h->cls = cont_cls;
-  reconnect_arm_monitor (h);
+  if (GNUNET_OK != reconnect_arm_monitor (h))
+  {
+    GNUNET_free (h);
+    return NULL;
+  }
+  return h;
 }
 
 
 /**
  * Disconnect from the ARM service (if connected) and destroy the context.
- * Don't call inside a callback!
  *
  * @param h the handle that was being used
  */
 void
-GNUNET_ARM_monitor_disconnect (struct GNUNET_ARM_MonitorHandle *handle)
+GNUNET_ARM_monitor_disconnect_and_free (struct GNUNET_ARM_MonitorHandle *h)
 {
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from ARM service\n");
-  if (NULL != handle->cth)
+  if (NULL != h->cth)
   {
-    GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
-    handle->cth = NULL;
+    GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
+    h->cth = NULL;
   }
-  if (GNUNET_SCHEDULER_NO_TASK != handle->init_timeout_task_id)
+  if (GNUNET_SCHEDULER_NO_TASK != h->init_timeout_task_id)
   {
-    GNUNET_SCHEDULER_cancel (handle->init_timeout_task_id);
-    handle->init_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
+    GNUNET_SCHEDULER_cancel (h->init_timeout_task_id);
+    h->init_timeout_task_id = GNUNET_SCHEDULER_NO_TASK;
   }
-  if (NULL != handle->monitor)
+  if (NULL != h->monitor)
   {
-    GNUNET_CLIENT_disconnect (handle->monitor);
-    handle->monitor = NULL;
+    GNUNET_CLIENT_disconnect (h->monitor);
+    h->monitor = NULL;
   }
-  if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
+  if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
   {
-    GNUNET_SCHEDULER_cancel (handle->reconnect_task);
-    handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+    GNUNET_SCHEDULER_cancel (h->reconnect_task);
+    h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
   }
-  GNUNET_CONFIGURATION_destroy (handle->cfg);
-  GNUNET_free (handle);
+  GNUNET_CONFIGURATION_destroy (h->cfg);
+  GNUNET_free (h);
 }
 
 
@@ -352,15 +345,15 @@ monitor_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg)
          "Received response from ARM for service `%s': %u\n",
          (const char *) &res[1], ntohs (msg->type));
     status = (enum GNUNET_ARM_ServiceStatus) ntohl (res->status);
-    if ((NULL != h->service_status))
-      h->service_status (h->cls, h, (const char *) &res[1], status);
+    GNUNET_CLIENT_receive (h->monitor, &monitor_notify_handler, h,
+                           GNUNET_TIME_UNIT_FOREVER_REL);
+    if (NULL != h->service_status)
+      h->service_status (h->cls, (const char *) &res[1], status);
     break;
   default:
     reconnect_arm_monitor_later (h);
     return;
   }
-  GNUNET_CLIENT_receive (h->monitor, &monitor_notify_handler, h,
-      GNUNET_TIME_UNIT_FOREVER_REL);
 }