Skip to content

Commit

Permalink
attempt to implement urlFetch with a timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
pajlada committed Jun 26, 2017
1 parent 1d8795e commit c2e67e4
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "lib/humanize"]
path = lib/humanize
url = https://github.com/pajlada/humanize.git
[submodule "lib/signals"]
path = lib/signals
url = https://github.com/pajlada/signals.git
8 changes: 6 additions & 2 deletions chatterino.pro
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ SOURCES += \
src/accountmanager.cpp \
src/twitch/twitchuser.cpp \
src/ircaccount.cpp \
src/widgets/accountpopup.cpp
src/widgets/accountpopup.cpp \
src/messagefactory.cpp

HEADERS += \
src/asyncexec.hpp \
Expand Down Expand Up @@ -150,7 +151,9 @@ HEADERS += \
src/twitch/twitchuser.hpp \
src/ircaccount.hpp \
src/widgets/accountpopup.hpp \
src/util/distancebetweenpoints.hpp
src/util/distancebetweenpoints.hpp \
src/messagefactory.hpp \
src/widgets/basewidget.hpp

PRECOMPILED_HEADER =

Expand Down Expand Up @@ -197,4 +200,5 @@ werr {
# External dependencies
include(dependencies/rapidjson.pri)
include(dependencies/settings.pri)
include(dependencies/signals.pri)
include(dependencies/humanize.pri)
3 changes: 0 additions & 3 deletions dependencies/settings.pri
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,3 @@ SOURCES += \
$$PWD/../lib/settings/src/settings/settingmanager.cpp

INCLUDEPATH += $$PWD/../lib/settings/include/

# signals
INCLUDEPATH += $$PWD/../lib/settings/external/signals/include
2 changes: 2 additions & 0 deletions dependencies/signals.pri
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# signals
INCLUDEPATH += $$PWD/../lib/signals/include/
1 change: 1 addition & 0 deletions lib/signals
Submodule signals added at 2c9c92
6 changes: 6 additions & 0 deletions src/messagefactory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "messagefactory.hpp"

MessageFactory::MessageFactory()
{

}
11 changes: 11 additions & 0 deletions src/messagefactory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef MESSAGEFACTORY_HPP
#define MESSAGEFACTORY_HPP


class MessageFactory
{
public:
MessageFactory();
};

#endif // MESSAGEFACTORY_HPP
107 changes: 97 additions & 10 deletions src/util/urlfetch.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <QEventLoop>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
Expand All @@ -8,15 +9,22 @@
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QString>
#include <QTimer>

#include <functional>

namespace chatterino {
namespace util {

static void urlFetch(const QString &url, std::function<void(QNetworkReply &)> successCallback)
static void urlFetch(const QString &url, std::function<void(QNetworkReply &)> successCallback,
QNetworkAccessManager *manager = nullptr)
{
QNetworkAccessManager *manager = new QNetworkAccessManager();
bool customManager = true;

if (manager == nullptr) {
manager = new QNetworkAccessManager();
customManager = false;
}

QUrl requestUrl(url);
QNetworkRequest request(requestUrl);
Expand All @@ -39,24 +47,103 @@ static void urlFetch(const QString &url, std::function<void(QNetworkReply &)> su
}

reply->deleteLater();
manager->deleteLater();
if (!customManager) {
manager->deleteLater();
}
});
}

static void urlJsonFetch(const QString &url, std::function<void(QJsonObject &)> successCallback)
static void urlFetchJSON(const QString &url, std::function<void(QJsonObject &)> successCallback,
QNetworkAccessManager *manager = nullptr)
{
urlFetch(url, [=](QNetworkReply &reply) {
QByteArray data = reply.readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
urlFetch(url,
[=](QNetworkReply &reply) {
QByteArray data = reply.readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));

if (jsonDoc.isNull()) {
return;
}

QJsonObject rootNode = jsonDoc.object();

successCallback(rootNode);
},
manager);
}

static void urlFetchTimeout(const QString &url,
std::function<void(QNetworkReply &)> successCallback, int timeoutMs,
QNetworkAccessManager *manager = nullptr)
{
bool customManager = true;

if (manager == nullptr) {
manager = new QNetworkAccessManager();
customManager = false;
}

if (jsonDoc.isNull()) {
QUrl requestUrl(url);
QNetworkRequest request(requestUrl);

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

QTimer timer;
timer.setSingleShot(true);

QEventLoop loop;
QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
QObject::connect(reply, &QNetworkReply::finished, [=] {
/* uncomment to follow redirects
QVariant replyStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
if (replyStatus >= 300 && replyStatus <= 304) {
QString newUrl =
reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toString();
urlFetch(newUrl, successCallback);
return;
}
*/

QJsonObject rootNode = jsonDoc.object();
if (reply->error() == QNetworkReply::NetworkError::NoError) {
successCallback(*reply);
}

successCallback(rootNode);
reply->deleteLater();
if (!customManager) {
manager->deleteLater();
}
});
timer.start(timeoutMs);
loop.exec();

if (!timer.isActive()) {
qDebug() << "TIMED OUT";
QObject::disconnect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
reply->abort();
} else {
// qDebug() << "XDDD HEHEHE";
}
}

static void urlFetchJSONTimeout(const QString &url,
std::function<void(QJsonObject &)> successCallback, int timeoutMs,
QNetworkAccessManager *manager = nullptr)
{
urlFetchTimeout(url,
[=](QNetworkReply &reply) {
QByteArray data = reply.readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));

if (jsonDoc.isNull()) {
return;
}

QJsonObject rootNode = jsonDoc.object();

successCallback(rootNode);
},
timeoutMs, manager);
}

} // namespace util
Expand Down
4 changes: 4 additions & 0 deletions src/widgets/basewidget.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef BASEWIDGET_HPP
#define BASEWIDGET_HPP

#endif // BASEWIDGET_HPP

0 comments on commit c2e67e4

Please sign in to comment.