diff options
author | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2016-01-24 13:45:40 +0300 |
---|---|---|
committer | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2016-01-24 13:45:40 +0300 |
commit | 3a9a8aa78ada7c5137b4b74c41be7088d2d1fd00 (patch) | |
tree | d8bec65519f070944738cbaeecd70536f4b7abcb | |
parent | 7c4d85348ecbb9913948edc53b82efaa97d85fda (diff) |
server:
curl_downloader_module:
basic data transfer handling (now able to save file, still need a lot of validation)
some tracing code
this may be called first working prototype with vary basic functionality implemented.
currently udm have working server, working qt client and able to download url via curl module (most of additional events like
download progress currently not
implemented, code need lots of sanity checks and exception handling)
-rw-r--r-- | client-qt/udm-client-qt/udm_main.cpp | 1 | ||||
-rw-r--r-- | server/modules/downloaders/curl/include/curl_download.h | 6 | ||||
-rw-r--r-- | server/modules/downloaders/curl/src/curl_download.cpp | 54 |
3 files changed, 59 insertions, 2 deletions
diff --git a/client-qt/udm-client-qt/udm_main.cpp b/client-qt/udm-client-qt/udm_main.cpp index 4110f03..5df22a7 100644 --- a/client-qt/udm-client-qt/udm_main.cpp +++ b/client-qt/udm-client-qt/udm_main.cpp @@ -543,5 +543,6 @@ void udm_main::client_disconnected() btn_del->setEnabled(false); btn_start->setEnabled(false); btn_stop->setEnabled(false); + //TODO: disable all actions which require alive connection lbl_state->setText(tr("State") + ": " + tr("Disconnected")); } diff --git a/server/modules/downloaders/curl/include/curl_download.h b/server/modules/downloaders/curl/include/curl_download.h index c98b64e..5aea97e 100644 --- a/server/modules/downloaders/curl/include/curl_download.h +++ b/server/modules/downloaders/curl/include/curl_download.h @@ -23,6 +23,7 @@ #include <api_module_downloader.h> #include <curl/curl.h> +#include <fstream> class curl_download : public download_s { @@ -41,6 +42,10 @@ class curl_download : public download_s { return download_path; } + std::ofstream &get_ofile() + { + return ofile; + } private: void perform_internal(); CURL *easy_handle = nullptr; @@ -49,6 +54,7 @@ class curl_download : public download_s std::string download_path; core_api *api = nullptr; module_base *module = nullptr; + std::ofstream ofile; }; #endif // CURL_DOWNLOAD_H diff --git a/server/modules/downloaders/curl/src/curl_download.cpp b/server/modules/downloaders/curl/src/curl_download.cpp index 054ce22..d80a6e4 100644 --- a/server/modules/downloaders/curl/src/curl_download.cpp +++ b/server/modules/downloaders/curl/src/curl_download.cpp @@ -21,6 +21,7 @@ #include "curl_download.h" #include <boost/thread.hpp> +#include <boost/log/trivial.hpp> size_t curl_w_callback(char *ptr, size_t size, size_t nmemb, void *userdata) @@ -29,10 +30,34 @@ size_t curl_w_callback(char *ptr, size_t size, size_t nmemb, void *userdata) if(d->get_cancel_state()) return -1; //cancel transfer //TODO: implement pause + std::ofstream &of(d->get_ofile()); + if(!of.is_open()) + { + //TODO: sanity check and path validation + try{ + of.open(d->get_download_path(), std::ios::out | std::ios::binary); + } + catch(...) //TODO: handle exception + { + } + } size_t size_ = size * nmemb; if(size_) { - //TODO: write data + if(of.is_open()) + { + try{ + of.write(ptr, size_); + } + catch(...) //TODO: handle exception + { + } + } + else + { + return -1; + //TODO: handle error + } } return size_; @@ -78,6 +103,7 @@ bool curl_download::delete_download() { cancel_transfer = true; curl_easy_cleanup(easy_handle); + //TODO: cleanup metadata return true; //TODO: } @@ -88,6 +114,30 @@ void curl_download::perform_internal() state = core_events::download_error; else state = core_events::download_completed; + ofile.close(); + {//curl debug + char *url; + curl_easy_getinfo(easy_handle, CURLINFO_EFFECTIVE_URL, &url); + BOOST_LOG_TRIVIAL(debug)<<__FILE__<<":"<<__LINE__<<"\t"<<__func__<<"\nCURLINFO_EFFECTIVE_URL:\t"<<url; + long code = 0; + curl_easy_getinfo(easy_handle, CURLINFO_RESPONSE_CODE, &code); + BOOST_LOG_TRIVIAL(debug)<<"\nCURLINFO_RESPONSE_CODE:\t"<<code; + curl_easy_getinfo(easy_handle, CURLINFO_HTTP_CONNECTCODE, &code); + BOOST_LOG_TRIVIAL(debug)<<"\nCURLINFO_HTTP_CONNECTCODE:\t"<<code; + curl_easy_getinfo(easy_handle, CURLINFO_REDIRECT_COUNT, &code); + BOOST_LOG_TRIVIAL(debug)<<"\nCURLINFO_REDIRECT_COUNT:\t"<<code; + curl_easy_getinfo(easy_handle, CURLINFO_SIZE_DOWNLOAD, &code); + BOOST_LOG_TRIVIAL(debug)<<"\nCURLINFO_SIZE_DOWNLOAD:\t"<<code; + curl_easy_getinfo(easy_handle, CURLINFO_HEADER_SIZE, &code); + BOOST_LOG_TRIVIAL(debug)<<"\nCURLINFO_HEADER_SIZE:\t"<<code; + double length = 0; + curl_easy_getinfo(easy_handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &length); + BOOST_LOG_TRIVIAL(debug)<<"\nCURLINFO_CONTENT_LENGTH_DOWNLOAD:\t"<<length; + curl_easy_getinfo(easy_handle, CURLINFO_CONTENT_TYPE, &url); + BOOST_LOG_TRIVIAL(debug)<<"\nCURLINFO_CONTENT_TYPE:\t"<<url; + + std::cout<<std::endl; + } std::list<core_events::download_state_info> l; core_events::download_state_info i; i.download_id = id; @@ -99,6 +149,6 @@ void curl_download::perform_internal() curl_download::~curl_download() { - curl_easy_cleanup(easy_handle); + //curl_easy_cleanup(easy_handle); //this crashes for some reason } |