mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-06-08 22:52:54 -04:00
created separate class for restbed services
This commit is contained in:
parent
a584b822a8
commit
3ca22f0052
7 changed files with 267 additions and 134 deletions
|
@ -35,11 +35,11 @@ namespace rb = restbed;
|
||||||
|
|
||||||
struct JsonApiServer;
|
struct JsonApiServer;
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * Pointer to global instance of JsonApiServer
|
* Pointer to global instance of JsonApiServer
|
||||||
// * @jsonapi{development}
|
* @jsonapi{development}
|
||||||
// */
|
*/
|
||||||
// extern JsonApiServer* jsonApiServer;
|
extern JsonApiServer* jsonApiServer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple usage
|
* Simple usage
|
||||||
|
@ -236,6 +236,7 @@ private:
|
||||||
serviceInstance.get(), serviceName, ctx, session );
|
serviceInstance.get(), serviceName, ctx, session );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JsonApiServer *_instance;
|
||||||
static p3ConfigMgr *_config_mgr;
|
static p3ConfigMgr *_config_mgr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
137
libretroshare/src/jsonapi/restbedservice.cc
Normal file
137
libretroshare/src/jsonapi/restbedservice.cc
Normal file
|
@ -0,0 +1,137 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* libretroshare/src/jsonapi/: restbedservice.cc *
|
||||||
|
* *
|
||||||
|
* libretroshare: retroshare core library *
|
||||||
|
* *
|
||||||
|
* Copyright 2019-2019 Cyril Soler *
|
||||||
|
* *
|
||||||
|
* This program is free software: you can redistribute it and/or modify *
|
||||||
|
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License *
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
||||||
|
* *
|
||||||
|
*******************************************************************************/
|
||||||
|
|
||||||
|
#include "util/rsthreads.h"
|
||||||
|
#include "util/rsdebug.h"
|
||||||
|
#include "restbedservice.h"
|
||||||
|
|
||||||
|
class RestbedThread: public RsThread
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
RestbedThread()
|
||||||
|
{
|
||||||
|
_service = std::make_shared<restbed::Service>(); // this is a place holder, in case we request some internal values.
|
||||||
|
_listening_port = 1984;
|
||||||
|
}
|
||||||
|
|
||||||
|
void runloop() override
|
||||||
|
{
|
||||||
|
if(_resources.empty())
|
||||||
|
{
|
||||||
|
RsErr() << "(EE) please call RestbedService::setResources() before launching the service!" << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto settings = std::make_shared< restbed::Settings >( );
|
||||||
|
settings->set_port( _listening_port );
|
||||||
|
settings->set_default_header( "Connection", "close" );
|
||||||
|
|
||||||
|
if(_service->is_up())
|
||||||
|
{
|
||||||
|
std::cerr << "(II) WebUI is already running. Killing it." << std::endl;
|
||||||
|
_service->stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
_service = std::make_shared<restbed::Service>();
|
||||||
|
|
||||||
|
for(auto& r:_resources)
|
||||||
|
_service->publish( r );
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
std::cerr << "(II) Starting web service on port " << std::dec << _listening_port << std::endl;
|
||||||
|
_service->start( settings );
|
||||||
|
}
|
||||||
|
catch(std::exception& e)
|
||||||
|
{
|
||||||
|
RsErr() << "Could not start web interface: " << e.what() << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cerr << "(II) webui service stopped." << std::endl;
|
||||||
|
}
|
||||||
|
void stop()
|
||||||
|
{
|
||||||
|
_service->stop();
|
||||||
|
|
||||||
|
RsThread::ask_for_stop();
|
||||||
|
|
||||||
|
while(isRunning())
|
||||||
|
{
|
||||||
|
std::cerr << "(II) shutting down webui service." << std::endl;
|
||||||
|
rstime::rs_usleep(1000*1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setListeningPort(uint16_t p) { _listening_port = p ; }
|
||||||
|
void setResources(const std::vector<std::shared_ptr<restbed::Resource> >& r) { _resources = r ; }
|
||||||
|
uint16_t listeningPort() const { return _listening_port;}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::shared_ptr<restbed::Service> _service;
|
||||||
|
uint16_t _listening_port;
|
||||||
|
std::vector<std::shared_ptr<restbed::Resource> > _resources;
|
||||||
|
};
|
||||||
|
|
||||||
|
RestbedService::RestbedService()
|
||||||
|
{
|
||||||
|
_restbed_thread = new RestbedThread();
|
||||||
|
}
|
||||||
|
RestbedService::~RestbedService()
|
||||||
|
{
|
||||||
|
while(_restbed_thread->isRunning())
|
||||||
|
{
|
||||||
|
stop();
|
||||||
|
std::cerr << "Deleting webUI object while webUI thread is still running. Trying shutdown...." << std::endl;
|
||||||
|
rstime::rs_usleep(1000*1000);
|
||||||
|
}
|
||||||
|
delete _restbed_thread;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RestbedService::restart()
|
||||||
|
{
|
||||||
|
RsDbg() << "Restarting web interface listening on port " << _restbed_thread->listeningPort() << std::endl;
|
||||||
|
|
||||||
|
if(_restbed_thread->isRunning())
|
||||||
|
_restbed_thread->stop();
|
||||||
|
|
||||||
|
_restbed_thread->setResources(getResources());
|
||||||
|
_restbed_thread->start();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RestbedService::stop()
|
||||||
|
{
|
||||||
|
_restbed_thread->stop();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool RestbedService::isRunning() const
|
||||||
|
{
|
||||||
|
return _restbed_thread->isRunning();
|
||||||
|
}
|
||||||
|
void RestbedService::setListeningPort(uint16_t port)
|
||||||
|
{
|
||||||
|
_restbed_thread->setListeningPort(port);
|
||||||
|
|
||||||
|
if(_restbed_thread->isRunning())
|
||||||
|
restart();
|
||||||
|
}
|
||||||
|
|
47
libretroshare/src/jsonapi/restbedservice.h
Normal file
47
libretroshare/src/jsonapi/restbedservice.h
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* libretroshare/src/jsonapi/: restbedservice.h *
|
||||||
|
* *
|
||||||
|
* libretroshare: retroshare core library *
|
||||||
|
* *
|
||||||
|
* Copyright 2019-2019 Cyril Soler *
|
||||||
|
* *
|
||||||
|
* This program is free software: you can redistribute it and/or modify *
|
||||||
|
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License *
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
||||||
|
* *
|
||||||
|
*******************************************************************************/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <restbed>
|
||||||
|
|
||||||
|
class RestbedThread;
|
||||||
|
|
||||||
|
class RestbedService
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
RestbedService() ;
|
||||||
|
virtual ~RestbedService();
|
||||||
|
|
||||||
|
bool isRunning() const ;
|
||||||
|
|
||||||
|
virtual bool restart();
|
||||||
|
virtual bool stop();
|
||||||
|
|
||||||
|
virtual void setListeningPort(uint16_t port) ;
|
||||||
|
|
||||||
|
virtual std::vector<std::shared_ptr<restbed::Resource> > getResources()const =0;
|
||||||
|
|
||||||
|
private:
|
||||||
|
RestbedThread *_restbed_thread;
|
||||||
|
};
|
||||||
|
|
|
@ -901,8 +901,8 @@ rs_jsonapi {
|
||||||
# Force recalculation of libretroshare dependencies see https://stackoverflow.com/a/47884045
|
# Force recalculation of libretroshare dependencies see https://stackoverflow.com/a/47884045
|
||||||
QMAKE_EXTRA_TARGETS += libretroshare
|
QMAKE_EXTRA_TARGETS += libretroshare
|
||||||
|
|
||||||
HEADERS += jsonapi/jsonapi.h jsonapi/jsonapiitems.h
|
HEADERS += jsonapi/jsonapi.h jsonapi/jsonapiitems.h jsonapi/restbedservice.h retroshare/rsjsonapi.h
|
||||||
SOURCES += jsonapi/jsonapi.cpp
|
SOURCES += jsonapi/jsonapi.cpp jsonapi/restbedservice.cc
|
||||||
}
|
}
|
||||||
|
|
||||||
rs_deep_channels_index {
|
rs_deep_channels_index {
|
||||||
|
|
38
libretroshare/src/retroshare/rsjsonapi.h
Normal file
38
libretroshare/src/retroshare/rsjsonapi.h
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* libretroshare/src/retroshare: rsjsonapi.h *
|
||||||
|
* *
|
||||||
|
* Copyright (C) 2019-2019 Cyril Soler <csoler@users.sourceforge.net> *
|
||||||
|
* *
|
||||||
|
* This program is free software: you can redistribute it and/or modify *
|
||||||
|
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details. *
|
||||||
|
* *
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License *
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
||||||
|
*
|
||||||
|
*******************************************************************************/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
class RsJsonAPI
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static const uint16_t DEFAULT_PORT = 9092 ;
|
||||||
|
static const std::string DEFAULT_BINDING_ADDRESS ; // 127.0.0.1
|
||||||
|
|
||||||
|
virtual bool restart() =0;
|
||||||
|
virtual bool stop() =0;
|
||||||
|
|
||||||
|
virtual void setHtmlFilesDirectory(const std::string& html_dir) =0;
|
||||||
|
virtual void setListeningPort(uint16_t port) =0;
|
||||||
|
|
||||||
|
virtual int status() const=0;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern RsJsonAPI *rsJsonAPI;
|
||||||
|
|
|
@ -28,7 +28,6 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <restbed>
|
|
||||||
#include "util/rsthreads.h"
|
#include "util/rsthreads.h"
|
||||||
#include "util/rsdebug.h"
|
#include "util/rsdebug.h"
|
||||||
#include "retroshare/rswebui.h"
|
#include "retroshare/rswebui.h"
|
||||||
|
@ -93,17 +92,10 @@ static void service_ready_handler( restbed::Service& )
|
||||||
fprintf( stderr, "Hey! The service is up and running." );
|
fprintf( stderr, "Hey! The service is up and running." );
|
||||||
}
|
}
|
||||||
|
|
||||||
class WebUIThread: public RsThread
|
std::vector<std::shared_ptr<restbed::Resource> > p3WebUI::getResources() const
|
||||||
{
|
{
|
||||||
public:
|
std::vector<std::shared_ptr<restbed::Resource> > rtab;
|
||||||
WebUIThread()
|
|
||||||
{
|
|
||||||
_service = std::make_shared<restbed::Service>(); // this is a place holder, in case we request some internal values.
|
|
||||||
_listening_port = 1984;
|
|
||||||
}
|
|
||||||
|
|
||||||
void runloop() override
|
|
||||||
{
|
|
||||||
auto resource1 = std::make_shared< restbed::Resource >( );
|
auto resource1 = std::make_shared< restbed::Resource >( );
|
||||||
resource1->set_paths( {
|
resource1->set_paths( {
|
||||||
"/{filename: index.html}",
|
"/{filename: index.html}",
|
||||||
|
@ -126,103 +118,22 @@ public:
|
||||||
} );
|
} );
|
||||||
resource3->set_method_handler( "GET", handler<TEXT_SVG>::get_handler );
|
resource3->set_method_handler( "GET", handler<TEXT_SVG>::get_handler );
|
||||||
|
|
||||||
auto settings = std::make_shared< restbed::Settings >( );
|
rtab.push_back(resource1);
|
||||||
settings->set_port( _listening_port );
|
rtab.push_back(resource2);
|
||||||
settings->set_default_header( "Connection", "close" );
|
rtab.push_back(resource3);
|
||||||
|
|
||||||
if(_service->is_up())
|
return rtab;
|
||||||
{
|
|
||||||
std::cerr << "WebUI is already running. Killing it." << std::endl;
|
|
||||||
_service->stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
_service = std::make_shared<restbed::Service>();
|
|
||||||
|
|
||||||
_service->publish( resource1 );
|
|
||||||
_service->publish( resource2 );
|
|
||||||
_service->publish( resource3 );
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
std::cerr << "(II) Starting web service on port " << std::dec << _listening_port << std::endl;
|
|
||||||
_service->start( settings );
|
|
||||||
}
|
|
||||||
catch(std::exception& e)
|
|
||||||
{
|
|
||||||
RsErr() << "Could not start web interface: " << e.what() << std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cerr << "(II) webui service stopped." << std::endl;
|
|
||||||
}
|
|
||||||
void stop()
|
|
||||||
{
|
|
||||||
_service->stop();
|
|
||||||
|
|
||||||
RsThread::ask_for_stop();
|
|
||||||
|
|
||||||
while(isRunning())
|
|
||||||
{
|
|
||||||
std::cerr << "(II) shutting down webui service." << std::endl;
|
|
||||||
rstime::rs_usleep(1000*1000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void setListeningPort(uint16_t p) { _listening_port = p ; }
|
|
||||||
uint16_t listeningPort() const { return _listening_port;}
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::shared_ptr<restbed::Service> _service;
|
|
||||||
uint16_t _listening_port;
|
|
||||||
};
|
|
||||||
|
|
||||||
p3WebUI::p3WebUI()
|
|
||||||
{
|
|
||||||
_webui_thread = new WebUIThread;
|
|
||||||
}
|
|
||||||
p3WebUI::~p3WebUI()
|
|
||||||
{
|
|
||||||
while(_webui_thread->isRunning())
|
|
||||||
{
|
|
||||||
stop();
|
|
||||||
std::cerr << "Deleting webUI object while webUI thread is still running. Trying shutdown...." << std::endl;
|
|
||||||
rstime::rs_usleep(1000*1000);
|
|
||||||
}
|
|
||||||
delete _webui_thread;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool p3WebUI::restart()
|
|
||||||
{
|
|
||||||
RsDbg() << "Restarting web interface listening on port " << _webui_thread->listeningPort() << std::endl;
|
|
||||||
|
|
||||||
if(_webui_thread->isRunning())
|
|
||||||
_webui_thread->stop();
|
|
||||||
|
|
||||||
_webui_thread->start();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool p3WebUI::stop()
|
|
||||||
{
|
|
||||||
_webui_thread->stop();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void p3WebUI::setHtmlFilesDirectory(const std::string& html_dir)
|
void p3WebUI::setHtmlFilesDirectory(const std::string& html_dir)
|
||||||
{
|
{
|
||||||
_base_directory = html_dir;
|
_base_directory = html_dir;
|
||||||
}
|
}
|
||||||
void p3WebUI::setListeningPort(uint16_t port)
|
|
||||||
{
|
|
||||||
_webui_thread->setListeningPort(port);
|
|
||||||
|
|
||||||
if(_webui_thread->isRunning())
|
|
||||||
restart();
|
|
||||||
}
|
|
||||||
|
|
||||||
int p3WebUI::status() const
|
int p3WebUI::status() const
|
||||||
{
|
{
|
||||||
if(_webui_thread->isRunning())
|
if(isRunning())
|
||||||
return WEBUI_STATUS_RUNNING;
|
return WEBUI_STATUS_RUNNING;
|
||||||
else
|
else
|
||||||
return WEBUI_STATUS_NOT_RUNNING;
|
return WEBUI_STATUS_NOT_RUNNING;
|
||||||
|
|
|
@ -21,26 +21,25 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
#include "retroshare/rswebui.h"
|
#include "retroshare/rswebui.h"
|
||||||
|
#include "jsonapi/restbedservice.h"
|
||||||
|
|
||||||
class WebUIThread;
|
class p3WebUI: public RsWebUI, public RestbedService
|
||||||
|
|
||||||
class p3WebUI: public RsWebUI
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
p3WebUI();
|
p3WebUI(){}
|
||||||
virtual ~p3WebUI();
|
virtual ~p3WebUI(){}
|
||||||
|
|
||||||
virtual bool restart() override;
|
|
||||||
virtual bool stop() override;
|
|
||||||
|
|
||||||
virtual void setHtmlFilesDirectory(const std::string& html_dir) override;
|
virtual void setHtmlFilesDirectory(const std::string& html_dir) override;
|
||||||
virtual void setListeningPort(uint16_t port) override;
|
|
||||||
|
virtual bool restart() override { return RestbedService::restart();}
|
||||||
|
virtual bool stop() override { return RestbedService::stop();}
|
||||||
|
virtual void setListeningPort(uint16_t port) override { RestbedService::setListeningPort(port) ;}
|
||||||
|
|
||||||
virtual int status() const override;
|
virtual int status() const override;
|
||||||
|
virtual std::vector<std::shared_ptr<restbed::Resource> > getResources() const override;
|
||||||
private:
|
|
||||||
WebUIThread *_webui_thread;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue