Skip to content
This repository has been archived by the owner on May 16, 2020. It is now read-only.

Commit

Permalink
Networking fix for selecting correct interface for accessmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
Antti Lamminsalo committed Mar 18, 2016
1 parent 5cd64ae commit fa977a4
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 6 deletions.
102 changes: 98 additions & 4 deletions src/network/networkmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
#include "../util/fileutils.h"
#include "../util/jsonparser.h"
#include "../util/m3u8parser.h"
#include <QNetworkConfiguration>
#include <QEventLoop>

NetworkManager::NetworkManager()
{
operation = new QNetworkAccessManager();

//Set configuration
conf = new QNetworkConfigurationManager();
operation->setConfiguration(conf->defaultConfiguration());
//Select interface
selectNetworkInterface();

//SSL errors handle (down the drain)
connect(operation, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), this, SLOT(handleSslErrors(QNetworkReply*,QList<QSslError>)));
Expand All @@ -22,7 +23,100 @@ NetworkManager::~NetworkManager()
{
qDebug() << "Destroyer: NetworkManager";
operation->deleteLater();
conf->deleteLater();
}

void NetworkManager::selectNetworkInterface()
{
//Chooses a working network interface from interfaces list, if default configuration doesn't work

QNetworkConfigurationManager conf;
connectionOK = false;
QString identifier;

QEventLoop loop;
connect(this, SIGNAL(finishedConnectionTest()), &loop, SLOT(quit()));

//Test default configuration
operation->setConfiguration(conf.defaultConfiguration());

testConnection();
loop.exec();

if (connectionOK == true) {
qDebug() << "Selected default network configuration";
return;
}

else {
qDebug() << "Failure on default configuration, attempt to choose another interaface..";

foreach (QNetworkInterface interface, QNetworkInterface::allInterfaces())
{
if (!interface.isValid())
continue;

// qDebug() << "Identifier: " << interface.name();
// qDebug() << "HW addr: " << interface.hardwareAddress();

bool isUp = interface.flags().testFlag(QNetworkInterface::IsUp);
bool isLoopback = interface.flags().testFlag(QNetworkInterface::IsLoopBack);
bool isActive = interface.flags().testFlag(QNetworkInterface::IsRunning);
bool isPtP = interface.flags().testFlag(QNetworkInterface::IsPointToPoint);

// qDebug() << "Properties: ";
// qDebug() << (isUp ? "Is up" : "Is down");

// if (isLoopback)
// qDebug() << "Loopback";

// qDebug() << (isActive ? "Active" : "Inactive");

// if (isPtP)
// qDebug() << "Is Point-to-Point";

// qDebug() << "";

if (isUp && isActive && !isLoopback) {
identifier = interface.name();
qDebug() << "Testing connection for interface " << identifier;
operation->setConfiguration(conf.configurationFromIdentifier(identifier));

testConnection();
loop.exec();

if (connectionOK == true) {
qDebug() << "Success!";
return;
}

else
qDebug() << "Failure, trying another interface...";
}

}
}
}

void NetworkManager::testConnection()
{
QNetworkRequest request;
request.setUrl(QUrl(TWITCH_API_BASE));

QNetworkReply *reply = operation->get(request);

connect(reply, SIGNAL(finished()), this, SLOT(testConnectionReply()));
}

void NetworkManager::testConnectionReply()
{
QNetworkReply* reply = qobject_cast<QNetworkReply *>(sender());

// if (reply->error() == QNetworkReply::NoError)
// qDebug() << "Got response: " << reply->readAll();

connectionOK = (reply->error() == QNetworkReply::NoError);

emit finishedConnectionTest();
}

void NetworkManager::getStreams(const QString &url)
Expand Down
10 changes: 9 additions & 1 deletion src/network/networkmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QObject>
#include <QtNetwork/QNetworkAccessManager>
#include <QNetworkConfigurationManager>
#include <QNetworkInterface>
#include <QUrl>
#include <QtNetwork/QNetworkReply>
#include <QJsonDocument>
Expand All @@ -25,6 +26,9 @@ class NetworkManager: public QObject

protected:

void selectNetworkInterface();
void testConnection();

enum M3U8TYPE {
LIVE = QNetworkRequest::CustomVerbAttribute + 1,
VOD
Expand All @@ -49,6 +53,8 @@ class NetworkManager: public QObject
QNetworkAccessManager *getManager() const;

signals:
void finishedConnectionTest();

void allStreamsOperationFinished(const QList<Channel *>&);
void gamesOperationFinished(const QList<Game *>&);
void gameStreamsOperationFinished(const QList<Channel *>&);
Expand All @@ -63,6 +69,7 @@ class NetworkManager: public QObject
void fileOperationFinished(const QByteArray&);

private slots:
void testConnectionReply();
void handleSslErrors(QNetworkReply * reply, QList<QSslError> errors);

void allStreamsReply();
Expand All @@ -78,7 +85,8 @@ private slots:

private:
QNetworkAccessManager *operation;
QNetworkConfigurationManager *conf;

bool connectionOK;
};

#endif // NETWORKMANAGER_H
1 change: 1 addition & 0 deletions src/network/urls.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

#define KRAKEN_API "https://api.twitch.tv/kraken"
#define TWITCH_API "https://api.twitch.tv/api"
#define TWITCH_API_BASE "https://api.twitch.tv/kraken/base"

#endif // URLS_H
2 changes: 1 addition & 1 deletion src/qml/components/Channel.qml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Rectangle {
}

onProgressChanged: {
if (progress >= 1.0)
if (progress > 0.99)
_spinner.visible = false
}

Expand Down

0 comments on commit fa977a4

Please sign in to comment.