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
-19
View File
@@ -1,19 +0,0 @@
#include "callback.h"
Callback::Callback()
{
}
size_t
Callback::writeCallback(char* buf, size_t size, size_t nmemb, void* up)
{
data = "";
for (int c = 0; c<size*nmemb; c++)
{
data.push_back(buf[c]);
}
return size*nmemb;
}
-21
View File
@@ -1,21 +0,0 @@
#ifndef CALLBACK_H
#define CALLBACK_H
#include <iostream>
class Callback
{
public:
Callback();
size_t writeCallback(char* buf, size_t size, size_t nmemb, void* up);
std::string getData() { return data; }
private:
std::string data;
};
#endif // CALLBACK_H
+64 -59
View File
@@ -27,8 +27,7 @@
#include "guilog.h"
#include "configfile.h"
#include "mymessagebox.h"
#include "callback.h"
#include "curl/curl.h"
#include <net/uploaderthread.h>
LogFileDialog::LogFileDialog(QWidget *parent, ConfigFile *c) :
QDialog(parent), myConfig(c),
@@ -46,7 +45,10 @@ ui(new Ui::LogFileDialog)
connect( ui->treeWidget_logFiles, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), this, SLOT(showLogFilePreviewInit()));
connect( ui->pushButton_analyseLogfile, SIGNAL(clicked()), this, SLOT (uploadFile()));
connect( ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(showLogFilePreviewSelected()));
connect( this, SIGNAL(signalUploadCompleted(QString,QString)), this, SLOT(showLogAnalysis(QString,QString)));
connect( this, SIGNAL(signalUploadError(QString,QString)), this, SLOT(showUploadError(QString,QString)));
uploader.reset(new UploaderThread(this));
}
LogFileDialog::~LogFileDialog()
@@ -56,8 +58,21 @@ LogFileDialog::~LogFileDialog()
void LogFileDialog::exec()
{
uploader->Run();
refreshLogFileList();
QDialog::exec();
uploader->SignalTermination();
uploader->Join(THREAD_WAIT_INFINITE);
}
void LogFileDialog::UploadCompleted(const std::string &filename, const std::string &returnMessage)
{
signalUploadCompleted(QString::fromStdString(filename), QString::fromStdString(returnMessage));
}
void LogFileDialog::UploadError(const std::string &filename, const std::string &errorMessage)
{
signalUploadError(QString::fromStdString(filename), QString::fromStdString(errorMessage));
}
void LogFileDialog::refreshLogFileList()
@@ -109,7 +124,7 @@ void LogFileDialog::deleteLogFile()
if(selectedItemsList.at(i)->data(0, Qt::UserRole+1).toString() != "current") {
if(!QFile::remove(selectedItemsList.at(i)->data(0, Qt::UserRole).toString())) {
MyMessageBox::warning(this, "Remove log file", "PokerTH cannot remove this log file, please verify that you have write access to this file!", QMessageBox::Close );
MyMessageBox::warning(this, tr("Remove log file"), tr("PokerTH cannot remove this log file, please verify that you have write access to this file!"), QMessageBox::Close );
}
}
}
@@ -199,71 +214,28 @@ void LogFileDialog::keyPressEvent ( QKeyEvent * event )
}
}
Callback* curl_writedata = new Callback();
void writeCallback(char* buf, size_t size, size_t nmemb, void* up)
{
curl_writedata->writeCallback(buf, size, nmemb, up);
}
void LogFileDialog::uploadFile()
{
const QString buttonText(tr("Upload in progress"));
const QString errorMessage(tr("Upload failed. Please check your internet connection!\nUploading log files may fail if you are using an http proxy."));
const QString processMessage(tr("Processing of the log file on the web server failed.\nPlease verify that you are uploading a valid PokerTH log file."));
const QString reasonMessage(tr("Failure reason: "));
QTreeWidgetItem* selectedItem = ui->treeWidget_logFiles->currentItem();
if(selectedItem) {
file.setFileName(selectedItem->data(0, Qt::UserRole).toString());
CURL *curl;
CURLcode res;
struct curl_httppost* post = NULL;
struct curl_httppost* last = NULL;
curl = curl_easy_init();
if(curl) {
curl_formadd(&post, &last,
CURLFORM_COPYNAME, "pdb_file",
CURLFORM_FILE, file.fileName().toStdString().c_str(),
CURLFORM_END);
curl_easy_setopt(curl, CURLOPT_URL, "http://pokerth.net/floty_upload/upload.php");
curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
res = curl_easy_perform(curl);
if(res) {
qDebug() << "curl_easy_perform failed: " << res << endl;
}
id = QString(curl_writedata->getData().data()).trimmed();
curl_formfree(post);
if(id.length() == 40 && !id.contains("ERROR")) {
qDebug() << id << endl;
QDesktopServices::openUrl(QUrl("http://logfile-analysis.pokerth.net/?ID="+id));
} else {
qDebug() << QString::fromStdString(curl_writedata->getData()) << endl;
}
} else {
qDebug() << "curl_easy_init failed\n" << endl;
}
}
if(selectedItem) {
file.setFileName(selectedItem->data(0, Qt::UserRole).toString());
uploadInProgressAnimationStart();
uploader->QueueUpload(
"http://pokerth.nett/floty_upload/upload.php",
"",
"",
file.fileName().toStdString(),
file.size(),
"pdb_file");
}
}
void LogFileDialog::uploadInProgressAnimationStart()
{
ui->pushButton_analyseLogfile->setDisabled(TRUE);
//const QString buttonText(tr("Upload in progress"));
ui->pushButton_analyseLogfile->setDisabled(TRUE);
QMovie *movie = new QMovie(":/gfx/loader.gif");
ui->label_animation->setMovie(movie);
@@ -281,3 +253,36 @@ void LogFileDialog::uploadInProgressAnimationStop()
ui->horizontalLayout_animation->setSpacing(0);
ui->label_animation->setGeometry(0,0,0,0);
}
void LogFileDialog::showLogAnalysis(QString /*filename*/, QString returnMessage)
{
uploadInProgressAnimationStop();
QString id = returnMessage.trimmed();
if(id.length() == 40 && !id.contains("ERROR")) {
qDebug() << id << endl;
QDesktopServices::openUrl(QUrl("http://logfile-analysis.pokerth.net/?ID="+id));
} else {
qDebug() << returnMessage << endl;
QString serverMsg(tr("Processing of the log file on the web server failed.\nPlease verify that you are uploading a valid PokerTH log file."));
// if there is a readable message, display it.
if (!returnMessage.isEmpty() && returnMessage.size() < 128) {
serverMsg += "\n" + tr("Failure reason: ") + returnMessage;
}
MyMessageBox::warning(
this, tr("Uploading log file"), serverMsg, QMessageBox::Close );
}
}
void LogFileDialog::showUploadError(QString /*filename*/, QString errorMessage)
{
uploadInProgressAnimationStop();
QString uploadMsg(tr("Upload failed. Please check your internet connection!\nUploading log files may fail if you are using an http proxy."));
uploadMsg += "\n" + tr("Failure reason: ") + errorMessage;
MyMessageBox::warning(
this, tr("Uploading log file"), uploadMsg, QMessageBox::Close );
}
+16 -2
View File
@@ -21,6 +21,10 @@
#include <QDialog>
#include <QFile>
#include <net/uploadcallback.h>
#include <boost/shared_ptr.hpp>
class UploaderThread;
namespace Ui {
class LogFileDialog;
@@ -32,7 +36,7 @@ class guiLog;
class ConfigFile;
class Callback;
class LogFileDialog : public QDialog
class LogFileDialog : public QDialog, public UploadCallback
{
Q_OBJECT
@@ -43,6 +47,13 @@ public:
void setGuiLog(guiLog *g) { myGuiLog = g; }
void exec();
virtual void UploadCompleted(const std::string &filename, const std::string &returnMessage);
virtual void UploadError(const std::string &filename, const std::string &errorMessage);
signals:
void signalUploadCompleted(QString filename, QString returnMessage);
void signalUploadError(QString filename, QString errorMessage);
public slots:
void refreshLogFileList();
void deleteLogFile();
@@ -57,6 +68,9 @@ public slots:
void uploadInProgressAnimationStart();
void uploadInProgressAnimationStop();
void showLogAnalysis(QString filename, QString returnMessage);
void showUploadError(QString filename, QString errorMessage);
private:
ConfigFile *myConfig;
guiLog *myGuiLog;
@@ -64,7 +78,7 @@ private:
int writer(char *data, size_t size, size_t nmemb,std::string *buffer);
QFile file;
QString id;
boost::shared_ptr<UploaderThread> uploader;
};
#endif // LOGFILEDIALOG_H