src: for every AGPL3.0 file, add SPDX identifier.
[oweals/gnunet.git] / src / arm / test_arm_api.c
index 2bdc0e941ad8eaec4f69bb8327b0df8fa0a00008..f851595b0855a1ac39762c126d9aed192d729916 100644 (file)
@@ -1,21 +1,21 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2009, 2011 GNUnet e.V.
+     Copyright (C) 2009, 2011, 2016 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
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
+     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/>.
 
-     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., 51 Franklin Street, Fifth Floor,
-     Boston, MA 02110-1301, USA.
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 /**
  * @file arm/test_arm_api.c
 
 #define LOG(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
 
-#define START_ARM GNUNET_YES
-
-#define START_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 1500)
-
 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
 
 static const struct GNUNET_CONFIGURATION_Handle *cfg;
 
 static struct GNUNET_ARM_Handle *arm;
 
+static struct GNUNET_ARM_Operation *op;
+
 static int ok = 1;
 
 static int phase = 0;
 
+
 static void
 arm_stop_cb (void *cls,
             enum GNUNET_ARM_RequestStatus status,
-            const char *servicename,
             enum GNUNET_ARM_Result result)
 {
+  op = NULL;
   /* (6), a stop request should be sent to ARM successfully */
   /* ARM should report that it is stopping */
   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
   GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
   GNUNET_break (phase == 6);
   phase++;
-  LOG ("Sent 'STOP' request for arm to ARM %s\n", (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
+  LOG ("Sent 'STOP' request for arm to ARM %s\n",
+       (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
+  GNUNET_SCHEDULER_shutdown ();
 }
 
 
 static void
 resolver_stop_cb (void *cls,
                  enum GNUNET_ARM_RequestStatus status,
-                 const char *servicename, enum GNUNET_ARM_Result result)
+                  enum GNUNET_ARM_Result result)
 {
+  op = NULL;
   /* (5), a stop request should be sent to ARM successfully.
    * ARM should report that resolver is stopped.
    */
   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
   GNUNET_break (result == GNUNET_ARM_RESULT_STOPPED);
   GNUNET_break (phase == 5);
-  LOG ("Sent 'STOP' request for resolver to ARM %s\n", (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
+  LOG ("Sent 'STOP' request for resolver to ARM %s\n",
+       (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
   phase++;
-#if START_ARM
-  GNUNET_ARM_request_service_stop (arm, "arm", TIMEOUT, arm_stop_cb, NULL);
-#else
-  arm_stop_cb (NULL, GNUNET_ARM_STATUS_SENT_OK, "arm", GNUNET_ARM_SERVICE_STOPPING);
-  arm_conn (NULL, GNUNET_NO);
-#endif
+  GNUNET_assert (NULL == op);
+  op = GNUNET_ARM_request_service_stop (arm,
+                                        "arm",
+                                        &arm_stop_cb,
+                                        NULL);
 }
 
 
 static void
-dns_notify (void *cls, const struct sockaddr *addr, socklen_t addrlen)
+dns_notify (void *cls,
+            const struct sockaddr *addr,
+            socklen_t addrlen)
 {
   if (addr == NULL)
     {
@@ -91,7 +95,11 @@ dns_notify (void *cls, const struct sockaddr *addr, socklen_t addrlen)
       LOG ("Finished resolving localhost\n");
       if (ok != 0)
         ok = 2;
-      GNUNET_ARM_request_service_stop (arm, "resolver", TIMEOUT, resolver_stop_cb, NULL);
+      GNUNET_assert (NULL == op);
+      op = GNUNET_ARM_request_service_stop (arm,
+                                            "resolver",
+                                            &resolver_stop_cb,
+                                            NULL);
       return;
     }
   /* (3), resolver should resolve localhost */
@@ -106,25 +114,22 @@ dns_notify (void *cls, const struct sockaddr *addr, socklen_t addrlen)
 static void
 resolver_start_cb (void *cls,
                   enum GNUNET_ARM_RequestStatus status,
-                  const char *servicename,
                   enum GNUNET_ARM_Result result)
 {
+  op = NULL;
   /* (2), the start request for resolver should be sent successfully
    * ARM should report that resolver service is starting.
    */
   GNUNET_assert (status == GNUNET_ARM_REQUEST_SENT_OK);
   GNUNET_break (phase == 2);
   GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
-  LOG ("Sent 'START' request for resolver to ARM %s\n", (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
+  LOG ("Sent 'START' request for resolver to ARM %s\n",
+       (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
   phase++;
-  GNUNET_RESOLVER_ip_get ("localhost", AF_INET, TIMEOUT, &dns_notify, NULL);
-}
-
-
-static void
-trigger_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  GNUNET_ARM_disconnect_and_free ((struct GNUNET_ARM_Handle *) cls);
+  GNUNET_RESOLVER_ip_get ("localhost",
+                          AF_INET,
+                          TIMEOUT,
+                          &dns_notify, NULL);
 }
 
 
@@ -146,7 +151,12 @@ arm_conn (void *cls,
     LOG ("Connected to ARM\n");
     GNUNET_break (phase == 1);
     phase++;
-    GNUNET_ARM_request_service_start (arm, "resolver", GNUNET_OS_INHERIT_STD_OUT_AND_ERR, START_TIMEOUT, resolver_start_cb, NULL);
+    GNUNET_assert (NULL == op);
+    op = GNUNET_ARM_request_service_start (arm,
+                                           "resolver",
+                                           GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
+                                           &resolver_start_cb,
+                                           NULL);
   }
   else
   {
@@ -157,7 +167,6 @@ arm_conn (void *cls,
       ok = 3;
     else if (ok == 1)
       ok = 0;
-    GNUNET_SCHEDULER_add_now (trigger_disconnect, arm);
   }
 }
 
@@ -165,9 +174,9 @@ arm_conn (void *cls,
 static void
 arm_start_cb (void *cls,
              enum GNUNET_ARM_RequestStatus status,
-             const char *servicename,
              enum GNUNET_ARM_Result result)
 {
+  op = NULL;
   /* (0) The request should be "sent" successfully
    * ("sent", because it isn't going anywhere, ARM API starts ARM service
    * by itself).
@@ -175,26 +184,48 @@ arm_start_cb (void *cls,
    */
   GNUNET_break (status == GNUNET_ARM_REQUEST_SENT_OK);
   GNUNET_break (phase == 0);
-  LOG ("Sent 'START' request for arm to ARM %s\n", (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
+  LOG ("Sent 'START' request for arm to ARM %s\n",
+       (status == GNUNET_ARM_REQUEST_SENT_OK) ? "successfully" : "unsuccessfully");
   GNUNET_break (result == GNUNET_ARM_RESULT_STARTING);
   phase++;
 }
 
 
 static void
-task (void *cls, char *const *args, const char *cfgfile,
+do_shutdown (void *cls)
+{
+  if (NULL != op)
+  {
+    GNUNET_ARM_operation_cancel (op);
+    op = NULL;
+  }
+  if (NULL != arm)
+  {
+    GNUNET_ARM_disconnect (arm);
+    arm = NULL;
+  }
+}
+
+
+static void
+task (void *cls,
+      char *const *args,
+      const char *cfgfile,
       const struct GNUNET_CONFIGURATION_Handle *c)
 {
   cfg = c;
-  arm = GNUNET_ARM_connect (cfg, &arm_conn, NULL);
+  arm = GNUNET_ARM_connect (cfg,
+                            &arm_conn,
+                            NULL);
   if (NULL == arm)
     return;
-#if START_ARM
-  GNUNET_ARM_request_service_start (arm, "arm", GNUNET_OS_INHERIT_STD_OUT_AND_ERR, START_TIMEOUT, arm_start_cb, NULL);
-#else
-  arm_start_cb (NULL, arm, GNUNET_ARM_REQUEST_SENT_OK, "arm", GNUNET_ARM_RESULT_STARTING);
-  arm_conn (NULL, arm, GNUNET_YES);
-#endif
+  GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
+                                 NULL);
+  op = GNUNET_ARM_request_service_start (arm,
+                                         "arm",
+                                         GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
+                                         &arm_start_cb,
+                                         NULL);
 }