Move the server management in kiwixapp.

`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`.
This commit is contained in:
Matthieu Gautier 2022-10-18 17:56:07 +02:00
parent 30b242f91c
commit 9a9e5815f9
4 changed files with 39 additions and 19 deletions

View File

@ -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;

View File

@ -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<kiwix::Server> mp_server;
Translation m_translation;
QFileSystemWatcher m_watcher;

View File

@ -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);

View File

@ -22,7 +22,6 @@ public slots:
private:
Ui::LocalKiwixServer *ui;
kiwix::Server* mp_server;
bool m_active = false;
QString m_ipAddress;
int m_port;