mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-10 05:55:33 +08:00
refs #857, monitor accessible network in application
This commit is contained in:
committed by
Mathew Sutcliffe
parent
2bd9120c1c
commit
04e980a6ae
@@ -130,6 +130,7 @@ namespace BlackCore
|
|||||||
// Init network
|
// Init network
|
||||||
this->m_cookieManager.setParent(&this->m_accessManager);
|
this->m_cookieManager.setParent(&this->m_accessManager);
|
||||||
this->m_accessManager.setCookieJar(&this->m_cookieManager);
|
this->m_accessManager.setCookieJar(&this->m_cookieManager);
|
||||||
|
connect(&this->m_accessManager, &QNetworkAccessManager::networkAccessibleChanged, this, &CApplication::ps_networkAccessibleChanged);
|
||||||
|
|
||||||
// global setup
|
// global setup
|
||||||
sApp = this;
|
sApp = this;
|
||||||
@@ -154,7 +155,7 @@ namespace BlackCore
|
|||||||
CApplicationInfoList apps = getRunningApplications();
|
CApplicationInfoList apps = getRunningApplications();
|
||||||
apps.push_back(instance()->getApplicationInfo());
|
apps.push_back(instance()->getApplicationInfo());
|
||||||
bool ok = CFileUtils::writeStringToLockedFile(apps.toJsonString(), swiftDataRoot() + "apps.json");
|
bool ok = CFileUtils::writeStringToLockedFile(apps.toJsonString(), swiftDataRoot() + "apps.json");
|
||||||
if (!ok) { CLogMessage(static_cast<CApplication*>(nullptr)).error("Failed to write to application list file"); }
|
if (!ok) { CLogMessage(static_cast<CApplication *>(nullptr)).error("Failed to write to application list file"); }
|
||||||
|
|
||||||
return QCoreApplication::exec();
|
return QCoreApplication::exec();
|
||||||
}
|
}
|
||||||
@@ -176,13 +177,13 @@ namespace BlackCore
|
|||||||
{
|
{
|
||||||
CApplicationInfoList apps;
|
CApplicationInfoList apps;
|
||||||
apps.convertFromJsonNoThrow(CFileUtils::readLockedFileToString(swiftDataRoot() + "apps.json"), {}, {});
|
apps.convertFromJsonNoThrow(CFileUtils::readLockedFileToString(swiftDataRoot() + "apps.json"), {}, {});
|
||||||
apps.removeIf([](const CApplicationInfo &info) { return !info.processInfo().exists(); });
|
apps.removeIf([](const CApplicationInfo & info) { return !info.processInfo().exists(); });
|
||||||
return apps;
|
return apps;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CApplication::isAlreadyRunning() const
|
bool CApplication::isAlreadyRunning() const
|
||||||
{
|
{
|
||||||
return getRunningApplications().containsBy([this](const CApplicationInfo &info) { return info.application() == getSwiftApplication(); });
|
return getRunningApplications().containsBy([this](const CApplicationInfo & info) { return info.application() == getSwiftApplication(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString &CApplication::getApplicationNameAndVersion() const
|
const QString &CApplication::getApplicationNameAndVersion() const
|
||||||
@@ -490,6 +491,7 @@ namespace BlackCore
|
|||||||
|
|
||||||
QNetworkReply *CApplication::postToNetwork(const QNetworkRequest &request, QHttpMultiPart *multiPart, const CSlot<void(QNetworkReply *)> &callback)
|
QNetworkReply *CApplication::postToNetwork(const QNetworkRequest &request, QHttpMultiPart *multiPart, const CSlot<void(QNetworkReply *)> &callback)
|
||||||
{
|
{
|
||||||
|
if (!this->isNetworkConnectedAndAccessible()) { return nullptr; }
|
||||||
if (QThread::currentThread() != this->m_accessManager.thread())
|
if (QThread::currentThread() != this->m_accessManager.thread())
|
||||||
{
|
{
|
||||||
multiPart->moveToThread(this->m_accessManager.thread());
|
multiPart->moveToThread(this->m_accessManager.thread());
|
||||||
@@ -519,6 +521,22 @@ namespace BlackCore
|
|||||||
this->m_cookieManager.deleteAllCookies();
|
this->m_cookieManager.deleteAllCookies();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CApplication::isNetworkAccessible() const
|
||||||
|
{
|
||||||
|
return this->m_accessManager.networkAccessible() == QNetworkAccessManager::Accessible;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CApplication::isNetworkConnected() const
|
||||||
|
{
|
||||||
|
static bool con = CNetworkUtils::hasConnectedInterface();
|
||||||
|
return con;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CApplication::isNetworkConnectedAndAccessible() const
|
||||||
|
{
|
||||||
|
return this->isNetworkConnected() && this->isNetworkAccessible();
|
||||||
|
}
|
||||||
|
|
||||||
void CApplication::exit(int retcode)
|
void CApplication::exit(int retcode)
|
||||||
{
|
{
|
||||||
if (instance())
|
if (instance())
|
||||||
@@ -749,6 +767,22 @@ namespace BlackCore
|
|||||||
// void
|
// void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CApplication::ps_networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility accessible)
|
||||||
|
{
|
||||||
|
switch (accessible)
|
||||||
|
{
|
||||||
|
case QNetworkAccessManager::Accessible:
|
||||||
|
CLogMessage(this).info("Network is accessible");
|
||||||
|
break;
|
||||||
|
case QNetworkAccessManager::NotAccessible:
|
||||||
|
CLogMessage(this).error("Network not accessible");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
CLogMessage(this).warning("Network accessibility unknown");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
CStatusMessageList CApplication::asyncWebAndContextStart()
|
CStatusMessageList CApplication::asyncWebAndContextStart()
|
||||||
{
|
{
|
||||||
if (this->m_started) { return CStatusMessage(this).info("Already started "); }
|
if (this->m_started) { return CStatusMessage(this).info("Already started "); }
|
||||||
@@ -1091,6 +1125,7 @@ namespace BlackCore
|
|||||||
QNetworkReply *CApplication::httpRequestImpl(const QNetworkRequest &request, const BlackMisc::CSlot<void (QNetworkReply *)> &callback, std::function<QNetworkReply *(QNetworkAccessManager &, const QNetworkRequest &)> method)
|
QNetworkReply *CApplication::httpRequestImpl(const QNetworkRequest &request, const BlackMisc::CSlot<void (QNetworkReply *)> &callback, std::function<QNetworkReply *(QNetworkAccessManager &, const QNetworkRequest &)> method)
|
||||||
{
|
{
|
||||||
if (this->m_shutdown) { return nullptr; }
|
if (this->m_shutdown) { return nullptr; }
|
||||||
|
if (!this->isNetworkConnectedAndAccessible()) { return nullptr; }
|
||||||
QWriteLocker locker(&m_accessManagerLock);
|
QWriteLocker locker(&m_accessManagerLock);
|
||||||
Q_ASSERT_X(QCoreApplication::instance()->thread() == m_accessManager.thread(), Q_FUNC_INFO, "Network manager supposed to be in main thread");
|
Q_ASSERT_X(QCoreApplication::instance()->thread() == m_accessManager.thread(), Q_FUNC_INFO, "Network manager supposed to be in main thread");
|
||||||
if (QThread::currentThread() != this->m_accessManager.thread())
|
if (QThread::currentThread() != this->m_accessManager.thread())
|
||||||
|
|||||||
@@ -139,6 +139,15 @@ namespace BlackCore
|
|||||||
//! Delete all cookies from cookier manager
|
//! Delete all cookies from cookier manager
|
||||||
void deleteAllCookies();
|
void deleteAllCookies();
|
||||||
|
|
||||||
|
//! Network accessible?
|
||||||
|
bool isNetworkAccessible() const;
|
||||||
|
|
||||||
|
//! Network connected (at startup time)
|
||||||
|
bool isNetworkConnected() const;
|
||||||
|
|
||||||
|
//! Network connected and also accessible
|
||||||
|
bool isNetworkConnectedAndAccessible() const;
|
||||||
|
|
||||||
//! Setup already synchronized
|
//! Setup already synchronized
|
||||||
bool isSetupAvailable() const;
|
bool isSetupAvailable() const;
|
||||||
|
|
||||||
@@ -368,6 +377,9 @@ namespace BlackCore
|
|||||||
//! Startup completed
|
//! Startup completed
|
||||||
virtual void ps_startupCompleted();
|
virtual void ps_startupCompleted();
|
||||||
|
|
||||||
|
//! Problem with network access manager
|
||||||
|
virtual void ps_networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility accessible);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
//! Init class, allows to init from BlackGui::CGuiApplication as well (pseudo virtual)
|
//! Init class, allows to init from BlackGui::CGuiApplication as well (pseudo virtual)
|
||||||
void init(bool withMetadata);
|
void init(bool withMetadata);
|
||||||
|
|||||||
Reference in New Issue
Block a user