[libcalamares] Expand the number of URLs to check for connectivity

- introduce a list of URLs instead of just one
- ping each of them, in turn, until one responds
This commit is contained in:
Adriaan de Groot 2021-08-01 22:07:51 +02:00
parent 12a361556b
commit b8583a1e59

View File

@ -27,7 +27,7 @@ namespace Network
void
RequestOptions::applyToRequest( QNetworkRequest* request ) const
{
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
#if QT_VERSION < QT_VERSION_CHECK( 5, 15, 0 )
constexpr const auto RedirectPolicyAttribute = QNetworkRequest::FollowRedirectsAttribute;
#else
constexpr const auto RedirectPolicyAttribute = QNetworkRequest::RedirectPolicyAttribute;
@ -60,8 +60,9 @@ public slots:
void cleanupNam();
public:
QUrl m_hasInternetUrl;
bool m_hasInternet;
QVector< QUrl > m_hasInternetUrls;
bool m_hasInternet = false;
int m_lastCheckedUrlIndex = -1;
Private();
@ -155,8 +156,33 @@ Manager::hasInternet()
bool
Manager::checkHasInternet()
{
if ( d->m_hasInternetUrls.empty() )
{
return false;
}
if ( d->m_lastCheckedUrlIndex < 0 )
{
d->m_lastCheckedUrlIndex = 0;
}
int attempts = 0;
do
{
// Start by pinging the same one as last time
d->m_hasInternet = synchronousPing( d->m_hasInternetUrls.at( d->m_lastCheckedUrlIndex ) );
// if it's not responding, **then** move on to the next one,
// and wrap around if needed
if ( !d->m_hasInternet )
{
if ( ++( d->m_lastCheckedUrlIndex ) >= d->m_hasInternetUrls.size() )
{
d->m_lastCheckedUrlIndex = 0;
}
}
// keep track of how often we've tried, because there's no point in
// going around more than once.
attempts++;
} while ( !d->m_hasInternet && ( attempts < d->m_hasInternetUrls.size() ) );
d->m_hasInternet = synchronousPing( d->m_hasInternetUrl );
// For earlier Qt versions (< 5.15.0), set the accessibility flag to
// NotAccessible if synchronous ping has failed, so that any module
@ -177,7 +203,11 @@ Manager::checkHasInternet()
void
Manager::setCheckHasInternetUrl( const QUrl& url )
{
d->m_hasInternetUrl = url;
if ( d->m_hasInternetUrls.empty() )
{
d->m_lastCheckedUrlIndex = -1;
}
d->m_hasInternetUrls.append( url );
}
/** @brief Does a request asynchronously, returns the (pending) reply