mirror of
https://github.com/QuasarApp/Patronum.git
synced 2025-04-28 00:24:31 +00:00
added Feature class
This commit is contained in:
parent
34c0fc513b
commit
f5efdb9a50
@ -11,7 +11,6 @@ project(Patronum LANGUAGES CXX)
|
||||
add_subdirectory(qt-solutions/qtservice)
|
||||
|
||||
include(qt-solutions/CMake/ProjectOut.cmake)
|
||||
include(qt-solutions/CMake/ccache.cmake)
|
||||
include(qt-solutions/CMake/Version.cmake)
|
||||
|
||||
|
||||
|
@ -1,9 +1,13 @@
|
||||
#include "controller.h"
|
||||
#include "serviceprivate.h"
|
||||
namespace Patronum {
|
||||
|
||||
Controller::Controller(const QString &name):
|
||||
QtServiceController(name)
|
||||
{
|
||||
QtServiceController(name) {
|
||||
d_ptr = new ServicePrivate(name, this);
|
||||
}
|
||||
|
||||
void Controller::handleFeatures(const QList<Feature> &features) {
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,14 +1,25 @@
|
||||
#ifndef CONTROLLER_H
|
||||
#define CONTROLLER_H
|
||||
#include "Patronum_global.h"
|
||||
#include "icontroller.h"
|
||||
#include <qtservice.h>
|
||||
|
||||
namespace Patronum {
|
||||
|
||||
class PATRONUM_LIBRARYSHARED_EXPORT Controller : public QtServiceController
|
||||
class ServicePrivate;
|
||||
|
||||
class PATRONUM_LIBRARYSHARED_EXPORT Controller : public QtServiceController, protected IController
|
||||
{
|
||||
public:
|
||||
Controller(const QString & name);
|
||||
Controller(const QString& name);
|
||||
|
||||
// IControler interface
|
||||
protected:
|
||||
void handleFeatures(const QList<Feature> &features);
|
||||
|
||||
private:
|
||||
ServicePrivate * d_ptr = nullptr;
|
||||
|
||||
};
|
||||
}
|
||||
#endif // CONTROLLER_H
|
||||
|
39
Patronum/src/feature.cpp
Normal file
39
Patronum/src/feature.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
#include "feature.h"
|
||||
|
||||
#include <QDataStream>
|
||||
|
||||
namespace Patronum {
|
||||
|
||||
Feature::Feature(const QString &cmd, const QVariantList &arg) {
|
||||
_cmd = cmd;
|
||||
_arg = arg;
|
||||
}
|
||||
|
||||
QString Feature::cmd() const {
|
||||
return _cmd;
|
||||
}
|
||||
|
||||
void Feature::setCmd(const QString &cmd) {
|
||||
_cmd = cmd;
|
||||
}
|
||||
|
||||
QVariantList Feature::arg() const {
|
||||
return _arg;
|
||||
}
|
||||
|
||||
void Feature::setArg(const QVariantList &arg) {
|
||||
_arg = arg;
|
||||
}
|
||||
|
||||
QDataStream &operator<<(QDataStream &stream, const Feature &obj) {
|
||||
stream << obj._cmd << obj._arg;
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
QDataStream &operator>>(QDataStream &stream, Feature &obj) {
|
||||
stream >> obj._cmd >> obj._arg;
|
||||
return stream;
|
||||
}
|
||||
|
||||
}
|
36
Patronum/src/feature.h
Normal file
36
Patronum/src/feature.h
Normal file
@ -0,0 +1,36 @@
|
||||
#ifndef FEaTURE_H
|
||||
#define FEaTURE_H
|
||||
|
||||
#include <QString>
|
||||
#include <QVariantList>
|
||||
#include <QList>
|
||||
#include <Patronum_global.h>
|
||||
|
||||
namespace Patronum {
|
||||
|
||||
/**
|
||||
* @brief The Feature class - it is atomic type for describe service command
|
||||
*/
|
||||
|
||||
class PATRONUM_LIBRARYSHARED_EXPORT Feature
|
||||
{
|
||||
public:
|
||||
Feature() = default;
|
||||
Feature(const QString& cmd, const QVariantList& arg);
|
||||
QString cmd() const;
|
||||
void setCmd(const QString &cmd);
|
||||
|
||||
QVariantList arg() const;
|
||||
void setArg(const QVariantList &arg);
|
||||
|
||||
friend QDataStream& operator<<(QDataStream& stream, const Feature& obj);
|
||||
friend QDataStream& operator>>(QDataStream& stream, Feature& obj);
|
||||
|
||||
private:
|
||||
QString _cmd;
|
||||
QVariantList _arg;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
#endif // FEaTURE_H
|
1
Patronum/src/icontroller.cpp
Normal file
1
Patronum/src/icontroller.cpp
Normal file
@ -0,0 +1 @@
|
||||
#include "icontroller.h"
|
19
Patronum/src/icontroller.h
Normal file
19
Patronum/src/icontroller.h
Normal file
@ -0,0 +1,19 @@
|
||||
#ifndef ICONTROLER_H
|
||||
#define ICONTROLER_H
|
||||
|
||||
#include <QList>
|
||||
|
||||
|
||||
namespace Patronum {
|
||||
|
||||
class Feature;
|
||||
|
||||
class IController
|
||||
{
|
||||
public:
|
||||
IController();
|
||||
virtual void handleFeatures(const QList<Feature>& features) = 0;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // ICONTROLER_H
|
8
Patronum/src/iservice.cpp
Normal file
8
Patronum/src/iservice.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
#include "iservice.h"
|
||||
namespace Patronum {
|
||||
|
||||
IService::IService()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
20
Patronum/src/iservice.h
Normal file
20
Patronum/src/iservice.h
Normal file
@ -0,0 +1,20 @@
|
||||
#ifndef ISERVICE_H
|
||||
#define ISERVICE_H
|
||||
|
||||
#include <QList>
|
||||
|
||||
namespace Patronum {
|
||||
|
||||
class Feature;
|
||||
|
||||
class IService
|
||||
{
|
||||
public:
|
||||
IService();
|
||||
|
||||
virtual void handleReceve(const Feature& data) = 0;
|
||||
virtual QList<Feature> supportedFeatures() = 0;
|
||||
|
||||
};
|
||||
}
|
||||
#endif // ISERVICE_H
|
3
Patronum/src/package.cpp
Normal file
3
Patronum/src/package.cpp
Normal file
@ -0,0 +1,3 @@
|
||||
#include "package.h"
|
||||
|
||||
|
22
Patronum/src/package.h
Normal file
22
Patronum/src/package.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef PACKAGE_H
|
||||
#define PACKAGE_H
|
||||
|
||||
#include <QDataStream>
|
||||
|
||||
namespace Patronum {
|
||||
|
||||
enum class Command: char {
|
||||
FeaturesRequest,
|
||||
Features,
|
||||
Feature
|
||||
};
|
||||
|
||||
struct Package
|
||||
{
|
||||
Command cmd;
|
||||
char* data;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // PACKAGE_H
|
@ -1,14 +1,36 @@
|
||||
#ifndef SERVICE_H
|
||||
#define SERVICE_H
|
||||
#include "Patronum_global.h"
|
||||
#include "feature.h"
|
||||
#include "iservice.h"
|
||||
#include "localsocket.h"
|
||||
#include "package.h"
|
||||
#include "serviceprivate.h"
|
||||
#include <qtservice.h>
|
||||
|
||||
namespace Patronum {
|
||||
|
||||
template<class Application>
|
||||
class PATRONUM_LIBRARYSHARED_EXPORT Service : public QtService<Application>
|
||||
class PATRONUM_LIBRARYSHARED_EXPORT Service : public QtService<Application>, protected IService
|
||||
{
|
||||
public:
|
||||
Service();
|
||||
Service(int argc, char **argv, const QString &name)
|
||||
: QtService<Application>(argc, argv, name) {
|
||||
d_ptr = new ServicePrivate(name, nullptr, this);
|
||||
|
||||
}
|
||||
// IService interface
|
||||
protected:
|
||||
void handleReceve(const Feature &data) {
|
||||
Q_UNUSED(data)
|
||||
};
|
||||
|
||||
QList<Feature> supportedFeatures() {
|
||||
return QList<Feature>();
|
||||
}
|
||||
|
||||
private:
|
||||
ServicePrivate *d_ptr = nullptr;
|
||||
};
|
||||
|
||||
}
|
||||
|
96
Patronum/src/serviceprivate.cpp
Normal file
96
Patronum/src/serviceprivate.cpp
Normal file
@ -0,0 +1,96 @@
|
||||
#include "serviceprivate.h"
|
||||
|
||||
#include "iservice.h"
|
||||
#include "icontroller.h"
|
||||
#include "localsocket.h"
|
||||
#include "package.h"
|
||||
|
||||
namespace Patronum {
|
||||
|
||||
Patronum::ServicePrivate::ServicePrivate(const QString &name, IController *controller, IService *service, QObject *parent):
|
||||
QObject(parent) {
|
||||
_socket = new LocalSocket(name);
|
||||
_service = service;
|
||||
_controller = controller;
|
||||
|
||||
QObject::connect(_socket, &LocalSocket::sigReceve,
|
||||
this, &ServicePrivate::handleReceve);
|
||||
|
||||
}
|
||||
|
||||
void ServicePrivate::handleReceve(QByteArray data) {
|
||||
|
||||
if (data.size() < 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
const Package *package = reinterpret_cast<const Package *>( data.data());
|
||||
|
||||
switch (package->cmd) {
|
||||
|
||||
case Command::FeaturesRequest: {
|
||||
|
||||
if (!_service) {
|
||||
// to-do logs
|
||||
break;
|
||||
}
|
||||
|
||||
if (!_socket->isValid()) {
|
||||
// to-do logs
|
||||
break;
|
||||
}
|
||||
|
||||
QList<Feature> features = _service->supportedFeatures();
|
||||
QByteArray sendData;
|
||||
QDataStream stream(&sendData, QIODevice::WriteOnly);
|
||||
|
||||
stream << Command::Features << features;
|
||||
if (!_socket->send(sendData)) {
|
||||
// to-do logs
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
case Command::Features: {
|
||||
|
||||
if (!_controller) {
|
||||
// to-do logs
|
||||
break;
|
||||
}
|
||||
|
||||
QDataStream stream(package->data);
|
||||
|
||||
QList<Feature> features;
|
||||
stream >> features;
|
||||
|
||||
_controller->handleFeatures(features);
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
case Command::Feature: {
|
||||
if (!_service) {
|
||||
// to-do logs
|
||||
break;
|
||||
}
|
||||
|
||||
QDataStream stream(package->data);
|
||||
|
||||
Feature feature;
|
||||
stream >> feature;
|
||||
_service->handleReceve(feature);
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
default: {
|
||||
// to-do add logs
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
32
Patronum/src/serviceprivate.h
Normal file
32
Patronum/src/serviceprivate.h
Normal file
@ -0,0 +1,32 @@
|
||||
#ifndef SERVICEPRIVATE_H
|
||||
#define SERVICEPRIVATE_H
|
||||
#include <QObject>
|
||||
#include <feature.h>
|
||||
|
||||
namespace Patronum {
|
||||
|
||||
class LocalSocket;
|
||||
class IService;
|
||||
class IController;
|
||||
|
||||
class ServicePrivate : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ServicePrivate(const QString& name, IController* controller = nullptr,
|
||||
IService* service = nullptr, QObject *parent = nullptr);
|
||||
|
||||
signals:
|
||||
void sigListFeatures(QList<Feature>);
|
||||
|
||||
private:
|
||||
LocalSocket *_socket = nullptr;
|
||||
IService *_service = nullptr;
|
||||
IController *_controller = nullptr;
|
||||
|
||||
private slots:
|
||||
void handleReceve(QByteArray data);
|
||||
};
|
||||
|
||||
}
|
||||
#endif // SERVICEPRIVATE_H
|
Loading…
x
Reference in New Issue
Block a user