gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [taler-twister] branch master updated: finish response code


From: gnunet
Subject: [GNUnet-SVN] [taler-twister] branch master updated: finish response code hacking
Date: Sat, 20 Jan 2018 14:54:28 +0100

This is an automated email from the git hooks/post-receive script.

grothoff pushed a commit to branch master
in repository twister.

The following commit(s) were added to refs/heads/master by this push:
     new 8a811ee  finish response code hacking
8a811ee is described below

commit 8a811ee0dba732be528c3d729318a4a2a9ec2930
Author: Christian Grothoff <address@hidden>
AuthorDate: Sat Jan 20 14:54:24 2018 +0100

    finish response code hacking
---
 .gitignore                                         |   9 +-
 src/include/taler_twister_service.h                |   2 +-
 src/twister/Makefile.am                            |  20 +-
 .../{taler-twister.c => taler-twister-service.c}   | 141 ++++++++----
 src/twister/twister.conf                           |  23 ++
 src/twister/twister.h                              |  63 ++++++
 src/twister/twister_api.c                          | 236 +++++++++++++++++++++
 7 files changed, 442 insertions(+), 52 deletions(-)

diff --git a/.gitignore b/.gitignore
index 08be0c7..cf07346 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,6 @@
+*~
+.deps/
+.libs/
 Makefile
 Makefile.in
 aclocal.m4
@@ -19,14 +22,10 @@ src/Makefile
 src/Makefile.in
 src/include/Makefile
 src/include/Makefile.in
-src/include/platform.h~
 src/twister/Makefile
 src/twister/Makefile.in
-src/twister/taler-twister
-src/twister/taler-twister.c~
-src/twister/twister.conf~
+src/twister/taler-twister-service
 stamp-h1
 test-driver
 twister_config.h
 twister_config.h.in
-twister_config.h.in~
diff --git a/src/include/taler_twister_service.h 
b/src/include/taler_twister_service.h
index dbb5e6c..cb43bf6 100644
--- a/src/include/taler_twister_service.h
+++ b/src/include/taler_twister_service.h
@@ -51,7 +51,7 @@ struct TALER_TWISTER_Handle;
  * @return handle to use in #TALER_TWISTER_disconnect to disconnect
  */
 struct TALER_TWISTER_Handle *
-TALER_TWISTER_connect (const struct TALER_CONFIGURATION_Handle *cfg);
+TALER_TWISTER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg);
 
 
 /**
diff --git a/src/twister/Makefile.am b/src/twister/Makefile.am
index bca7dfb..ceedf0a 100644
--- a/src/twister/Makefile.am
+++ b/src/twister/Makefile.am
@@ -7,13 +7,25 @@ if USE_COVERAGE
 endif
 
 bin_PROGRAMS = \
-  taler-twister
+  taler-twister-service
 
-taler_twister_SOURCES = \
-  taler-twister.c
-taler_twister_LDADD = \
+taler_twister_service_SOURCES = \
+  taler-twister-service.c
+taler_twister_service_LDADD = \
   $(LIBGCRYPT_LIBS) \
   -lmicrohttpd \
   -lcurl \
   -ljansson \
   -lgnunetutil
+
+
+lib_LTLIBRARIES = libtalertwister.la
+
+libtalertwister_la_SOURCES = \
+  twister_api.c twister.h
+libtalertwister_la_LIBADD = \
+  -lgnunetutil \
+  $(XLIB)
+libtalertwister_la_LDFLAGS = \
+  $(GN_LIB_LDFLAGS)  $(WINFLAGS) \
+  -version-info 0:0:0
diff --git a/src/twister/taler-twister.c b/src/twister/taler-twister-service.c
similarity index 94%
rename from src/twister/taler-twister.c
rename to src/twister/taler-twister-service.c
index d5a8244..4699d17 100644
--- a/src/twister/taler-twister.c
+++ b/src/twister/taler-twister-service.c
@@ -34,6 +34,7 @@
 #include <gnurl/curl.h>
 #endif
 #include <gnunet/gnunet_util_lib.h>
+#include "twister.h"
 
 
 /**
@@ -225,6 +226,15 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg;
  */
 static char *target_server_base_url;
 
+/* ************************** Transformations ***************** */
+
+/**
+ * Set to non-zero if we should change the next response code.
+ * In this case, this is the value we should use.
+ */
+static unsigned int hack_response_code;
+
+
 
 /* ************************* Global helpers ********************* */
 
@@ -1011,6 +1021,11 @@ create_response (void *cls,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Queueing response with MHD\n");
   run_mhd_now ();
+  if (0 != hack_response_code)
+  {
+    hr->response_code = hack_response_code;
+    hack_response_code = 0; /* reset for next request */
+  }
   return MHD_queue_response (con,
                              hr->response_code,
                              hr->response);
@@ -1270,19 +1285,18 @@ do_shutdown (void *cls)
  * Main function that will be run
  *
  * @param cls closure
- * @param args remaining command-line arguments
- * @param cfgfile name of the configuration file used (for saving, can be 
NULL!)
  * @param c configuration
- */
+ * @param service the initialized service
+*/
 static void
 run (void *cls,
-     char *const *args,
-     const char *cfgfile,
-     const struct GNUNET_CONFIGURATION_Handle *c)
+     const struct GNUNET_CONFIGURATION_Handle *c,
+     struct GNUNET_SERVICE_Handle *service)
 {
   unsigned long long port;
 
-
+  (void) cls;
+  (void) service;
   cfg = c;
   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
   {
@@ -1358,46 +1372,89 @@ run (void *cls,
 }
 
 
+
 /**
- * The main function for gnunet-gns-proxy.
+ * Callback called when a client connects to the service.
  *
- * @param argc number of arguments from the command line
- * @param argv command line arguments
- * @return 0 ok, 1 on error
+ * @param cls closure for the service
+ * @param c the new client that connected to the service
+ * @param mq the message queue used to send messages to the client
+ * @return @a c
  */
-int
-main (int argc, char *const *argv)
+static void *
+client_connect_cb (void *cls,
+                  struct GNUNET_SERVICE_Client *c,
+                  struct GNUNET_MQ_Handle *mq)
 {
-  struct GNUNET_GETOPT_CommandLineOption options[] = {
-    GNUNET_GETOPT_OPTION_END
-  };
-  static const char* page =
-    "<html><head><title>taler-twister</title>"
-    "</head><body>cURL fail</body></html>";
-  int ret;
+  return c;
+}
 
-  if (GNUNET_OK !=
-      GNUNET_STRINGS_get_utf8_args (argc, argv,
-                                    &argc, &argv))
-    return 2;
-  GNUNET_log_setup ("taler-twister",
-                    "WARNING",
-                    NULL);
-  curl_failure_response
-    = MHD_create_response_from_buffer (strlen (page),
-                                       (void *) page,
-                                       MHD_RESPMEM_PERSISTENT);
-
-  ret =
-    (GNUNET_OK ==
-     GNUNET_PROGRAM_run (argc, argv,
-                         "taler-twister",
-                         _("Taler Man-in-the-Middle HTTP proxy"),
-                         options,
-                         &run, NULL)) ? 0 : 1;
-  MHD_destroy_response (curl_failure_response);
-  GNUNET_free_non_null ((char *) argv);
-  return ret;
+
+/**
+ * Callback called when a client disconnected from the service
+ *
+ * @param cls closure for the service
+ * @param c the client that disconnected
+ * @param internal_cls should be equal to @a c
+ */
+static void
+client_disconnect_cb (void *cls,
+                     struct GNUNET_SERVICE_Client *c,
+                     void *internal_cls)
+{
+  /* intentionally empty */
 }
 
+
+/**
+ * Comment!
+ */
+static void
+send_acknowledgement (struct GNUNET_SERVICE_Client *c)
+{
+  struct GNUNET_MQ_Envelope *env;
+  struct GNUNET_MessageHeader *hdr;
+
+  env = GNUNET_MQ_msg (hdr,
+                       TWISTER_MESSAGE_TYPE_ACKNOWLEDGEMENT);
+  GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (c),
+                  env);
+  GNUNET_SERVICE_client_continue (c);
+}
+
+
+/**
+ * Control handler for changing the response code
+ *
+ * @param cls message queue for sending replies
+ * @param src received message
+ */
+static void
+handle_set_response_code (void *cls,
+                          const struct TWISTER_SetResponseCode *src)
+{
+  struct GNUNET_SERVICE_Client *c = cls;
+
+  hack_response_code = ntohl (src->response_code);
+  send_acknowledgement (c);
+}
+
+
+/**
+ * Define "main" method using service macro.
+ */
+GNUNET_SERVICE_MAIN
+("twister",
+ GNUNET_SERVICE_OPTION_NONE,
+ &run,
+ &client_connect_cb,
+ &client_disconnect_cb,
+ NULL,
+ GNUNET_MQ_hd_fixed_size (set_response_code,
+                         TWISTER_MESSAGE_TYPE_SET_RESPONSE_CODE,
+                         struct TWISTER_SetResponseCode,
+                         NULL),
+ GNUNET_MQ_handler_end ());
+
+
 /* end of taler-twister.c */
diff --git a/src/twister/twister.conf b/src/twister/twister.conf
new file mode 100644
index 0000000..6103824
--- /dev/null
+++ b/src/twister/twister.conf
@@ -0,0 +1,23 @@
+[twister]
+# HTTP listen port for twister
+HTTP_PORT = 8888
+
+# HTTP Destination for twister
+# Note: no trailing '/'!
+DESTINATION_BASE_URL = http://localhost:8080
+
+# Control port for TCP
+# PORT = 8889
+HOSTNAME = localhost
+ACCEPT_FROM = 127.0.0.1;
+ACCEPT_FROM6 = ::1;
+
+# Control port for UNIX
+UNIXPATH = $TALER_RUNTIME_DIR/taler-service-twister.sock
+UNIX_MATCH_UID = NO
+UNIX_MATCH_GID = YES
+
+# Launching of twister by ARM
+# BINARY = taler-service-twister
+# AUTOSTART = NO
+# FORCESTART = NO
diff --git a/src/twister/twister.h b/src/twister/twister.h
new file mode 100644
index 0000000..6f07f81
--- /dev/null
+++ b/src/twister/twister.h
@@ -0,0 +1,63 @@
+/*
+     This file is part of GNUnet.
+     Copyright (C) 2018 Taler Systems SA
+
+     Twister 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.
+
+     Twister 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.
+
+     You should have received a copy of the GNU General Public License
+     along with Twister; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
+*/
+
+/**
+ * @author
+ * @file twister.h
+ *
+ * @brief Common type definitions
+ */
+#ifndef TWISTER_H
+#define TWISTER_H
+
+#include <gnunet/gnunet_common.h>
+
+
+#define TWISTER_MESSAGE_TYPE_ACKNOWLEDGEMENT 1
+
+#define TWISTER_MESSAGE_TYPE_SET_RESPONSE_CODE 2
+
+
+GNUNET_NETWORK_STRUCT_BEGIN
+
+/**
+ * Network size estimate sent from the service
+ * to clients.  Contains the current size estimate
+ * (or 0 if none has been calculated) and the
+ * standard deviation of known estimates.
+ *
+ */
+struct TWISTER_SetResponseCode
+{
+  /**
+   * Type: #TWISTER_MESSAGE_TYPE_SET_RESPONSE_CODE
+   */
+  struct GNUNET_MessageHeader header;
+
+  /**
+   * The new response code, in big endian.
+   */
+  uint32_t response_code GNUNET_PACKED;
+
+};
+GNUNET_NETWORK_STRUCT_END
+
+
+#endif
diff --git a/src/twister/twister_api.c b/src/twister/twister_api.c
new file mode 100644
index 0000000..b791bc7
--- /dev/null
+++ b/src/twister/twister_api.c
@@ -0,0 +1,236 @@
+/*
+     This file is part of Taler.
+     Copyright (C) 2009, 2010, 2011, 2016 GNUnet e.V.
+     Copyright (C) 2018 Taler Systems SA
+
+     Taler 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.
+
+     Taler 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.
+
+     You should have received a copy of the GNU General Public License
+     along with Taler; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
+*/
+
+/**
+ * @file twister_api.c
+ * @brief api to control twister proxy
+ * @author Christian Grothoff
+ * @author Marcello Stanisci
+ */
+#include "platform.h"
+#include <gnunet/gnunet_util_lib.h>
+#include "taler_twister_service.h"
+#include "twister.h"
+
+#define LOG(kind,...) GNUNET_log_from (kind, "twister-api",__VA_ARGS__)
+
+
+
+/**
+ * Opaque handle for asynchronous operation.
+ */
+struct TALER_TWISTER_Operation
+{
+  struct TALER_TWISTER_Operation *next;
+  struct TALER_TWISTER_Operation *prev;
+
+  struct TALER_TWISTER_Handle *h;
+
+  GNUNET_SCHEDULER_TaskCallback cb;
+
+  void *cb_cls;
+};
+
+
+/**
+ * Handle for talking with the Twister service.
+ */
+struct TALER_TWISTER_Handle
+{
+  /**
+   * Configuration to use.
+   */
+  const struct GNUNET_CONFIGURATION_Handle *cfg;
+
+  /**
+   * Message queue (if available).
+   */
+  struct GNUNET_MQ_Handle *mq;
+
+  struct TALER_TWISTER_Operation *op_head;
+
+  struct TALER_TWISTER_Operation *op_tail;
+
+};
+
+
+/**
+ * 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 TALER_TWISTER_Handle *`
+ * @param error error code
+ */
+static void
+mq_error_handler (void *cls,
+                  enum GNUNET_MQ_Error error)
+{
+  struct TALER_TWISTER_Handle *h = cls;
+
+  GNUNET_MQ_destroy (h->mq);
+  h->mq = NULL;
+  GNUNET_assert (0); /* FIXME: maybe give test case nicer way to shut down... 
*/
+}
+
+
+/**
+ * Type of a function to call when we receive a message
+ * from the service.
+ *
+ * @param cls closure
+ * @param client_msg message received
+ */
+static void
+handle_acknowledgement (void *cls,
+                        const struct GNUNET_MessageHeader *ack)
+{
+  struct TALER_TWISTER_Handle *h = cls;
+  struct TALER_TWISTER_Operation *op;
+
+  op = h->op_head;
+  GNUNET_assert (NULL != op); /* twister very wrong, fail test */
+  GNUNET_CONTAINER_DLL_remove (h->op_head,
+                               h->op_tail,
+                               op);
+  if (NULL != op->cb)
+    op->cb (op->cb_cls);
+  GNUNET_free (op);
+}
+
+
+/**
+ * Connect to the twister service.
+ *
+ * @param cfg the configuration to use
+ * @return handle to use
+ */
+struct TALER_TWISTER_Handle *
+TALER_TWISTER_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
+{
+  struct TALER_TWISTER_Handle *h;
+
+  h = GNUNET_new (struct TALER_TWISTER_Handle);
+  h->cfg = cfg;
+  {
+    struct GNUNET_MQ_MessageHandler handlers[] = {
+      GNUNET_MQ_hd_fixed_size (acknowledgement,
+                               TWISTER_MESSAGE_TYPE_ACKNOWLEDGEMENT,
+                               struct GNUNET_MessageHeader,
+                               h),
+      GNUNET_MQ_handler_end ()
+    };
+
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Connecting to twister service.\n");
+    h->mq = GNUNET_CLIENT_connect (h->cfg,
+                                   "twister",
+                                   handlers,
+                                   &mq_error_handler,
+                                   h);
+  }
+  if (NULL == h->mq)
+  {
+    GNUNET_free (h);
+    return NULL;
+  }
+  return h;
+}
+
+
+/**
+ * Disconnect from twister service.
+ *
+ * @param h handle to destroy
+ */
+void
+TALER_TWISTER_disconnect (struct TALER_TWISTER_Handle *h)
+{
+  struct TALER_TWISTER_Operation *op;
+
+  while (NULL != (op = h->op_head))
+  {
+    GNUNET_CONTAINER_DLL_remove (h->op_head,
+                                 h->op_tail,
+                                 op);
+    GNUNET_free (op);
+  }
+  if (NULL != h->mq)
+  {
+    GNUNET_MQ_destroy (h->mq);
+    h->mq = NULL;
+  }
+  GNUNET_free (h);
+}
+
+
+/**
+ * Abort operation.  Twister behavior may then include the
+ * changes requested by the operation, or not!  Must be called
+ * before the operations callback was invoked.
+ *
+ * @param op operation to cancel, operation's callback will not be called
+ */
+void
+TALER_TWISTER_cancel (struct TALER_TWISTER_Operation *op)
+{
+  /* Just don't call the callback anymore */
+  op->cb = NULL;
+}
+
+
+/**
+ * Change the next response code to @a new_rc.
+ *
+ * @param h twister instance to control
+ * @param new_rc response code to return from the next response
+ * @param cb function to call once twister is ready
+ * @param cb_cls closure for @a cb
+ * @return operation handle (to possibly abort)
+ */
+struct TALER_TWISTER_Operation *
+TALER_TWISTER_change_response_code (struct TALER_TWISTER_Handle *h,
+                                    unsigned int new_rc,
+                                    GNUNET_SCHEDULER_TaskCallback cb,
+                                    void *cb_cls)
+{
+  struct TALER_TWISTER_Operation *op;
+  struct GNUNET_MQ_Envelope *env;
+  struct TWISTER_SetResponseCode *src;
+
+  op = GNUNET_new (struct TALER_TWISTER_Operation);
+  op->h = h;
+  op->cb = cb;
+  op->cb_cls = cb_cls;
+  GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
+                                    h->op_tail,
+                                    op);
+  env = GNUNET_MQ_msg (src,
+                       TWISTER_MESSAGE_TYPE_SET_RESPONSE_CODE);
+  src->response_code = htonl ((uint32_t) new_rc);
+  GNUNET_MQ_send (h->mq,
+                  env);
+  return op;
+}
+
+
+/* end of twister_api.c */

-- 
To stop receiving notification emails like this one, please contact
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]