publishcopyfactory.cpp
2.95 KB
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
116
#include <cassert>
#include "publishcopyfactory.h"
#include "mqttpacket.h"
PublishCopyFactory::PublishCopyFactory(MqttPacket *packet) :
packet(packet),
orgQos(packet->getQos())
{
}
PublishCopyFactory::PublishCopyFactory(Publish *publish) :
publish(publish),
orgQos(publish->qos)
{
}
MqttPacket *PublishCopyFactory::getOptimumPacket(const char max_qos, const ProtocolVersion protocolVersion)
{
if (packet)
{
if (packet->getProtocolVersion() == protocolVersion && orgQos == max_qos)
{
assert(orgQos == packet->getQos());
return packet;
}
const int cache_key = (static_cast<uint8_t>(protocolVersion) * 10) + max_qos;
std::unique_ptr<MqttPacket> &cachedPack = constructedPacketCache[cache_key];
if (!cachedPack)
{
Publish newPublish(packet->getPublishData());
newPublish.splitTopic = false;
newPublish.qos = max_qos;
if (protocolVersion >= ProtocolVersion::Mqtt5)
newPublish.setClientSpecificProperties();
cachedPack = std::make_unique<MqttPacket>(protocolVersion, newPublish);
}
return cachedPack.get();
}
// Getting a packet of a Publish object happens on will messages and SYS topics and maybe some others. It's low traffic, anyway.
assert(publish);
if (protocolVersion >= ProtocolVersion::Mqtt5)
publish->setClientSpecificProperties();
this->oneShotPacket = std::make_unique<MqttPacket>(protocolVersion, *publish);
return this->oneShotPacket.get();
}
char PublishCopyFactory::getEffectiveQos(char max_qos) const
{
const char effectiveQos = std::min<char>(orgQos, max_qos);
return effectiveQos;
}
const std::string &PublishCopyFactory::getTopic() const
{
if (packet)
return packet->getTopic();
assert(publish);
return publish->topic;
}
const std::vector<std::string> &PublishCopyFactory::getSubtopics()
{
if (packet)
{
assert(!packet->getSubtopics().empty());
return packet->getSubtopics();
}
else if (publish)
{
if (publish->subtopics.empty())
splitTopic(publish->topic, publish->subtopics);
return publish->subtopics;
}
throw std::runtime_error("Bug in &PublishCopyFactory::getSubtopics()");
}
bool PublishCopyFactory::getRetain() const
{
if (packet)
return packet->getRetain();
assert(publish);
return publish->retain;
}
Publish PublishCopyFactory::getNewPublish() const
{
assert(packet->getQos() > 0);
assert(orgQos > 0); // We only need to construct new publishes for QoS. If you're doing it elsewhere, it's a bug.
if (packet)
{
Publish p(packet->getPublishData());
p.qos = orgQos;
return p;
}
Publish p(*publish);
p.qos = orgQos;
return p;
}
std::shared_ptr<Client> PublishCopyFactory::getSender()
{
if (packet)
return packet->getSender();
return std::shared_ptr<Client>(0);
}