summaryrefslogtreecommitdiff
path: root/src/curl/curl_reschedule.c
blob: cc3c1db950da41d3f01833a11cff7280df4e5420 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
  This file is part of GNUnet
  Copyright (C) 2015, 2016 GNUnet e.V.

  GNUnet is free software: you can redistribute it and/or modify it
  under the terms of the GNU Affero General Public License as published
  by the Free Software Foundation, either version 3 of the License,
  or (at your option) any later version.

  GNUnet is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Affero General Public License for more details.
 
  You should have received a copy of the GNU Affero General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * @file curl/curl_reschedule.c
 * @brief API for event loop integration with GNUnet SCHEDULER.
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_curl_lib.h"
#include "gnunet_util_lib.h"

extern void *
download_get_result (struct GNUNET_CURL_DownloadBuffer *db,
                     CURL *eh,
                     long *response_code);

/**
 * Closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
 */
struct GNUNET_CURL_RescheduleContext
{
  /**
   * Just the task.
   */
  struct GNUNET_SCHEDULER_Task *task;

  /**
   * Context we manage.
   */
  struct GNUNET_CURL_Context *ctx;

  /**
   * Parser of the raw response.
   */
  GNUNET_CURL_RawParser parser;

  /**
   * Deallocate the response object.
   */
  GNUNET_CURL_ResponseCleaner cleaner;
};


/**
 * Initialize reschedule context; with custom response parser
 *
 * @param ctx context to manage
 * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
 */
struct GNUNET_CURL_RescheduleContext *
GNUNET_CURL_gnunet_rc_create_with_parser (struct GNUNET_CURL_Context *ctx,
                                          GNUNET_CURL_RawParser rp,
                                          GNUNET_CURL_ResponseCleaner rc)
{
  struct GNUNET_CURL_RescheduleContext *rctx;

  rctx = GNUNET_new (struct GNUNET_CURL_RescheduleContext);
  rctx->ctx = ctx;
  rctx->parser = rp;
  rctx->cleaner = rc;

  return rctx;
}


/**
 * Just a wrapper to avoid casting of function pointers.
 *
 * @param response the (JSON) response to clean.
 */
static void
clean_result (void *response)
{
  json_decref (response);
}

/**
 * Initialize reschedule context.
 *
 * @param ctx context to manage
 * @return closure for #GNUNET_CURL_gnunet_scheduler_reschedule().
 */
struct GNUNET_CURL_RescheduleContext *
GNUNET_CURL_gnunet_rc_create (struct GNUNET_CURL_Context *ctx)
{
  struct GNUNET_CURL_RescheduleContext *rc;

  rc = GNUNET_new (struct GNUNET_CURL_RescheduleContext);
  rc->ctx = ctx;
  rc->parser = &download_get_result;
  rc->cleaner = &clean_result;
  return rc;
}


/**
 * Destroy reschedule context.
 *
 * @param rc context to destroy
 */
void
GNUNET_CURL_gnunet_rc_destroy (struct GNUNET_CURL_RescheduleContext *rc)
{
  if (NULL != rc->task)
    GNUNET_SCHEDULER_cancel (rc->task);
  GNUNET_free (rc);
}


/**
 * Task that runs the context's event loop with the GNUnet scheduler.
 *
 * @param cls a `struct GNUNET_CURL_RescheduleContext *`
 */
static void
context_task (void *cls)
{
  struct GNUNET_CURL_RescheduleContext *rc = 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;

  rc->task = NULL;

  GNUNET_CURL_perform2 (rc->ctx,
                        rc->parser,
                        rc->cleaner);
  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 (rc->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);
  if (NULL == rc->task)
    rc->task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
                                            delay,
                                            rs,
                                            ws,
                                            &context_task,
                                            rc);
  GNUNET_NETWORK_fdset_destroy (rs);
  GNUNET_NETWORK_fdset_destroy (ws);
}


/**
 * Implementation of the #GNUNET_CURL_RescheduleCallback for GNUnet's
 * scheduler.  Will run the CURL context using GNUnet's scheduler.
 * Note that you MUST immediately destroy the reschedule context after
 * calling #GNUNET_CURL_fini().
 *
 * @param cls must point to a `struct GNUNET_CURL_RescheduleContext *`
 *           (pointer to a pointer!)
 */
void
GNUNET_CURL_gnunet_scheduler_reschedule (void *cls)
{
  struct GNUNET_CURL_RescheduleContext *rc = *(void**) cls;

  if (NULL != rc->task)
    GNUNET_SCHEDULER_cancel (rc->task);
  rc->task = GNUNET_SCHEDULER_add_now (&context_task,
                                       rc);
}

/* end of curl_reschedule.c */