fixes some problems with WebServerThrad

- use -h& command so the thread does end
- set the QRCore in the constructor
- try to disable the websever with =h-
This commit is contained in:
ballessay 2017-04-01 03:50:14 +02:00
parent c42faba81c
commit 1f36c55f6a
2 changed files with 85 additions and 9 deletions

View File

@ -1,14 +1,78 @@
#include "webserverthread.h"
#include "qrcore.h"
#include <cassert>
WebServerThread::WebServerThread(QObject *parent) :
QThread(parent)
WebServerThread::WebServerThread(QRCore *core, QObject *parent) :
QThread(parent),
core(core),
started(false)
{
// MEOW
}
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;
}
void WebServerThread::run() {
if (core == NULL)
QMutexLocker locker(&mutex);
if (core == nullptr)
return;
//eprintf ("Starting webserver!");
core->cmd ("=h");
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;
}

View File

@ -2,20 +2,32 @@
#define WEBSERVERTHREAD_H
#include <QThread>
#include "qrcore.h"
#include <QMutex>
class QRCore;
class WebServerThread : public QThread
{
Q_OBJECT
public:
QRCore *core;
explicit WebServerThread(QObject *parent = 0);
signals:
explicit WebServerThread(QRCore *core, QObject *parent = 0);
~WebServerThread();
void startServer();
void stopServer();
bool isStarted() const;
public slots:
private:
void run();
using QThread::start;
void toggleWebServer();
mutable QMutex mutex;
QRCore *core;
bool started;
};
#endif // WEBSERVERTHREAD_H