Fix reply parent relationship for a clean shutdown

This commit is contained in:
Simon Stürz 2018-01-24 16:17:32 +01:00 committed by Michael Zanetti
parent 6a13cedace
commit 2f7cd2f028
8 changed files with 124 additions and 28 deletions

View File

@ -1,6 +1,6 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * *
* Copyright (C) 2017 Simon Stürz <simon.stuerz@guh.io * * Copyright (C) 2017-2018 Simon Stürz <simon.stuerz@guh.io *
* * * *
* This file is part of guh. * * This file is part of guh. *
* * * *

View File

@ -1,6 +1,6 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * *
* Copyright (C) 2017 Simon Stürz <simon.stuerz@guh.io * * Copyright (C) 2017-2018 Simon Stürz <simon.stuerz@guh.io *
* * * *
* This file is part of guh. * * This file is part of guh. *
* * * *

View File

@ -1,6 +1,30 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2017-2018 Simon Stürz <simon.stuerz@guh.io *
* *
* This file is part of guh. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; If not, see *
* <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "snapdconnection.h" #include "snapdconnection.h"
#include "extern-plugininfo.h" #include "extern-plugininfo.h"
#include <QPointer>
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonParseError> #include <QJsonParseError>
@ -15,9 +39,9 @@ SnapdConnection::SnapdConnection(QObject *parent) :
connect(this, SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(onError(QLocalSocket::LocalSocketError))); connect(this, SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(onError(QLocalSocket::LocalSocketError)));
} }
SnapdReply *SnapdConnection::get(const QString &path) SnapdReply *SnapdConnection::get(const QString &path, QObject *parent)
{ {
SnapdReply *reply = new SnapdReply(this); SnapdReply *reply = new SnapdReply(parent);
reply->setRequestPath(path); reply->setRequestPath(path);
reply->setRequestMethod("GET"); reply->setRequestMethod("GET");
reply->setRequestRawMessage(createRequestHeader("GET", path)); reply->setRequestRawMessage(createRequestHeader("GET", path));
@ -30,9 +54,9 @@ SnapdReply *SnapdConnection::get(const QString &path)
return reply; return reply;
} }
SnapdReply *SnapdConnection::post(const QString &path, const QByteArray &payload) SnapdReply *SnapdConnection::post(const QString &path, const QByteArray &payload, QObject *parent)
{ {
SnapdReply *reply = new SnapdReply(this); SnapdReply *reply = new SnapdReply(parent);
reply->setRequestPath(path); reply->setRequestPath(path);
reply->setRequestMethod("POST"); reply->setRequestMethod("POST");
QByteArray header = createRequestHeader("POST", path, payload); QByteArray header = createRequestHeader("POST", path, payload);
@ -57,22 +81,28 @@ void SnapdConnection::setConnected(const bool &connected)
return; return;
m_connected = connected; m_connected = connected;
emit connectedChanged(m_connected);
// Clean up replies of disconnected // Clean up replies of disconnected
if (!m_connected) { if (!m_connected) {
foreach (SnapdReply *reply, m_replyQueue) {
reply->setFinished(false);
}
if (m_currentReply) { if (m_currentReply) {
m_currentReply->setFinished(false); m_currentReply->setFinished(false);
m_currentReply = nullptr; m_currentReply = nullptr;
} }
m_replyQueue.clear(); while (!m_replyQueue.isEmpty()) {
} QPointer<SnapdReply> reply = m_replyQueue.dequeue();
if (!reply.isNull()) {
reply->setFinished(false);
}
}
emit connectedChanged(m_connected); } else {
// Start with a clean parsing
m_payload.clear();
m_header.clear();
m_chuncked = false;
}
} }
QByteArray SnapdConnection::createRequestHeader(const QString &method, const QString &path, const QByteArray &payload) QByteArray SnapdConnection::createRequestHeader(const QString &method, const QString &path, const QByteArray &payload)

View File

@ -1,3 +1,25 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2017-2018 Simon Stürz <simon.stuerz@guh.io *
* *
* This file is part of guh. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; If not, see *
* <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef SNAPDCONNECTION_H #ifndef SNAPDCONNECTION_H
#define SNAPDCONNECTION_H #define SNAPDCONNECTION_H
@ -13,8 +35,8 @@ class SnapdConnection : public QLocalSocket
public: public:
explicit SnapdConnection(QObject *parent = nullptr); explicit SnapdConnection(QObject *parent = nullptr);
SnapdReply *get(const QString &path); SnapdReply *get(const QString &path, QObject *parent);
SnapdReply *post(const QString &path, const QByteArray &payload); SnapdReply *post(const QString &path, const QByteArray &payload, QObject *parent);
bool isConnected() const; bool isConnected() const;
@ -39,6 +61,9 @@ private:
void processData(); void processData();
void sendNextRequest(); void sendNextRequest();
signals:
void connectedChanged(const bool &connected);
private slots: private slots:
void onConnected(); void onConnected();
void onDisconnected(); void onDisconnected();
@ -46,9 +71,6 @@ private slots:
void onStateChanged(const QLocalSocket::LocalSocketState &state); void onStateChanged(const QLocalSocket::LocalSocketState &state);
void onReadyRead(); void onReadyRead();
signals:
void connectedChanged(const bool &connected);
}; };
#endif // SNAPDCONNECTION_H #endif // SNAPDCONNECTION_H

View File

@ -1,6 +1,6 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * *
* Copyright (C) 2017 Simon Stürz <simon.stuerz@guh.io * * Copyright (C) 2017-2018 Simon Stürz <simon.stuerz@guh.io *
* * * *
* This file is part of guh. * * This file is part of guh. *
* * * *
@ -81,7 +81,7 @@ void SnapdControl::loadSystemInfo()
if (!m_snapConnection->isConnected()) if (!m_snapConnection->isConnected())
return; return;
SnapdReply *reply = m_snapConnection->get("/v2/system-info"); SnapdReply *reply = m_snapConnection->get("/v2/system-info", this);
connect(reply, &SnapdReply::finished, this, &SnapdControl::onLoadSystemInfoFinished); connect(reply, &SnapdReply::finished, this, &SnapdControl::onLoadSystemInfoFinished);
} }
@ -93,7 +93,7 @@ void SnapdControl::loadSnapList()
if (!m_snapConnection->isConnected()) if (!m_snapConnection->isConnected())
return; return;
SnapdReply *reply = m_snapConnection->get("/v2/snaps"); SnapdReply *reply = m_snapConnection->get("/v2/snaps", this);
connect(reply, &SnapdReply::finished, this, &SnapdControl::onLoadSnapListFinished); connect(reply, &SnapdReply::finished, this, &SnapdControl::onLoadSnapListFinished);
} }
@ -105,7 +105,7 @@ void SnapdControl::loadRunningChanges()
if (!m_snapConnection->isConnected()) if (!m_snapConnection->isConnected())
return; return;
SnapdReply *reply = m_snapConnection->get("/v2/changes"); SnapdReply *reply = m_snapConnection->get("/v2/changes", this);
connect(reply, &SnapdReply::finished, this, &SnapdControl::onLoadRunningChangesFinished); connect(reply, &SnapdReply::finished, this, &SnapdControl::onLoadRunningChangesFinished);
} }
@ -117,7 +117,7 @@ void SnapdControl::loadChange(const int &change)
if (!m_snapConnection->isConnected()) if (!m_snapConnection->isConnected())
return; return;
SnapdReply *reply = m_snapConnection->get(QString("/v2/changes/%1").arg(QString::number(change))); SnapdReply *reply = m_snapConnection->get(QString("/v2/changes/%1").arg(QString::number(change)), this);
connect(reply, &SnapdReply::finished, this, &SnapdControl::onLoadChangeFinished); connect(reply, &SnapdReply::finished, this, &SnapdControl::onLoadChangeFinished);
} }
@ -381,7 +381,7 @@ void SnapdControl::snapRefresh()
request.insert("action", "refresh"); request.insert("action", "refresh");
qCDebug(dcSnapd()) << "Refresh all snaps"; qCDebug(dcSnapd()) << "Refresh all snaps";
SnapdReply *reply = m_snapConnection->post("/v2/snaps", QJsonDocument::fromVariant(request).toJson(QJsonDocument::Compact)); SnapdReply *reply = m_snapConnection->post("/v2/snaps", QJsonDocument::fromVariant(request).toJson(QJsonDocument::Compact), this);
connect(reply, &SnapdReply::finished, this, &SnapdControl::onSnapRefreshFinished); connect(reply, &SnapdReply::finished, this, &SnapdControl::onSnapRefreshFinished);
} }
@ -398,7 +398,7 @@ void SnapdControl::changeSnapChannel(const QString &snapName, const QString &cha
request.insert("channel", channel); request.insert("channel", channel);
qCDebug(dcSnapd()) << "Refresh snap" << snapName << "to channel" << channel; qCDebug(dcSnapd()) << "Refresh snap" << snapName << "to channel" << channel;
SnapdReply *reply = m_snapConnection->post(QString("/v2/snaps/%1").arg(snapName), QJsonDocument::fromVariant(request).toJson(QJsonDocument::Compact)); SnapdReply *reply = m_snapConnection->post(QString("/v2/snaps/%1").arg(snapName), QJsonDocument::fromVariant(request).toJson(QJsonDocument::Compact), this);
connect(reply, &SnapdReply::finished, this, &SnapdControl::onChangeSnapChannelFinished); connect(reply, &SnapdReply::finished, this, &SnapdControl::onChangeSnapChannelFinished);
} }
@ -410,7 +410,7 @@ void SnapdControl::checkForUpdates()
if (!m_snapConnection->isConnected()) if (!m_snapConnection->isConnected())
return; return;
SnapdReply *reply = m_snapConnection->get("/v2/find?select=refresh"); SnapdReply *reply = m_snapConnection->get("/v2/find?select=refresh", this);
connect(reply, &SnapdReply::finished, this, &SnapdControl::onCheckForUpdatesFinished); connect(reply, &SnapdReply::finished, this, &SnapdControl::onCheckForUpdatesFinished);
} }
@ -426,6 +426,6 @@ void SnapdControl::snapRevert(const QString &snapName)
request.insert("action", "revert"); request.insert("action", "revert");
qCDebug(dcSnapd()) << "Revert snap" << snapName; qCDebug(dcSnapd()) << "Revert snap" << snapName;
SnapdReply *reply = m_snapConnection->post(QString("/v2/snaps/%1").arg(snapName), QJsonDocument::fromVariant(request).toJson(QJsonDocument::Compact)); SnapdReply *reply = m_snapConnection->post(QString("/v2/snaps/%1").arg(snapName), QJsonDocument::fromVariant(request).toJson(QJsonDocument::Compact), this);
connect(reply, &SnapdReply::finished, this, &SnapdControl::onSnapRevertFinished); connect(reply, &SnapdReply::finished, this, &SnapdControl::onSnapRevertFinished);
} }

View File

@ -1,6 +1,6 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * *
* Copyright (C) 2017 Simon Stürz <simon.stuerz@guh.io * * Copyright (C) 2017-2018 Simon Stürz <simon.stuerz@guh.io *
* * * *
* This file is part of guh. * * This file is part of guh. *
* * * *

View File

@ -1,3 +1,25 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2017-2018 Simon Stürz <simon.stuerz@guh.io *
* *
* This file is part of guh. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; If not, see *
* <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "snapdreply.h" #include "snapdreply.h"
QString SnapdReply::requestPath() const QString SnapdReply::requestPath() const

View File

@ -1,3 +1,25 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2017-2018 Simon Stürz <simon.stuerz@guh.io *
* *
* This file is part of guh. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; If not, see *
* <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef SNAPDREPLY_H #ifndef SNAPDREPLY_H
#define SNAPDREPLY_H #define SNAPDREPLY_H