Browse Source

Add Network Provider

basic-view
Ali Hatami Tajik 6 months ago
parent
commit
f7595c64d6
  1. 2
      model/include/parser/IParser.h
  2. 20
      model/include/provider/IProvider.h
  3. 40
      model/include/provider/NetworkProvider.h
  4. 42
      model/include/provider/NetworkProvider.h.autosave
  5. 6
      model/model.pro
  6. 80
      model/src/provider/NetworkProvider.cpp

2
model/include/parser/IParser.h

@ -14,4 +14,6 @@ public:
virtual Log_ptr parse(const QString& input) const = 0; virtual Log_ptr parse(const QString& input) const = 0;
}; };
using Parser_ptr = std::shared_ptr<IParser>;
#endif //IPARSER #endif //IPARSER

20
model/include/provider/IProvider.h

@ -0,0 +1,20 @@
#ifndef IPROVIDER
#define IPROVIDER
#include <QObject>
#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

40
model/include/provider/NetworkProvider.h

@ -0,0 +1,40 @@
#ifndef NETWORKPROVIDER
#define NETWORKPROVIDER
#include <QHostAddress>
#include <QTcpServer>
#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

42
model/include/provider/NetworkProvider.h.autosave

@ -0,0 +1,42 @@
#ifndef NETWORKPROVIDER
#define NETWORKPROVIDER
#include <QHostAddress>
#include <QTcpServer>
#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

6
model/model.pro

@ -1,5 +1,6 @@
TEMPLATE = lib TEMPLATE = lib
QT += network
CONFIG += c++17 CONFIG += c++17
CONFIG += staticlib CONFIG += staticlib
TARGET = model TARGET = model
@ -8,6 +9,8 @@ HEADERS += \
include/LogWarehouse.h \ include/LogWarehouse.h \
include/parser/JsonParseConfig.h \ include/parser/JsonParseConfig.h \
include/parser/JsonParser.h \ include/parser/JsonParser.h \
include/provider/IProvider.h \
include/provider/NetworkProvider.h \
include/type/Log.h \ include/type/Log.h \
include/type/ELogLevel.h \ include/type/ELogLevel.h \
include/type/MessagePart.h \ include/type/MessagePart.h \
@ -17,6 +20,7 @@ HEADERS += \
SOURCES += \ SOURCES += \
src/LogWarehouse.cpp \ src/LogWarehouse.cpp \
src/parser/JsonParser.cpp src/parser/JsonParser.cpp \
src/provider/NetworkProvider.cpp
INCLUDEPATH += $$PWD/include INCLUDEPATH += $$PWD/include

80
model/src/provider/NetworkProvider.cpp

@ -0,0 +1,80 @@
#include "provider/NetworkProvider.h"
#include <QThread>
/*************************************************************************************************/
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;
}
Loading…
Cancel
Save