2017-03-29 10:18:37 +00:00
|
|
|
#include "webserverthread.h"
|
2017-04-01 01:50:14 +00:00
|
|
|
#include "qrcore.h"
|
|
|
|
#include <cassert>
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-04-01 01:50:14 +00:00
|
|
|
WebServerThread::WebServerThread(QRCore *core, QObject *parent) :
|
|
|
|
QThread(parent),
|
|
|
|
core(core),
|
|
|
|
started(false)
|
2017-03-29 10:18:37 +00:00
|
|
|
{
|
|
|
|
// MEOW
|
|
|
|
}
|
|
|
|
|
2017-04-01 01:50:14 +00:00
|
|
|
WebServerThread::~WebServerThread()
|
|
|
|
{
|
|
|
|
if (isRunning()) {
|
|
|
|
quit();
|
|
|
|
wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebServerThread::startServer()
|
|
|
|
{
|
|
|
|
assert(nullptr != core);
|
|
|
|
|
|
|
|
if (!isRunning() && !started) {
|
|
|
|
QThread::start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebServerThread::stopServer()
|
|
|
|
{
|
|
|
|
assert(nullptr != core);
|
|
|
|
|
|
|
|
if (!isRunning() && started)
|
|
|
|
{
|
|
|
|
QThread::start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebServerThread::isStarted() const
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&mutex);
|
|
|
|
return started;
|
|
|
|
}
|
|
|
|
|
2017-03-29 10:18:37 +00:00
|
|
|
void WebServerThread::run() {
|
2017-04-01 01:50:14 +00:00
|
|
|
QMutexLocker locker(&mutex);
|
|
|
|
|
|
|
|
if (core == nullptr)
|
2017-03-29 10:18:37 +00:00
|
|
|
return;
|
|
|
|
//eprintf ("Starting webserver!");
|
2017-04-01 01:50:14 +00:00
|
|
|
|
|
|
|
toggleWebServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebServerThread::toggleWebServer()
|
|
|
|
{
|
|
|
|
// access already locked
|
|
|
|
|
|
|
|
// see libr/core/rtr.c
|
|
|
|
// "=h", " port", "listen for http connections (r2 -qc=H /bin/ls)",
|
|
|
|
// "=h-", "", "stop background webserver",
|
|
|
|
// "=h*", "", "restart current webserver",
|
|
|
|
// "=h&", " port", "start http server in background)",
|
|
|
|
|
|
|
|
if (started) {
|
|
|
|
// after this the only reaction to this commands is:
|
|
|
|
// sandbox: connect disabled
|
|
|
|
// and the webserver is still running
|
|
|
|
// TODO: find out why
|
|
|
|
core->cmd("=h-");
|
|
|
|
} else {
|
|
|
|
core->cmd("=h&");
|
|
|
|
}
|
|
|
|
|
|
|
|
// cmd has no usefull return value for this commands, so just toogle the state
|
|
|
|
started = !started;
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|