From 34851314d39307f22df01a4b711e6fd3c5618e23 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sun, 29 Mar 2015 11:22:24 +0200 Subject: Don't try to restart the resource on every disconnect. There's a chance that the resource actually wanted to shut-down. Instead ResourceAccess should only reopen the connection if it still has work to do. --- common/resourceaccess.cpp | 23 +++++++++++++++++++---- common/resourceaccess.h | 1 + 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/common/resourceaccess.cpp b/common/resourceaccess.cpp index 20e1e8c..22d7d97 100644 --- a/common/resourceaccess.cpp +++ b/common/resourceaccess.cpp @@ -79,6 +79,7 @@ public: QLocalSocket *socket; QTimer *tryOpenTimer; bool startingProcess; + bool openingConnection; QByteArray partialMessageBuffer; flatbuffers::FlatBufferBuilder fbb; QVector commandQueue; @@ -91,6 +92,7 @@ ResourceAccess::Private::Private(const QString &name, ResourceAccess *q) socket(new QLocalSocket(q)), tryOpenTimer(new QTimer(q)), startingProcess(false), + openingConnection(false), messageId(0) { } @@ -191,6 +193,7 @@ void ResourceAccess::open() log("Socket valid, so not opening again"); return; } + d->openingConnection = true; //TODO: if we try and try and the process does not pick up // we should probably try to start the process again @@ -209,6 +212,7 @@ void ResourceAccess::close() void ResourceAccess::connected() { d->startingProcess = false; + d->openingConnection = false; if (!isReady()) { return; @@ -246,7 +250,6 @@ void ResourceAccess::disconnected() d->socket->close(); log(QString("Disconnected from %1").arg(d->socket->fullServerName())); emit ready(false); - open(); } void ResourceAccess::connectionError(QLocalSocket::LocalSocketError error) @@ -257,18 +260,30 @@ void ResourceAccess::connectionError(QLocalSocket::LocalSocketError error) } return; } - log(QString("Connection error: %1 : %2").arg(error).arg(d->socket->errorString())); + if (error == QLocalSocket::PeerClosedError) { - log("The resource closed the connection. It probably crashed."); + Log() << "The resource closed the connection."; + } else { + Warning() << QString("Connection error: %1 : %2").arg(error).arg(d->socket->errorString()); } + //TODO We could first try to reconnect and resend the message if necessary. for(auto handler : d->resultHandler.values()) { handler(1, "The resource closed unexpectedly"); } d->resultHandler.clear(); + //We're trying to connect but failed, start the resource and retry. + //Don't automatically restart on later disconnects. + if (d->openingConnection) { + startResourceAndConnect(); + } +} + +void ResourceAccess::startResourceAndConnect() +{ d->startingProcess = true; - log(QString("Attempting to start resource ") + d->resourceName); + Log() << "Attempting to start resource " + d->resourceName; QStringList args; args << d->resourceName; if (QProcess::startDetached("akonadi2_synchronizer", args, QDir::homePath())) { diff --git a/common/resourceaccess.h b/common/resourceaccess.h index c5b8a6c..a5a2547 100644 --- a/common/resourceaccess.h +++ b/common/resourceaccess.h @@ -66,6 +66,7 @@ private Q_SLOTS: private: void log(const QString &message); void registerCallback(uint messageId, const std::function &callback); + void startResourceAndConnect(); class Private; Private * const d; -- cgit v1.2.3