C++17/20 interface
Daggy Core C++ interface

The Core object interface

Description

The Core class is a QObject. It aggregates all streams from providers into once Core Streams Session.
The Core class is a main class of Daggy Core library and can be included by:
1
#include <DaggyCore/Core.hpp>
Copied!

Constructor

1
Core(Sources sources,
2
QObject* parent = nullptr);
3
Core(QString session,
4
Sources sources,
5
QObject* parent = nullptr);
Copied!
Session is id for Core Streams Session. If no session was passed to constructor - new session id will generate (in uuid format).
Sources is required parameter. It contains provider commands for streaming:
1
namespace daggy {
2
namespace sources {
3
namespace commands {
4
namespace streams {
5
struct Meta {
6
QString session;
7
std::chrono::time_point<std::chrono::system_clock> start_time;
8
QString extension;
9
DaggyStreamTypes type;
10
11
std::uint64_t seq_num;
12
std::chrono::time_point<std::chrono::system_clock> time;
13
};
14
}
15
16
struct Stream {
17
streams::Meta meta;
18
QByteArray part;
19
};
20
21
struct DAGGYCORE_EXPORT Properties {
22
QString extension;
23
QString exec;
24
QVariantMap parameters = {};
25
bool restart = false;
26
27
bool operator==(const Properties& other) const;
28
};
29
}
30
31
using Commands = QMap<QString, commands::Properties>;
32
using Command = QPair<QString, commands::Properties>;
33
34
struct DAGGYCORE_EXPORT Properties {
35
QString type;
36
QString host;
37
Commands commands;
38
bool reconnect = false;
39
QVariantMap parameters = {};
40
41
bool operator==(const Properties& other) const;
42
};
43
}
44
45
using Sources = QMap<QString, sources::Properties>;
46
using Source = QPair<QString, sources::Properties>;
47
}
Copied!
Properties of sources is described here

Prepare and extend supported providers

1
std::error_code prepare();
2
std::error_code prepare(std::span<providers::IFabric*> fabrics);
3
4
std::error_code prepare(QString& error) noexcept;
5
std::error_code prepare(std::span<providers::IFabric*> fabrics, QString& error) noexcept;
Copied!
Before Daggy Core start, need to prepare providers. If you need to extend provider types, that not out of box from Daggy Core lib (local and ssh2), you can create own providers fabric:
1
class DAGGYCORE_EXPORT IFabric
2
{
3
public:
4
IFabric();
5
virtual ~IFabric();
6
7
virtual const QString& type() const = 0;
8
Result<IProvider*> create(const QString& session,
9
const Source& source,
10
QObject* parent);
11
12
protected:
13
virtual Result<IProvider*> createProvider(const QString& session,
14
const Source& source,
15
QObject* parent) = 0;
16
};
17
Copied!
Own fabric instances need to pass in prepare method.

Connect aggregators

1
std::error_code connectAggregator(aggregators::IAggregator* aggregator) noexcept;
Copied!
Before Daggy Core start, you can add aggregators.

Out of box Daggy Core Aggregators

There are three types of aggregators, that already described:
1
#include <DaggyCore/aggregators/CFile.hpp>
2
#include <DaggyCore/aggregators/CConsole.hpp>
3
#include <DaggyCore/aggregators/CCallback.hpp>
Copied!

User defined aggregator

User can create own aggregator by implementation of IAggregator interface:
1
class DAGGYCORE_EXPORT IAggregator : public QObject
2
{
3
Q_OBJECT
4
public:
5
IAggregator(QObject* parent = nullptr);
6
virtual ~IAggregator();
7
8
virtual bool isReady() const = 0;
9
10
public slots:
11
virtual void onDataProviderStateChanged(QString provider_id,
12
DaggyProviderStates state) = 0;
13
virtual void onDataProviderError(QString provider_id,
14
std::error_code error_code) = 0;
15
16
virtual void onCommandStateChanged(QString provider_id,
17
QString command_id,
18
DaggyCommandStates state,
19
int exit_code) = 0;
20
21
virtual void onCommandError(QString provider_id,
22
QString command_id,
23
std::error_code error_code) = 0;
24
25
virtual void onCommandStream(QString provider_id,
26
QString command_id,
27
sources::commands::Stream stream) = 0;
28
29
virtual void onDaggyStateChanged(DaggyStates state) = 0;
30
31
};
Copied!

Start and stop Core

1
std::error_code start() noexcept;
2
std::error_code stop() noexcept;
Copied!
After start is called, Core changed state by the next state machine logic:
Daggy Core states
Finished state reached by calling stop method, or after all providers are finished.

Streams viewing

1
signals:
2
void stateChanged(DaggyStates state);
3
4
void dataProviderStateChanged(QString provider_id,
5
DaggyProviderStates state);
6
void dataProviderError(QString provider_id,
7
std::error_code error_code);
8
9
void commandStateChanged(QString provider_id,
10
QString command_id,
11
DaggyCommandStates state,
12
int exit_code);
13
void commandStream(QString provider_id,
14
QString command_id,
15
sources::commands::Stream stream);
16
void commandError(QString provider_id,
17
QString command_id,
18
std::error_code error_code);
Copied!

Usage Example

test.cpp
1
#include <DaggyCore/Core.hpp>
2
#include <DaggyCore/Sources.hpp>
3
#include <DaggyCore/aggregators/CFile.hpp>
4
#include <DaggyCore/aggregators/CConsole.hpp>
5
6
#include <QCoreApplication>
7
#include <QTimer>
8
9
namespace {
10
constexpr const char* json_data = R"JSON(
11
{
12
"sources": {
13
"localhost" : {
14
"type": "local",
15
"commands": {
16
"ping1": {
17
"exec": "ping 127.0.0.1",
18
"extension": "log"
19
},
20
"ping2": {
21
"exec": "ping 127.0.0.1",
22
"extension": "log",
23
"restart": true
24
}
25
}
26
}
27
}
28
}
29
)JSON";
30
}
31
32
int main(int argc, char** argv)
33
{
34
QCoreApplication app(argc, argv);
35
daggy::Core core(*daggy::sources::convertors::json(json_data));
36
37
daggy::aggregators::CFile file_aggregator("test");
38
daggy::aggregators::CConsole console_aggregator("test");
39
40
core.connectAggregator(&file_aggregator);
41
core.connectAggregator(&console_aggregator);
42
43
QObject::connect(&core, &daggy::Core::stateChanged, &core,
44
[&](DaggyStates state){
45
if(state == DaggyFinished)
46
app.quit();
47
});
48
49
QTimer::singleShot(3000, &core, [&]()
50
{
51
core.stop();
52
});
53
54
QTimer::singleShot(5000, &core, [&]()
55
{
56
app.exit(-1);
57
});
58
59
core.prepare();
60
core.start();
61
62
return app.exec();
63
}
Copied!