Switch to uploader thread for log file uploads.

This commit is contained in:
lotodore
2012-12-20 14:29:43 -08:00
parent fe7f34a504
commit 26507df7ef
17 changed files with 240 additions and 142 deletions
+1 -1
View File
@@ -36,7 +36,7 @@ DownloadHelper::~DownloadHelper()
}
void
DownloadHelper::InternalInit(const string &/*url*/, const string &targetFileName, const string &/*user*/, const string &/*password*/, size_t /*filesize*/)
DownloadHelper::InternalInit(const string &/*url*/, const string &targetFileName, const string &/*user*/, const string &/*password*/, size_t /*filesize*/, const string &/*httpPost*/)
{
// Open target file for writing.
GetData()->targetFile = fopen(targetFileName.c_str(), "wb");
+17 -5
View File
@@ -31,9 +31,6 @@ using namespace std;
TransferHelper::TransferHelper()
{
m_data.reset(new TransferData);
m_data->curlHandle = NULL;
m_data->curlMultiHandle = NULL;
m_data->targetFile = NULL;
}
TransferHelper::~TransferHelper()
@@ -42,8 +39,11 @@ TransferHelper::~TransferHelper()
}
void
TransferHelper::Init(const string &url, const string &targetFileName, const string &user, const string &password, size_t filesize)
TransferHelper::Init(const string &url, const string &targetFileName, const string &user, const string &password, size_t filesize, const string &httpPost)
{
// Cleanup data.
Cleanup();
m_data->returnMessage.clear();
// Initialise curl.
m_data->curlHandle = curl_easy_init();
if (!m_data->curlHandle)
@@ -57,7 +57,7 @@ TransferHelper::Init(const string &url, const string &targetFileName, const stri
if (curl_easy_setopt(m_data->curlHandle, CURLOPT_URL, m_data->curlUrl.c_str()) != CURLE_OK)
throw NetException(__FILE__, __LINE__, ERR_SOCK_TRANSFER_INVALID_URL, 0);
InternalInit(url, targetFileName, user, password, filesize);
InternalInit(url, targetFileName, user, password, filesize, httpPost);
// Use the multi interface for better abort handling.
if (curl_multi_add_handle(m_data->curlMultiHandle, m_data->curlHandle) != CURLM_OK)
@@ -128,6 +128,10 @@ TransferHelper::Process()
void
TransferHelper::Cleanup()
{
if (m_data->post) {
curl_formfree(m_data->post);
m_data->post = NULL;
}
if (m_data->curlMultiHandle) {
curl_multi_cleanup(m_data->curlMultiHandle);
m_data->curlMultiHandle = NULL;
@@ -143,6 +147,14 @@ TransferHelper::Cleanup()
}
}
string
TransferHelper::ResetLastMessage()
{
string retVal(m_data->returnMessage);
m_data->returnMessage.clear();
return retVal;
}
boost::shared_ptr<TransferData>
TransferHelper::GetData()
{
+26
View File
@@ -0,0 +1,26 @@
/*****************************************************************************
* PokerTH - The open source texas holdem engine *
* Copyright (C) 2006-2012 Felix Hammer, Florian Thauer, Lothar May *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*****************************************************************************/
#include <net/uploadcallback.h>
UploadCallback::~UploadCallback()
{
}
+20 -7
View File
@@ -18,6 +18,7 @@
#include <net/uploaderthread.h>
#include <net/uploadhelper.h>
#include <net/uploadcallback.h>
#include <net/netexception.h>
#include <boost/filesystem.hpp>
#include <core/loghelper.h>
@@ -28,8 +29,8 @@ using namespace std;
using namespace boost::filesystem;
UploaderThread::UploaderThread()
: m_uploadInProgress(false)
UploaderThread::UploaderThread(UploadCallback *callback)
: m_uploadInProgress(false), m_callback(callback)
{
m_uploadHelper.reset(new UploadHelper());
}
@@ -39,15 +40,16 @@ UploaderThread::~UploaderThread()
}
void
UploaderThread::QueueUpload(const string &url, const string &user, const string &pwd, const string &filename, size_t filesize)
UploaderThread::QueueUpload(const string &url, const string &user, const string &pwd, const string &filename, size_t filesize, const string &httpPost)
{
boost::mutex::scoped_lock lock(m_uploadQueueMutex);
m_uploadQueue.push(UploadData(url, user, pwd, filename, filesize));
m_uploadQueue.push(UploadData(url, user, pwd, filename, filesize, httpPost));
}
void
UploaderThread::Main()
{
string lastfile;
while (!ShouldTerminate()) {
try {
if (m_uploadInProgress) {
@@ -55,29 +57,40 @@ UploaderThread::Main()
}
if (!m_uploadInProgress) {
string lastMsg(m_uploadHelper->ResetLastMessage());
if (!lastMsg.empty() && m_callback) {
m_callback->UploadCompleted(lastfile, lastMsg);
}
Msleep(UPLOAD_DELAY_MSEC);
// The upload needs only local state, as no value needs to be returned.
UploadData data;
{
boost::mutex::scoped_lock lock(m_uploadQueueMutex);
if (!m_uploadQueue.empty()) {
data = m_uploadQueue.front();
lastfile = data.filename;
m_uploadQueue.pop();
}
}
if (!data.filename.empty() && data.filesize > 0) {
path filepath(data.filename);
string url(data.address);
if (data.httpPost.empty()) {
#if BOOST_FILESYSTEM_VERSION != 3
m_uploadHelper->Init(data.address + filepath.leaf(), filepath.file_string(), data.user, data.pwd, data.filesize);
url += filepath.leaf();
#else
m_uploadHelper->Init(data.address + filepath.filename().string(), filepath.file_string(), data.user, data.pwd, data.filesize);
url += filepath.filename().string();
#endif
}
m_uploadHelper->Init(url, filepath.file_string(), data.user, data.pwd, data.filesize, data.httpPost);
m_uploadInProgress = true;
}
}
} catch (const NetException &e) {
LOG_ERROR("Upload failed: " << e.what());
m_uploadInProgress = false;
if (m_callback) {
m_callback->UploadError(lastfile, e.what());
}
}
}
}
+35 -14
View File
@@ -34,6 +34,14 @@ readFunction(char *bufptr, size_t size, size_t nitems, void *userp)
return fread(bufptr, size, nitems, (FILE *)userp);
}
size_t
writeFunction(char *bufptr, size_t size, size_t nitems, void *userp)
{
string msgPart(bufptr, size * nitems);
((string *)userp)->append(msgPart);
return size * nitems;
}
UploadHelper::UploadHelper()
{
}
@@ -43,22 +51,35 @@ UploadHelper::~UploadHelper()
}
void
UploadHelper::InternalInit(const string &/*url*/, const string &targetFileName, const string &user, const string &password, size_t filesize)
UploadHelper::InternalInit(const string &/*url*/, const string &targetFileName, const string &user, const string &password, size_t filesize, const string &httpPost)
{
// Open target file for reading.
GetData()->targetFile = fopen(targetFileName.c_str(), "rb");
if (!GetData()->targetFile)
throw NetException(__FILE__, __LINE__, ERR_SOCK_TRANSFER_OPEN_FAILED, 0);
GetData()->userCredentials = user + ":" + password;
// Assume that the following calls never fail.
curl_easy_setopt(GetData()->curlHandle, CURLOPT_READFUNCTION, readFunction);
curl_easy_setopt(GetData()->curlHandle, CURLOPT_READDATA, GetData()->targetFile);
curl_easy_setopt(GetData()->curlHandle, CURLOPT_INFILESIZE, filesize);
curl_easy_setopt(GetData()->curlHandle, CURLOPT_USERPWD, GetData()->userCredentials.c_str());
curl_easy_setopt(GetData()->curlHandle, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(GetData()->curlHandle, CURLOPT_PUT, 1L);
curl_easy_setopt(GetData()->curlHandle, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(GetData()->curlHandle, CURLOPT_SSL_VERIFYHOST, 0L);
if (!user.empty() || !password.empty()) {
GetData()->userCredentials = user + ":" + password;
curl_easy_setopt(GetData()->curlHandle, CURLOPT_USERPWD, GetData()->userCredentials.c_str());
}
if (httpPost.empty()) {
// Open target file for reading.
GetData()->targetFile = fopen(targetFileName.c_str(), "rb");
if (!GetData()->targetFile)
throw NetException(__FILE__, __LINE__, ERR_SOCK_TRANSFER_OPEN_FAILED, 0);
curl_easy_setopt(GetData()->curlHandle, CURLOPT_READFUNCTION, readFunction);
curl_easy_setopt(GetData()->curlHandle, CURLOPT_READDATA, GetData()->targetFile);
curl_easy_setopt(GetData()->curlHandle, CURLOPT_PUT, 1L);
curl_easy_setopt(GetData()->curlHandle, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(GetData()->curlHandle, CURLOPT_INFILESIZE, filesize);
} else {
// Curl will handle file I/O.
struct curl_httppost *last = NULL;
curl_formadd(&GetData()->post, &last,
CURLFORM_COPYNAME, httpPost.c_str(),
CURLFORM_FILE, targetFileName.c_str(),
CURLFORM_END);
curl_easy_setopt(GetData()->curlHandle, CURLOPT_HTTPPOST, GetData()->post);
curl_easy_setopt(GetData()->curlHandle, CURLOPT_WRITEFUNCTION, writeFunction);
curl_easy_setopt(GetData()->curlHandle, CURLOPT_WRITEDATA, &GetData()->returnMessage);
}
}