2 This file is part of GNUnet
3 Copyright (C) 2015, 2016 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along with
14 GNUnet; see the file COPYING. If not, If not, see
15 <http://www.gnu.org/licenses/>
18 * @file curl/curl_reschedule.c
19 * @brief API for event loop integration with GNUnet SCHEDULER.
20 * @author Christian Grothoff
23 #include "gnunet_curl_lib.h"
24 #include "gnunet_util_lib.h"
28 * Closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
30 struct GNUNET_CURL_RescheduleContext
35 struct GNUNET_SCHEDULER_Task *task;
40 struct GNUNET_CURL_Context *ctx;
45 * Initialize reschedule context.
47 * @param ctx context to manage
48 * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
50 struct GNUNET_CURL_RescheduleContext *
51 GNUNET_CURL_gnunet_rc_create (struct GNUNET_CURL_Context *ctx)
53 struct GNUNET_CURL_RescheduleContext *rc;
55 rc = GNUNET_new (struct GNUNET_CURL_RescheduleContext);
62 * Destroy reschedule context.
64 * @param rc context to destroy
67 GNUNET_CURL_gnunet_rc_destroy (struct GNUNET_CURL_RescheduleContext *rc)
70 GNUNET_SCHEDULER_cancel (rc->task);
76 * Task that runs the context's event loop with the GNUnet scheduler.
78 * @param cls a `struct GNUNET_CURL_RescheduleContext *`
81 context_task (void *cls)
83 struct GNUNET_CURL_RescheduleContext *rc = cls;
89 struct GNUNET_NETWORK_FDSet *rs;
90 struct GNUNET_NETWORK_FDSet *ws;
91 struct GNUNET_TIME_Relative delay;
94 GNUNET_CURL_perform (rc->ctx);
97 FD_ZERO (&read_fd_set);
98 FD_ZERO (&write_fd_set);
99 FD_ZERO (&except_fd_set);
100 GNUNET_CURL_get_select_info (rc->ctx,
107 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
110 delay = GNUNET_TIME_UNIT_FOREVER_REL;
111 rs = GNUNET_NETWORK_fdset_create ();
112 GNUNET_NETWORK_fdset_copy_native (rs,
115 ws = GNUNET_NETWORK_fdset_create ();
116 GNUNET_NETWORK_fdset_copy_native (ws,
119 rc->task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
125 GNUNET_NETWORK_fdset_destroy (rs);
126 GNUNET_NETWORK_fdset_destroy (ws);
131 * Implementation of the #GNUNET_CURL_RescheduleCallback for GNUnet's
132 * scheduler. Will run the CURL context using GNUnet's scheduler.
133 * Note that you MUST immediately destroy the reschedule context after
134 * calling #GNUNET_CURL_fini().
136 * @param cls must point to a `struct GNUNET_CURL_RescheduleContext *`
137 * (pointer to a pointer!)
140 GNUNET_CURL_gnunet_scheduler_reschedule (void *cls)
142 struct GNUNET_CURL_RescheduleContext *rc = *(void**) cls;
144 if (NULL != rc->task)
145 GNUNET_SCHEDULER_cancel (rc->task);
146 rc->task = GNUNET_SCHEDULER_add_now (&context_task,
150 /* end of curl_reschedule.c */