~ubuntu-branches/ubuntu/lucid/curl/lucid-201101212007

« back to all changes in this revision

Viewing changes to docs/examples/10-at-a-time.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-05-16 15:16:54 UTC
  • mto: (3.1.1 lenny) (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: james.westby@ubuntu.com-20070516151654-x9nkigtr2j0i8d0v
Tags: upstream-7.16.2
ImportĀ upstreamĀ versionĀ 7.16.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 *                                  _   _ ____  _
 
3
 *  Project                     ___| | | |  _ \| |
 
4
 *                             / __| | | | |_) | |
 
5
 *                            | (__| |_| |  _ <| |___
 
6
 *                             \___|\___/|_| \_\_____|
 
7
 *
 
8
 * $Id: 10-at-a-time.c,v 1.2 2006-10-13 14:01:19 bagder Exp $
 
9
 *
 
10
 * Example application source code using the multi interface to download many
 
11
 * files, but with a capped maximum amount of simultaneous transfers.
 
12
 *
 
13
 * Written by Michael Wallner
 
14
 */
 
15
 
 
16
#include <stdlib.h>
 
17
#include <curl/multi.h>
 
18
 
 
19
static const char *urls[] = {
 
20
  "http://www.microsoft.com",
 
21
  "http://www.opensource.org",
 
22
  "http://www.google.com",
 
23
  "http://www.yahoo.com",
 
24
  "http://www.ibm.com",
 
25
  "http://www.mysql.com",
 
26
  "http://www.oracle.com",
 
27
  "http://www.ripe.net",
 
28
  "http://www.iana.org",
 
29
  "http://www.amazon.com",
 
30
  "http://www.netcraft.com",
 
31
  "http://www.heise.de",
 
32
  "http://www.chip.de",
 
33
  "http://www.ca.com",
 
34
  "http://www.cnet.com",
 
35
  "http://www.news.com",
 
36
  "http://www.cnn.com",
 
37
  "http://www.wikipedia.org",
 
38
  "http://www.dell.com",
 
39
  "http://www.hp.com",
 
40
  "http://www.cert.org",
 
41
  "http://www.mit.edu",
 
42
  "http://www.nist.gov",
 
43
  "http://www.ebay.com",
 
44
  "http://www.playstation.com",
 
45
  "http://www.uefa.com",
 
46
  "http://www.ieee.org",
 
47
  "http://www.apple.com",
 
48
  "http://www.sony.com",
 
49
  "http://www.symantec.com",
 
50
  "http://www.zdnet.com",
 
51
  "http://www.fujitsu.com",
 
52
  "http://www.supermicro.com",
 
53
  "http://www.hotmail.com",
 
54
  "http://www.ecma.com",
 
55
  "http://www.bbc.co.uk",
 
56
  "http://news.google.com",
 
57
  "http://www.foxnews.com",
 
58
  "http://www.msn.com",
 
59
  "http://www.wired.com",
 
60
  "http://www.sky.com",
 
61
  "http://www.usatoday.com",
 
62
  "http://www.cbs.com",
 
63
  "http://www.nbc.com",
 
64
  "http://slashdot.org",
 
65
  "http://www.bloglines.com",
 
66
  "http://www.techweb.com",
 
67
  "http://www.newslink.org",
 
68
  "http://www.un.org",
 
69
};
 
70
 
 
71
#define MAX 10 /* number of simultaneous transfers */
 
72
#define CNT sizeof(urls)/sizeof(char*) /* total number of transfers to do */
 
73
 
 
74
static int cb(char *d, size_t n, size_t l, void *p)
 
75
{
 
76
  /* take care of the data here, ignored in this example */
 
77
  (void)d;
 
78
  (void)p;
 
79
  return n*l;
 
80
}
 
81
 
 
82
static void init(CURLM *cm, int i)
 
83
{
 
84
  CURL *eh = curl_easy_init();
 
85
 
 
86
  curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, cb);
 
87
  curl_easy_setopt(eh, CURLOPT_HEADER, 0);
 
88
  curl_easy_setopt(eh, CURLOPT_URL, urls[i]);
 
89
  curl_easy_setopt(eh, CURLOPT_PRIVATE, urls[i]);
 
90
  curl_easy_setopt(eh, CURLOPT_VERBOSE, 0);
 
91
 
 
92
  curl_multi_add_handle(cm, eh);
 
93
}
 
94
 
 
95
int main(void)
 
96
{
 
97
  CURLM *cm;
 
98
  CURLMsg *msg;
 
99
  long L;
 
100
  unsigned int C=0;
 
101
  int M, Q, U = -1;
 
102
  fd_set R, W, E;
 
103
  struct timeval T;
 
104
 
 
105
  curl_global_init(CURL_GLOBAL_ALL);
 
106
 
 
107
  cm = curl_multi_init();
 
108
 
 
109
  for (C = 0; C < MAX; ++C) {
 
110
    init(cm, C);
 
111
  }
 
112
 
 
113
  while (U) {
 
114
    while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(cm, &U));
 
115
 
 
116
    if (U) {
 
117
      FD_ZERO(&R);
 
118
      FD_ZERO(&W);
 
119
      FD_ZERO(&E);
 
120
 
 
121
      if (curl_multi_fdset(cm, &R, &W, &E, &M)) {
 
122
        fprintf(stderr, "E: curl_multi_fdset\n");
 
123
        return EXIT_FAILURE;
 
124
      }
 
125
 
 
126
      /* In a real-world program you OF COURSE check the return that maxfd is
 
127
         bigger than -1 so that the call to select() below makes sense! */
 
128
 
 
129
      if (curl_multi_timeout(cm, &L)) {
 
130
        fprintf(stderr, "E: curl_multi_timeout\n");
 
131
        return EXIT_FAILURE;
 
132
      }
 
133
 
 
134
      T.tv_sec = L/1000;
 
135
      T.tv_usec = (L%1000)*1000;
 
136
 
 
137
      if (0 > select(M+1, &R, &W, &E, &T)) {
 
138
        fprintf(stderr, "E: select\n");
 
139
        return EXIT_FAILURE;
 
140
      }
 
141
    }
 
142
 
 
143
    while ((msg = curl_multi_info_read(cm, &Q))) {
 
144
      if (msg->msg == CURLMSG_DONE) {
 
145
        char *url;
 
146
        CURL *e = msg->easy_handle;
 
147
        curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url);
 
148
        fprintf(stderr, "R: %d - %s <%s>\n",
 
149
                msg->data.result, curl_easy_strerror(msg->data.result), url);
 
150
        curl_multi_remove_handle(cm, e);
 
151
        curl_easy_cleanup(e);
 
152
      }
 
153
      else {
 
154
        fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg);
 
155
      }
 
156
      if (C < CNT) {
 
157
        init(cm, C++);
 
158
      }
 
159
    }
 
160
  }
 
161
 
 
162
  curl_multi_cleanup(cm);
 
163
  curl_global_cleanup();
 
164
 
 
165
  return EXIT_SUCCESS;
 
166
}