2017-04-11 07:11:57 -04:00
|
|
|
#pragma once
|
2016-09-04 09:01:44 -04:00
|
|
|
/*
|
|
|
|
* libresapi local socket client
|
2017-04-11 07:11:57 -04:00
|
|
|
* Copyright (C) 2016-2017 Gioacchino Mazzurco <gio@eigenlab.org>
|
2016-09-04 09:01:44 -04:00
|
|
|
* Copyright (C) 2016 Manu Pineda <manu@cooperativa.cat>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QLocalSocket>
|
2016-12-08 09:56:23 -05:00
|
|
|
#include <QQueue>
|
|
|
|
#include <QJSValue>
|
2017-04-10 09:00:14 -04:00
|
|
|
#include <QTimer>
|
2016-09-04 09:01:44 -04:00
|
|
|
|
|
|
|
class LibresapiLocalClient : public QObject
|
|
|
|
{
|
2016-09-15 07:07:13 -04:00
|
|
|
Q_OBJECT
|
2016-09-04 09:01:44 -04:00
|
|
|
|
2016-09-15 07:07:13 -04:00
|
|
|
public:
|
2017-04-03 15:09:12 -04:00
|
|
|
LibresapiLocalClient() :
|
|
|
|
#ifdef QT_DEBUG
|
|
|
|
reqCount(0), ansCount(0), mDebug(true),
|
|
|
|
#endif // QT_DEBUG
|
2017-04-10 09:00:14 -04:00
|
|
|
mLocalSocket(this)
|
|
|
|
{
|
|
|
|
mConnectAttemptTimer.setSingleShot(true);
|
|
|
|
mConnectAttemptTimer.setInterval(500);
|
|
|
|
connect(&mConnectAttemptTimer, SIGNAL(timeout()),
|
|
|
|
this, SLOT(socketConnectAttempt()));
|
|
|
|
}
|
2016-09-04 09:01:44 -04:00
|
|
|
|
2016-12-08 09:56:23 -05:00
|
|
|
Q_INVOKABLE int request( const QString& path, const QString& jsonData = "",
|
2017-04-03 15:09:12 -04:00
|
|
|
QJSValue callback = QJSValue::NullValue );
|
2017-04-10 09:00:14 -04:00
|
|
|
Q_INVOKABLE void openConnection(const QString& socketPath);
|
2016-09-04 09:01:44 -04:00
|
|
|
|
2017-04-03 15:09:12 -04:00
|
|
|
#ifdef QT_DEBUG
|
|
|
|
Q_PROPERTY(bool debug READ debug WRITE setDebug NOTIFY debugChanged)
|
|
|
|
|
|
|
|
bool debug() const { return mDebug; }
|
|
|
|
void setDebug(bool v);
|
|
|
|
|
|
|
|
uint64_t reqCount;
|
|
|
|
uint64_t ansCount;
|
|
|
|
bool mDebug;
|
|
|
|
#endif // QT_DEBUG
|
|
|
|
|
2016-09-15 07:07:13 -04:00
|
|
|
private:
|
2017-04-10 09:00:14 -04:00
|
|
|
QTimer mConnectAttemptTimer;
|
|
|
|
QString mSocketPath;
|
2016-09-15 07:07:13 -04:00
|
|
|
QLocalSocket mLocalSocket;
|
2017-04-03 15:09:12 -04:00
|
|
|
|
|
|
|
struct PQRecord
|
|
|
|
{
|
|
|
|
PQRecord( const QString& path, const QString& jsonData,
|
|
|
|
const QJSValue& callback);
|
|
|
|
|
|
|
|
#ifdef QT_DEBUG
|
|
|
|
QString mPath;
|
|
|
|
QString mJsonData;
|
|
|
|
#endif //QT_DEBUG
|
|
|
|
|
|
|
|
QJSValue mCallback;
|
|
|
|
};
|
|
|
|
QQueue<PQRecord> processingQueue;
|
2016-09-04 09:01:44 -04:00
|
|
|
|
2016-09-15 07:07:13 -04:00
|
|
|
private slots:
|
2017-04-10 09:00:14 -04:00
|
|
|
void socketConnectAttempt() { mLocalSocket.connectToServer(mSocketPath); }
|
2016-09-15 07:07:13 -04:00
|
|
|
void socketError(QLocalSocket::LocalSocketError error);
|
|
|
|
void read();
|
2016-09-04 09:01:44 -04:00
|
|
|
|
2016-09-15 07:07:13 -04:00
|
|
|
signals:
|
2016-12-08 09:56:23 -05:00
|
|
|
/// @deprecated @see LibresapiLocalClient::responseReceived instead
|
|
|
|
void goodResponseReceived(const QString & msg);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief responseReceived emitted when a response is received
|
|
|
|
* @param msg
|
|
|
|
*/
|
|
|
|
void responseReceived(const QString & msg);
|
2017-04-03 15:09:12 -04:00
|
|
|
|
|
|
|
#ifdef QT_DEBUG
|
|
|
|
void debugChanged();
|
|
|
|
#endif // QT_DEBUG
|
2016-09-04 09:01:44 -04:00
|
|
|
};
|