move also transports into servermanager so we can share ssl config etc

This makes WebSocketServer pick up the global SSL configuration from
ServerManager.
This commit is contained in:
Michael Zanetti 2017-08-02 18:01:37 +02:00
parent 39e1807f94
commit 2b4972de52
6 changed files with 97 additions and 54 deletions

View File

@ -370,13 +370,13 @@ TimeManager *GuhCore::timeManager() const
/*! Returns a pointer to the \l{WebServer} instance owned by GuhCore.*/ /*! Returns a pointer to the \l{WebServer} instance owned by GuhCore.*/
WebServer *GuhCore::webServer() const WebServer *GuhCore::webServer() const
{ {
return m_webServer; return m_serverManager->webServer();
} }
/*! Returns a pointer to the \l{WebSocketServer} instance owned by GuhCore.*/ /*! Returns a pointer to the \l{WebSocketServer} instance owned by GuhCore.*/
WebSocketServer *GuhCore::webSocketServer() const WebSocketServer *GuhCore::webSocketServer() const
{ {
return m_webSocketServer; return m_serverManager->webSocketServer();
} }
/*! Returns a pointer to the \l{ServerManager} instance owned by GuhCore. */ /*! Returns a pointer to the \l{ServerManager} instance owned by GuhCore. */
@ -395,7 +395,7 @@ QStringList GuhCore::getAvailableLanguages()
/*! Returns a pointer to the \l{BluetoothServer} instance owned by GuhCore. */ /*! Returns a pointer to the \l{BluetoothServer} instance owned by GuhCore. */
BluetoothServer *GuhCore::bluetoothServer() const BluetoothServer *GuhCore::bluetoothServer() const
{ {
return m_bluetoothServer; return m_serverManager->bluetoothServer();
} }
/*! Returns a pointer to the \l{NetworkManager} instance owned by GuhCore. */ /*! Returns a pointer to the \l{NetworkManager} instance owned by GuhCore. */
@ -410,17 +410,15 @@ UserManager *GuhCore::userManager() const
} }
#ifdef TESTING_ENABLED #ifdef TESTING_ENABLED
MockTcpServer *GuhCore::tcpServer() const MockTcpServer
{
return m_tcpServer;
}
#else #else
/*! Returns a pointer to the \l{TcpServer} instance owned by GuhCore. */ /*! Returns a pointer to the \l{TcpServer} instance owned by GuhCore. */
TcpServer *GuhCore::tcpServer() const TcpServer
{
return m_tcpServer;
}
#endif #endif
*GuhCore::tcpServer() const
{
return m_serverManager->tcpServer();
}
/*! Constructs GuhCore with the given \a parent. This is private. /*! Constructs GuhCore with the given \a parent. This is private.
Use \l{GuhCore::instance()} to access the single instance.*/ Use \l{GuhCore::instance()} to access the single instance.*/
@ -443,26 +441,7 @@ GuhCore::GuhCore(QObject *parent) :
m_ruleEngine = new RuleEngine(this); m_ruleEngine = new RuleEngine(this);
qCDebug(dcApplication) << "Creating Server Manager"; qCDebug(dcApplication) << "Creating Server Manager";
m_serverManager = new ServerManager(this); m_serverManager = new ServerManager(m_configuration, this);
#ifdef TESTING_ENABLED
m_tcpServer = new MockTcpServer(this);
#else
m_tcpServer = new TcpServer(m_configuration->tcpServerAddress(), m_configuration->tcpServerPort(), this);
#endif
m_webSocketServer = new WebSocketServer(m_configuration->webSocketAddress(), m_configuration->webSocketPort(), m_configuration->sslEnabled(), this);
m_bluetoothServer = new BluetoothServer(this);
// Register transport interface in the JSON RPC server
m_serverManager->jsonServer()->registerTransportInterface(m_tcpServer);
m_serverManager->jsonServer()->registerTransportInterface(m_webSocketServer);
m_serverManager->jsonServer()->registerTransportInterface(m_bluetoothServer, m_configuration->bluetoothServerEnabled());
// Webserver setup
m_webServer = new WebServer(m_configuration->webServerAddress(), m_configuration->webServerPort(), m_configuration->webServerPublicFolder(), this);
m_serverManager->restServer()->registerWebserver(m_webServer);
// Create the NetworkManager // Create the NetworkManager
m_networkManager = new NetworkManager(this); m_networkManager = new NetworkManager(this);

View File

@ -33,17 +33,9 @@
#include "devicemanager.h" #include "devicemanager.h"
#include "ruleengine.h" #include "ruleengine.h"
#include "servermanager.h" #include "servermanager.h"
#include "websocketserver.h"
#include "bluetoothserver.h"
#include "time/timemanager.h" #include "time/timemanager.h"
#ifndef TESTING_ENABLED
#include "tcpserver.h"
#else
#include "mocktcpserver.h"
#endif
#include <QObject> #include <QObject>
class Device; class Device;
@ -51,6 +43,8 @@ class Device;
namespace guhserver { namespace guhserver {
class JsonRPCServer; class JsonRPCServer;
class WebSocketServer;
class TcpServer;
class LogEngine; class LogEngine;
class NetworkManager; class NetworkManager;
@ -128,15 +122,6 @@ private:
NetworkManager *m_networkManager; NetworkManager *m_networkManager;
UserManager *m_userManager; UserManager *m_userManager;
#ifdef TESTING_ENABLED
MockTcpServer *m_tcpServer;
#else
TcpServer *m_tcpServer;
#endif
WebSocketServer *m_webSocketServer;
WebServer *m_webServer;
BluetoothServer *m_bluetoothServer;
QHash<ActionId, Action> m_pendingActions; QHash<ActionId, Action> m_pendingActions;
private slots: private slots:

View File

@ -42,7 +42,7 @@
namespace guhserver { namespace guhserver {
/*! Constructs a \l{ServerManager} with the given \a parent. */ /*! Constructs a \l{ServerManager} with the given \a parent. */
ServerManager::ServerManager(QObject *parent) : ServerManager::ServerManager(GuhConfiguration* configuration, QObject *parent) :
QObject(parent), QObject(parent),
m_sslConfiguration(QSslConfiguration()) m_sslConfiguration(QSslConfiguration())
{ {
@ -56,7 +56,7 @@ ServerManager::ServerManager(QObject *parent) :
GuhSettings settings(GuhSettings::SettingsRoleGlobal); GuhSettings settings(GuhSettings::SettingsRoleGlobal);
qCDebug(dcConnection) << "Loading SSL-configuration from" << settings.fileName(); qCDebug(dcConnection) << "Loading SSL-configuration from" << settings.fileName();
settings.beginGroup("SSL-Configuration"); settings.beginGroup("SSL");
QString certificateFileName = settings.value("certificate", QVariant("/etc/ssl/certs/guhd-certificate.crt")).toString(); QString certificateFileName = settings.value("certificate", QVariant("/etc/ssl/certs/guhd-certificate.crt")).toString();
QString keyFileName = settings.value("certificate-key", QVariant("/etc/ssl/private/guhd-certificate.key")).toString(); QString keyFileName = settings.value("certificate-key", QVariant("/etc/ssl/private/guhd-certificate.key")).toString();
settings.endGroup(); settings.endGroup();
@ -70,9 +70,30 @@ ServerManager::ServerManager(QObject *parent) :
} }
} }
// Interfaces
m_jsonServer = new JsonRPCServer(m_sslConfiguration, this); m_jsonServer = new JsonRPCServer(m_sslConfiguration, this);
m_restServer = new RestServer(m_sslConfiguration, this); m_restServer = new RestServer(m_sslConfiguration, this);
// Transports
#ifdef TESTING_ENABLED
m_tcpServer = new MockTcpServer(this);
#else
m_tcpServer = new TcpServer(configuration->tcpServerAddress(), configuration->tcpServerPort(), this);
#endif
m_webSocketServer = new WebSocketServer(configuration->webSocketAddress(), configuration->webSocketPort(), configuration->sslEnabled(), m_sslConfiguration, this);
m_bluetoothServer = new BluetoothServer(this);
// Register transport interfaces for the JSON RPC server
m_jsonServer->registerTransportInterface(m_tcpServer);
m_jsonServer->registerTransportInterface(m_webSocketServer);
m_jsonServer->registerTransportInterface(m_bluetoothServer, configuration->bluetoothServerEnabled());
// Register transport itnerfaces for the Webserver
m_webServer = new WebServer(configuration->webServerAddress(), configuration->webServerPort(), configuration->webServerPublicFolder(), this);
m_restServer->registerWebserver(m_webServer);
} }
/*! Returns the pointer to the created \l{JsonRPCServer} in this \l{ServerManager}. */ /*! Returns the pointer to the created \l{JsonRPCServer} in this \l{ServerManager}. */
@ -87,6 +108,31 @@ RestServer *ServerManager::restServer() const
return m_restServer; return m_restServer;
} }
WebServer *ServerManager::webServer() const
{
return m_webServer;
}
WebSocketServer *ServerManager::webSocketServer() const
{
return m_webSocketServer;
}
BluetoothServer *ServerManager::bluetoothServer() const
{
return m_bluetoothServer;
}
#ifdef TESTING_ENABLED
MockTcpServer
#else
TcpServer
#endif
*ServerManager::tcpServer() const
{
return m_tcpServer;
}
bool ServerManager::loadCertificate(const QString &certificateKeyFileName, const QString &certificateFileName) bool ServerManager::loadCertificate(const QString &certificateKeyFileName, const QString &certificateFileName)
{ {
QFile certificateKeyFile(certificateKeyFileName); QFile certificateKeyFile(certificateKeyFileName);

View File

@ -26,6 +26,14 @@
#include "loggingcategories.h" #include "loggingcategories.h"
#include "jsonrpc/jsonrpcserver.h" #include "jsonrpc/jsonrpcserver.h"
#include "rest/restserver.h" #include "rest/restserver.h"
#include "websocketserver.h"
#include "bluetoothserver.h"
#ifndef TESTING_ENABLED
#include "tcpserver.h"
#else
#include "mocktcpserver.h"
#endif
class QSslConfiguration; class QSslConfiguration;
class QSslCertificate; class QSslCertificate;
@ -37,15 +45,39 @@ class ServerManager : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit ServerManager(QObject *parent = 0); explicit ServerManager(GuhConfiguration *configuration, QObject *parent = 0);
// Interfaces
JsonRPCServer *jsonServer() const; JsonRPCServer *jsonServer() const;
RestServer *restServer() const; RestServer *restServer() const;
// Transports
WebServer* webServer() const;
WebSocketServer* webSocketServer() const;
BluetoothServer* bluetoothServer() const;
#ifdef TESTING_ENABLED
MockTcpServer *tcpServer() const;
#else
TcpServer *tcpServer() const;
#endif
private: private:
// Interfaces
JsonRPCServer *m_jsonServer; JsonRPCServer *m_jsonServer;
RestServer *m_restServer; RestServer *m_restServer;
// Transports
#ifdef TESTING_ENABLED
MockTcpServer *m_tcpServer;
#else
TcpServer *m_tcpServer;
#endif
WebSocketServer *m_webSocketServer;
WebServer *m_webServer;
BluetoothServer *m_bluetoothServer;
// Encrytption and stuff
QSslConfiguration m_sslConfiguration; QSslConfiguration m_sslConfiguration;
QSslKey m_certificateKey; QSslKey m_certificateKey;
QSslCertificate m_certificate; QSslCertificate m_certificate;

View File

@ -60,11 +60,12 @@ namespace guhserver {
* *
* \sa ServerManager * \sa ServerManager
*/ */
WebSocketServer::WebSocketServer(const QHostAddress &address, const uint &port, const bool &sslEnabled, QObject *parent) : WebSocketServer::WebSocketServer(const QHostAddress &address, const uint &port, const bool &sslEnabled, const QSslConfiguration &sslConfiguration, QObject *parent) :
TransportInterface(parent), TransportInterface(parent),
m_server(0), m_server(0),
m_host(address), m_host(address),
m_port(port), m_port(port),
m_sslConfiguration(sslConfiguration),
m_useSsl(sslEnabled), m_useSsl(sslEnabled),
m_enabled(false) m_enabled(false)
{ {

View File

@ -42,7 +42,7 @@ class WebSocketServer : public TransportInterface
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit WebSocketServer(const QHostAddress &address, const uint &port, const bool &sslEnabled, QObject *parent = 0); explicit WebSocketServer(const QHostAddress &address, const uint &port, const bool &sslEnabled, const QSslConfiguration &sslConfiguration, QObject *parent = 0);
~WebSocketServer(); ~WebSocketServer();
void sendData(const QUuid &clientId, const QByteArray &data) override; void sendData(const QUuid &clientId, const QByteArray &data) override;