Skip to content

Commit

Permalink
be more rebust to server restart
Browse files Browse the repository at this point in the history
  • Loading branch information
notmart committed Jan 18, 2019
1 parent 15eeeca commit 89b3cab
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
12 changes: 6 additions & 6 deletions import/abstractskillview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ AbstractSkillView::AbstractSkillView(QQuickItem *parent)
}
});

connect(m_guiWebSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), this,
[this](QAbstractSocket::SocketError error) {
qWarning() << "Gui socket Connection Error:" << error;
m_reconnectTimer.start();
});


connect(m_controller, &MycroftController::socketStatusChanged, this,
[this]() {
if (m_controller->status() == MycroftController::Open) {
if (m_url.isValid()) {
m_guiWebSocket->close();
m_guiWebSocket->open(m_url);
}
} else if (status() != MycroftController::Open) {
if (m_controller->status() != MycroftController::Open) {
m_guiWebSocket->close();
//don't assume the url will be still valid
m_url = QUrl();
Expand Down
21 changes: 19 additions & 2 deletions import/mycroftcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ MycroftController::MycroftController(QObject *parent)
[this] (QAbstractSocket::SocketState state) {
emit socketStatusChanged();
if (state == QAbstractSocket::ConnectedState) {
qWarning() << "Main Socket connected, trying to connect gui";
for (const auto &guiId : m_views.keys()) {
sendRequest(QStringLiteral("mycroft.gui.connected"),
QVariantMap({{QStringLiteral("gui_id"), guiId}}));
}
m_reannounceGuiTimer.start();
}
});

Expand All @@ -74,6 +76,20 @@ MycroftController::MycroftController(QObject *parent)
m_mainWebSocket.open(QUrl(socket));
});

m_reannounceGuiTimer.setInterval(10000);
connect(&m_reannounceGuiTimer, &QTimer::timeout, this, [this]() {
if (m_mainWebSocket.state() != QAbstractSocket::ConnectedState) {
return;
}
for (const auto &guiId : m_views.keys()) {
if (m_views[guiId]->status() != Open) {
qWarning()<<"Retrying to announce gui";
sendRequest(QStringLiteral("mycroft.gui.connected"),
QVariantMap({{QStringLiteral("gui_id"), guiId}}));
}
}
});

#ifdef Q_OS_ANDROID
m_speech = new QTextToSpeech(this);
#endif
Expand Down Expand Up @@ -116,14 +132,14 @@ void MycroftController::onMainSocketMessageReceived(const QString &message)
auto doc = QJsonDocument::fromJson(message.toUtf8());

if (doc.isEmpty()) {
qWarning() << "Empty or invalid JSON message arrived on the gui socket:" << message;
qWarning() << "Empty or invalid JSON message arrived on the main socket:" << message;
return;
}

auto type = doc[QStringLiteral("type")].toString();

if (type.isEmpty()) {
qWarning() << "Empty type in the JSON message on the gui socket";
qWarning() << "Empty type in the JSON message on the main socket";
return;
}

Expand Down Expand Up @@ -195,6 +211,7 @@ void MycroftController::onMainSocketMessageReceived(const QString &message)
return;
}

qWarning() << "Received port" << port << "for gui" << guiId;
if (!m_views.contains(guiId)) {
qWarning() << "Unknown guiId from mycroft.gui.port";
return;
Expand Down
1 change: 1 addition & 0 deletions import/mycroftcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public Q_SLOTS:
QWebSocket m_mainWebSocket;

QTimer m_reconnectTimer;
QTimer m_reannounceGuiTimer;
GlobalSettings *m_appSettingObj;

//TODO: remove
Expand Down

0 comments on commit 89b3cab

Please sign in to comment.