mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-02 14:16:16 -04:00
Basic support to access libresapi via unix socket / windows named pipes
This commit is contained in:
parent
c59686ff3c
commit
47414acb62
6 changed files with 174 additions and 3 deletions
74
libresapi/src/api/ApiServerLocal.cpp
Normal file
74
libresapi/src/api/ApiServerLocal.cpp
Normal file
|
@ -0,0 +1,74 @@
|
|||
#include "ApiServerLocal.h"
|
||||
#include "JsonStream.h"
|
||||
|
||||
namespace resource_api{
|
||||
|
||||
ApiServerLocal::ApiServerLocal(ApiServer* server) : QThread(),
|
||||
mApiServer(server), mLocalServer(this)
|
||||
{
|
||||
start();
|
||||
mLocalServer.removeServer(serverName());
|
||||
#if QT_VERSION >= 0x050000
|
||||
mLocalServer.setSocketOptions(QLocalServer::UserAccessOption);
|
||||
#endif
|
||||
connect(&mLocalServer, SIGNAL(newConnection()), this, SLOT(handleConnection()));
|
||||
mLocalServer.listen(serverName());
|
||||
}
|
||||
|
||||
ApiServerLocal::~ApiServerLocal()
|
||||
{
|
||||
mLocalServer.close();
|
||||
quit();
|
||||
}
|
||||
|
||||
void ApiServerLocal::handleConnection()
|
||||
{
|
||||
new ApiLocalConnectionHandler(mApiServer, mLocalServer.nextPendingConnection());
|
||||
}
|
||||
|
||||
ApiLocalConnectionHandler::ApiLocalConnectionHandler(ApiServer* apiServer, QLocalSocket* sock) :
|
||||
QThread(), mApiServer(apiServer), mLocalSocket(sock), mState(WAITING_PATH)
|
||||
{
|
||||
sock->moveToThread(this);
|
||||
connect(mLocalSocket, SIGNAL(disconnected()), this, SLOT(quit()));
|
||||
connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
|
||||
connect(sock, SIGNAL(readyRead()), this, SLOT(handleRequest()));
|
||||
start();
|
||||
}
|
||||
|
||||
void ApiLocalConnectionHandler::handleRequest()
|
||||
{
|
||||
switch(mState)
|
||||
{
|
||||
case WAITING_PATH:
|
||||
{
|
||||
char path[1024];
|
||||
mLocalSocket->readLine(path, 1023);
|
||||
reqPath = path;
|
||||
mState = WAITING_DATA;
|
||||
break;
|
||||
}
|
||||
case WAITING_DATA:
|
||||
{
|
||||
char jsonData[1024];
|
||||
mLocalSocket->read(jsonData, 1023);
|
||||
resource_api::JsonStream reqJson;
|
||||
reqJson.setJsonString(std::string(jsonData));
|
||||
resource_api::Request req(reqJson);
|
||||
req.setPath(reqPath);
|
||||
std::string resultString = mApiServer->handleRequest(req);
|
||||
mLocalSocket->write(resultString.data(), resultString.length());
|
||||
mState = WAITING_PATH;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ApiLocalConnectionHandler::~ApiLocalConnectionHandler()
|
||||
{
|
||||
mLocalSocket->close();
|
||||
delete mLocalSocket;
|
||||
}
|
||||
|
||||
|
||||
} // namespace resource_api
|
58
libresapi/src/api/ApiServerLocal.h
Normal file
58
libresapi/src/api/ApiServerLocal.h
Normal file
|
@ -0,0 +1,58 @@
|
|||
#pragma once
|
||||
|
||||
#include <QLocalServer>
|
||||
#include <QString>
|
||||
#include <QThread>
|
||||
#include <QLocalSocket>
|
||||
#include <retroshare/rsinit.h>
|
||||
#include <string>
|
||||
|
||||
#include "ApiServer.h"
|
||||
|
||||
namespace resource_api
|
||||
{
|
||||
class ApiServer;
|
||||
|
||||
class ApiServerLocal : public QThread
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ApiServerLocal(ApiServer* server);
|
||||
~ApiServerLocal();
|
||||
|
||||
public slots:
|
||||
void handleConnection();
|
||||
|
||||
private:
|
||||
ApiServer* mApiServer;
|
||||
QLocalServer mLocalServer;
|
||||
|
||||
const static QString& serverName()
|
||||
{
|
||||
const static QString sockPath(RsAccounts::AccountDirectory()
|
||||
.append("/libresapi.sock").c_str());
|
||||
return sockPath;
|
||||
}
|
||||
};
|
||||
|
||||
class ApiLocalConnectionHandler : public QThread
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ApiLocalConnectionHandler(ApiServer* apiServer, QLocalSocket* sock);
|
||||
~ApiLocalConnectionHandler();
|
||||
enum State {WAITING_PATH, WAITING_DATA};
|
||||
|
||||
public slots:
|
||||
void handleRequest();
|
||||
|
||||
private:
|
||||
ApiServer* mApiServer;
|
||||
QLocalSocket* mLocalSocket;
|
||||
State mState;
|
||||
std::string reqPath;
|
||||
};
|
||||
|
||||
} // namespace resource_api
|
|
@ -193,6 +193,33 @@ public:
|
|||
// then each handler should pop the top element
|
||||
std::stack<std::string> mPath;
|
||||
std::string mFullPath;
|
||||
bool setPath(std::string reqPath)
|
||||
{
|
||||
std::string str;
|
||||
for(std::string::reverse_iterator sit = reqPath.rbegin(); sit != reqPath.rend(); sit++)
|
||||
{
|
||||
if((*sit) != '/')
|
||||
{
|
||||
// add to front because we are traveling in reverse order
|
||||
str = *sit + str;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(str != "")
|
||||
{
|
||||
mPath.push(str);
|
||||
str.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
if(str != "")
|
||||
{
|
||||
mPath.push(str);
|
||||
}
|
||||
mFullPath = reqPath;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// parameters should be used to influence the result
|
||||
// for example include or exclude some information
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue