diff --git a/src/webserverthread.cpp b/src/webserverthread.cpp index 65cb87e1..8be19e3f 100644 --- a/src/webserverthread.cpp +++ b/src/webserverthread.cpp @@ -1,14 +1,78 @@ #include "webserverthread.h" +#include "qrcore.h" +#include -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; } diff --git a/src/webserverthread.h b/src/webserverthread.h index 1a8dee7b..5df49cf2 100644 --- a/src/webserverthread.h +++ b/src/webserverthread.h @@ -2,20 +2,32 @@ #define WEBSERVERTHREAD_H #include -#include "qrcore.h" +#include + +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