5b1b32ce4612c9dbcb81749f462b0956047c54fe
[platform/upstream/libzypp.git] / devel / devel.dmacvicar / CURLM_tp.cc
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 extern "C" {
5 #include <curl/curl.h>
6 }
7 #include <list>
8 #include <sstream>
9 #include "zypp/base/Exception.h"
10 #include "zypp/base/Logger.h"
11 #include "zypp/Pathname.h"
12 #include "zypp/ExternalProgram.cc"
13 //#include 
14
15 using namespace zypp;
16 using namespace std;
17
18 size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
19 {
20   MIL << "got data : " << size*nmemb << " bytes" << endl;
21   return size*nmemb;
22 }
23
24 int socket_callback(CURL *easy, curl_socket_t s, int what, void *userp,  void *socketp)
25 {
26   MIL << "socket : " << s << " : " << what << endl;
27   return 0;
28 }
29
30 struct Range
31 {
32   Range( off_t f, off_t t)
33     : from(t), to(t)
34   {}
35
36   off_t from;
37   off_t to;
38 };
39
40 int main()
41 {
42    FILE *f = fopen("piece", "w" );
43   curl_global_init(CURL_GLOBAL_ALL);
44   CURLM *curlm;
45   curlm = curl_multi_init();
46   //curl_multi_setopt( curlm, CURLMOPT_PIPELINING, 1);
47   curl_multi_setopt( curlm, CURLMOPT_SOCKETFUNCTION, socket_callback);
48   
49 //   0-1000
50 //   1001-2000
51 //   2001-3000
52 //   
53 //   3000-4000
54 //   4001-5000
55
56   int i=1;
57   for ( ; i < 10; i++ ) {
58     CURL *curl;
59     curl = curl_easy_init();
60     CURLcode success;
61     // http://download.opensuse.org/distribution/10.2/repo/oss/suse/setup/descr/packages
62     if ( (success = curl_easy_setopt(curl, CURLOPT_URL, "http://ftp5.gwdg.de/pub/opensuse/distribution/10.2/repo/oss/suse/setup/descr/packages")) != CURLE_OK)
63       ZYPP_THROW(Exception("url"));
64
65      if ( (success = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data)) != CURLE_OK)
66        ZYPP_THROW(Exception("write data"));
67     //curl_easy_setopt(curl, CURLOPT_WRITEDATA, f);
68     stringstream rs;
69     int k=1;
70     for(; k<2; k++) {
71       rs << ( k!=1 ? "," : "") << (i*k)*1000 << "-" << ((i*k)*1000 + 1000);
72     }
73     MIL << "range: " << rs.str() << endl;
74     if ( (success = curl_easy_setopt(curl, CURLOPT_RANGE, rs.str().c_str())) != CURLE_OK)
75       ZYPP_THROW(Exception("write data"));
76
77     CURLMcode code;
78     if ( (code = curl_multi_add_handle( curlm, curl)) != CURLM_OK)
79       ZYPP_THROW(Exception("write data"));
80     
81   }
82   int still_running = 0;
83   /* we start some action by calling perform right away */
84   while(CURLM_CALL_MULTI_PERFORM ==
85         curl_multi_perform(curlm, &still_running));
86
87   while(still_running) {
88     struct timeval timeout;
89     int rc; /* select() return code */
90
91     fd_set fdread;
92     fd_set fdwrite;
93     fd_set fdexcep;
94     int maxfd;
95
96     FD_ZERO(&fdread);
97     FD_ZERO(&fdwrite);
98     FD_ZERO(&fdexcep);
99
100     /* set a suitable timeout to play around with */
101     timeout.tv_sec = 1;
102     timeout.tv_usec = 0;
103
104     /* get file descriptors from the transfers */
105     curl_multi_fdset(curlm, &fdread, &fdwrite, &fdexcep, &maxfd);
106
107     /* In a real-world program you OF COURSE check the return code of the
108        function calls, *and* you make sure that maxfd is bigger than -1 so
109        that the call to select() below makes sense! */
110
111     rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
112
113     switch(rc) {
114     case -1:
115       /* select error */
116       still_running = 0;
117       ERR << "select() returns error, this is badness" << endl;
118       break;
119     case 0:
120     default:
121       /* timeout or readable/writable sockets */
122       while(CURLM_CALL_MULTI_PERFORM ==
123             curl_multi_perform(curlm, &still_running));
124       break;
125     }
126   }
127
128   int c=999;
129   CURLMsg *m;
130   while ( m = curl_multi_info_read( curlm, &c) )
131   {
132     MIL<< m->msg << " : " << curl_easy_strerror(m->data.result) << endl;
133   }
134   Pathname root("/home/duncan/suse/metadata-diff");
135   
136
137  curl_multi_cleanup(curlm);
138  //curl_easy_cleanup(http_handle);
139
140   return 0;
141 }