File: Message.cpp

package info (click to toggle)
js8call 2.2.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 22,416 kB
  • sloc: cpp: 563,285; f90: 9,265; ansic: 937; python: 132; sh: 93; makefile: 6
file content (115 lines) | stat: -rw-r--r-- 3,157 bytes parent folder | download | duplicates (3)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
 * This file is part of JS8Call.
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * (C) 2018 Jordan Sherer <kn4crd@gmail.com> - All Rights Reserved
 *
 **/

#include "Message.h"
#include "DriftingDateTime.h"

const qint64 EPOCH = 1499299200000; // July 6, 2017

#if USE_SNOWFLAKE
quint64 snowflake(quint64 epoch, quint16 machine, quint16 sequence){
    quint64 value = (DriftingDateTime::currentMSecsSinceEpoch() - epoch) << 22;
    value |= machine & 0x3FF << 12;
    value |= sequence & 0xFFF;
    return value;
}
#endif

Message::Message()
{
}

Message::Message(QString const &type, QString const &value):
    type_{ type },
    value_{ value }
{
    params_["_ID"] = QString::number(DriftingDateTime::currentMSecsSinceEpoch()-EPOCH);
}

Message::Message(QString const &type, QString const &value,  QMap<QString, QVariant> const &params):
    type_{ type },
    value_{ value },
    params_{ params }
{
    if(params_.value("_ID", 0).toLongLong() == 0){
        params_["_ID"] = QString::number(DriftingDateTime::currentMSecsSinceEpoch()-EPOCH);
    }
}

qint64 Message::ensureId(){
    // if a non-zero id exists, we're good
    if(params_.contains("_ID")){
        auto id = params_.value("_ID", 0).toLongLong();
        if(id != 0){
            return id;
        }
    }

    // otherwise, generate one
    auto id = DriftingDateTime::currentMSecsSinceEpoch()-EPOCH;
    params_["_ID"] = QString::number(id);
    return id;
}

void Message::read(const QJsonObject &json){
    if(json.contains("type") && json["type"].isString()){
        type_ = json["type"].toString();
    }

    if(json.contains("value") && json["value"].isString()){
        value_ = json["value"].toString();
    }

    if(json.contains("params") && json["params"].isObject()){
        params_.clear();

        QJsonObject params = json["params"].toObject();
        foreach(auto key, params.keys()){
            params_[key] = params[key].toVariant();
        }
    }
}

void Message::write(QJsonObject &json) const{
    json["type"] = type_;
    json["value"] = value_;

    QJsonObject params;
    foreach(auto key, params_.keys()){
        params.insert(key, QJsonValue::fromVariant(params_[key]));
    }
    json["params"] = params;
}

QByteArray Message::toJson() const {
    QJsonObject o;
    write(o);

    QJsonDocument d(o);
    return d.toJson(QJsonDocument::Compact);
}

QVariantMap Message::toVariantMap() const {
    QVariantMap m;
    m["type"] = QVariant(type_);
    m["value"] = QVariant(value_);
    m["params"] = QVariant(params_);
    return m;
}