From 9a9e5815f93d7c11cbbf012848e6fe922491e1d4 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 18 Oct 2022 17:56:07 +0200 Subject: [PATCH] Move the server management in kiwixapp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `LocalKiwixServer` is "just" a UI to start/configure or stop the server. One important change is that instead of having one server we configure and start/stop when needed, we are now creating a new server each time we want to start the server. It doesn't change the behavior, as libkiwix is destroying/recreating for us, but it will help us to use the new API which use a `Server::Configuration`. --- src/kiwixapp.cpp | 24 ++++++++++++++++++++++-- src/kiwixapp.h | 6 ++++-- src/localkiwixserver.cpp | 27 +++++++++++++-------------- src/localkiwixserver.h | 1 - 4 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/kiwixapp.cpp b/src/kiwixapp.cpp index 37ebe0a..94e677d 100644 --- a/src/kiwixapp.cpp +++ b/src/kiwixapp.cpp @@ -33,7 +33,7 @@ KiwixApp::KiwixApp(int& argc, char *argv[]) mp_manager(nullptr), mp_mainWindow(nullptr), m_nameMapper(m_library.getKiwixLibrary(), false), - m_server(&m_library.getKiwixLibrary(), &m_nameMapper) + mp_server(nullptr) { try { m_translation.setTranslation(QLocale()); @@ -114,7 +114,9 @@ void KiwixApp::init() KiwixApp::~KiwixApp() { - m_server.stop(); + if (mp_server) { + mp_server->stop(); + } if (mp_downloader) { mp_downloader->close(); delete mp_downloader; @@ -456,6 +458,24 @@ void KiwixApp::updateNameMapper() m_nameMapper.update(); } +bool KiwixApp::runServer(const QString& ipAddress, int port) { + auto settings = getSettingsManager(); + if (mp_server) { + mp_server->stop(); + } + mp_server.reset(new kiwix::Server(&m_library.getKiwixLibrary(), &m_nameMapper)); + mp_server->setPort(port); + mp_server->setAddress(ipAddress.toStdString()); + settings->setKiwixServerPort(port); + settings->setKiwixServerIpAddress(ipAddress); + return mp_server->start(); +} + +void KiwixApp::stopServer() { + mp_server->stop(); + mp_server.reset(); +} + void KiwixApp::printVersions(std::ostream& out) { out << version.toStdString() << std::endl; out << "+ libqt (compile time) " << QT_VERSION_STR << std::endl; diff --git a/src/kiwixapp.h b/src/kiwixapp.h index 6042006..e84e531 100644 --- a/src/kiwixapp.h +++ b/src/kiwixapp.h @@ -79,12 +79,14 @@ public: TabBar* getTabWidget() { return getMainWindow()->getTabBar(); } QAction* getAction(Actions action); QString getLibraryDirectory() { return m_libraryDirectory; }; - kiwix::Server* getLocalServer() { return &m_server; } SettingsManager* getSettingsManager() { return &m_settingsManager; }; QString getText(const QString &key) { return m_translation.getText(key); }; void setMonitorDir(const QString &dir); bool isCurrentArticleBookmarked(); + bool runServer(const QString& ipAddress, int port); + void stopServer(); + public slots: void openZimFile(const QString& zimfile=""); void openUrl(const QString& url, bool newTab=true); @@ -109,7 +111,7 @@ private: MainWindow* mp_mainWindow; QErrorMessage* mp_errorDialog; kiwix::UpdatableNameMapper m_nameMapper; - kiwix::Server m_server; + std::unique_ptr mp_server; Translation m_translation; QFileSystemWatcher m_watcher; diff --git a/src/localkiwixserver.cpp b/src/localkiwixserver.cpp index c471250..2bd4a1d 100644 --- a/src/localkiwixserver.cpp +++ b/src/localkiwixserver.cpp @@ -20,7 +20,6 @@ LocalKiwixServer::LocalKiwixServer(QWidget *parent) : QString style(byteContent); setStyleSheet(style); - mp_server = KiwixApp::instance()->getLocalServer(); m_port = KiwixApp::instance()->getSettingsManager()->getKiwixServerPort(); connect(ui->KiwixServerButton, SIGNAL(clicked()), this, SLOT(runOrStopServer())); @@ -81,28 +80,28 @@ void LocalKiwixServer::openInBrowser() void LocalKiwixServer::runOrStopServer() { if (!m_active) { - auto settingsManager = KiwixApp::instance()->getSettingsManager(); m_port = ui->PortChooser->text().toInt(); - mp_server->setPort(m_port); - m_ipAddress = (ui->IpChooser->currentText() != gt("all")) ? ui->IpChooser->currentText() : "0.0.0.0"; - settingsManager->setKiwixServerPort(m_port); - settingsManager->setKiwixServerIpAddress(m_ipAddress); - mp_server->setAddress(m_ipAddress.toStdString()); - m_ipAddress = (m_ipAddress != "0.0.0.0") ? m_ipAddress : QString::fromStdString(kiwix::getBestPublicIp()); - ui->IpAddress->setText("http://" + m_ipAddress + ":" + QString::number(m_port)); - ui->IpAddress->setReadOnly(true); - if (!mp_server->start()) { + m_ipAddress = ui->IpChooser->currentText(); + + m_active = KiwixApp::instance()->runServer( + (m_ipAddress != gt("all") ? m_ipAddress : "0.0.0.0"), + m_port + ); + + if (!m_active) { QMessageBox messageBox; messageBox.critical(0,gt("error-title"),gt("error-launch-server-message")); - return; } - m_active = true; } else { - mp_server->stop(); + KiwixApp::instance()->stopServer(); m_active = false; } if (m_active) { + // Update UI to display how to acces the server + m_ipAddress = m_ipAddress != gt("all") ? m_ipAddress : QString::fromStdString(kiwix::getBestPublicIp()); + ui->IpAddress->setText("http://" + m_ipAddress + ":" + QString::number(m_port)); + ui->IpAddress->setReadOnly(true); ui->KiwixServerButton->setText(gt("stop-kiwix-server")); ui->KiwixServerText->setText(gt("kiwix-server-running-message")); ui->stackedWidget->setCurrentIndex(1); diff --git a/src/localkiwixserver.h b/src/localkiwixserver.h index 6f479fc..9f8ebba 100644 --- a/src/localkiwixserver.h +++ b/src/localkiwixserver.h @@ -22,7 +22,6 @@ public slots: private: Ui::LocalKiwixServer *ui; - kiwix::Server* mp_server; bool m_active = false; QString m_ipAddress; int m_port;