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/>.
19 * @file curl/curl_reschedule.c
20 * @brief API for event loop integration with GNUnet SCHEDULER.
21 * @author Christian Grothoff
24 #include "gnunet_curl_lib.h"
25 #include "gnunet_util_lib.h"
28 download_get_result (struct GNUNET_CURL_DownloadBuffer *db,
33 * Closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
35 struct GNUNET_CURL_RescheduleContext
40 struct GNUNET_SCHEDULER_Task *task;
45 struct GNUNET_CURL_Context *ctx;
48 * Parser of the raw response.
50 GNUNET_CURL_RawParser parser;
53 * Deallocate the response object.
55 GNUNET_CURL_ResponseCleaner cleaner;
60 * Initialize reschedule context; with custom response parser
62 * @param ctx context to manage
63 * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
65 struct GNUNET_CURL_RescheduleContext *
66 GNUNET_CURL_gnunet_rc_create_with_parser (struct GNUNET_CURL_Context *ctx,
67 GNUNET_CURL_RawParser rp,
68 GNUNET_CURL_ResponseCleaner rc)
70 struct GNUNET_CURL_RescheduleContext *rctx;
72 rctx = GNUNET_new (struct GNUNET_CURL_RescheduleContext);
82 * Just a wrapper to avoid casting of function pointers.
84 * @param response the (JSON) response to clean.
87 clean_result (void *response)
89 json_decref (response);
93 * Initialize reschedule context.
95 * @param ctx context to manage
96 * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
98 struct GNUNET_CURL_RescheduleContext *
99 GNUNET_CURL_gnunet_rc_create (struct GNUNET_CURL_Context *ctx)
101 struct GNUNET_CURL_RescheduleContext *rc;
103 rc = GNUNET_new (struct GNUNET_CURL_RescheduleContext);
105 rc->parser = &download_get_result;
106 rc->cleaner = &clean_result;
112 * Destroy reschedule context.
114 * @param rc context to destroy
117 GNUNET_CURL_gnunet_rc_destroy (struct GNUNET_CURL_RescheduleContext *rc)
119 if (NULL != rc->task)
120 GNUNET_SCHEDULER_cancel (rc->task);
126 * Task that runs the context's event loop with the GNUnet scheduler.
128 * @param cls a `struct GNUNET_CURL_RescheduleContext *`
131 context_task (void *cls)
133 struct GNUNET_CURL_RescheduleContext *rc = cls;
138 fd_set except_fd_set;
139 struct GNUNET_NETWORK_FDSet *rs;
140 struct GNUNET_NETWORK_FDSet *ws;
141 struct GNUNET_TIME_Relative delay;
145 GNUNET_CURL_perform2 (rc->ctx,
150 FD_ZERO (&read_fd_set);
151 FD_ZERO (&write_fd_set);
152 FD_ZERO (&except_fd_set);
153 GNUNET_CURL_get_select_info (rc->ctx,
160 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
163 delay = GNUNET_TIME_UNIT_FOREVER_REL;
164 rs = GNUNET_NETWORK_fdset_create ();
165 GNUNET_NETWORK_fdset_copy_native (rs,
168 ws = GNUNET_NETWORK_fdset_create ();
169 GNUNET_NETWORK_fdset_copy_native (ws,
172 if (NULL == rc->task)
173 rc->task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
179 GNUNET_NETWORK_fdset_destroy (rs);
180 GNUNET_NETWORK_fdset_destroy (ws);
185 * Implementation of the #GNUNET_CURL_RescheduleCallback for GNUnet's
186 * scheduler. Will run the CURL context using GNUnet's scheduler.
187 * Note that you MUST immediately destroy the reschedule context after
188 * calling #GNUNET_CURL_fini().
190 * @param cls must point to a `struct GNUNET_CURL_RescheduleContext *`
191 * (pointer to a pointer!)
194 GNUNET_CURL_gnunet_scheduler_reschedule (void *cls)
196 struct GNUNET_CURL_RescheduleContext *rc = *(void**) cls;
198 if (NULL != rc->task)
199 GNUNET_SCHEDULER_cancel (rc->task);
200 rc->task = GNUNET_SCHEDULER_add_now (&context_task,
204 /* end of curl_reschedule.c */