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
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file curl/curl_reschedule.c
22 * @brief API for event loop integration with GNUnet SCHEDULER.
23 * @author Christian Grothoff
26 #include "gnunet_curl_lib.h"
27 #include "gnunet_util_lib.h"
30 download_get_result (struct GNUNET_CURL_DownloadBuffer *db,
35 * Closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
37 struct GNUNET_CURL_RescheduleContext
42 struct GNUNET_SCHEDULER_Task *task;
47 struct GNUNET_CURL_Context *ctx;
50 * Parser of the raw response.
52 GNUNET_CURL_RawParser parser;
55 * Deallocate the response object.
57 GNUNET_CURL_ResponseCleaner cleaner;
62 * Initialize reschedule context; with custom response parser
64 * @param ctx context to manage
65 * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
67 struct GNUNET_CURL_RescheduleContext *
68 GNUNET_CURL_gnunet_rc_create_with_parser (struct GNUNET_CURL_Context *ctx,
69 GNUNET_CURL_RawParser rp,
70 GNUNET_CURL_ResponseCleaner rc)
72 struct GNUNET_CURL_RescheduleContext *rctx;
74 rctx = GNUNET_new (struct GNUNET_CURL_RescheduleContext);
84 * Just a wrapper to avoid casting of function pointers.
86 * @param response the (JSON) response to clean.
89 clean_result (void *response)
91 json_decref (response);
95 * Initialize reschedule context.
97 * @param ctx context to manage
98 * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
100 struct GNUNET_CURL_RescheduleContext *
101 GNUNET_CURL_gnunet_rc_create (struct GNUNET_CURL_Context *ctx)
103 struct GNUNET_CURL_RescheduleContext *rc;
105 rc = GNUNET_new (struct GNUNET_CURL_RescheduleContext);
107 rc->parser = &download_get_result;
108 rc->cleaner = &clean_result;
114 * Destroy reschedule context.
116 * @param rc context to destroy
119 GNUNET_CURL_gnunet_rc_destroy (struct GNUNET_CURL_RescheduleContext *rc)
121 if (NULL != rc->task)
122 GNUNET_SCHEDULER_cancel (rc->task);
128 * Task that runs the context's event loop with the GNUnet scheduler.
130 * @param cls a `struct GNUNET_CURL_RescheduleContext *`
133 context_task (void *cls)
135 struct GNUNET_CURL_RescheduleContext *rc = cls;
140 fd_set except_fd_set;
141 struct GNUNET_NETWORK_FDSet *rs;
142 struct GNUNET_NETWORK_FDSet *ws;
143 struct GNUNET_TIME_Relative delay;
147 GNUNET_CURL_perform2 (rc->ctx,
152 FD_ZERO (&read_fd_set);
153 FD_ZERO (&write_fd_set);
154 FD_ZERO (&except_fd_set);
155 GNUNET_CURL_get_select_info (rc->ctx,
162 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
165 delay = GNUNET_TIME_UNIT_FOREVER_REL;
166 rs = GNUNET_NETWORK_fdset_create ();
167 GNUNET_NETWORK_fdset_copy_native (rs,
170 ws = GNUNET_NETWORK_fdset_create ();
171 GNUNET_NETWORK_fdset_copy_native (ws,
174 if (NULL == rc->task)
175 rc->task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
181 GNUNET_NETWORK_fdset_destroy (rs);
182 GNUNET_NETWORK_fdset_destroy (ws);
187 * Implementation of the #GNUNET_CURL_RescheduleCallback for GNUnet's
188 * scheduler. Will run the CURL context using GNUnet's scheduler.
189 * Note that you MUST immediately destroy the reschedule context after
190 * calling #GNUNET_CURL_fini().
192 * @param cls must point to a `struct GNUNET_CURL_RescheduleContext *`
193 * (pointer to a pointer!)
196 GNUNET_CURL_gnunet_scheduler_reschedule (void *cls)
198 struct GNUNET_CURL_RescheduleContext *rc = *(void**) cls;
200 if (NULL != rc->task)
201 GNUNET_SCHEDULER_cancel (rc->task);
202 rc->task = GNUNET_SCHEDULER_add_now (&context_task,
206 /* end of curl_reschedule.c */