From 1e7352528012f6d156a09cca9083aaf30d57c1e3 Mon Sep 17 00:00:00 2001 From: f4exb Date: Sat, 18 Nov 2017 05:06:43 +0100 Subject: [PATCH] Implemented arguments parser to pass web API host address and port --- app/main.cpp | 4 +- sdrbase/CMakeLists.txt | 4 ++ sdrbase/mainparser.cpp | 82 +++++++++++++++++++++++++++++++++ sdrbase/mainparser.h | 46 ++++++++++++++++++ sdrbase/sdrbase.pro | 6 ++- sdrbase/webapi/webapiserver.cpp | 16 +++++-- sdrbase/webapi/webapiserver.h | 4 +- sdrgui/mainwindow.cpp | 4 +- sdrgui/mainwindow.h | 3 +- 9 files changed, 159 insertions(+), 10 deletions(-) create mode 100644 sdrbase/mainparser.cpp create mode 100644 sdrbase/mainparser.h diff --git a/app/main.cpp b/app/main.cpp index 123e43408..7d277aa1a 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -90,8 +90,10 @@ static int runQtApplication(int argc, char* argv[], qtwebapp::LoggerWithFile *lo #endif #endif + MainParser parser; + parser.parse(*qApp); - MainWindow w(logger); + MainWindow w(logger, parser); w.show(); return a.exec(); diff --git a/sdrbase/CMakeLists.txt b/sdrbase/CMakeLists.txt index ed2b48c02..252b74352 100644 --- a/sdrbase/CMakeLists.txt +++ b/sdrbase/CMakeLists.txt @@ -67,6 +67,8 @@ set(sdrbase_SOURCES webapi/webapirequestmapper.cpp webapi/webapiserver.cpp + + mainparser.cpp ) set(sdrbase_HEADERS @@ -157,6 +159,8 @@ set(sdrbase_HEADERS webapi/webapiadapterinterface.h webapi/webapirequestmapper.h webapi/webapiserver + + mainparser.h ) set(sdrbase_SOURCES diff --git a/sdrbase/mainparser.cpp b/sdrbase/mainparser.cpp new file mode 100644 index 000000000..47f9cb867 --- /dev/null +++ b/sdrbase/mainparser.cpp @@ -0,0 +1,82 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2017 F4EXB // +// written by Edouard Griffiths // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include + +#include "mainparser.h" + +MainParser::MainParser() : + m_serverAddressOption(QStringList() << "a" << "api-address", + "Web API server address.", + "address", + "127.0.0.1"), + m_serverPortOption(QStringList() << "p" << "api-port", + "Web API server port.", + "port", + "8001") +{ + m_serverAddress = "127.0.0.1"; + m_serverPort = 8001; + + m_parser.setApplicationDescription("Software Defined Radio application"); + m_parser.addHelpOption(); + m_parser.addVersionOption(); + + m_parser.addOption(m_serverAddressOption); + m_parser.addOption(m_serverPortOption); +} + +MainParser::~MainParser() +{ } + +void MainParser::parse(const QCoreApplication& app) +{ + m_parser.process(app); + + int pos; + bool ok; + + // server address + + QString serverAddress = m_parser.value(m_serverAddressOption); + + QString ipRange = "(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])"; + QRegExp ipRegex ("^" + ipRange + + "\\." + ipRange + + "\\." + ipRange + + "\\." + ipRange + "$"); + QRegExpValidator ipValidator(ipRegex); + + if (ipValidator.validate(serverAddress, pos) == QValidator::Acceptable) { + m_serverAddress = serverAddress; + } else { + qWarning() << "MainParser::parse: server address invalid. Defaulting to " << m_serverAddress; + } + + // server port + + QString serverPortStr = m_parser.value(m_serverPortOption); + int serverPort = serverPortStr.toInt(&ok); + + if (ok && (serverPort > 1023) && (serverPort < 65536)) { + m_serverPort = serverPort; + } else { + qWarning() << "MainParser::parse: server port invalid. Defaulting to " << m_serverPort; + } +} diff --git a/sdrbase/mainparser.h b/sdrbase/mainparser.h new file mode 100644 index 000000000..3d0c19765 --- /dev/null +++ b/sdrbase/mainparser.h @@ -0,0 +1,46 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2017 F4EXB // +// written by Edouard Griffiths // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#ifndef SDRBASE_MAINPARSER_H_ +#define SDRBASE_MAINPARSER_H_ + +#include +#include + +class MainParser +{ +public: + MainParser(); + ~MainParser(); + + void parse(const QCoreApplication& app); + + const QString& getServerAddress() const { return m_serverAddress; } + uint16_t getServerPort() const { return m_serverPort; } + +private: + QString m_serverAddress; + uint16_t m_serverPort; + + QCommandLineParser m_parser; + QCommandLineOption m_serverAddressOption; + QCommandLineOption m_serverPortOption; +}; + + + +#endif /* SDRBASE_MAINPARSER_H_ */ diff --git a/sdrbase/sdrbase.pro b/sdrbase/sdrbase.pro index ce2e6ce5c..87734b143 100644 --- a/sdrbase/sdrbase.pro +++ b/sdrbase/sdrbase.pro @@ -106,7 +106,8 @@ SOURCES += audio/audiodeviceinfo.cpp\ plugin/pluginapi.cpp\ plugin/pluginmanager.cpp\ webapi/webapirequestmapper.cpp\ - webapi/webapiserver.cpp + webapi/webapiserver.cpp\ + mainparser.cpp HEADERS += audio/audiodeviceinfo.h\ audio/audiofifo.h\ @@ -185,7 +186,8 @@ HEADERS += audio/audiodeviceinfo.h\ util/simpleserializer.h\ webapi/webapiadapterinterface.h\ webapi/webapirequestmapper.h\ - webapi/webapiserver.h + webapi/webapiserver.h\ + mainparser.h !macx:LIBS += -L../serialdv/$${build_subdir} -lserialdv LIBS += -L../httpserver/$${build_subdir} -lhttpserver diff --git a/sdrbase/webapi/webapiserver.cpp b/sdrbase/webapi/webapiserver.cpp index c36ffc5ea..d17136643 100644 --- a/sdrbase/webapi/webapiserver.cpp +++ b/sdrbase/webapi/webapiserver.cpp @@ -22,12 +22,12 @@ #include "webapirequestmapper.h" #include "webapiserver.h" -WebAPIServer::WebAPIServer(WebAPIRequestMapper *requestMapper) : +WebAPIServer::WebAPIServer(const QString& host, uint16_t port, WebAPIRequestMapper *requestMapper) : m_requestMapper(requestMapper), m_listener(0) { - m_settings.host = "127.0.0.1"; - m_settings.port = 8001; + m_settings.host = host; + m_settings.port = port; } WebAPIServer::~WebAPIServer() @@ -40,6 +40,7 @@ void WebAPIServer::start() if (!m_listener) { m_listener = new qtwebapp::HttpListener(m_settings, m_requestMapper, qApp); + qInfo("WebAPIServer::start: starting web API server at http://%s:%d", qPrintable(m_settings.host), m_settings.port); } } @@ -49,5 +50,14 @@ void WebAPIServer::stop() { delete m_listener; m_listener = 0; + qInfo("WebAPIServer::stop: stopped web API server at http://%s:%d", qPrintable(m_settings.host), m_settings.port); } } + +void WebAPIServer::setHostAndPort(const QString& host, uint16_t port) +{ + stop(); + m_settings.host = host; + m_settings.port = port; + m_listener = new qtwebapp::HttpListener(m_settings, m_requestMapper, qApp); +} diff --git a/sdrbase/webapi/webapiserver.h b/sdrbase/webapi/webapiserver.h index 06da3a42a..8c5b75312 100644 --- a/sdrbase/webapi/webapiserver.h +++ b/sdrbase/webapi/webapiserver.h @@ -30,12 +30,14 @@ class WebAPIRequestMapper; class WebAPIServer { public: - WebAPIServer(WebAPIRequestMapper *requestMapper); + WebAPIServer(const QString& host, uint16_t port, WebAPIRequestMapper *requestMapper); ~WebAPIServer(); void start(); void stop(); + void setHostAndPort(const QString& host, uint16_t port); + private: WebAPIRequestMapper *m_requestMapper; qtwebapp::HttpListener *m_listener; diff --git a/sdrgui/mainwindow.cpp b/sdrgui/mainwindow.cpp index 435879b12..307127abf 100644 --- a/sdrgui/mainwindow.cpp +++ b/sdrgui/mainwindow.cpp @@ -63,7 +63,7 @@ MainWindow *MainWindow::m_instance = 0; -MainWindow::MainWindow(qtwebapp::LoggerWithFile *logger, QWidget* parent) : +MainWindow::MainWindow(qtwebapp::LoggerWithFile *logger, const MainParser& parser, QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow), m_settings(), @@ -198,7 +198,7 @@ MainWindow::MainWindow(qtwebapp::LoggerWithFile *logger, QWidget* parent) : connect(ui->tabInputsView, SIGNAL(currentChanged(int)), this, SLOT(tabInputViewIndexChanged())); m_requestMapper = new WebAPIRequestMapper(qApp); - m_apiServer = new WebAPIServer(m_requestMapper); + m_apiServer = new WebAPIServer(parser.getServerAddress(), parser.getServerPort(), m_requestMapper); m_apiServer->start(); qDebug() << "MainWindow::MainWindow: end"; diff --git a/sdrgui/mainwindow.h b/sdrgui/mainwindow.h index 6d77c5fb8..fde50beff 100644 --- a/sdrgui/mainwindow.h +++ b/sdrgui/mainwindow.h @@ -25,6 +25,7 @@ #include "settings/mainsettings.h" #include "util/messagequeue.h" #include "util/export.h" +#include "mainparser.h" class QLabel; class QTreeWidgetItem; @@ -64,7 +65,7 @@ class SDRANGEL_API MainWindow : public QMainWindow { Q_OBJECT public: - explicit MainWindow(qtwebapp::LoggerWithFile *logger, QWidget* parent = 0); + explicit MainWindow(qtwebapp::LoggerWithFile *logger, const MainParser& parser, QWidget* parent = 0); ~MainWindow(); static MainWindow *getInstance() { return m_instance; } // Main Window is de facto a singleton so this just returns its reference