-
Notifications
You must be signed in to change notification settings - Fork 2
/
exchangemessages.h
82 lines (69 loc) · 2.38 KB
/
exchangemessages.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* Copyright (C) 2019 Alessandro Arrabito
*/
/*
* This file is part of QtExchangeDemo.
*
* QtExchangeDemo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QtExchangeDemo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QtExchangeDemo. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef EXCHANGEMESSAGE_H
#define EXCHANGEMESSAGE_H
#include <QJsonDocument>
#include <QJsonObject>
#include <memory>
#include "config.h"
// Interface for ExchangeMessage.
// QtExchangeDemo use a simple protocol based on Json messages.
// The ExchangeMessage subclasses define three types of messages:
// 1 - parameter is an outgoing message to configure exchange server ( dispatcher and fetcher ).
// 2 - set active status is an outgoing message activate/deactivate fetch of data
// 3 - data is an incoming message with the fetched exchange value
struct ExchangeMessage
{
enum class MessageType
{
Parameters,
SetActiveStatus,
Data
};
virtual ~ExchangeMessage();
virtual QJsonDocument toJsonDoc() = 0;
virtual MessageType type() = 0;
// factory method
static std::unique_ptr<ExchangeMessage> createFromJson(QByteArray &&json_data);
};
struct ExchangeMessageParameters: public ExchangeMessage
{
ExchangeMessageParameters(int _refresh_interval, QString _provider_url, QString _provider_field);
MessageType type(){return MessageType::Parameters;}
QJsonDocument toJsonDoc();
int refresh_interval;
QString provider_url;
QString provider_field;
};
struct ExchangeMessageStatus: public ExchangeMessage
{
ExchangeMessageStatus(bool _is_active);
MessageType type(){return MessageType::SetActiveStatus;}
QJsonDocument toJsonDoc();
bool is_active;
};
struct ExchangeMessageData: public ExchangeMessage
{
ExchangeMessageData(double _data);
MessageType type(){return MessageType::SetActiveStatus;}
QJsonDocument toJsonDoc();
double data;
};
#endif // EXCHANGEMESSAGE_H