aboutsummaryrefslogtreecommitdiff
path: root/src/backend/taler-merchant-httpd.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/taler-merchant-httpd.c')
-rw-r--r--src/backend/taler-merchant-httpd.c174
1 files changed, 33 insertions, 141 deletions
diff --git a/src/backend/taler-merchant-httpd.c b/src/backend/taler-merchant-httpd.c
index 0492ed96..ea7a6c62 100644
--- a/src/backend/taler-merchant-httpd.c
+++ b/src/backend/taler-merchant-httpd.c
@@ -24,6 +24,7 @@
#include "platform.h"
#include <taler/taler_dbevents.h>
#include <taler/taler_bank_service.h>
+#include <taler/taler_mhd_lib.h>
#include <taler/taler_exchange_service.h>
#include "taler-merchant-httpd_auditors.h"
#include "taler-merchant-httpd_config.h"
@@ -131,21 +132,11 @@ static uint16_t port;
static int merchant_connection_close;
/**
- * Task running the HTTP server.
- */
-static struct GNUNET_SCHEDULER_Task *mhd_task;
-
-/**
* Global return code
*/
static int result;
/**
- * The MHD Daemon
- */
-static struct MHD_Daemon *mhd;
-
-/**
* MIN-Heap of suspended connections to resume when the timeout expires,
* ordered by timeout. Values are of type `struct MHD_Connection`
*/
@@ -427,7 +418,7 @@ do_resume (void *cls)
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Resuming long polled job due to timeout\n");
MHD_resume_connection (sc->con);
- TMH_trigger_daemon (); /* we resumed, kick MHD */
+ TALER_MHD_daemon_trigger (); /* we resumed, kick MHD */
}
if (NULL != resume_timeout_task)
GNUNET_SCHEDULER_cancel (resume_timeout_task);
@@ -569,7 +560,7 @@ resume_operation (void *cls,
GNUNET_CONTAINER_heap_remove_node (sc->hn));
sc->hn = NULL;
MHD_resume_connection (sc->con);
- TMH_trigger_daemon ();
+ TALER_MHD_daemon_trigger ();
return GNUNET_OK;
}
@@ -649,7 +640,7 @@ resume_operation2 (void *cls,
GNUNET_CONTAINER_heap_remove_node (sc->hn));
sc->hn = NULL;
MHD_resume_connection (sc->con);
- TMH_trigger_daemon ();
+ TALER_MHD_daemon_trigger ();
return GNUNET_OK;
}
@@ -697,6 +688,7 @@ static void
do_shutdown (void *cls)
{
struct TMH_SuspendedConnection *sc;
+ struct MHD_Daemon *mhd;
(void) cls;
TMH_force_ac_resume ();
@@ -706,11 +698,7 @@ do_shutdown (void *cls)
TMH_force_tip_pickup_resume ();
TMH_force_wallet_get_order_resume ();
TMH_force_wallet_refund_order_resume ();
- if (NULL != mhd_task)
- {
- GNUNET_SCHEDULER_cancel (mhd_task);
- mhd_task = NULL;
- }
+ mhd = TALER_MHD_daemon_stop ();
/* resume all suspended connections, must be done before stopping #mhd */
if (NULL != resume_timeout_heap)
{
@@ -829,108 +817,6 @@ handle_mhd_completion_callback (void *cls,
}
-/**
- * Function that queries MHD's select sets and
- * starts the task waiting for them.
- */
-static struct GNUNET_SCHEDULER_Task *
-prepare_daemon (void);
-
-
-/**
- * Set if we should immediately #MHD_run again.
- */
-static int triggered;
-
-
-/**
- * Call MHD to process pending requests and then go back
- * and schedule the next run.
- *
- * @param cls NULL
- */
-static void
-run_daemon (void *cls)
-{
- mhd_task = NULL;
- do {
- triggered = 0;
- GNUNET_assert (MHD_YES == MHD_run (mhd));
- } while (0 != triggered);
- mhd_task = prepare_daemon ();
-}
-
-
-void
-TMH_trigger_daemon (void)
-{
- if (NULL != mhd_task)
- {
- GNUNET_SCHEDULER_cancel (mhd_task);
- mhd_task = NULL;
- run_daemon (NULL);
- }
- else
- {
- triggered = 1;
- }
-}
-
-
-/**
- * Function that queries MHD's select sets and starts the task waiting for
- * them.
- *
- * @return task handle for the MHD task.
- */
-static struct GNUNET_SCHEDULER_Task *
-prepare_daemon (void)
-{
- struct GNUNET_SCHEDULER_Task *ret;
- fd_set rs;
- fd_set ws;
- fd_set es;
- struct GNUNET_NETWORK_FDSet *wrs;
- struct GNUNET_NETWORK_FDSet *wws;
- int max;
- MHD_UNSIGNED_LONG_LONG timeout;
- int haveto;
- struct GNUNET_TIME_Relative tv;
-
- FD_ZERO (&rs);
- FD_ZERO (&ws);
- FD_ZERO (&es);
- wrs = GNUNET_NETWORK_fdset_create ();
- wws = GNUNET_NETWORK_fdset_create ();
- max = -1;
- GNUNET_assert (MHD_YES ==
- MHD_get_fdset (mhd,
- &rs,
- &ws,
- &es,
- &max));
- haveto = MHD_get_timeout (mhd, &timeout);
- if (haveto == MHD_YES)
- tv = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
- timeout);
- else
- tv = GNUNET_TIME_UNIT_FOREVER_REL;
- GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
- GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Adding run_daemon select task\n");
- ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
- tv,
- wrs,
- wws,
- &run_daemon,
- NULL);
- GNUNET_NETWORK_fdset_destroy (wrs);
- GNUNET_NETWORK_fdset_destroy (wws);
- return ret;
-}
-
-
struct TMH_MerchantInstance *
TMH_lookup_instance (const char *instance_id)
{
@@ -2345,26 +2231,31 @@ run (void *cls,
GNUNET_SCHEDULER_shutdown ();
return;
}
- mhd = MHD_start_daemon (MHD_USE_SUSPEND_RESUME | MHD_USE_DUAL_STACK
- | MHD_USE_AUTO,
- port,
- NULL, NULL,
- &url_handler, NULL,
- MHD_OPTION_LISTEN_SOCKET, fh,
- MHD_OPTION_NOTIFY_COMPLETED,
- &handle_mhd_completion_callback, NULL,
- MHD_OPTION_CONNECTION_TIMEOUT,
- (unsigned int) 10 /* 10s */,
- MHD_OPTION_END);
- if (NULL == mhd)
+
{
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Failed to launch HTTP service. Is the port in use?\n");
- GNUNET_SCHEDULER_shutdown ();
- return;
+ struct MHD_Daemon *mhd;
+
+ mhd = MHD_start_daemon (MHD_USE_SUSPEND_RESUME | MHD_USE_DUAL_STACK
+ | MHD_USE_AUTO,
+ port,
+ NULL, NULL,
+ &url_handler, NULL,
+ MHD_OPTION_LISTEN_SOCKET, fh,
+ MHD_OPTION_NOTIFY_COMPLETED,
+ &handle_mhd_completion_callback, NULL,
+ MHD_OPTION_CONNECTION_TIMEOUT,
+ (unsigned int) 10 /* 10s */,
+ MHD_OPTION_END);
+ if (NULL == mhd)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Failed to launch HTTP service. Is the port in use?\n");
+ GNUNET_SCHEDULER_shutdown ();
+ return;
+ }
+ result = GNUNET_OK;
+ TALER_MHD_daemon_start (mhd);
}
- result = GNUNET_OK;
- mhd_task = prepare_daemon ();
}
@@ -2395,14 +2286,15 @@ main (int argc,
GNUNET_GETOPT_OPTION_END
};
+ TALER_OS_init ();
res = GNUNET_PROGRAM_run (argc, argv,
"taler-merchant-httpd",
"Taler merchant's HTTP backend interface",
options,
&run, NULL);
if (GNUNET_SYSERR == res)
- return 3;
+ return EXIT_INVALIDARGUMENT;
if (GNUNET_NO == res)
- return 0;
- return (GNUNET_OK == result) ? 0 : 1;
+ return EXIT_SUCCESS;
+ return (GNUNET_OK == result) ? EXIT_SUCCESS : 1;
}