Basic failure propagation from failed uploads

This commit is contained in:
Anton Thomasson 2021-11-17 20:43:58 +01:00
parent 81c85f3a51
commit 6b11d16bfc
10 changed files with 75 additions and 27 deletions

View file

@ -14,6 +14,8 @@
#include "pdf2printable.h"
#include "ppm2pwg.h"
#define OK(call) if(!(call)) throw ConvertFailedException()
ConvertWorker::ConvertWorker(IppPrinter* parent) // : QObject((QObject*)parent) borks multithereading?!
{
_printer = parent;
@ -21,46 +23,52 @@ ConvertWorker::ConvertWorker(IppPrinter* parent) // : QObject((QObject*)parent)
void ConvertWorker::command(QByteArray msg)
{
CurlRequester cid(_printer->httpUrl());
cid.setFinishedCallback(_printer, &IppPrinter::getPrinterAttributesFinished);
CurlRequester cr(_printer->httpUrl());
cr.setFinishedCallback(_printer, &IppPrinter::getPrinterAttributesFinished);
qDebug() << "command...";
cid.write(msg.data(), msg.length());
cr.write(msg.data(), msg.length());
}
// TODO: de-duplicate
void ConvertWorker::getJobs(QByteArray msg)
{
CurlRequester cid(_printer->httpUrl());
cid.setFinishedCallback(_printer, &IppPrinter::getJobsRequestFinished);
CurlRequester cr(_printer->httpUrl());
cr.setFinishedCallback(_printer, &IppPrinter::getJobsRequestFinished);
cid.write(msg.data(), msg.length());
cr.write(msg.data(), msg.length());
}
void ConvertWorker::cancelJob(QByteArray msg)
{
CurlRequester cid(_printer->httpUrl());
cid.setFinishedCallback(_printer, &IppPrinter::cancelJobFinished);
CurlRequester cr(_printer->httpUrl());
cr.setFinishedCallback(_printer, &IppPrinter::cancelJobFinished);
cid.write(msg.data(), msg.length());
cr.write(msg.data(), msg.length());
}
void ConvertWorker::justUpload(QString filename, QByteArray header)
{
try {
emit busyMessage(tr("Printing"));
CurlRequester cid(_printer->httpUrl());
cid.setFinishedCallback(_printer, &IppPrinter::printRequestFinished);
CurlRequester cr(_printer->httpUrl());
cr.setFinishedCallback(_printer, &IppPrinter::printRequestFinished);
QFile file(filename);
file.open(QFile::ReadOnly);
cid.write(header.data(), header.length());
OK(cr.write(header.data(), header.length()));
QByteArray tmp = file.readAll();
cid.write(tmp.data(), tmp.length());
OK(cr.write(tmp.data(), tmp.length()));
file.close();
}
catch(const ConvertFailedException& e)
{
emit failed(e.what() == QString("") ? tr("Upload error") : e.what());
}
}
void ConvertWorker::convertPdf(QString filename, QByteArray header,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
@ -98,17 +106,16 @@ try {
Colors = 3;
}
CurlRequester cid(_printer->httpUrl());
cid.setFinishedCallback(_printer, &IppPrinter::printRequestFinished);
CurlRequester cr(_printer->httpUrl());
cr.setFinishedCallback(_printer, &IppPrinter::printRequestFinished);
cid.write(header.data(), header.length());
OK(cr.write(header.data(), header.length()));
write_fun WriteFun([&cid](unsigned char const* buf, unsigned int len) -> bool
write_fun WriteFun([&cr](unsigned char const* buf, unsigned int len) -> bool
{
if(len == 0)
return true;
cid.write((const char*)buf, len);
return true;
return cr.write((const char*)buf, len);
});
progress_fun ProgressFun([this](size_t page, size_t total) -> void
@ -287,11 +294,11 @@ try {
emit busyMessage(tr("Printing"));
CurlRequester cid(_printer->httpUrl());
cid.setFinishedCallback(_printer, &IppPrinter::printRequestFinished);
CurlRequester cr(_printer->httpUrl());
cr.setFinishedCallback(_printer, &IppPrinter::printRequestFinished);
cid.write(header.data(), header.length());
cid.write((char*)(outBts.raw()), outBts.size());
OK(cr.write(header.data(), header.length()));
OK(cr.write((char*)(outBts.raw()), outBts.size()));
}
qDebug() << "posted";

View file

@ -10,7 +10,13 @@ CurlRequester::CurlRequester(QUrl addr) : _addr(addr), _canWrite(1), _canRead(),
CurlRequester::~CurlRequester()
{
_canWrite.acquire();
while(!_canWrite.tryAcquire(1, 500))
{
if(!_performer.isRunning())
{
break;
}
}
_done = true;
_canRead.release();
_performer.wait();
@ -21,10 +27,16 @@ CurlRequester::~CurlRequester()
}
}
void CurlRequester::write(const char *data, size_t size)
bool CurlRequester::write(const char *data, size_t size)
{
qDebug() << "write " << size;
_canWrite.acquire();
while(!_canWrite.tryAcquire(1, 500))
{
if(!_performer.isRunning())
{
return false;
}
}
if(_dest != nullptr)
{
@ -35,6 +47,7 @@ void CurlRequester::write(const char *data, size_t size)
_size = size;
_offset = 0;
_canRead.release();
return true;
}
size_t CurlRequester::requestWrite(char* dest, size_t size)

View file

@ -23,7 +23,7 @@ public:
return true;
}
void write(const char *data, size_t size);
bool write(const char *data, size_t size);
size_t requestWrite(char* dest, size_t size);
public slots:

View file

@ -199,6 +199,10 @@
<source>Conversion failed</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Upload error</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>CoverPage</name>

View file

@ -199,6 +199,10 @@
<source>Conversion failed</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Upload error</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>CoverPage</name>

View file

@ -199,6 +199,10 @@
<source>Conversion failed</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Upload error</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>CoverPage</name>

View file

@ -199,6 +199,10 @@
<source>Conversion failed</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Upload error</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>CoverPage</name>

View file

@ -199,6 +199,10 @@
<source>Conversion failed</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Upload error</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>CoverPage</name>

View file

@ -199,6 +199,10 @@
<source>Conversion failed</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Upload error</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>CoverPage</name>

View file

@ -199,6 +199,10 @@
<source>Conversion failed</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Upload error</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>CoverPage</name>