mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-02 14:16:16 -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;
|
||||
|
||||
// /**
|
||||
// * Pointer to global instance of JsonApiServer
|
||||
// * @jsonapi{development}
|
||||
// */
|
||||
// extern JsonApiServer* jsonApiServer;
|
||||
/**
|
||||
* Pointer to global instance of JsonApiServer
|
||||
* @jsonapi{development}
|
||||
*/
|
||||
extern JsonApiServer* jsonApiServer;
|
||||
|
||||
/**
|
||||
* Simple usage
|
||||
|
@ -236,6 +236,7 @@ private:
|
|||
serviceInstance.get(), serviceName, ctx, session );
|
||||
}
|
||||
|
||||
JsonApiServer *_instance;
|
||||
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;
|
||||
};
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue