Ref T730, added deleteResourceFromNetwork and modified CSlot (directly accept object witb std::function)

This commit is contained in:
Klaus Basan
2019-10-03 18:51:18 +02:00
committed by Mat Sutcliffe
parent 6e9fa0b2ad
commit ff6b7f8deb
3 changed files with 35 additions and 12 deletions

View File

@@ -669,6 +669,16 @@ namespace BlackCore
}); });
} }
QNetworkReply *CApplication::deleteResourceFromNetwork(const QNetworkRequest &request, int logId, const CApplication::CallbackSlot &callback, int maxRedirects)
{
const CApplication::ProgressSlot progress;
return this->httpRequestImpl(request, logId, callback, progress, maxRedirects, [](QNetworkAccessManager & qam, const QNetworkRequest & request)
{
QNetworkReply *nr = qam.deleteResource(request);
return nr;
});
}
QNetworkReply *CApplication::postToNetwork(const QNetworkRequest &request, int logId, const QByteArray &data, const CSlot<void(QNetworkReply *)> &callback) QNetworkReply *CApplication::postToNetwork(const QNetworkRequest &request, int logId, const QByteArray &data, const CSlot<void(QNetworkReply *)> &callback)
{ {
return this->httpRequestImpl(request, logId, callback, NoRedirects, [ data ](QNetworkAccessManager & qam, const QNetworkRequest & request) return this->httpRequestImpl(request, logId, callback, NoRedirects, [ data ](QNetworkAccessManager & qam, const QNetworkRequest & request)
@@ -739,13 +749,13 @@ namespace BlackCore
callback(msg); callback(msg);
}); });
}); });
callbackSlot.setObject(this); // object for thread
ProgressSlot progressSlot([ = ](int, qint64, qint64, const QUrl &) ProgressSlot progressSlot([ = ](int, qint64, qint64, const QUrl &)
{ {
// so far not implemented // so far not implemented
}); });
callbackSlot.setObject(this); // object for thread
QNetworkReply *reply = this->getFromNetwork(url, callbackSlot, progressSlot, maxRedirects); QNetworkReply *reply = this->getFromNetwork(url, callbackSlot, progressSlot, maxRedirects);
return reply; return reply;
} }
@@ -1165,7 +1175,7 @@ namespace BlackCore
if (config.state() == QNetworkConfiguration::Active) { activeCount++; m_noNwAccessPoint = false; } if (config.state() == QNetworkConfiguration::Active) { activeCount++; m_noNwAccessPoint = false; }
if (config.isValid()) { validCount++; } if (config.isValid()) { validCount++; }
} }
Q_UNUSED(validCount); Q_UNUSED(validCount)
const bool canStartIAP = (m_networkConfigManager->capabilities() & QNetworkConfigurationManager::CanStartAndStopInterfaces); const bool canStartIAP = (m_networkConfigManager->capabilities() & QNetworkConfigurationManager::CanStartAndStopInterfaces);
const bool disable = activeCount < 1; // only inactive const bool disable = activeCount < 1; // only inactive
@@ -1413,7 +1423,7 @@ namespace BlackCore
bool CApplication::cmdLineErrorMessage(const QString &errorMessage, bool retry) const bool CApplication::cmdLineErrorMessage(const QString &errorMessage, bool retry) const
{ {
Q_UNUSED(retry); // only works with UI version Q_UNUSED(retry) // only works with UI version
fputs(qPrintable(errorMessage), stderr); fputs(qPrintable(errorMessage), stderr);
fputs("\n\n", stderr); fputs("\n\n", stderr);
fputs(qPrintable(m_parser.helpText()), stderr); fputs(qPrintable(m_parser.helpText()), stderr);
@@ -1422,7 +1432,7 @@ namespace BlackCore
bool CApplication::cmdLineErrorMessage(const CStatusMessageList &msgs, bool retry) const bool CApplication::cmdLineErrorMessage(const CStatusMessageList &msgs, bool retry) const
{ {
Q_UNUSED(retry); // only works with UI version Q_UNUSED(retry) // only works with UI version
if (msgs.isEmpty()) { return false; } if (msgs.isEmpty()) { return false; }
if (!msgs.hasErrorMessages()) { return false; } if (!msgs.hasErrorMessages()) { return false; }
CApplication::cmdLineErrorMessage( CApplication::cmdLineErrorMessage(
@@ -1667,7 +1677,7 @@ namespace BlackCore
#endif #endif
} }
void CApplication::httpRequestImplInQAMThread(const QNetworkRequest &request, int logId, const CallbackSlot &callback, const ProgressSlot &progress, int maxRedirects, NetworkRequestOrPostFunction requestOrPostMethod) void CApplication::httpRequestImplInQAMThread(const QNetworkRequest &request, int logId, const CallbackSlot &callback, const ProgressSlot &progress, int maxRedirects, NetworkRequestOrPostFunction getPostOrDeleteRequest)
{ {
// run in QAM thread // run in QAM thread
if (this->isShuttingDown()) { return; } if (this->isShuttingDown()) { return; }
@@ -1676,7 +1686,7 @@ namespace BlackCore
// should be now in QAM thread // should be now in QAM thread
if (!sApp || sApp->isShuttingDown()) { return; } if (!sApp || sApp->isShuttingDown()) { return; }
Q_ASSERT_X(CThreadUtils::isCurrentThreadObjectThread(sApp->m_accessManager), Q_FUNC_INFO, "Wrong thread, must be QAM thread"); Q_ASSERT_X(CThreadUtils::isCurrentThreadObjectThread(sApp->m_accessManager), Q_FUNC_INFO, "Wrong thread, must be QAM thread");
this->httpRequestImpl(request, logId, callback, progress, maxRedirects, requestOrPostMethod); this->httpRequestImpl(request, logId, callback, progress, maxRedirects, getPostOrDeleteRequest);
}); });
} }
@@ -1704,7 +1714,7 @@ namespace BlackCore
QNetworkReply *CApplication::httpRequestImpl( QNetworkReply *CApplication::httpRequestImpl(
const QNetworkRequest &request, int logId, const QNetworkRequest &request, int logId,
const CallbackSlot &callback, const ProgressSlot &progress, int maxRedirects, NetworkRequestOrPostFunction requestOrPostMethod) const CallbackSlot &callback, const ProgressSlot &progress, int maxRedirects, NetworkRequestOrPostFunction getPostOrDeleteRequest)
{ {
if (this->isShuttingDown()) { return nullptr; } if (this->isShuttingDown()) { return nullptr; }
if (!this->isNetworkAccessible()) { return nullptr; } if (!this->isNetworkAccessible()) { return nullptr; }
@@ -1713,7 +1723,7 @@ namespace BlackCore
Q_ASSERT_X(CThreadUtils::isApplicationThreadObjectThread(m_accessManager), Q_FUNC_INFO, "Network manager supposed to be in main thread"); Q_ASSERT_X(CThreadUtils::isApplicationThreadObjectThread(m_accessManager), Q_FUNC_INFO, "Network manager supposed to be in main thread");
if (!CThreadUtils::isCurrentThreadObjectThread(m_accessManager)) if (!CThreadUtils::isCurrentThreadObjectThread(m_accessManager))
{ {
this->httpRequestImplInQAMThread(request, logId, callback, progress, maxRedirects, requestOrPostMethod); this->httpRequestImplInQAMThread(request, logId, callback, progress, maxRedirects, getPostOrDeleteRequest);
return nullptr; // not yet started, will be called again in QAM thread return nullptr; // not yet started, will be called again in QAM thread
} }
@@ -1723,7 +1733,7 @@ namespace BlackCore
// If URL is one of the shared URLs, add swift client SSL certificate to request // If URL is one of the shared URLs, add swift client SSL certificate to request
// CNetworkUtils::setSwiftClientSslCertificate(copiedRequest, this->getGlobalSetup().getSwiftSharedUrls()); // CNetworkUtils::setSwiftClientSslCertificate(copiedRequest, this->getGlobalSetup().getSwiftSharedUrls());
QNetworkReply *reply = requestOrPostMethod(*m_accessManager, copiedRequest); QNetworkReply *reply = getPostOrDeleteRequest(*m_accessManager, copiedRequest);
reply->setProperty("started", QVariant(QDateTime::currentMSecsSinceEpoch())); reply->setProperty("started", QVariant(QDateTime::currentMSecsSinceEpoch()));
reply->setProperty(CUrlLog::propertyNameId(), QVariant(logId)); reply->setProperty(CUrlLog::propertyNameId(), QVariant(logId));
const QUrl url(reply->url()); const QUrl url(reply->url());
@@ -1754,7 +1764,7 @@ namespace BlackCore
QNetworkRequest redirectRequest(redirectUrl); QNetworkRequest redirectRequest(redirectUrl);
const int redirectsLeft = maxRedirects - 1; const int redirectsLeft = maxRedirects - 1;
CLogMessage(sApp).info(u"Redirecting '%1' to '%2'") << urlStr << redirectUrl.toString(); CLogMessage(sApp).info(u"Redirecting '%1' to '%2'") << urlStr << redirectUrl.toString();
this->httpRequestImplInQAMThread(redirectRequest, logId, callback, progress, redirectsLeft, requestOrPostMethod); this->httpRequestImplInQAMThread(redirectRequest, logId, callback, progress, redirectsLeft, getPostOrDeleteRequest);
return; return;
} }
} }

View File

@@ -509,6 +509,12 @@ namespace BlackCore
QNetworkReply *getFromNetwork(const QNetworkRequest &request, int logId, QNetworkReply *getFromNetwork(const QNetworkRequest &request, int logId,
const CallbackSlot &callback, const ProgressSlot &progress, int maxRedirects = DefaultMaxRedirects); const CallbackSlot &callback, const ProgressSlot &progress, int maxRedirects = DefaultMaxRedirects);
//! Request to delete a network ressource from network, supporting BlackMisc::Network::CUrlLog
//! \threadsafe
QNetworkReply *deleteResourceFromNetwork(const QNetworkRequest &request, int logId,
const CallbackSlot &callback,
int maxRedirects = DefaultMaxRedirects);
//! Post to network //! Post to network
//! \threadsafe //! \threadsafe
QNetworkReply *postToNetwork(const QNetworkRequest &request, int logId, const QByteArray &data, const CallbackSlot &callback); QNetworkReply *postToNetwork(const QNetworkRequest &request, int logId, const QByteArray &data, const CallbackSlot &callback);
@@ -677,12 +683,12 @@ namespace BlackCore
//! \return QNetworkReply reply will only be returned, if the QNetworkAccessManager is in the same thread //! \return QNetworkReply reply will only be returned, if the QNetworkAccessManager is in the same thread
QNetworkReply *httpRequestImpl(const QNetworkRequest &request, QNetworkReply *httpRequestImpl(const QNetworkRequest &request,
int logId, const CallbackSlot &callback, const ProgressSlot &progress, int logId, const CallbackSlot &callback, const ProgressSlot &progress,
int maxRedirects, NetworkRequestOrPostFunction requestOrPostMethod); int maxRedirects, NetworkRequestOrPostFunction getPostOrDeleteRequest);
//! Call httpRequestImpl in correct thread //! Call httpRequestImpl in correct thread
void httpRequestImplInQAMThread(const QNetworkRequest &request, void httpRequestImplInQAMThread(const QNetworkRequest &request,
int logId, const CallbackSlot &callback, const ProgressSlot &progress, int logId, const CallbackSlot &callback, const ProgressSlot &progress,
int maxRedirects, NetworkRequestOrPostFunction requestOrPostMethod); int maxRedirects, NetworkRequestOrPostFunction getPostOrDeleteRequest);
//! Triggers a check of the network accessibility //! Triggers a check of the network accessibility
//! \remark this is a check that will double check that the watchdog will receive the correct QNetworkAccessManager::NetworkAccessibility //! \remark this is a check that will double check that the watchdog will receive the correct QNetworkAccessManager::NetworkAccessibility

View File

@@ -86,6 +86,13 @@ namespace BlackMisc
m_function(function) m_function(function)
{} {}
//! Construct a slot from the given object passing a function and a object
template <typename T>
CSlot(T *object, std::function<R(Args...)> function) :
m_object(object),
m_function(function)
{}
//! Call the slot. The behaviour is undefined if the slot is empty. //! Call the slot. The behaviour is undefined if the slot is empty.
R operator()(Args... args) const R operator()(Args... args) const
{ {