aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-05-02 08:31:05 +0200
committerChristian Grothoff <christian@grothoff.org>2016-05-02 08:31:05 +0200
commit6616d3086631520a235b2d1cd9576cae14be1673 (patch)
tree937cb6d4eebfc396b86c0f26e1b1fbfc7676eab0
parentb398deb2539298ccfb798a6cefa2ae2ddc52a7fc (diff)
code simplification via new extended libgnunetcurl API
-rw-r--r--src/backend/taler-merchant-httpd_exchanges.c101
-rw-r--r--src/lib/test_merchant_api.c120
2 files changed, 41 insertions, 180 deletions
diff --git a/src/backend/taler-merchant-httpd_exchanges.c b/src/backend/taler-merchant-httpd_exchanges.c
index 03a243d8..72ee8c2d 100644
--- a/src/backend/taler-merchant-httpd_exchanges.c
+++ b/src/backend/taler-merchant-httpd_exchanges.c
@@ -144,10 +144,10 @@ struct Exchange
static struct GNUNET_CURL_Context *merchant_curl_ctx;
/**
- * Task that pumps events into curl as soon as any
- * curl-related events are available.
+ * Context for integrating #merchant_curl_ctx with the
+ * GNUnet event loop.
*/
-static struct GNUNET_SCHEDULER_Task *merchant_curl_task;
+static struct GNUNET_CURL_RescheduleContext *merchant_curl_rc;
/**
* Head of exchanges we know about.
@@ -168,9 +168,6 @@ json_t *trusted_exchanges;
/* forward declarations */
static void
-merchant_curl_cb (void *cls);
-
-static void
retry_exchange (void *cls);
@@ -232,86 +229,6 @@ keys_mgmt_cb (void *cls,
/**
- * Restart the task that pumps events into curl
- * with updated file descriptors.
- *
- * @param cls NULL
- */
-static void
-merchant_curl_refresh (void *cls)
-{
- if (NULL != merchant_curl_task)
- {
- GNUNET_SCHEDULER_cancel (merchant_curl_task);
- merchant_curl_task = NULL;
- }
- merchant_curl_task = GNUNET_SCHEDULER_add_now (&merchant_curl_cb,
- NULL);
-}
-
-
-/**
- * Task that runs the exchange's event loop using the GNUnet scheduler.
- *
- * @param cls a `struct Exchange *`
- */
-static void
-merchant_curl_cb (void *cls)
-{
- long timeout;
- int max_fd;
- fd_set read_fd_set;
- fd_set write_fd_set;
- fd_set except_fd_set;
- struct GNUNET_NETWORK_FDSet *rs;
- struct GNUNET_NETWORK_FDSet *ws;
- struct GNUNET_TIME_Relative delay;
-
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "In exchange context polling task\n");
-
- merchant_curl_task = NULL;
- GNUNET_CURL_perform (merchant_curl_ctx);
- max_fd = -1;
- timeout = -1;
- FD_ZERO (&read_fd_set);
- FD_ZERO (&write_fd_set);
- FD_ZERO (&except_fd_set);
- GNUNET_CURL_get_select_info (merchant_curl_ctx,
- &read_fd_set,
- &write_fd_set,
- &except_fd_set,
- &max_fd,
- &timeout);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "In exchange context polling task, max_fd=%d, timeout=%ld\n",
- max_fd, timeout);
- if (timeout >= 0)
- delay =
- GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
- timeout);
- else
- delay = GNUNET_TIME_UNIT_FOREVER_REL;
- rs = GNUNET_NETWORK_fdset_create ();
- GNUNET_NETWORK_fdset_copy_native (rs,
- &read_fd_set,
- max_fd + 1);
- ws = GNUNET_NETWORK_fdset_create ();
- GNUNET_NETWORK_fdset_copy_native (ws,
- &write_fd_set,
- max_fd + 1);
- merchant_curl_task =
- GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
- delay,
- rs,
- ws,
- &merchant_curl_cb,
- NULL);
- GNUNET_NETWORK_fdset_destroy (rs);
- GNUNET_NETWORK_fdset_destroy (ws);
-}
-
-
-/**
* Task to return find operation result asynchronously to caller.
*
* @param cls a `struct TMH_EXCHANGES_FindOperation`
@@ -575,13 +492,14 @@ TMH_EXCHANGES_init (const struct GNUNET_CONFIGURATION_Handle *cfg)
struct Exchange *exchange;
json_t *j_exchange;
- merchant_curl_ctx = GNUNET_CURL_init (&merchant_curl_refresh,
- NULL);
+ merchant_curl_ctx = GNUNET_CURL_init (&GNUNET_CURL_gnunet_scheduler_reschedule,
+ &merchant_curl_rc);
if (NULL == merchant_curl_ctx)
{
GNUNET_break (0);
return GNUNET_SYSERR;
}
+ merchant_curl_rc = GNUNET_CURL_gnunet_rc_create (merchant_curl_ctx);
GNUNET_CONFIGURATION_iterate_sections (cfg,
&parse_exchanges,
(void *) cfg);
@@ -620,11 +538,8 @@ TMH_EXCHANGES_done ()
GNUNET_free (exchange->uri);
GNUNET_free (exchange);
}
- if (NULL != merchant_curl_task)
- {
- GNUNET_SCHEDULER_cancel (merchant_curl_task);
- merchant_curl_task = NULL;
- }
GNUNET_CURL_fini (merchant_curl_ctx);
merchant_curl_ctx = NULL;
+ GNUNET_CURL_gnunet_rc_destroy (merchant_curl_rc);
+ merchant_curl_rc = NULL;
}
diff --git a/src/lib/test_merchant_api.c b/src/lib/test_merchant_api.c
index 9ef911e7..79d64775 100644
--- a/src/lib/test_merchant_api.c
+++ b/src/lib/test_merchant_api.c
@@ -55,14 +55,14 @@ static struct GNUNET_CURL_Context *ctx;
static struct TALER_MerchantPublicKeyP merchant_pub;
/**
- * Task run on shutdown.
+ * Task run on timeout.
*/
-static struct GNUNET_SCHEDULER_Task *shutdown_task;
+static struct GNUNET_SCHEDULER_Task *timeout_task;
/**
- * Task that runs the main event loop.
+ * Context for running the #ctx's event loop.
*/
-static struct GNUNET_SCHEDULER_Task *ctx_task;
+static struct GNUNET_CURL_RescheduleContext *rc;
/**
* Result of the testcases, #GNUNET_OK on success
@@ -1147,6 +1147,19 @@ interpreter_run (void *cls)
/**
+ * Function run when the test times out.
+ *
+ * @param cls NULL
+ */
+static void
+do_timeout (void *cls)
+{
+ timeout_task = NULL;
+ GNUNET_SCHEDULER_shutdown ();
+}
+
+
+/**
* Function run when the test terminates (good or bad).
* Cleans up our state.
*
@@ -1159,7 +1172,11 @@ do_shutdown (void *cls)
struct Command *cmd;
unsigned int i;
- shutdown_task = NULL;
+ if (NULL != timeout_task)
+ {
+ GNUNET_SCHEDULER_cancel (timeout_task);
+ timeout_task = NULL;
+ }
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Shutdown executing\n");
for (i=0;OC_END != (cmd = &is->commands[i])->oc;i++)
@@ -1238,11 +1255,6 @@ do_shutdown (void *cls)
is->task = NULL;
}
GNUNET_free (is);
- if (NULL != ctx_task)
- {
- GNUNET_SCHEDULER_cancel (ctx_task);
- ctx_task = NULL;
- }
if (NULL != exchange)
{
TALER_EXCHANGE_disconnect (exchange);
@@ -1253,6 +1265,11 @@ do_shutdown (void *cls)
GNUNET_CURL_fini (ctx);
ctx = NULL;
}
+ if (NULL != rc)
+ {
+ GNUNET_CURL_gnunet_rc_destroy (rc);
+ rc = NULL;
+ }
}
@@ -1293,77 +1310,6 @@ cert_cb (void *cls,
/**
- * Task that runs the context's event loop with the GNUnet scheduler.
- *
- * @param cls unused
- */
-static void
-context_task (void *cls)
-{
- long timeout;
- int max_fd;
- fd_set read_fd_set;
- fd_set write_fd_set;
- fd_set except_fd_set;
- struct GNUNET_NETWORK_FDSet *rs;
- struct GNUNET_NETWORK_FDSet *ws;
- struct GNUNET_TIME_Relative delay;
-
- ctx_task = NULL;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Event loop running\n");
- GNUNET_CURL_perform (ctx);
- max_fd = -1;
- timeout = -1;
- FD_ZERO (&read_fd_set);
- FD_ZERO (&write_fd_set);
- FD_ZERO (&except_fd_set);
- GNUNET_CURL_get_select_info (ctx,
- &read_fd_set,
- &write_fd_set,
- &except_fd_set,
- &max_fd,
- &timeout);
- if (timeout >= 0)
- delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
- timeout);
- else
- delay = GNUNET_TIME_UNIT_FOREVER_REL;
- rs = GNUNET_NETWORK_fdset_create ();
- GNUNET_NETWORK_fdset_copy_native (rs,
- &read_fd_set,
- max_fd + 1);
- ws = GNUNET_NETWORK_fdset_create ();
- GNUNET_NETWORK_fdset_copy_native (ws,
- &write_fd_set,
- max_fd + 1);
- ctx_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
- delay,
- rs,
- ws,
- &context_task,
- cls);
- GNUNET_NETWORK_fdset_destroy (rs);
- GNUNET_NETWORK_fdset_destroy (ws);
-}
-
-
-/**
- * Run the context task, the working set has changed.
- *
- * @param cls NULL
- */
-static void
-trigger_context_task (void *cls)
-{
- if (NULL != ctx_task)
- GNUNET_SCHEDULER_cancel (ctx_task);
- ctx_task = GNUNET_SCHEDULER_add_now (&context_task,
- NULL);
-}
-
-
-/**
* Main function that will be run by the scheduler.
*
* @param cls closure
@@ -1453,20 +1399,20 @@ run (void *cls)
is = GNUNET_new (struct InterpreterState);
is->commands = commands;
- ctx = GNUNET_CURL_init (&trigger_context_task,
- NULL);
+ ctx = GNUNET_CURL_init (&GNUNET_CURL_gnunet_scheduler_reschedule,
+ &rc);
GNUNET_assert (NULL != ctx);
- ctx_task = GNUNET_SCHEDULER_add_now (&context_task,
- ctx);
+ rc = GNUNET_CURL_gnunet_rc_create (ctx);
exchange = TALER_EXCHANGE_connect (ctx,
EXCHANGE_URI,
&cert_cb, is,
TALER_EXCHANGE_OPTION_END);
GNUNET_assert (NULL != exchange);
- shutdown_task
+ timeout_task
= GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
(GNUNET_TIME_UNIT_SECONDS, 150),
- &do_shutdown, is);
+ &do_timeout, NULL);
+ GNUNET_SCHEDULER_add_shutdown (&do_shutdown, is);
}