allow plugins to integrate into the JSON API

This commit is contained in:
electron128 2015-12-15 19:56:49 +01:00
parent e824a2faea
commit f7ab3ad04f
9 changed files with 204 additions and 94 deletions

View File

@ -0,0 +1,38 @@
#include "ApiPluginHandler.h"
namespace resource_api
{
ApiPluginHandler::ApiPluginHandler(StateTokenServer* statetokenserver, const RsPlugInInterfaces& ifaces)
{
for(int i = 0; i < ifaces.mPluginHandler->nbPlugins(); i++)
{
RsPlugin* plugin = ifaces.mPluginHandler->plugin(i);
std::string entrypoint;
ResourceRouter* child = plugin->new_resource_api_handler(ifaces, statetokenserver, entrypoint);
if(child != 0)
{
mChildren.push_back(child);
if(isNameUsed(entrypoint))
{
std::cerr << "Cannot add plugin api entry point with name=" << entrypoint << ", becaus ethis name is already in use!" << std::endl;
}
else
{
std::cerr << "Added libresapi plugin with entrypoint " << entrypoint << std::endl;
addResourceHandler(entrypoint, child, &ResourceRouter::handleRequest);
}
}
}
}
ApiPluginHandler::~ApiPluginHandler()
{
for(std::vector<ResourceRouter*>::iterator vit = mChildren.begin(); vit != mChildren.end(); ++vit)
{
delete *vit;
}
mChildren.clear();
}
} // namespace resource_api

View File

@ -0,0 +1,20 @@
#pragma once
#include "ResourceRouter.h"
#include <retroshare/rsplugin.h>
namespace resource_api
{
// forwards all incoming requests to retroshare plugins
class ApiPluginHandler: public ResourceRouter
{
public:
ApiPluginHandler(StateTokenServer* statetokenserver, const RsPlugInInterfaces& ifaces);
virtual ~ApiPluginHandler();
private:
std::vector<ResourceRouter*> mChildren;
};
} // namespace resource_api

View File

@ -13,6 +13,8 @@
#include "JsonStream.h"
#include "StateTokenServer.h" // for the state token serialisers
#include "ApiPluginHandler.h"
/*
data types in json http://json.org/
string (utf-8 unicode)
@ -226,10 +228,11 @@ public:
mPeersHandler(sts, ifaces.mNotify, ifaces.mPeers, ifaces.mMsgs),
mIdentityHandler(ifaces.mIdentity),
mForumHandler(ifaces.mGxsForums),
mServiceControlHandler(rsServiceControl), // TODO: don't use global variable here
mServiceControlHandler(ifaces.mServiceControl),
mFileSearchHandler(sts, ifaces.mNotify, ifaces.mTurtle, ifaces.mFiles),
mTransfersHandler(sts, ifaces.mFiles),
mChatHandler(sts, ifaces.mNotify, ifaces.mMsgs, ifaces.mPeers, ifaces.mIdentity, &mPeersHandler)
mChatHandler(sts, ifaces.mNotify, ifaces.mMsgs, ifaces.mPeers, ifaces.mIdentity, &mPeersHandler),
mApiPluginHandler(sts, ifaces)
{
// the dynamic cast is to not confuse the addResourceHandler template like this:
// addResourceHandler(derived class, parent class)
@ -249,6 +252,8 @@ public:
&TransfersHandler::handleRequest);
router.addResourceHandler("chat", dynamic_cast<ResourceRouter*>(&mChatHandler),
&ChatHandler::handleRequest);
router.addResourceHandler("apiplugin", dynamic_cast<ResourceRouter*>(&mApiPluginHandler),
&ChatHandler::handleRequest);
}
PeersHandler mPeersHandler;
@ -258,6 +263,7 @@ public:
FileSearchHandler mFileSearchHandler;
TransfersHandler mTransfersHandler;
ChatHandler mChatHandler;
ApiPluginHandler mApiPluginHandler;
};
ApiServer::ApiServer():

View File

@ -7,6 +7,7 @@
#include <retroshare/rsdisc.h>
#include <retroshare/rsdht.h>
#include <retroshare/rsnotify.h>
#include <retroshare/rsservicecontrol.h>
#include <retroshare/rsidentity.h>
#include <retroshare/rsgxscircles.h>
@ -28,6 +29,8 @@ bool getPluginInterfaces(RsPlugInInterfaces& interfaces)
interfaces.mDisc = rsDisc;
interfaces.mDht = rsDht;
interfaces.mNotify = rsNotify;
interfaces.mServiceControl = rsServiceControl;
interfaces.mPluginHandler = rsPlugins;
// gxs
interfaces.mGxsDir = "";

View File

@ -55,4 +55,17 @@ ResponseTask* ResourceRouter::handleRequest(Request& req, Response& resp)
return 0;
}
bool ResourceRouter::isNameUsed(std::string name)
{
std::vector<std::pair<std::string, HandlerBase*> >::iterator vit;
for(vit = mHandlers.begin(); vit != mHandlers.end(); vit++)
{
if(vit->first == name)
{
return true;
}
}
return false;
}
} // namespace resource_api

View File

@ -1,6 +1,7 @@
#pragma once
#include "ApiTypes.h"
#include <iostream>
namespace resource_api
{
@ -20,6 +21,8 @@ public:
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));
bool isNameUsed(std::string name);
private:
class HandlerBase
{
@ -61,6 +64,10 @@ private:
template <class T>
void ResourceRouter::addResourceHandler(std::string name, T* instance, ResponseTask* (T::*callback)(Request& req, Response& resp))
{
if(isNameUsed(name))
{
std::cerr << "ResourceRouter::addResourceHandler ERROR: name=" << name << " alerady in use. Not adding new Handler!" << std::endl;
}
Handler<T>* handler = new Handler<T>();
handler->instance = instance;
handler->method = callback;
@ -69,6 +76,10 @@ void ResourceRouter::addResourceHandler(std::string name, T* instance, ResponseT
template <class T>
void ResourceRouter::addResourceHandler(std::string name, T* instance, void (T::*callback)(Request& req, Response& resp))
{
if(isNameUsed(name))
{
std::cerr << "ResourceRouter::addResourceHandler ERROR: name=" << name << " alerady in use. Not adding new Handler!" << std::endl;
}
InstantResponseHandler<T>* handler = new InstantResponseHandler<T>();
handler->instance = instance;
handler->method = callback;

View File

@ -1,92 +1,94 @@
!include("../../retroshare.pri"): error("Could not include file ../../retroshare.pri")
TEMPLATE = lib
CONFIG += staticlib
CONFIG += create_prl
CONFIG -= qt
TARGET = resapi
TARGET_PRL = libresapi
DESTDIR = lib
CONFIG += libmicrohttpd
INCLUDEPATH += ../../libretroshare/src
unix {
webui_files.path = "$${DATA_DIR}/webui"
webui_files.files = webfiles/*
INSTALLS += webui_files
webui_img_files.path = "$${DATA_DIR}/webui/img"
webui_img_files.files = ../../retroshare-gui/src/gui/images/logo/logo_splash.png
INSTALLS += webui_img_files
}
win32{
DEFINES *= WINDOWS_SYS
INCLUDEPATH += . $$INC_DIR
}
libmicrohttpd{
linux {
CONFIG += link_pkgconfig
PKGCONFIG *= libmicrohttpd
} else {
mac {
INCLUDEPATH += /usr/local/include
LIBS *= /usr/local/lib/libmicrohttpd.a
} else {
LIBS *= -lmicrohttpd
}
}
SOURCES += \
api/ApiServerMHD.cpp
HEADERS += \
api/ApiServerMHD.h
}
SOURCES += \
api/ApiServer.cpp \
api/json.cpp \
api/JsonStream.cpp \
api/ResourceRouter.cpp \
api/PeersHandler.cpp \
api/Operators.cpp \
api/IdentityHandler.cpp \
api/ForumHandler.cpp \
api/ServiceControlHandler.cpp \
api/StateTokenServer.cpp \
api/GxsResponseTask.cpp \
api/FileSearchHandler.cpp \
api/TransfersHandler.cpp \
api/RsControlModule.cpp \
api/GetPluginInterfaces.cpp \
api/ChatHandler.cpp \
api/LivereloadHandler.cpp \
api/TmpBlobStore.cpp \
util/ContentTypes.cpp
HEADERS += \
api/ApiServer.h \
api/json.h \
api/JsonStream.h \
api/ApiTypes.h \
api/ResourceRouter.h \
api/PeersHandler.h \
api/Operators.h \
api/IdentityHandler.h \
api/ForumHandler.h \
api/ServiceControlHandler.h \
api/GxsMetaOperators.h \
api/StateTokenServer.h \
api/GxsResponseTask.h \
api/Pagination.h \
api/FileSearchHandler.h \
api/TransfersHandler.h \
api/RsControlModule.h \
api/GetPluginInterfaces.h \
api/ChatHandler.h \
api/LivereloadHandler.h \
api/TmpBlobStore.h \
util/ContentTypes.h
!include("../../retroshare.pri"): error("Could not include file ../../retroshare.pri")
TEMPLATE = lib
CONFIG += staticlib
CONFIG += create_prl
CONFIG -= qt
TARGET = resapi
TARGET_PRL = libresapi
DESTDIR = lib
CONFIG += libmicrohttpd
INCLUDEPATH += ../../libretroshare/src
unix {
webui_files.path = "$${DATA_DIR}/webui"
webui_files.files = webfiles/*
INSTALLS += webui_files
webui_img_files.path = "$${DATA_DIR}/webui/img"
webui_img_files.files = ../../retroshare-gui/src/gui/images/logo/logo_splash.png
INSTALLS += webui_img_files
}
win32{
DEFINES *= WINDOWS_SYS
INCLUDEPATH += . $$INC_DIR
}
libmicrohttpd{
linux {
CONFIG += link_pkgconfig
PKGCONFIG *= libmicrohttpd
} else {
mac {
INCLUDEPATH += /usr/local/include
LIBS *= /usr/local/lib/libmicrohttpd.a
} else {
LIBS *= -lmicrohttpd
}
}
SOURCES += \
api/ApiServerMHD.cpp
HEADERS += \
api/ApiServerMHD.h
}
SOURCES += \
api/ApiServer.cpp \
api/json.cpp \
api/JsonStream.cpp \
api/ResourceRouter.cpp \
api/PeersHandler.cpp \
api/Operators.cpp \
api/IdentityHandler.cpp \
api/ForumHandler.cpp \
api/ServiceControlHandler.cpp \
api/StateTokenServer.cpp \
api/GxsResponseTask.cpp \
api/FileSearchHandler.cpp \
api/TransfersHandler.cpp \
api/RsControlModule.cpp \
api/GetPluginInterfaces.cpp \
api/ChatHandler.cpp \
api/LivereloadHandler.cpp \
api/TmpBlobStore.cpp \
util/ContentTypes.cpp \
api/ApiPluginHandler.cpp
HEADERS += \
api/ApiServer.h \
api/json.h \
api/JsonStream.h \
api/ApiTypes.h \
api/ResourceRouter.h \
api/PeersHandler.h \
api/Operators.h \
api/IdentityHandler.h \
api/ForumHandler.h \
api/ServiceControlHandler.h \
api/GxsMetaOperators.h \
api/StateTokenServer.h \
api/GxsResponseTask.h \
api/Pagination.h \
api/FileSearchHandler.h \
api/TransfersHandler.h \
api/RsControlModule.h \
api/GetPluginInterfaces.h \
api/ChatHandler.h \
api/LivereloadHandler.h \
api/TmpBlobStore.h \
util/ContentTypes.h \
api/ApiPluginHandler.h

View File

@ -49,6 +49,7 @@ class RsMsgs ;
class RsGxsForums;
class RsGxsChannels;
class RsNotify;
class RsServiceControl;
class p3LinkMgr ;
class MainPage ;
class QIcon ;
@ -75,6 +76,12 @@ class RsGcxs;
class PgpAuxUtils;
class p3Config;
namespace resource_api
{
class ResourceRouter;
class StateTokenServer;
}
// Plugin API version. Not used yet, but will be in the future the
// main value that decides for compatibility.
//
@ -108,6 +115,8 @@ public:
RsUtil::inited_ptr<RsDisc> mDisc;
RsUtil::inited_ptr<RsDht> mDht;
RsUtil::inited_ptr<RsNotify> mNotify;
RsUtil::inited_ptr<RsServiceControl> mServiceControl;
RsUtil::inited_ptr<RsPluginHandler> mPluginHandler;
// gxs
std::string mGxsDir;
@ -147,6 +156,12 @@ class RsPlugin
virtual p3Config *p3_config() const { return NULL ; }
virtual uint16_t rs_service_id() const { return 0 ; }
// creates a new resource api handler object. ownership is transferred to the caller.
// the caller should supply a statetokenserver, and keep it valid until destruction
// the plugin should return a entry point name. this is to make the entry point name independent from file names
virtual resource_api::ResourceRouter* new_resource_api_handler(const RsPlugInInterfaces& ifaces, resource_api::StateTokenServer* sts, std::string &entrypoint) const { return 0;}
// Shutdown
virtual void stop() {}

View File

@ -1523,6 +1523,8 @@ int RsServer::StartupRetroShare()
interfaces.mDisc = rsDisc;
interfaces.mDht = rsDht;
interfaces.mNotify = mNotify;
interfaces.mServiceControl = serviceCtrl;
interfaces.mPluginHandler = mPluginsManager;
// gxs
interfaces.mGxsDir = currGxsDir;
interfaces.mIdentity = mGxsIdService;