summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRainer Keller <Rainer.Keller@qt.io>2018-02-07 09:09:16 +0100
committerRainer Keller <Rainer.Keller@qt.io>2018-02-12 11:37:22 +0000
commit709fcc7c5fd41b5fd409ea867b18f3d215bfb293 (patch)
treed970d19d35097451aa5c4d0a755fdae65ce55e75
parent24ca9064399d94b23cb4c5dd44c0377ee9404e91 (diff)
Refactor duplicated error handler
For extensions of qdb the default error handler (start server and try again) which was used for askDevices and watchDevices is used very often. In order to not duplicate this function for every additional command the same function is reused. Change-Id: I469ccf6955c5590e295d9182a80cdfb3a4dfbcd0 Reviewed-by: Kari Oikarinen <kari.oikarinen@qt.io> Reviewed-by: Samuli Piippo <samuli.piippo@qt.io>
-rw-r--r--qdb/client/client.cpp33
-rw-r--r--qdb/client/client.h6
2 files changed, 8 insertions, 31 deletions
diff --git a/qdb/client/client.cpp b/qdb/client/client.cpp
index 953919f..f3687a2 100644
--- a/qdb/client/client.cpp
+++ b/qdb/client/client.cpp
@@ -85,7 +85,7 @@ void Client::ignoreErrors(bool ignoreErrors)
void Client::askDevices()
{
- setupSocketAndConnect(&Client::handleDevicesConnection, &Client::handleDevicesError);
+ setupSocketAndConnect(&Client::handleDevicesConnection, std::bind(&Client::handleErrorWithRetry, this, std::placeholders::_1, &Client::askDevices));
}
void Client::startServer()
@@ -96,12 +96,12 @@ void Client::startServer()
void Client::stopServer()
{
- setupSocketAndConnect(&Client::handleStopConnection, &Client::handleStopError);
+ setupSocketAndConnect(&Client::handleStopConnection, std::bind(&Client::handleStopError, this, std::placeholders::_1));
}
void Client::watchDevices()
{
- setupSocketAndConnect(&Client::handleWatchConnection, &Client::handleWatchError);
+ setupSocketAndConnect(&Client::handleWatchConnection, std::bind(&Client::handleErrorWithRetry, this, std::placeholders::_1, &Client::watchDevices));
}
void Client::handleDevicesConnection()
@@ -121,7 +121,7 @@ void Client::handleDevicesConnection()
shutdown(0);
}
-void Client::handleDevicesError(QLocalSocket::LocalSocketError error)
+void Client::handleErrorWithRetry(QLocalSocket::LocalSocketError error, ConnectedSlot repeatFunction)
{
if (error == QLocalSocket::PeerClosedError)
return;
@@ -141,7 +141,7 @@ void Client::handleDevicesError(QLocalSocket::LocalSocketError error)
std::cout << "Starting QDB host server\n";
m_triedToStart = true;
forkHostServer();
- QTimer::singleShot(startupDelay, this, &Client::askDevices);
+ QTimer::singleShot(startupDelay, this, repeatFunction);
}
void Client::handleStopConnection()
@@ -180,29 +180,6 @@ void Client::handleWatchConnection()
m_socket->write(createRequest(RequestType::WatchDevices));
}
-void Client::handleWatchError(QLocalSocket::LocalSocketError error)
-{
- if (error == QLocalSocket::PeerClosedError)
- return;
- if (error != QLocalSocket::ServerNotFoundError &&
- error != QLocalSocket::ConnectionRefusedError) {
- std::cerr << "Unexpected QLocalSocket error:" << qUtf8Printable(m_socket->errorString())
- << std::endl;
- shutdown(1);
- return;
- }
-
- if (m_triedToStart) {
- std::cerr << "Could not connect QDB host server even after trying to start it\n";
- shutdown(1);
- return;
- }
- std::cout << "Starting QDB host server\n";
- m_triedToStart = true;
- forkHostServer();
- QTimer::singleShot(startupDelay, this, &Client::watchDevices);
-}
-
void Client::handleWatchMessage()
{
while (m_socket->bytesAvailable() > 0) {
diff --git a/qdb/client/client.h b/qdb/client/client.h
index eccc607..482641c 100644
--- a/qdb/client/client.h
+++ b/qdb/client/client.h
@@ -36,6 +36,7 @@ class QCoreApplication;
QT_END_NAMESPACE
#include <memory>
+#include <functional>
int execClient(const QCoreApplication &app, const QString &command, const QCommandLineParser &parser);
@@ -55,14 +56,13 @@ public slots:
private:
using ConnectedSlot = void (Client::*)();
- using ErrorSlot = void (Client::*)(QLocalSocket::LocalSocketError);
+ using ErrorSlot = std::function<void(QLocalSocket::LocalSocketError)>;
void handleDevicesConnection();
- void handleDevicesError(QLocalSocket::LocalSocketError error);
+ void handleErrorWithRetry(QLocalSocket::LocalSocketError error, ConnectedSlot repeatFunction);
void handleStopConnection();
void handleStopError(QLocalSocket::LocalSocketError error);
void handleWatchConnection();
- void handleWatchError(QLocalSocket::LocalSocketError error);
void handleWatchMessage();
void setupSocketAndConnect(ConnectedSlot handleConnection, ErrorSlot handleError);
void shutdown(int exitCode);