update tests to use new MQ API
authorChristian Grothoff <christian@grothoff.org>
Tue, 5 Jul 2016 11:51:32 +0000 (11:51 +0000)
committerChristian Grothoff <christian@grothoff.org>
Tue, 5 Jul 2016 11:51:32 +0000 (11:51 +0000)
src/util/test_client.c
src/util/test_server.c
src/util/test_service.c
src/util/test_socks.c

index 97fe1011053ef169d60269b766db87666cd75d1f..89adf41a36e87fc164db81b6b8654e2b7fa5d876 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2009 GNUnet e.V.
+     Copyright (C) 2009, 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
index 189c13e18e95c9eb7576e025d3f1ac37cbc01f84..f82ecb2b1ae481f323bdc18f33ebeff0eb493591 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2009, 2010, 2014 GNUnet e.V.
+     Copyright (C) 2009, 2010, 2014, 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
@@ -52,7 +52,7 @@ static struct GNUNET_SERVER_Handle *server;
 /**
  * Handle for the client.
  */
-static struct GNUNET_CLIENT_Connection *cc;
+static struct GNUNET_MQ_Handle *mq;
 
 /**
  * Handle of the server for the client.
@@ -81,7 +81,7 @@ finish_up (void *cls)
   GNUNET_assert (7 == ok);
   ok = 0;
   GNUNET_SERVER_destroy (server);
-  GNUNET_CLIENT_disconnect (cc);
+  GNUNET_MQ_destroy (mq);
   GNUNET_CONFIGURATION_destroy (cfg);
 }
 
@@ -105,32 +105,6 @@ recv_fin_cb (void *cls,
 }
 
 
-/**
- * The client connected to the server and is now allowed
- * to send a second message.  We send one.
- *
- * @param cls NULL
- * @param size number of bytes that can be transmitted
- * @param buf where to copy the message
- * @return number of bytes copied to @a buf
- */
-static size_t
-transmit_second_message (void *cls,
-                         size_t size,
-                         void *buf)
-{
-  struct GNUNET_MessageHeader msg;
-
-  GNUNET_assert (5 == ok);
-  ok = 6;
-  GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
-  msg.type = htons (MY_TYPE2);
-  msg.size = htons (sizeof (struct GNUNET_MessageHeader));
-  memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
-  return sizeof (struct GNUNET_MessageHeader);
-}
-
-
 /**
  * We have received the reply from the server, check that we are at
  * the right stage and queue the next message to the server.  Cleans
@@ -140,18 +114,18 @@ transmit_second_message (void *cls,
  * @param msg message we got from the server
  */
 static void
-first_reply_handler (void *cls,
-                     const struct GNUNET_MessageHeader *msg)
+handle_reply (void *cls,
+              const struct GNUNET_MessageHeader *msg)
 {
+  struct GNUNET_MQ_Envelope *env;
+  struct GNUNET_MessageHeader *m;
+
   GNUNET_assert (4 == ok);
-  ok = 5;
-  GNUNET_assert (NULL !=
-                 GNUNET_CLIENT_notify_transmit_ready (cc,
-                                                      sizeof (struct GNUNET_MessageHeader),
-                                                      TIMEOUT,
-                                                      GNUNET_YES,
-                                                      &transmit_second_message,
-                                                      NULL));
+  ok = 6;
+  env = GNUNET_MQ_msg (m,
+                       MY_TYPE2);
+  GNUNET_MQ_send (mq,
+                  env);
 }
 
 
@@ -210,40 +184,12 @@ recv_cb (void *cls,
   GNUNET_assert (NULL !=
                  GNUNET_SERVER_notify_transmit_ready (client,
                                                       ntohs (message->size),
-                                                      TIMEOUT, &reply_msg,
+                                                      TIMEOUT,
+                                                      &reply_msg,
                                                       NULL));
 }
 
 
-/**
- * The client connected to the server and is now allowed
- * to send a first message.  We transmit a simple message,
- * ask for a second transmission and get ready to receive
- * a response.
- *
- * @param cls NULL
- * @param size number of bytes that can be transmitted
- * @param buf where to copy the message
- * @return number of bytes copied to @a buf
- */
-static size_t
-transmit_initial_message (void *cls,
-                          size_t size,
-                          void *buf)
-{
-  struct GNUNET_MessageHeader msg;
-
-  GNUNET_assert (1 == ok);
-  ok = 2;
-  GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
-  msg.type = htons (MY_TYPE);
-  msg.size = htons (sizeof (struct GNUNET_MessageHeader));
-  memcpy (buf, &msg, sizeof (struct GNUNET_MessageHeader));
-  GNUNET_CLIENT_receive (cc, &first_reply_handler, NULL, TIMEOUT);
-  return sizeof (struct GNUNET_MessageHeader);
-}
-
-
 /**
  * Message handlers for the server.
  */
@@ -254,6 +200,22 @@ static struct GNUNET_SERVER_MessageHandler handlers[] = {
 };
 
 
+/**
+ * Generic error handler, called with the appropriate error code and
+ * the same closure specified at the creation of the message queue.
+ * Not every message queue implementation supports an error handler.
+ *
+ * @param cls closure with the `struct GNUNET_STATISTICS_Handle *`
+ * @param error error code
+ */
+static void
+mq_error_handler (void *cls,
+                  enum GNUNET_MQ_Error error)
+{
+  GNUNET_assert (0); /* should never happen */
+}
+
+
 /**
  * First task run by the scheduler.  Initializes the server and
  * a client and asks for a transmission from the client to the
@@ -267,6 +229,16 @@ task (void *cls)
   struct sockaddr_in sa;
   struct sockaddr *sap[2];
   socklen_t slens[2];
+  struct GNUNET_MQ_Envelope *env;
+  struct GNUNET_MessageHeader *msg;
+  GNUNET_MQ_hd_fixed_size (reply,
+                           MY_TYPE,
+                           struct GNUNET_MessageHeader);
+  struct GNUNET_MQ_MessageHandler chandlers[] = {
+    make_reply_handler (cls),
+    GNUNET_MQ_handler_end ()
+  };
+
 
   sap[0] = (struct sockaddr *) &sa;
   slens[0] = sizeof (sa);
@@ -296,15 +268,17 @@ task (void *cls)
                                          "resolver",
                                          "HOSTNAME",
                                          "localhost");
-  cc = GNUNET_CLIENT_connect ("test-server", cfg);
-  GNUNET_assert (cc != NULL);
-  GNUNET_assert (NULL !=
-                 GNUNET_CLIENT_notify_transmit_ready (cc,
-                                                      sizeof (struct
-                                                              GNUNET_MessageHeader),
-                                                      TIMEOUT, GNUNET_YES,
-                                                      &transmit_initial_message,
-                                                      NULL));
+  mq = GNUNET_CLIENT_connecT (cfg,
+                              "test-server",
+                              chandlers,
+                              &mq_error_handler,
+                              NULL);
+  GNUNET_assert (NULL != mq);
+  ok = 2;
+  env = GNUNET_MQ_msg (msg,
+                       MY_TYPE);
+  GNUNET_MQ_send (mq,
+                  env);
 }
 
 
index f340b724d46bc47757c40912d8d112eeb72eddd6..707f8658b0f7918daf01bf602c9185400923663c 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2009, 2013 GNUnet e.V.
+     Copyright (C) 2009, 2013, 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
@@ -37,16 +37,16 @@ static struct GNUNET_SERVICE_Context *sctx;
 
 static int ok = 1;
 
-static struct GNUNET_CLIENT_Connection *client;
+static struct GNUNET_MQ_Handle *mq;
 
 
 static void
 do_stop (void *cls)
 {
-  if (NULL != client)
+  if (NULL != mq)
   {
-    GNUNET_CLIENT_disconnect (client);
-    client = NULL;
+    GNUNET_MQ_destroy (mq);
+    mq = NULL;
   }
   if (NULL != sctx)
   {
@@ -60,49 +60,33 @@ do_stop (void *cls)
 }
 
 
-static size_t
-build_msg (void *cls, size_t size, void *buf)
-{
-  struct GNUNET_MessageHeader *msg = buf;
-
-  if (size < sizeof (struct GNUNET_MessageHeader))
-  {
-    /* timeout */
-    GNUNET_break (0);
-    GNUNET_SCHEDULER_add_now (&do_stop, NULL);
-    ok = 1;
-    return 0;
-  }
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client connected, transmitting\n");
-  GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
-  msg->type = htons (MY_TYPE);
-  msg->size = htons (sizeof (struct GNUNET_MessageHeader));
-  return sizeof (struct GNUNET_MessageHeader);
-}
-
-
 static void
 ready (void *cls,
        int result)
 {
   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
+  struct GNUNET_MQ_Envelope *env;
+  struct GNUNET_MessageHeader *msg;
 
   GNUNET_assert (GNUNET_YES == result);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service confirmed running\n");
-  client = GNUNET_CLIENT_connect ("test_service", cfg);
-  GNUNET_assert (client != NULL);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Client connecting, waiting to transmit\n");
-  GNUNET_CLIENT_notify_transmit_ready (client,
-                                       sizeof (struct GNUNET_MessageHeader),
-                                       GNUNET_TIME_UNIT_SECONDS, GNUNET_NO,
-                                       &build_msg, NULL);
+              "Service confirmed running\n");
+  mq = GNUNET_CLIENT_connecT (cfg,
+                              "test_service",
+                              NULL,
+                              NULL,
+                              NULL);
+  GNUNET_assert (NULL != mq);
+  env = GNUNET_MQ_msg (msg,
+                       MY_TYPE);
+  GNUNET_MQ_send (mq,
+                  env);
 }
 
 
 static void
-recv_cb (void *cls, struct GNUNET_SERVER_Client *sc,
+recv_cb (void *cls,
+         struct GNUNET_SERVER_Client *sc,
          const struct GNUNET_MessageHeader *message)
 {
   GNUNET_assert (NULL != message);
@@ -120,11 +104,14 @@ static struct GNUNET_SERVER_MessageHandler myhandlers[] = {
 
 
 static void
-runner (void *cls, struct GNUNET_SERVER_Handle *server,
+runner (void *cls,
+        struct GNUNET_SERVER_Handle *server,
         const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service initializing\n");
-  GNUNET_SERVER_add_handlers (server, myhandlers);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Service initializing\n");
+  GNUNET_SERVER_add_handlers (server,
+                              myhandlers);
   GNUNET_CLIENT_service_test ("test_service", cfg, GNUNET_TIME_UNIT_SECONDS,
                               &ready, (void *) cfg);
 }
@@ -158,21 +145,28 @@ ready6 (void *cls,
        int result)
 {
   const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
+  struct GNUNET_MQ_Envelope *env;
+  struct GNUNET_MessageHeader *msg;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 ready\n");
   GNUNET_assert (GNUNET_YES == result);
-  client = GNUNET_CLIENT_connect ("test_service6", cfg);
-  GNUNET_assert (client != NULL);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "V6 client connected\n");
-  GNUNET_CLIENT_notify_transmit_ready (client,
-                                       sizeof (struct GNUNET_MessageHeader),
-                                       GNUNET_TIME_UNIT_SECONDS, GNUNET_NO,
-                                       &build_msg, NULL);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "V6 ready\n");
+  mq = GNUNET_CLIENT_connecT (cfg,
+                              "test_service6",
+                              NULL,
+                              NULL,
+                              NULL);
+  GNUNET_assert (NULL != mq);
+  env = GNUNET_MQ_msg (msg,
+                       MY_TYPE);
+  GNUNET_MQ_send (mq,
+                  env);
 }
 
 
 static void
-runner6 (void *cls, struct GNUNET_SERVER_Handle *server,
+runner6 (void *cls,
+         struct GNUNET_SERVER_Handle *server,
          const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
index d63cce571571230f1509773e192d7aff1ad497f7..4b86d25dcfc58f470aca660f52097acc07cc6c37 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2015 GNUnet e.V.
+     Copyright (C) 2015, 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