2016-07-05 10:11:37 -04:00
|
|
|
#pragma once
|
2016-08-08 08:02:01 -04:00
|
|
|
/*
|
|
|
|
* libresapi local socket server
|
|
|
|
* Copyright (C) 2016 Gioacchino Mazzurco <gio@eigenlab.org>
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2016-07-05 10:11:37 -04:00
|
|
|
|
|
|
|
#include <QLocalServer>
|
|
|
|
#include <QString>
|
|
|
|
#include <QThread>
|
|
|
|
#include <QLocalSocket>
|
|
|
|
#include <retroshare/rsinit.h>
|
|
|
|
#include <string>
|
|
|
|
|
2016-09-22 06:48:08 -04:00
|
|
|
#include "ApiTypes.h"
|
2016-07-05 10:11:37 -04:00
|
|
|
#include "ApiServer.h"
|
|
|
|
|
|
|
|
namespace resource_api
|
|
|
|
{
|
|
|
|
|
2016-08-08 08:02:01 -04:00
|
|
|
class ApiLocalListener : public QObject
|
2016-07-05 10:11:37 -04:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2016-08-22 21:19:33 -04:00
|
|
|
ApiLocalListener(ApiServer* server, const QString &listenPath, QObject *parent=0);
|
2016-08-08 08:02:01 -04:00
|
|
|
~ApiLocalListener() { mLocalServer.close(); }
|
|
|
|
|
2016-07-05 10:11:37 -04:00
|
|
|
public slots:
|
|
|
|
void handleConnection();
|
|
|
|
|
|
|
|
private:
|
|
|
|
ApiServer* mApiServer;
|
|
|
|
QLocalServer mLocalServer;
|
2016-08-08 08:02:01 -04:00
|
|
|
};
|
2016-07-05 10:11:37 -04:00
|
|
|
|
2016-08-08 08:02:01 -04:00
|
|
|
class ApiServerLocal : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2016-08-22 21:19:33 -04:00
|
|
|
ApiServerLocal(ApiServer* server, const QString& listenPath, QObject *parent=0);
|
2016-08-08 08:02:01 -04:00
|
|
|
~ApiServerLocal();
|
|
|
|
|
2016-08-22 21:19:33 -04:00
|
|
|
const static QString& loginServerPath()
|
|
|
|
{
|
|
|
|
const static QString sockPath(RsAccounts::ConfigDirectory()
|
|
|
|
.append("/libresapi.sock").c_str());
|
|
|
|
return sockPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
const static QString& serverPath()
|
|
|
|
{
|
|
|
|
const static QString sockPath(RsAccounts::AccountDirectory()
|
|
|
|
.append("/libresapi.sock").c_str());
|
|
|
|
return sockPath;
|
|
|
|
}
|
|
|
|
|
2016-08-08 08:02:01 -04:00
|
|
|
private:
|
|
|
|
QThread serverThread;
|
|
|
|
ApiLocalListener localListener;
|
2016-07-05 10:11:37 -04:00
|
|
|
};
|
|
|
|
|
2016-08-08 08:02:01 -04:00
|
|
|
class ApiLocalConnectionHandler : public QObject
|
2016-07-05 10:11:37 -04:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2016-08-08 08:02:01 -04:00
|
|
|
ApiLocalConnectionHandler(ApiServer* apiServer, QLocalSocket* sock, QObject *parent = 0);
|
2016-07-05 10:11:37 -04:00
|
|
|
~ApiLocalConnectionHandler();
|
2016-08-08 08:02:01 -04:00
|
|
|
enum State {WAITING_PATH, WAITING_DATA};
|
2016-07-05 10:11:37 -04:00
|
|
|
|
|
|
|
public slots:
|
2016-08-08 08:02:01 -04:00
|
|
|
void handlePendingRequests();
|
2016-07-05 10:11:37 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
ApiServer* mApiServer;
|
|
|
|
QLocalSocket* mLocalSocket;
|
2016-08-08 08:02:01 -04:00
|
|
|
State mState;
|
2016-07-05 10:11:37 -04:00
|
|
|
std::string reqPath;
|
2016-09-22 06:48:08 -04:00
|
|
|
resource_api::Request::Method reqMeth;
|
2016-07-05 10:11:37 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace resource_api
|