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
27 #include "gnunet_curl_lib.h"
28 #include "gnunet_util_lib.h"
31 GNUNET_CURL_download_get_result_ (struct GNUNET_CURL_DownloadBuffer *db,
36 * Closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
38 struct GNUNET_CURL_RescheduleContext
43 struct GNUNET_SCHEDULER_Task *task;
48 struct GNUNET_CURL_Context *ctx;
51 * Parser of the raw response.
53 GNUNET_CURL_RawParser parser;
56 * Deallocate the response object.
58 GNUNET_CURL_ResponseCleaner cleaner;
63 * Initialize reschedule context; with custom response parser
65 * @param ctx context to manage
66 * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
68 struct GNUNET_CURL_RescheduleContext *
69 GNUNET_CURL_gnunet_rc_create_with_parser (struct GNUNET_CURL_Context *ctx,
70 GNUNET_CURL_RawParser rp,
71 GNUNET_CURL_ResponseCleaner rc)
73 struct GNUNET_CURL_RescheduleContext *rctx;
75 rctx = GNUNET_new (struct GNUNET_CURL_RescheduleContext);
85 * Just a wrapper to avoid casting of function pointers.
87 * @param response the (JSON) response to clean.
90 clean_result (void *response)
92 json_decref (response);
97 * Initialize reschedule context.
99 * @param ctx context to manage
100 * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
102 struct GNUNET_CURL_RescheduleContext *
103 GNUNET_CURL_gnunet_rc_create (struct GNUNET_CURL_Context *ctx)
105 struct GNUNET_CURL_RescheduleContext *rc;
107 rc = GNUNET_new (struct GNUNET_CURL_RescheduleContext);
109 rc->parser = &GNUNET_CURL_download_get_result_;
110 rc->cleaner = &clean_result;
116 * Destroy reschedule context.
118 * @param rc context to destroy
121 GNUNET_CURL_gnunet_rc_destroy (struct GNUNET_CURL_RescheduleContext *rc)
123 if (NULL != rc->task)
124 GNUNET_SCHEDULER_cancel (rc->task);
130 * Task that runs the context's event loop with the GNUnet scheduler.
132 * @param cls a `struct GNUNET_CURL_RescheduleContext *`
135 context_task (void *cls)
137 struct GNUNET_CURL_RescheduleContext *rc = cls;
142 fd_set except_fd_set;
143 struct GNUNET_NETWORK_FDSet *rs;
144 struct GNUNET_NETWORK_FDSet *ws;
145 struct GNUNET_TIME_Relative delay;
148 GNUNET_CURL_perform2 (rc->ctx, rc->parser, rc->cleaner);
151 FD_ZERO (&read_fd_set);
152 FD_ZERO (&write_fd_set);
153 FD_ZERO (&except_fd_set);
154 GNUNET_CURL_get_select_info (rc->ctx,
162 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, timeout);
164 delay = GNUNET_TIME_UNIT_FOREVER_REL;
165 rs = GNUNET_NETWORK_fdset_create ();
166 GNUNET_NETWORK_fdset_copy_native (rs, &read_fd_set, max_fd + 1);
167 ws = GNUNET_NETWORK_fdset_create ();
168 GNUNET_NETWORK_fdset_copy_native (ws, &write_fd_set, max_fd + 1);
169 if (NULL == rc->task)
170 rc->task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
176 GNUNET_NETWORK_fdset_destroy (rs);
177 GNUNET_NETWORK_fdset_destroy (ws);
182 * Implementation of the #GNUNET_CURL_RescheduleCallback for GNUnet's
183 * scheduler. Will run the CURL context using GNUnet's scheduler.
184 * Note that you MUST immediately destroy the reschedule context after
185 * calling #GNUNET_CURL_fini().
187 * @param cls must point to a `struct GNUNET_CURL_RescheduleContext *`
188 * (pointer to a pointer!)
191 GNUNET_CURL_gnunet_scheduler_reschedule (void *cls)
193 struct GNUNET_CURL_RescheduleContext *rc = *(void **) cls;
195 if (NULL != rc->task)
196 GNUNET_SCHEDULER_cancel (rc->task);
197 rc->task = GNUNET_SCHEDULER_add_now (&context_task, rc);
201 /* end of curl_reschedule.c */