~ubuntu-branches/ubuntu/natty/curl/natty-proposed

« back to all changes in this revision

Viewing changes to docs/examples/ghiper.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Ancell
  • Date: 2010-11-29 17:26:37 UTC
  • mfrom: (3.4.9 sid)
  • Revision ID: james.westby@ubuntu.com-20101129172637-7wwnlut14uxp2kfx
Tags: 7.21.2-1ubuntu1
* Merge with Debian unstable, remaining Ubuntu changes: (LP: #682286)
* debian/control:
  - (Keep build deps in main)
  - Drop build dependencies: stunnel, libssh2-1-dev
  - Add build-dependency on openssh-server
  - Drop libssh2-1-dev from libcurl4-openssl-dev's Depends.

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
typedef struct _GlobalInfo {
59
59
  CURLM *multi;
60
60
  guint timer_event;
61
 
  int prev_running;
62
61
  int still_running;
63
 
  int requested; /* count: curl_easy_init() */
64
 
  int completed; /* count: curl_easy_cleanup() */
65
62
} GlobalInfo;
66
63
 
67
64
 
95
92
    const char *s;
96
93
    switch (code) {
97
94
      case     CURLM_CALL_MULTI_PERFORM: s="CURLM_CALL_MULTI_PERFORM"; break;
98
 
      case     CURLM_OK:                 s="CURLM_OK";                 break;
99
95
      case     CURLM_BAD_HANDLE:         s="CURLM_BAD_HANDLE";         break;
100
96
      case     CURLM_BAD_EASY_HANDLE:    s="CURLM_BAD_EASY_HANDLE";    break;
101
97
      case     CURLM_OUT_OF_MEMORY:      s="CURLM_OUT_OF_MEMORY";      break;
113
109
 
114
110
 
115
111
/* Check for completed transfers, and remove their easy handles */
116
 
static void check_run_count(GlobalInfo *g)
 
112
static void check_multi_info(GlobalInfo *g)
117
113
{
118
 
  if (g->prev_running > g->still_running) {
119
 
    char *eff_url=NULL;
120
 
    CURLMsg *msg;
121
 
    int msgs_left;
122
 
    ConnInfo *conn=NULL;
123
 
    CURL*easy;
124
 
    CURLcode res;
 
114
  char *eff_url;
 
115
  CURLMsg *msg;
 
116
  int msgs_left;
 
117
  ConnInfo *conn;
 
118
  CURL *easy;
 
119
  CURLcode res;
125
120
 
126
 
    MSG_OUT("REMAINING: %d\n", g->still_running);
127
 
    /*
128
 
      I am still uncertain whether it is safe to remove an easy handle
129
 
      from inside the curl_multi_info_read loop, so here I will search
130
 
      for completed transfers in the inner "while" loop, and then remove
131
 
      them in the outer "do-while" loop...
132
 
   */
133
 
    do {
134
 
      easy=NULL;
135
 
      while ((msg = curl_multi_info_read(g->multi, &msgs_left))) {
136
 
        if (msg->msg == CURLMSG_DONE) {
137
 
          easy=msg->easy_handle;
138
 
          res=msg->data.result;
139
 
          break;
140
 
        }
141
 
      }
142
 
      if (easy) {
143
 
          curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn);
144
 
          curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url);
145
 
          MSG_OUT("DONE: %s => (%d) %s\n", eff_url, res, conn->error);
146
 
          curl_multi_remove_handle(g->multi, easy);
147
 
          g_free(conn->url);
148
 
          curl_easy_cleanup(easy);
149
 
          g_free(conn);
150
 
          g->completed++;
151
 
      }
152
 
    } while ( easy );
153
 
    MSG_OUT("Requested: %d Completed:%d\n", g->requested, g->completed);
 
121
  MSG_OUT("REMAINING: %d\n", g->still_running);
 
122
  while ((msg = curl_multi_info_read(g->multi, &msgs_left))) {
 
123
    if (msg->msg == CURLMSG_DONE) {
 
124
      easy = msg->easy_handle;
 
125
      res = msg->data.result;
 
126
      curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn);
 
127
      curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url);
 
128
      MSG_OUT("DONE: %s => (%d) %s\n", eff_url, res, conn->error);
 
129
      curl_multi_remove_handle(g->multi, easy);
 
130
      free(conn->url);
 
131
      curl_easy_cleanup(easy);
 
132
      free(conn);
 
133
    }
154
134
  }
155
 
  g->prev_running = g->still_running;
156
135
}
157
136
 
158
137
 
159
138
 
160
 
 
161
139
/* Called by glib when our timeout expires */
162
140
static gboolean timer_cb(gpointer data)
163
141
{
164
142
  GlobalInfo *g = (GlobalInfo *)data;
165
143
  CURLMcode rc;
166
144
 
167
 
  do {
168
 
    rc = curl_multi_socket(g->multi, CURL_SOCKET_TIMEOUT, &g->still_running);
169
 
  } while (rc == CURLM_CALL_MULTI_PERFORM);
170
 
  mcode_or_die("timer_cb: curl_multi_socket", rc);
171
 
  check_run_count(g);
 
145
  rc = curl_multi_socket_action(g->multi,
 
146
                                  CURL_SOCKET_TIMEOUT, 0, &g->still_running);
 
147
  mcode_or_die("timer_cb: curl_multi_socket_action", rc);
 
148
  check_multi_info(g);
172
149
  return FALSE;
173
150
}
174
151
 
198
175
  GlobalInfo *g = (GlobalInfo*) data;
199
176
  CURLMcode rc;
200
177
  int fd=g_io_channel_unix_get_fd(ch);
201
 
  do {
202
 
    rc = curl_multi_socket(g->multi, fd, &g->still_running);
203
 
  } while (rc == CURLM_CALL_MULTI_PERFORM);
204
 
  mcode_or_die("event_cb: curl_multi_socket", rc);
205
 
  check_run_count(g);
 
178
 
 
179
  int action =
 
180
    (condition & G_IO_IN ? CURL_CSELECT_IN : 0) |
 
181
    (condition & G_IO_OUT ? CURL_CSELECT_OUT : 0);
 
182
 
 
183
  rc = curl_multi_socket_action(g->multi, fd, action, &g->still_running);
 
184
  mcode_or_die("event_cb: curl_multi_socket_action", rc);
 
185
 
 
186
  check_multi_info(g);
206
187
  if(g->still_running) {
207
188
    return TRUE;
208
189
  } else {
338
319
  MSG_OUT("Adding easy %p to multi %p (%s)\n", conn->easy, g->multi, url);
339
320
  rc =curl_multi_add_handle(g->multi, conn->easy);
340
321
  mcode_or_die("new_conn: curl_multi_add_handle", rc);
341
 
  g->requested++;
342
 
  do {
343
 
    rc = curl_multi_socket_all(g->multi, &g->still_running);
344
 
  } while (CURLM_CALL_MULTI_PERFORM == rc);
345
 
  mcode_or_die("new_conn: curl_multi_socket_all", rc);
346
 
  check_run_count(g);
 
322
 
 
323
  /* note that the add_handle() will set a time-out to trigger very soon so
 
324
     that the necessary socket_action() call will be called by this app */
347
325
}
348
326
 
349
327
 
451
429
  curl_multi_setopt(g->multi, CURLMOPT_SOCKETDATA, g);
452
430
  curl_multi_setopt(g->multi, CURLMOPT_TIMERFUNCTION, update_timeout_cb);
453
431
  curl_multi_setopt(g->multi, CURLMOPT_TIMERDATA, g);
454
 
  do {
455
 
    rc = curl_multi_socket_all(g->multi, &g->still_running);
456
 
  } while (CURLM_CALL_MULTI_PERFORM == rc);
 
432
 
 
433
  /* we don't call any curl_multi_socket*() function yet as we have no handles
 
434
     added! */
 
435
 
457
436
  g_main_loop_run(gmain);
458
437
  curl_multi_cleanup(g->multi);
459
438
  return 0;