1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-22 08:04:49 -05:00

Implemented arguments parser to pass web API host address and port

This commit is contained in:
f4exb 2017-11-18 05:06:43 +01:00
parent 382a825614
commit 1e73525280
9 changed files with 159 additions and 10 deletions

View File

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

View File

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

82
sdrbase/mainparser.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <QCommandLineOption>
#include <QRegExpValidator>
#include <QDebug>
#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;
}
}

46
sdrbase/mainparser.h Normal file
View File

@ -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 <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#ifndef SDRBASE_MAINPARSER_H_
#define SDRBASE_MAINPARSER_H_
#include <QCommandLineParser>
#include <stdint.h>
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_ */

View File

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

View File

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

View File

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

View File

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

View File

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