GNUnet  0.11.x
Data Structures | Functions
curl_reschedule.c File Reference

API for event loop integration with GNUnet SCHEDULER. More...

#include "platform.h"
#include <jansson.h>
#include "gnunet_curl_lib.h"
#include "gnunet_util_lib.h"
Include dependency graph for curl_reschedule.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_CURL_RescheduleContext
 Closure for GNUNET_CURL_gnunet_scheduler_reschedule(). More...
 

Functions

void * GNUNET_CURL_download_get_result_ (struct GNUNET_CURL_DownloadBuffer *db, CURL *eh, long *response_code)
 Obtain information about the final result about the HTTP download. More...
 
struct GNUNET_CURL_RescheduleContextGNUNET_CURL_gnunet_rc_create_with_parser (struct GNUNET_CURL_Context *ctx, GNUNET_CURL_RawParser rp, GNUNET_CURL_ResponseCleaner rc)
 Initialize reschedule context; with custom response parser. More...
 
static void clean_result (void *response)
 Just a wrapper to avoid casting of function pointers. More...
 
struct GNUNET_CURL_RescheduleContextGNUNET_CURL_gnunet_rc_create (struct GNUNET_CURL_Context *ctx)
 Initialize reschedule context. More...
 
void GNUNET_CURL_gnunet_rc_destroy (struct GNUNET_CURL_RescheduleContext *rc)
 Destroy reschedule context. More...
 
static void context_task (void *cls)
 Task that runs the context's event loop with the GNUnet scheduler. More...
 
void GNUNET_CURL_gnunet_scheduler_reschedule (void *cls)
 Implementation of the GNUNET_CURL_RescheduleCallback for GNUnet's scheduler. More...
 

Detailed Description

API for event loop integration with GNUnet SCHEDULER.

Author
Christian Grothoff

Definition in file curl_reschedule.c.

Function Documentation

◆ GNUNET_CURL_download_get_result_()

void* GNUNET_CURL_download_get_result_ ( struct GNUNET_CURL_DownloadBuffer db,
CURL *  eh,
long *  response_code 
)

Obtain information about the final result about the HTTP download.

If the download was successful, parses the JSON in the db and returns it. Also returns the HTTP response_code. If the download failed, the return value is NULL. The response code is set in any case, on download errors to zero.

Calling this function also cleans up db.

Parameters
dbdownload buffer
ehCURL handle (to get the response code)
[out]response_codeset to the HTTP response code (or zero if we aborted the download, i.e. because the response was too big, or if the JSON we received was malformed).
Returns
NULL if downloading a JSON reply failed.

Definition at line 547 of file curl.c.

References GNUNET_CURL_DownloadBuffer::buf, GNUNET_CURL_DownloadBuffer::buf_size, GNUNET_CURL_DownloadBuffer::eno, GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_free_non_null, GNUNET_log, and JSON_WARN.

Referenced by GNUNET_CURL_gnunet_rc_create(), and GNUNET_CURL_perform().

550 {
551  json_t *json;
552  json_error_t error;
553  char *ct;
554 
556  "Downloaded body: %.*s\n",
557  (int) db->buf_size,
558  (char *) db->buf);
559 
560  if ((CURLE_OK !=
561  curl_easy_getinfo (eh,
562  CURLINFO_CONTENT_TYPE,
563  &ct)) ||
564  (NULL == ct) ||
565  (0 != strcasecmp (ct,
566  "application/json")))
567  {
568  /* No content type or explicitly not JSON, refuse to parse
569  (but keep response code) */
570  if (CURLE_OK !=
571  curl_easy_getinfo (eh,
572  CURLINFO_RESPONSE_CODE,
573  response_code))
574  {
575  /* unexpected error... */
576  GNUNET_break (0);
577  *response_code = 0;
578  }
579  if (0 != db->buf_size)
581  "Did NOT detect response `%.*s' as JSON\n",
582  (int) db->buf_size,
583  (const char *) db->buf);
584  return NULL;
585  }
586  json = NULL;
587  if (0 == db->eno)
588  {
589  json = json_loadb (db->buf,
590  db->buf_size,
591  JSON_REJECT_DUPLICATES | JSON_DISABLE_EOF_CHECK,
592  &error);
593  if (NULL == json)
594  {
595  JSON_WARN (error);
596  *response_code = 0;
597  }
598  }
600  db->buf = NULL;
601  db->buf_size = 0;
602  if (NULL != json)
603  {
604  if (CURLE_OK !=
605  curl_easy_getinfo (eh,
606  CURLINFO_RESPONSE_CODE,
607  response_code))
608  {
609  /* unexpected error... */
610  GNUNET_break (0);
611  *response_code = 0;
612  }
613  }
614  return json;
615 }
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define JSON_WARN(error)
Print JSON parsing related error information.
Definition: curl.c:53
void * buf
Download buffer.
#define GNUNET_log(kind,...)
size_t buf_size
The size of the download buffer.
int eno
Error code (based on libc errno) if we failed to download (i.e.
Here is the caller graph for this function:

◆ clean_result()

static void clean_result ( void *  response)
static

Just a wrapper to avoid casting of function pointers.

Parameters
responsethe (JSON) response to clean.

Definition at line 90 of file curl_reschedule.c.

Referenced by GNUNET_CURL_gnunet_rc_create().

91 {
92  json_decref (response);
93 }
static struct MHD_Response * response
Our canonical response.
Here is the caller graph for this function:

◆ context_task()

static void context_task ( void *  cls)
static

Task that runs the context's event loop with the GNUnet scheduler.

Parameters
clsa struct GNUNET_CURL_RescheduleContext *

Definition at line 135 of file curl_reschedule.c.

References GNUNET_CURL_RescheduleContext::cleaner, GNUNET_CURL_RescheduleContext::ctx, GNUNET_CURL_get_select_info(), GNUNET_CURL_perform2(), GNUNET_NETWORK_fdset_copy_native(), GNUNET_NETWORK_fdset_create(), GNUNET_NETWORK_fdset_destroy(), GNUNET_SCHEDULER_add_select(), GNUNET_SCHEDULER_PRIORITY_DEFAULT, GNUNET_TIME_relative_multiply(), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_TIME_UNIT_MILLISECONDS, GNUNET_CURL_RescheduleContext::parser, GNUNET_CURL_RescheduleContext::task, and timeout.

Referenced by GNUNET_CURL_gnunet_scheduler_reschedule().

136 {
137  struct GNUNET_CURL_RescheduleContext *rc = cls;
138  long timeout;
139  int max_fd;
140  fd_set read_fd_set;
141  fd_set write_fd_set;
142  fd_set except_fd_set;
143  struct GNUNET_NETWORK_FDSet *rs;
144  struct GNUNET_NETWORK_FDSet *ws;
146 
147  rc->task = NULL;
148  GNUNET_CURL_perform2 (rc->ctx, rc->parser, rc->cleaner);
149  max_fd = -1;
150  timeout = -1;
151  FD_ZERO (&read_fd_set);
152  FD_ZERO (&write_fd_set);
153  FD_ZERO (&except_fd_set);
155  &read_fd_set,
156  &write_fd_set,
157  &except_fd_set,
158  &max_fd,
159  &timeout);
160  if (timeout >= 0)
161  delay =
163  else
166  GNUNET_NETWORK_fdset_copy_native (rs, &read_fd_set, max_fd + 1);
168  GNUNET_NETWORK_fdset_copy_native (ws, &write_fd_set, max_fd + 1);
169  if (NULL == rc->task)
171  delay,
172  rs,
173  ws,
174  &context_task,
175  rc);
178 }
struct GNUNET_CURL_Context * ctx
Context we manage.
GNUNET_CURL_ResponseCleaner cleaner
Deallocate the response object.
GNUNET_CURL_RawParser parser
Parser of the raw response.
void GNUNET_NETWORK_fdset_copy_native(struct GNUNET_NETWORK_FDSet *to, const fd_set *from, int nfds)
Copy a native fd set into the GNUnet representation.
Definition: network.c:1120
Closure for GNUNET_CURL_gnunet_scheduler_reschedule().
void GNUNET_NETWORK_fdset_destroy(struct GNUNET_NETWORK_FDSet *fds)
Releases the associated memory of an fd set.
Definition: network.c:1266
struct GNUNET_NETWORK_FDSet * GNUNET_NETWORK_fdset_create(void)
Creates an fd set.
Definition: network.c:1250
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
Definition: gnunet-abd.c:61
collection of IO descriptors
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_select(enum GNUNET_SCHEDULER_Priority prio, struct GNUNET_TIME_Relative delay, const struct GNUNET_NETWORK_FDSet *rs, const struct GNUNET_NETWORK_FDSet *ws, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when any of the specified file descriptor set...
Definition: scheduler.c:1843
struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Multiply relative time by a given factor.
Definition: time.c:442
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
#define GNUNET_TIME_UNIT_MILLISECONDS
One millisecond.
static struct GNUNET_TIME_Relative delay
When should dkg communication start?
struct GNUNET_SCHEDULER_Task * task
Just the task.
void GNUNET_CURL_get_select_info(struct GNUNET_CURL_Context *ctx, fd_set *read_fd_set, fd_set *write_fd_set, fd_set *except_fd_set, int *max_fd, long *timeout)
Obtain the information for a select() call to wait until GNUNET_CURL_perform() is ready again...
Definition: curl.c:812
void GNUNET_CURL_perform2(struct GNUNET_CURL_Context *ctx, GNUNET_CURL_RawParser rp, GNUNET_CURL_ResponseCleaner rc)
Run the main event loop for the HTTP interaction.
Definition: curl.c:715
Run with the default priority (normal P2P operations).
static void context_task(void *cls)
Task that runs the context&#39;s event loop with the GNUnet scheduler.
Time for relative time used by GNUnet, in microseconds.
Here is the call graph for this function:
Here is the caller graph for this function: