215 lines
3.6 KiB
C++
Executable File
215 lines
3.6 KiB
C++
Executable File
/*
|
|
* This file is a part of Winix
|
|
* and is not publicly distributed
|
|
*
|
|
* Copyright (c) 2011, Tomasz Sowa
|
|
* All rights reserved.
|
|
*
|
|
*/
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <unistd.h>
|
|
#include <curl/curl.h>
|
|
#include <string.h>
|
|
#include "exportthread.h"
|
|
#include "core/log.h"
|
|
#include "core/misc.h"
|
|
#include "utf8.h"
|
|
|
|
|
|
|
|
namespace Export
|
|
{
|
|
ExportThread * ExportThread::exp_thread;
|
|
|
|
|
|
ExportThread::ExportThread()
|
|
{
|
|
exp_thread = 0;
|
|
utf8 = false;
|
|
}
|
|
|
|
|
|
|
|
void ExportThread::SetUTF8(bool use_utf8)
|
|
{
|
|
utf8 = use_utf8;
|
|
}
|
|
|
|
|
|
void ExportThread::AddMessage(const Message & message)
|
|
{
|
|
message_tab.insert(message_tab.end(), message);
|
|
WakeUpThread();
|
|
|
|
log << log1 << "yes ser" << logend;
|
|
}
|
|
|
|
|
|
// first thread (objects locked)
|
|
void ExportThread::AddMessage(int type, const std::wstring & url, const std::wstring & path)
|
|
{
|
|
message_add_temp.type = type;
|
|
message_add_temp.url = url;
|
|
message_add_temp.path = path;
|
|
|
|
AddMessage(message_add_temp);
|
|
WakeUpThread();
|
|
}
|
|
|
|
|
|
|
|
// second thread
|
|
// objects are locked
|
|
bool ExportThread::SignalReceived()
|
|
{
|
|
log << log1 << "------------- a ---------------" << logend;
|
|
|
|
return !message_tab.empty();
|
|
}
|
|
|
|
|
|
|
|
// second thread
|
|
// objects are not locked
|
|
void ExportThread::Do()
|
|
{
|
|
MessageTab::iterator i;
|
|
bool end;
|
|
|
|
Lock();
|
|
log << log1 << "------------- swinka ---------------" << logend;
|
|
i = message_tab.begin();
|
|
Unlock();
|
|
|
|
do
|
|
{
|
|
Lock();
|
|
|
|
if( i != message_tab.end() )
|
|
{
|
|
message_work = *i;
|
|
message_tab.erase(i++);
|
|
end = false;
|
|
}
|
|
else
|
|
{
|
|
end = true;
|
|
}
|
|
|
|
Unlock();
|
|
|
|
if( !end )
|
|
DoMessage();
|
|
|
|
}
|
|
while( !end && !IsExitSignal() );
|
|
}
|
|
|
|
|
|
// second thread
|
|
// objects are not locked
|
|
// current message we have in 'message_work'
|
|
void ExportThread::DoMessage()
|
|
{
|
|
Lock();
|
|
|
|
if( utf8 )
|
|
Ezc::WideToUTF8(message_work.url, url_a);
|
|
else
|
|
AssignString(message_work.url, url_a);
|
|
|
|
Unlock();
|
|
|
|
Fetch(url_a.c_str());
|
|
|
|
Lock();
|
|
log << log1 << "sciagnalem takie cos ---------------------------------------------------" << logend;
|
|
|
|
log << "rozmiar: " << buffer.size() << logend;
|
|
|
|
log << log1 << "koniec takiego cosia ---------------------------------------------------" << logend << logsave;
|
|
Unlock();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// second thread
|
|
// objects are not locked
|
|
bool ExportThread::Fetch(const char * url)
|
|
{
|
|
CURL * curl;
|
|
CURLcode res;
|
|
long code;
|
|
|
|
curl = curl_easy_init();
|
|
|
|
if( !curl )
|
|
{
|
|
Lock();
|
|
log << log1 << "Export: I can't use curl" << logend;
|
|
Unlock();
|
|
return false;
|
|
}
|
|
|
|
exp_thread = this;
|
|
buffer.clear();
|
|
|
|
/*
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
|
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); // the http code will be from the last request
|
|
*/
|
|
//curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, StaticSaveFunction);
|
|
curl_easy_setopt(curl, CURLOPT_URL, url);
|
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Winix");
|
|
|
|
res = curl_easy_perform(curl);
|
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
|
|
curl_easy_cleanup(curl);
|
|
|
|
if( res!=0 || code!=200 )
|
|
{
|
|
Lock();
|
|
log << log1 << "Ezport: error: operation result: " << (int)res << ", http code: " << code << logend;
|
|
Unlock();
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
size_t ExportThread::StaticSaveFunction(char * ptr, size_t size, size_t nmemb, void *userdata)
|
|
{
|
|
if( exp_thread )
|
|
return exp_thread->SaveFunction(ptr, size, nmemb, userdata);
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
size_t ExportThread::SaveFunction(char * ptr, size_t size, size_t nmemb, void *userdata)
|
|
{
|
|
size_t len = size * nmemb;
|
|
|
|
if( len > 0 )
|
|
buffer.append(ptr, len);
|
|
|
|
Lock();
|
|
log << log1 << "odebralem cosik: size: " << size << ", nmemb: " << nmemb << logend;
|
|
Unlock();
|
|
|
|
return len;
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace
|