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

View File

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

View File

@ -55,4 +55,17 @@ ResponseTask* ResourceRouter::handleRequest(Request& req, Response& resp)
return 0; 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 } // namespace resource_api

View File

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

View File

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

View File

@ -49,6 +49,7 @@ class RsMsgs ;
class RsGxsForums; class RsGxsForums;
class RsGxsChannels; class RsGxsChannels;
class RsNotify; class RsNotify;
class RsServiceControl;
class p3LinkMgr ; class p3LinkMgr ;
class MainPage ; class MainPage ;
class QIcon ; class QIcon ;
@ -75,6 +76,12 @@ class RsGcxs;
class PgpAuxUtils; class PgpAuxUtils;
class p3Config; class p3Config;
namespace resource_api
{
class ResourceRouter;
class StateTokenServer;
}
// Plugin API version. Not used yet, but will be in the future the // Plugin API version. Not used yet, but will be in the future the
// main value that decides for compatibility. // main value that decides for compatibility.
// //
@ -108,6 +115,8 @@ public:
RsUtil::inited_ptr<RsDisc> mDisc; RsUtil::inited_ptr<RsDisc> mDisc;
RsUtil::inited_ptr<RsDht> mDht; RsUtil::inited_ptr<RsDht> mDht;
RsUtil::inited_ptr<RsNotify> mNotify; RsUtil::inited_ptr<RsNotify> mNotify;
RsUtil::inited_ptr<RsServiceControl> mServiceControl;
RsUtil::inited_ptr<RsPluginHandler> mPluginHandler;
// gxs // gxs
std::string mGxsDir; std::string mGxsDir;
@ -147,6 +156,12 @@ class RsPlugin
virtual p3Config *p3_config() const { return NULL ; } virtual p3Config *p3_config() const { return NULL ; }
virtual uint16_t rs_service_id() const { return 0 ; } 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 // Shutdown
virtual void stop() {} virtual void stop() {}

View File

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