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