summaryrefslogtreecommitdiffstats
path: root/test/src/test-curl-multi-api.cpp
blob: 67735d2eb2d0121dc0105af153ff1f72df4ced09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include <curl/curl.h>
#include <glog/logging.h>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>

#include "uscxml/concurrency/tinythread.h"

// use arabica URL parser
#include <io/uri.hpp>

class URL;

class URLMonitor {
public:
	virtual void downloadStarted(const URL& url) {};
	virtual void downloadCompleted(const URL& url) {};
	virtual void downloadFailed(const URL& url, int errorCode) {};
	virtual void headerChunkReceived(const URL& url, const std::string& headerChunk) {};
	virtual void contentChunkReceived(const URL& url, const std::string& contentChunk) {};
};

class URLImpl : public boost::enable_shared_from_this<URLImpl> {
public:
	URLImpl(const std::string& url) : _handle(NULL), _uri(url), _isDownloaded(false) {
		_handle = curl_easy_init();
		if (_handle != NULL) {
			CURLcode curlError;
			curlError = curl_easy_setopt(_handle, CURLOPT_URL, _uri.as_string().c_str());
			if (curlError != CURLE_OK)
				LOG(ERROR) << "Cannot set url to " << _uri.as_string() << ": " << curl_easy_strerror(curlError);

			curlError = curl_easy_setopt(_handle, CURLOPT_WRITEDATA, this);
			if (curlError != CURLE_OK)
				LOG(ERROR) << "Cannot register this as write userdata: " << curl_easy_strerror(curlError);

			curlError = curl_easy_setopt(_handle, CURLOPT_WRITEFUNCTION, URLImpl::writeHandler);
			if (curlError != CURLE_OK)
				LOG(ERROR) << "Cannot set write callback: " << curl_easy_strerror(curlError);

			curlError = curl_easy_setopt(_handle, CURLOPT_HEADERFUNCTION, URLImpl::headerHandler);
			if (curlError != CURLE_OK)
				LOG(ERROR) << "Cannot request header from curl: " << curl_easy_strerror(curlError);

			curlError = curl_easy_setopt(_handle, CURLOPT_HEADERDATA, this);
			if (curlError != CURLE_OK)
				LOG(ERROR) << "Cannot register this as header userdata: " << curl_easy_strerror(curlError);
		} else {
			LOG(ERROR) << "curl_easy_init returned NULL, this is bad!";
		}
	}

	~URLImpl() {
		if (_handle != NULL)
			curl_easy_cleanup(_handle);
	}

	static size_t writeHandler(void *ptr, size_t size, size_t nmemb, void *userdata) {
		URLImpl* url = (URLImpl*)userdata;
		url->_content.write((char*)ptr, size * nmemb);
		return size * nmemb;
	}

	static size_t headerHandler(void *ptr, size_t size, size_t nmemb, void *userdata) {
		URLImpl* url = (URLImpl*)userdata;
		url->_header.write((char*)ptr, size * nmemb);
		return size * nmemb;
	}

	void addMonitor(URLMonitor* monitor)    {
		_monitors.insert(monitor);
	}
	void removeMonitor(URLMonitor* monitor) {
		_monitors.erase(monitor);
	}

	const bool isAbsolute()      const {
		return _uri.is_absolute();
	}
	const std::string scheme()   const {
		return _uri.scheme();
	}
	const std::string host()     const {
		return _uri.host();
	}
	const std::string port()     const {
		return _uri.port();
	}
	const std::string path()     const {
		return _uri.path();
	}
	const std::string asString() const {
		return _uri.as_string();
	}

	void downloadStarted() {
		std::cout << "Starting download of " << asString() << std::endl;
		_content.str("");
		_content.clear();
		_header.str("");
		_header.clear();
		monIter_t monIter = _monitors.begin();
		while(monIter != _monitors.end()) {
//      (*monIter)->downloadStarted(URL(shared_from_this()));
			monIter++;
		}
	}

	void downloadCompleted() {
		std::cout << "Finished loading " << asString() << " with " << _content.str().size() << " bytes" << std::endl;
		_isDownloaded = true;
	}

	void downloadFailed(int errorCode) {
		std::cout << "FAILED!" << strerror(errorCode) << std::endl;
	}

	std::string getHeader(bool forceReload = false) {
		return _header.str();
	}

	std::string getContent(bool forceReload = false) {
		return _content.str();
	}

	std::stringstream _content;
	std::stringstream _header;
	CURL* _handle;
	Arabica::io::URI _uri;
	bool _isDownloaded;

	std::set<URLMonitor*> _monitors;
	typedef std::set<URLMonitor*>::iterator monIter_t;
};

class URL {
public:
	URL() : _impl() {}
	URL(const std::string url) : _impl(new URLImpl(url)) {}
	URL(boost::shared_ptr<URLImpl> const impl) : _impl(impl) { }
	URL(const URL& other) : _impl(other._impl) { }
	virtual ~URL() {};

	operator bool() const {
		return _impl;
	}
	bool operator< (const URL& other) const {
		return _impl < other._impl;
	}
	bool operator==(const URL& other) const {
		return _impl == other._impl;
	}
	bool operator!=(const URL& other) const {
		return _impl != other._impl;
	}
	URL& operator= (const URL& other)       {
		_impl = other._impl;
		return *this;
	}

	std::string getHeader() {
		return _impl->getHeader();
	}
	std::string getContent() {
		return _impl->getContent();
	}

	const bool toAbsoluteCwd()                        {
		return _impl->toAbsoluteCwd();
	}
	const bool toAbsolute(const std::string& baseUrl) {
		return _impl->toAbsolute(baseUrl);
	}
	const bool toAbsolute(const URL& baseUrl)         {
		return _impl->toAbsolute(baseUrl.asString());
	}
	const std::string asLocalFile(const std::string& suffix, bool reload = false) {
		return _impl->asLocalFile(suffix, reload);
	}

	void addMonitor(URLMonitor* monitor)    {
		_impl->addMonitor(monitor);
	}
	void removeMonitor(URLMonitor* monitor) {
		_impl->removeMonitor(monitor);
	}

	const bool isAbsolute()      const {
		return _impl->isAbsolute();
	}
	const std::string scheme()   const {
		return _impl->scheme();
	}
	const std::string host()     const {
		return _impl->host();
	}
	const std::string port()     const {
		return _impl->port();
	}
	const std::string path()     const {
		return _impl->path();
	}
	const std::string asString() const {
		return _impl->asString();
	}

	friend class URLFetcher;
	friend std::ostream & operator<<(std::ostream &stream, const URL& p);

protected:
	void downloadStarted() {
		return _impl->downloadStarted();
	}
	void downloadCompleted() {
		return _impl->downloadCompleted();
	}
	void downloadFailed(int errorCode) {
		return _impl->downloadFailed(errorCode);
	}

	boost::shared_ptr<URLImpl> _impl;
};

class URLFetcher {
public:
	URLFetcher() {
		_multiHandle = curl_multi_init();
		start();
	}

	~URLFetcher() {
		curl_multi_cleanup(_multiHandle);
		stop();
	}

	void fetchURL(URL& url) {
		tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
		url.downloadStarted();
		_handlesToURLs[url._impl->_handle] = url;
		curl_multi_add_handle(_multiHandle, url._impl->_handle);
		_condVar.notify_all();
	}

	void breakURL(URL& url) {
		tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
		if (_handlesToURLs.find(url._impl->_handle) != _handlesToURLs.end()) {
			url.downloadFailed(0);
			curl_multi_remove_handle(_multiHandle, url._impl->_handle);
			_handlesToURLs.erase(url._impl->_handle);
		}
	}

	void start() {
		tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
		if (!_isStarted) {
			_isStarted = true;
			_thread = new tthread::thread(URLFetcher::run, this);
		}
	}

	void stop() {
		tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
		if (_isStarted) {
			_isStarted = false;
			_thread->join();
			delete _thread;
		}
	}

	static void run(void* instance) {
		URLFetcher* THIS = (URLFetcher*)instance;
		THIS->_mutex.lock();
		while(THIS->_isStarted) {
			if(THIS->_handlesToURLs.size() > 0) {
				THIS->_mutex.unlock();
				THIS->perform();
				THIS->_mutex.lock();
			}
			THIS->_condVar.wait(THIS->_mutex);
		}
		THIS->_mutex.unlock();
	}

	void perform() {

		CURLMsg *msg; /* for picking up messages with the transfer status */
		int msgsLeft; /* how many messages are left */
		int stillRunning;

		{
			tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
			curl_multi_perform(_multiHandle, &stillRunning);
		}

		do {
			struct timeval timeout;
			int rc; /* select() return code */

			fd_set fdread, fdwrite, fdexcep;
			FD_ZERO(&fdread);
			FD_ZERO(&fdwrite);
			FD_ZERO(&fdexcep);

			int maxfd = -1;
			long curlTimeOut = -1;

			/* set a suitable timeout to play around with */
			timeout.tv_sec = 1;
			timeout.tv_usec = 0;

			{
				tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
				curl_multi_timeout(_multiHandle, &curlTimeOut);
			}

			if(curlTimeOut >= 0) {
				timeout.tv_sec = curlTimeOut / 1000;
				if(timeout.tv_sec > 1)
					timeout.tv_sec = 1;
				else
					timeout.tv_usec = (curlTimeOut % 1000) * 1000;
			}

			/* get file descriptors from the transfers */
			{
				tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
				curl_multi_fdset(_multiHandle, &fdread, &fdwrite, &fdexcep, &maxfd);
			}

			rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);

			switch(rc) {
			case -1:
				/* select error */
				break;
			case 0: /* timeout */
			default: { /* action */
				tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
				curl_multi_perform(_multiHandle, &stillRunning);
			}
			break;
			}

			{
				tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
				while ((msg = curl_multi_info_read(_multiHandle, &msgsLeft))) {
					if (msg->msg == CURLMSG_DONE) {
						_handlesToURLs[msg->easy_handle].downloadCompleted();
						curl_multi_remove_handle(_multiHandle, msg->easy_handle);
						_handlesToURLs.erase(msg->easy_handle);
					} else {
						switch (msg->data.result) {
						case CURLM_OK:
							break;
						case CURLM_BAD_HANDLE:
						case CURLM_BAD_EASY_HANDLE:
						case CURLM_OUT_OF_MEMORY:
						case CURLM_INTERNAL_ERROR:
						case CURLM_BAD_SOCKET:
						case CURLM_UNKNOWN_OPTION:
						case CURLM_LAST:
							_handlesToURLs[msg->easy_handle].downloadFailed(msg->data.result);
							curl_multi_remove_handle(_multiHandle, msg->easy_handle);
							_handlesToURLs.erase(msg->easy_handle);
						default:
							break;
						}
					}
				}
			}
		} while(stillRunning && _isStarted);

	}

	tthread::condition_variable _condVar;
	tthread::thread* _thread;
	tthread::recursive_mutex _mutex;
	bool _isStarted;

	std::map<CURL*, URL> _handlesToURLs;
	CURLM* _multiHandle;
};


int main(int argc, char** argv) {
	URLFetcher fetcher;
	URL heise("http://www.heise.de");
	URL localFile("file:///Users/sradomski/Desktop/scxml.xsd");
	URL slashdot("http://slashdot.org");
	URL asdf("daf://localhost:234");
	URL bahn("http://www.bahn.de");

	fetcher.fetchURL(heise);
	fetcher.fetchURL(localFile);
	fetcher.fetchURL(asdf);
	fetcher.fetchURL(slashdot);
	fetcher.fetchURL(bahn);

	while(1) {}
}