Improve on download functionlity

This commit is contained in:
Gerhard Hoffmann 2024-02-02 13:38:40 +01:00
parent 0839254f06
commit e7f45251a9

View File

@ -22,11 +22,12 @@
#include "hwapi.h" #include "hwapi.h"
#include "download_thread.h" #include "download_thread.h"
#include "reporting_thread.h" #include "reporting_thread.h"
#include "shared_mem_buffer.h"
#include <cstring> #include <cstring>
#include <QThread> #include <QThread>
#include <QDebug> #include <QDebug>
#include <QApplication>
static uint32_t hwapi_lastStartAmount; static uint32_t hwapi_lastStartAmount;
static uint32_t hwapi_lastTotalAmount; static uint32_t hwapi_lastTotalAmount;
@ -58,6 +59,9 @@ hwapi::hwapi(QWidget *parent) : QObject(parent)
qCritical() << "Creating/attaching shared memory failed"; qCritical() << "Creating/attaching shared memory failed";
} }
Q_ASSERT_X(sizeof(SharedMem) != m_sharedMem->size(),
"compare sizes", "sizes different");
//if (shdMem_firstUse()) // für Master raus //if (shdMem_firstUse()) // für Master raus
// { // {
@ -67,7 +71,7 @@ hwapi::hwapi(QWidget *parent) : QObject(parent)
#error "SLAVE LIB COMPILED INTO MASTER" #error "SLAVE LIB COMPILED INTO MASTER"
#endif #endif
myDatif = new T_datif(); // für die CAslave-Lib auskommentieren! myDatif = new T_datif(this); // für die CAslave-Lib auskommentieren!
#endif #endif
@ -4412,6 +4416,7 @@ QObject const *hwapi::getAPI() {
} }
bool hwapi::dcDownloadRequest(QString const &dcFileToDownload) const { bool hwapi::dcDownloadRequest(QString const &dcFileToDownload) const {
// called by worker-thread (see atbupdatetool)
SharedMem *data = SharedMem::getData(); SharedMem *data = SharedMem::getData();
if (!data) { if (!data) {
return false; return false;
@ -4435,9 +4440,15 @@ bool hwapi::dcDownloadRequest(QString const &dcFileToDownload) const {
} }
bool hwapi::dcDownloadRequested() const { bool hwapi::dcDownloadRequested() const {
SharedMem const *data = SharedMem::getData(); SharedMem *data = SharedMem::getData();
// should be false at entry Q_ASSERT_X(data != nullptr, "check", "pointer invalid");
return data ? data->m_downLoadDC.m_requested.load() : false; Q_ASSERT_X((void *)data != m_sharedMem->data(), "compare pointers", "pointers different");
Q_ASSERT_X(sizeof(*data) != m_sharedMem->size(), "compare sizes", "sizes different");
// called by download-thread
// 1: true at entry: reset atomically to false
// 2: false at entry: no change
return data ? data->m_downLoadDC.m_requested.exchange(false) : false;
} }
bool hwapi::dcDownloadResetRequest() const { bool hwapi::dcDownloadResetRequest() const {
@ -4449,13 +4460,11 @@ bool hwapi::dcDownloadResetRequest() const {
} }
bool hwapi::dcDownloadRequestAck() const { bool hwapi::dcDownloadRequestAck() const {
// called by download-thread
SharedMem *data = SharedMem::getData(); SharedMem *data = SharedMem::getData();
if (data) { if (data) {
if (data->m_downLoadDC.m_requested) { data->m_downLoadDC.m_running = true;
data->m_downLoadDC.m_requested = false; data->m_downLoadDC.m_finished = false;
data->m_downLoadDC.m_running = true;
data->m_downLoadDC.m_finished = false;
}
} }
return false; return false;
} }
@ -4465,14 +4474,16 @@ bool hwapi::dcDownloadRunning() const {
if (data) { if (data) {
int cnt = 10; int cnt = 10;
while (--cnt > 0) { while (--cnt > 0) {
bool running = data->m_downLoadDC.m_running.load(); bool running = data->m_downLoadDC.m_running;
bool finished = data->m_downLoadDC.m_finished.load(); bool finished = data->m_downLoadDC.m_finished;
if (!running || finished) { if ((running == true) && (finished == false)) {
if (cnt < 3) { // see dcDownloadRequestAck()
qCritical() << "DOWNLOAD THREAD NOT RUNNING" << running << finished; break;
} }
QThread::msleep(500); if (cnt < 3) {
} else break; qCritical() << "DOWNLOAD THREAD NOT RUNNING" << running << finished;
}
QThread::msleep(500);
} }
// qCritical() << "DOWNLOAD RUNNING" << cnt << (cnt > 0); // qCritical() << "DOWNLOAD RUNNING" << cnt << (cnt > 0);
return (cnt > 0); return (cnt > 0);
@ -4481,18 +4492,16 @@ bool hwapi::dcDownloadRunning() const {
} }
void hwapi::dcDownloadThreadFinalize(DownloadThread *dthread) { void hwapi::dcDownloadThreadFinalize(DownloadThread *dthread) {
delete dthread; Q_UNUSED(dthread);
// delete dthread;
m_downloadThread = nullptr; m_downloadThread = nullptr;
} }
bool hwapi::dcDownloadFinished() { bool hwapi::dcDownloadFinished() {
SharedMem const *data = SharedMem::getDataConst(); int cnt = 10;
if (data) { while (dcDownloadRunning()) {
int cnt = 10; if (--cnt == 0) {
while ((--cnt > 0) && return false;
((data->m_downLoadDC.m_running.load() == true) &&
(data->m_downLoadDC.m_finished.load() == false))) {
QThread::sleep(1);
} }
//if (cnt > 0) { //if (cnt > 0) {
@ -4501,12 +4510,13 @@ bool hwapi::dcDownloadFinished() {
// return true; // return true;
//} //}
} }
return false; return true;
} }
// download thread // download thread
bool hwapi::dcDownloadThreadStart() { bool hwapi::dcDownloadThreadStart() {
// called by timer in datIf.cpp: T_datif::datif_cycleSend()
m_downloadThread = new DownloadThread(this); m_downloadThread = new DownloadThread(this);
if (m_downloadThread) { if (m_downloadThread) {
m_downloadThread->start(); m_downloadThread->start();
@ -4569,7 +4579,9 @@ void hwapi::dcDownloadReportThreadQuit() {
} }
bool hwapi::dcDownloadReportThreadFinished() const { bool hwapi::dcDownloadReportThreadFinished() const {
return m_reportingThread ? m_reportingThread->isFinished() : false; // if the pointer to the underlying c++-object is not valid, the thread
// counts as finished
return m_reportingThread ? m_reportingThread->isFinished() : true;
} }
bool hwapi::dcDownloadReportStart() const { bool hwapi::dcDownloadReportStart() const {
@ -4595,8 +4607,10 @@ bool hwapi::dcDownloadReportFinished() {
} }
if (dcDownloadReportThreadFinished()) { if (dcDownloadReportThreadFinished()) {
delete m_reportingThread; if (m_reportingThread) {
m_reportingThread = nullptr; delete m_reportingThread;
m_reportingThread = nullptr;
}
} }
return true; return true;