2015-03-18 14:48:43 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <retroshare/rsplugin.h>
|
|
|
|
|
|
|
|
#include "ApiTypes.h"
|
|
|
|
#include "PeersHandler.h"
|
|
|
|
#include "IdentityHandler.h"
|
|
|
|
#include "ServiceControlHandler.h"
|
|
|
|
#include "StateTokenServer.h"
|
|
|
|
#include "FileSearchHandler.h"
|
|
|
|
#include "TransfersHandler.h"
|
2015-04-29 07:57:56 -04:00
|
|
|
#include "LivereloadHandler.h"
|
2015-06-16 08:35:07 -04:00
|
|
|
#include "TmpBlobStore.h"
|
2015-07-29 09:02:10 -04:00
|
|
|
#include "ChatHandler.h"
|
2015-03-18 14:48:43 -04:00
|
|
|
|
|
|
|
namespace resource_api{
|
|
|
|
|
|
|
|
class ApiServerMainModules;
|
|
|
|
|
2015-03-31 14:00:40 -04:00
|
|
|
// main entry point for all resource_api calls
|
|
|
|
// general part of the api server
|
|
|
|
// should work with any http library or a different transport protocol (e.g. SSH)
|
2015-03-18 14:48:43 -04:00
|
|
|
|
|
|
|
// call chain is like this:
|
2015-03-31 14:00:40 -04:00
|
|
|
// HTTP server -> ApiServer -> different handlers
|
|
|
|
// or
|
|
|
|
// GUI -> ApiServer -> different handlers
|
|
|
|
// multiple clients can use the same ApiServer instance at the same time
|
2015-03-18 14:48:43 -04:00
|
|
|
|
2015-03-31 14:00:40 -04:00
|
|
|
// ALL public methods in this class are thread safe
|
|
|
|
// this allows differen threads to send requests
|
2015-03-18 14:48:43 -04:00
|
|
|
class ApiServer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ApiServer();
|
|
|
|
~ApiServer();
|
|
|
|
|
2015-03-31 14:00:40 -04:00
|
|
|
class RequestId{
|
|
|
|
public:
|
|
|
|
RequestId(): done(false), task(0), request(0), response(0){}
|
|
|
|
bool operator ==(const RequestId& r){
|
|
|
|
const RequestId& l = *this;
|
|
|
|
return (l.done==r.done)&&(l.task==r.task)&&(l.request==r.request)&&(l.response&&r.response);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
friend class ApiServer;
|
|
|
|
bool done; // this flag will be set to true, to signal the task id is valid and the task is done
|
|
|
|
// (in case there was no ResponseTask and task was zero)
|
|
|
|
ResponseTask* task; // null when the task id is invalid or when there was no task
|
|
|
|
Request* request;
|
|
|
|
Response* response;
|
|
|
|
};
|
|
|
|
|
|
|
|
// process the requestgiven by request and return the response as json string
|
|
|
|
// blocks until the request was processed
|
2015-03-18 14:48:43 -04:00
|
|
|
std::string handleRequest(Request& request);
|
|
|
|
|
2015-03-31 14:00:40 -04:00
|
|
|
// request and response must stay valid until isRequestDone returns true
|
|
|
|
// this method may do some work but it does not block
|
|
|
|
RequestId handleRequest(Request& request, Response& response);
|
|
|
|
|
|
|
|
// ticks the request
|
|
|
|
// returns true if the request is done or the id is invalid
|
|
|
|
// this method may do some work but it does not block
|
|
|
|
bool isRequestDone(RequestId id);
|
|
|
|
|
2015-03-18 14:48:43 -04:00
|
|
|
// load the main api modules
|
|
|
|
void loadMainModules(const RsPlugInInterfaces& ifaces);
|
|
|
|
|
|
|
|
// allows to add more handlers
|
|
|
|
// make sure the livetime of the handlers is longer than the api server
|
|
|
|
template <class T>
|
|
|
|
void addResourceHandler(std::string name, T* instance, ResponseTask* (T::*callback)(Request& req, Response& resp));
|
|
|
|
template <class T>
|
|
|
|
void addResourceHandler(std::string name, T* instance, void (T::*callback)(Request& req, Response& resp));
|
|
|
|
|
|
|
|
StateTokenServer* getStateTokenServer(){ return &mStateTokenServer; }
|
2015-06-16 08:35:07 -04:00
|
|
|
TmpBlobStore* getTmpBlobStore(){ return &mTmpBlobStore; }
|
2015-03-18 14:48:43 -04:00
|
|
|
|
|
|
|
private:
|
2015-03-31 14:00:40 -04:00
|
|
|
RsMutex mMtx;
|
2015-03-18 14:48:43 -04:00
|
|
|
StateTokenServer mStateTokenServer; // goes first, as others may depend on it
|
|
|
|
// is always loaded, because it has no dependencies
|
2015-04-29 07:57:56 -04:00
|
|
|
LivereloadHandler mLivereloadhandler;
|
2015-06-16 08:35:07 -04:00
|
|
|
TmpBlobStore mTmpBlobStore;
|
2015-03-18 14:48:43 -04:00
|
|
|
|
|
|
|
// only pointers here, to load/unload modules at runtime
|
|
|
|
ApiServerMainModules* mMainModules; // loaded when RS is started
|
|
|
|
|
|
|
|
ResourceRouter mRouter;
|
2015-03-31 14:00:40 -04:00
|
|
|
|
|
|
|
std::vector<RequestId> mRequests;
|
2015-03-18 14:48:43 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// implementations
|
|
|
|
template <class T>
|
|
|
|
void ApiServer::addResourceHandler(std::string name, T* instance, ResponseTask* (T::*callback)(Request& req, Response& resp))
|
|
|
|
{
|
2015-03-31 14:00:40 -04:00
|
|
|
RS_STACK_MUTEX(mMtx); // ********** LOCKED **********
|
2015-03-18 14:48:43 -04:00
|
|
|
mRouter.addResourceHandler(name, instance, callback);
|
|
|
|
}
|
|
|
|
template <class T>
|
|
|
|
void ApiServer::addResourceHandler(std::string name, T* instance, void (T::*callback)(Request& req, Response& resp))
|
|
|
|
{
|
2015-03-31 14:00:40 -04:00
|
|
|
RS_STACK_MUTEX(mMtx); // ********** LOCKED **********
|
2015-03-18 14:48:43 -04:00
|
|
|
mRouter.addResourceHandler(name, instance, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|