2011-07-02 14:38:06 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
|
* PokerTH - The open source texas holdem engine *
|
2012-12-25 15:06:23 +01:00
|
|
|
* Copyright (C) 2006-2012 Felix Hammer, Florian Thauer, Lothar May *
|
2011-07-02 14:38:06 +00:00
|
|
|
* *
|
|
|
|
|
* 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/>. *
|
2012-12-25 15:06:23 +01:00
|
|
|
* *
|
|
|
|
|
* *
|
|
|
|
|
* Additional permission under GNU AGPL version 3 section 7 *
|
|
|
|
|
* *
|
|
|
|
|
* If you modify this program, or any covered work, by linking or *
|
|
|
|
|
* combining it with the OpenSSL project's OpenSSL library (or a *
|
|
|
|
|
* modified version of that library), containing parts covered by the *
|
|
|
|
|
* terms of the OpenSSL or SSLeay licenses, the authors of PokerTH *
|
|
|
|
|
* (Felix Hammer, Florian Thauer, Lothar May) grant you additional *
|
|
|
|
|
* permission to convey the resulting work. *
|
|
|
|
|
* Corresponding Source for a non-source form of such a combination *
|
|
|
|
|
* shall include the source code for the parts of OpenSSL used as well *
|
|
|
|
|
* as that of the covered work. *
|
2011-07-02 14:38:06 +00:00
|
|
|
*****************************************************************************/
|
2009-03-21 01:39:00 +00:00
|
|
|
|
|
|
|
|
#include <net/downloaderthread.h>
|
|
|
|
|
#include <net/downloadhelper.h>
|
|
|
|
|
#include <net/netexception.h>
|
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
#include <core/loghelper.h>
|
|
|
|
|
|
2009-03-21 09:11:39 +00:00
|
|
|
#include <fstream>
|
2009-03-21 01:39:00 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
2009-03-22 18:57:23 +00:00
|
|
|
#define DOWNLOAD_DELAY_MSEC 20
|
2009-03-21 01:39:00 +00:00
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace boost::filesystem;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DownloaderThread::DownloaderThread()
|
2011-02-11 20:02:08 +00:00
|
|
|
: m_downloadInProgress(false)
|
2009-03-21 01:39:00 +00:00
|
|
|
{
|
|
|
|
|
m_downloadHelper.reset(new DownloadHelper());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DownloaderThread::~DownloaderThread()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
DownloaderThread::QueueDownload(unsigned downloadId, const std::string &url, const std::string &filename)
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_downloadQueueMutex);
|
|
|
|
|
m_downloadQueue.push(DownloadData(downloadId, url, filename));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2009-03-21 11:36:10 +00:00
|
|
|
DownloaderThread::HasDownloadResult() const
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_downloadDoneQueueMutex);
|
|
|
|
|
return !m_downloadDoneQueue.empty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
DownloaderThread::GetDownloadResult(unsigned &downloadId, std::vector<unsigned char> &filedata)
|
2009-03-21 01:39:00 +00:00
|
|
|
{
|
|
|
|
|
bool result = false;
|
|
|
|
|
boost::mutex::scoped_lock lock(m_downloadDoneQueueMutex);
|
2011-02-11 20:02:08 +00:00
|
|
|
if (!m_downloadDoneQueue.empty()) {
|
2009-03-21 01:39:00 +00:00
|
|
|
const ResultData &d = m_downloadDoneQueue.front();
|
|
|
|
|
downloadId = d.id;
|
|
|
|
|
filedata = d.data;
|
|
|
|
|
m_downloadDoneQueue.pop();
|
|
|
|
|
result = true;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
DownloaderThread::Main()
|
|
|
|
|
{
|
2011-02-11 20:02:08 +00:00
|
|
|
while (!ShouldTerminate()) {
|
|
|
|
|
try {
|
|
|
|
|
if (m_downloadInProgress) {
|
2009-03-21 01:39:00 +00:00
|
|
|
m_downloadInProgress = !m_downloadHelper->Process();
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-11 20:02:08 +00:00
|
|
|
if (!m_downloadInProgress) {
|
2009-03-21 01:39:00 +00:00
|
|
|
// Previous download was finished.
|
2011-02-11 20:02:08 +00:00
|
|
|
if (m_curDownloadData) {
|
2009-03-21 01:39:00 +00:00
|
|
|
path filepath(m_curDownloadData->filename);
|
2015-11-26 16:27:52 +00:00
|
|
|
std::ifstream instream(filepath.file_string().c_str(), ios_base::in | ios_base::binary);
|
2009-03-21 22:58:58 +00:00
|
|
|
// Find out file size.
|
|
|
|
|
// Not fully portable, but works on win/linux/mac.
|
|
|
|
|
instream.seekg(0, ios_base::beg);
|
|
|
|
|
std::streampos startPos = instream.tellg();
|
|
|
|
|
instream.seekg(0, ios_base::end);
|
|
|
|
|
std::streampos endPos = instream.tellg();
|
|
|
|
|
instream.seekg(0, ios_base::beg);
|
|
|
|
|
std::streamoff posDiff(endPos - startPos);
|
|
|
|
|
unsigned fileSize = (unsigned)posDiff;
|
|
|
|
|
|
|
|
|
|
vector<unsigned char> fileData(fileSize);
|
|
|
|
|
instream.read((char *)&fileData[0], fileSize);
|
2009-03-21 01:39:00 +00:00
|
|
|
instream.close();
|
|
|
|
|
remove(filepath);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_downloadDoneQueueMutex);
|
|
|
|
|
m_downloadDoneQueue.push(ResultData(m_curDownloadData->id, fileData));
|
|
|
|
|
}
|
|
|
|
|
m_curDownloadData.reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Take a break.
|
|
|
|
|
Msleep(DOWNLOAD_DELAY_MSEC);
|
|
|
|
|
|
|
|
|
|
// Start next download.
|
|
|
|
|
{
|
|
|
|
|
boost::mutex::scoped_lock lock(m_downloadQueueMutex);
|
2011-02-11 20:02:08 +00:00
|
|
|
if (!m_downloadQueue.empty()) {
|
2009-03-21 01:39:00 +00:00
|
|
|
m_curDownloadData.reset(new DownloadData(m_downloadQueue.front()));
|
|
|
|
|
m_downloadQueue.pop();
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-11 20:02:08 +00:00
|
|
|
if (m_curDownloadData && !m_curDownloadData->filename.empty()) {
|
2009-03-21 22:41:14 +00:00
|
|
|
path filepath(m_curDownloadData->filename);
|
2009-03-21 21:14:09 +00:00
|
|
|
m_downloadHelper->Init(m_curDownloadData->address, filepath.file_string());
|
2009-03-21 01:39:00 +00:00
|
|
|
m_downloadInProgress = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-11 20:02:08 +00:00
|
|
|
} catch (const NetException &e) {
|
2009-03-22 18:57:23 +00:00
|
|
|
LOG_ERROR("Download failed: " << e.what());
|
2009-03-21 20:40:52 +00:00
|
|
|
m_downloadInProgress = false;
|
|
|
|
|
m_curDownloadData.reset();
|
2009-03-21 01:39:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|