From f7595c64d6e69391637bb4258e57603395acb258 Mon Sep 17 00:00:00 2001 From: Ali Hatami Date: Sat, 8 Jun 2024 16:59:36 +0330 Subject: [PATCH] Add Network Provider --- model/include/parser/IParser.h | 2 + model/include/provider/IProvider.h | 20 +++++ model/include/provider/NetworkProvider.h | 40 ++++++++++ .../provider/NetworkProvider.h.autosave | 42 ++++++++++ model/model.pro | 6 +- model/src/provider/NetworkProvider.cpp | 80 +++++++++++++++++++ 6 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 model/include/provider/IProvider.h create mode 100644 model/include/provider/NetworkProvider.h create mode 100644 model/include/provider/NetworkProvider.h.autosave create mode 100644 model/src/provider/NetworkProvider.cpp diff --git a/model/include/parser/IParser.h b/model/include/parser/IParser.h index abc5a80..bdaf2db 100644 --- a/model/include/parser/IParser.h +++ b/model/include/parser/IParser.h @@ -14,4 +14,6 @@ public: virtual Log_ptr parse(const QString& input) const = 0; }; +using Parser_ptr = std::shared_ptr; + #endif //IPARSER diff --git a/model/include/provider/IProvider.h b/model/include/provider/IProvider.h new file mode 100644 index 0000000..a0e39df --- /dev/null +++ b/model/include/provider/IProvider.h @@ -0,0 +1,20 @@ +#ifndef IPROVIDER +#define IPROVIDER + +#include +#include "type/Log.h" + +class IDataProvider +{ +public: + virtual ~IDataProvider() + { + } + +signals: + virtual void provided(const Log_ptr log) = 0; +}; + +Q_DECLARE_INTERFACE(IDataProvider, "IDataProvider") //define this out of namespace scope + +#endif //IPROVIDER diff --git a/model/include/provider/NetworkProvider.h b/model/include/provider/NetworkProvider.h new file mode 100644 index 0000000..2fdd543 --- /dev/null +++ b/model/include/provider/NetworkProvider.h @@ -0,0 +1,40 @@ +#ifndef NETWORKPROVIDER +#define NETWORKPROVIDER + +#include +#include +#include "provider/IProvider.h" +#include "parser/IParser.h" + +class NetworkProvider : public IDataProvider +{ + Q_OBJECT + Q_INTERFACES(IDataProvider) + +public: + NetworkProvider(QHostAddress hostAddress, quint16 port); + void setParser(Parser_ptr parser); + void pauseServer(); + void resumeServer(); + void stopServer(); + +signals: + void provided(const Log_ptr log) override; + +//uncrustify off +private slots: +//uncrustify on + void onErrorOccured(QAbstractSocket::SocketError error); + void createNewConnection(); + +private: + bool _isRunning; + quint16 _port; + QHostAddress _address; + QTcpServer _server; + Parser_ptr _parser; + + void initServer(); +}; + +#endif //NEWTORKPROVIDER diff --git a/model/include/provider/NetworkProvider.h.autosave b/model/include/provider/NetworkProvider.h.autosave new file mode 100644 index 0000000..335f820 --- /dev/null +++ b/model/include/provider/NetworkProvider.h.autosave @@ -0,0 +1,42 @@ +#ifndef NETWORKPROVIDER +#define NETWORKPROVIDER + +#include +#include +#include "provider/IProvider.h" +#include "parser/IParser.h" + +class NetworkProvider : public IDataProvider +{ + Q_OBJECT + Q_INTERFACES(IDataProvider) + +public: + NetworkProvider(QHostAddress hostAddress, quint16 port); + void setParser(Parser_ptr parser); + void pauseServer(); + void resumeServer(); + void stopServer(); + +signals: + void provided(const Log_ptr log) override; + +//uncrustify off +private slots: +//uncrustify on + void onErrorOccured(QAbstractSocket::SocketError error); + void createNewConnection(); + +private: + bool _isRunning; + quint16 _port; + QHostAddress _address; + QTcpServer _server; + Parser_ptr _parser; + + void initServer(); + void resumeConnectedSessions(); + void stopConnectedSessions(); +}; + +#endif //NEWTORKPROVIDER diff --git a/model/model.pro b/model/model.pro index 774c13c..c1c9e72 100644 --- a/model/model.pro +++ b/model/model.pro @@ -1,5 +1,6 @@ TEMPLATE = lib +QT += network CONFIG += c++17 CONFIG += staticlib TARGET = model @@ -8,6 +9,8 @@ HEADERS += \ include/LogWarehouse.h \ include/parser/JsonParseConfig.h \ include/parser/JsonParser.h \ + include/provider/IProvider.h \ + include/provider/NetworkProvider.h \ include/type/Log.h \ include/type/ELogLevel.h \ include/type/MessagePart.h \ @@ -17,6 +20,7 @@ HEADERS += \ SOURCES += \ src/LogWarehouse.cpp \ - src/parser/JsonParser.cpp + src/parser/JsonParser.cpp \ + src/provider/NetworkProvider.cpp INCLUDEPATH += $$PWD/include diff --git a/model/src/provider/NetworkProvider.cpp b/model/src/provider/NetworkProvider.cpp new file mode 100644 index 0000000..6384869 --- /dev/null +++ b/model/src/provider/NetworkProvider.cpp @@ -0,0 +1,80 @@ +#include "provider/NetworkProvider.h" + +#include + +/*************************************************************************************************/ +NetworkProvider::NetworkProvider(QHostAddress hostAddress, quint16 port) +{ + _address = hostAddress; + _port = port; + _isRunning = false; + initServer(); +} + +/*************************************************************************************************/ +void NetworkProvider::setParser(Parser_ptr parser) +{ + _parser = parser; +} + +/*************************************************************************************************/ +void NetworkProvider::pauseServer() +{ + stopConnectedSessions(); + _server.pauseAccepting(); +} + +/*************************************************************************************************/ +void NetworkProvider::resumeServer() +{ + resumeConnectedSessions(); + _server.resumeAccepting(); +} + +/*************************************************************************************************/ +void NetworkProvider::stopServer() +{ + stopConnectedSessions(); + _server.close(); +} + +/*************************************************************************************************/ +void NetworkProvider::onErrorOccured(QAbstractSocket::SocketError error) +{ + //Handle Error +} + +/*************************************************************************************************/ +void NetworkProvider::createNewConnection() +{ +} + +/*************************************************************************************************/ +void NetworkProvider::initServer() +{ + QThread* thread = new QThread(); + _server.moveToThread(thread); + + QObject::connect(&_server, + &QTcpServer::newConnection, + this, + &NetworkProvider::createNewConnection); + + QObject::connect(&_server, + &QTcpServer::acceptError, + this, + &NetworkProvider::onErrorOccured); + _server.listen(_address, _port); +} + +/*************************************************************************************************/ +void NetworkProvider::resumeConnectedSessions() +{ + _isRunning = true; +} + +/*************************************************************************************************/ +void NetworkProvider::stopConnectedSessions() +{ + _isRunning = false; +}