quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

10-at-a-time.c (4426B)


      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
      9  *
     10  * This software is licensed as described in the file COPYING, which
     11  * you should have received as part of this distribution. The terms
     12  * are also available at https://curl.se/docs/copyright.html.
     13  *
     14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
     15  * copies of the Software, and permit persons to whom the Software is
     16  * furnished to do so, under the terms of the COPYING file.
     17  *
     18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
     19  * KIND, either express or implied.
     20  *
     21  * SPDX-License-Identifier: curl
     22  *
     23  ***************************************************************************/
     24 /* <DESC>
     25  * Download many files in parallel, in the same thread.
     26  * </DESC>
     27  */
     28 
     29 #include <stdlib.h>
     30 #include <string.h>
     31 #include <curl/curl.h>
     32 
     33 static const char *urls[] = {
     34   "https://www.microsoft.com",
     35   "https://opensource.org",
     36   "https://www.google.com",
     37   "https://www.yahoo.com",
     38   "https://www.ibm.com",
     39   "https://www.mysql.com",
     40   "https://www.oracle.com",
     41   "https://www.ripe.net",
     42   "https://www.iana.org",
     43   "https://www.amazon.com",
     44   "https://www.netcraft.com",
     45   "https://www.heise.de",
     46   "https://www.chip.de",
     47   "https://www.ca.com",
     48   "https://www.cnet.com",
     49   "https://www.mozilla.org",
     50   "https://www.cnn.com",
     51   "https://www.wikipedia.org",
     52   "https://www.dell.com",
     53   "https://www.hp.com",
     54   "https://www.cert.org",
     55   "https://www.mit.edu",
     56   "https://www.nist.gov",
     57   "https://www.ebay.com",
     58   "https://www.playstation.com",
     59   "https://www.uefa.com",
     60   "https://www.ieee.org",
     61   "https://www.apple.com",
     62   "https://www.symantec.com",
     63   "https://www.zdnet.com",
     64   "https://www.fujitsu.com/global/",
     65   "https://www.supermicro.com",
     66   "https://www.hotmail.com",
     67   "https://www.ietf.org",
     68   "https://www.bbc.co.uk",
     69   "https://news.google.com",
     70   "https://www.foxnews.com",
     71   "https://www.msn.com",
     72   "https://www.wired.com",
     73   "https://www.sky.com",
     74   "https://www.usatoday.com",
     75   "https://www.cbs.com",
     76   "https://www.nbc.com/",
     77   "https://slashdot.org",
     78   "https://www.informationweek.com",
     79   "https://apache.org",
     80   "https://www.un.org",
     81 };
     82 
     83 #define MAX_PARALLEL 10 /* number of simultaneous transfers */
     84 #define NUM_URLS sizeof(urls)/sizeof(char *)
     85 
     86 static size_t write_cb(char *data, size_t n, size_t l, void *userp)
     87 {
     88   /* take care of the data here, ignored in this example */
     89   (void)data;
     90   (void)userp;
     91   return n*l;
     92 }
     93 
     94 static void add_transfer(CURLM *cm, unsigned int i, int *left)
     95 {
     96   CURL *eh = curl_easy_init();
     97   curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, write_cb);
     98   curl_easy_setopt(eh, CURLOPT_URL, urls[i]);
     99   curl_easy_setopt(eh, CURLOPT_PRIVATE, urls[i]);
    100   curl_multi_add_handle(cm, eh);
    101   (*left)++;
    102 }
    103 
    104 int main(void)
    105 {
    106   CURLM *cm;
    107   CURLMsg *msg;
    108   unsigned int transfers = 0;
    109   int msgs_left = -1;
    110   int left = 0;
    111 
    112   curl_global_init(CURL_GLOBAL_ALL);
    113   cm = curl_multi_init();
    114 
    115   /* Limit the amount of simultaneous connections curl should allow: */
    116   curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, (long)MAX_PARALLEL);
    117 
    118   for(transfers = 0; transfers < MAX_PARALLEL && transfers < NUM_URLS;
    119       transfers++)
    120     add_transfer(cm, transfers, &left);
    121 
    122   do {
    123     int still_alive = 1;
    124     curl_multi_perform(cm, &still_alive);
    125 
    126     /* !checksrc! disable EQUALSNULL 1 */
    127     while((msg = curl_multi_info_read(cm, &msgs_left)) != NULL) {
    128       if(msg->msg == CURLMSG_DONE) {
    129         char *url;
    130         CURL *e = msg->easy_handle;
    131         curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url);
    132         fprintf(stderr, "R: %d - %s <%s>\n",
    133                 msg->data.result, curl_easy_strerror(msg->data.result), url);
    134         curl_multi_remove_handle(cm, e);
    135         curl_easy_cleanup(e);
    136         left--;
    137       }
    138       else {
    139         fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg);
    140       }
    141       if(transfers < NUM_URLS)
    142         add_transfer(cm, transfers++, &left);
    143     }
    144     if(left)
    145       curl_multi_wait(cm, NULL, 0, 1000, NULL);
    146 
    147   } while(left);
    148 
    149   curl_multi_cleanup(cm);
    150   curl_global_cleanup();
    151 
    152   return EXIT_SUCCESS;
    153 }