forked from halfgaar/FlashMQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retainedmessage.cpp
55 lines (43 loc) · 1.38 KB
/
retainedmessage.cpp
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
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021-2023 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of The Open Software License 3.0 (OSL-3.0).
See LICENSE for license details.
*/
#include "retainedmessage.h"
#include "threadglobals.h"
#include "settings.h"
RetainedMessage::RetainedMessage(const Publish &publish) :
publish(publish)
{
this->publish.retain = true;
const Settings *settings = ThreadGlobals::getSettings();
this->publish.setExpireAfterToCeiling(settings->expireRetainedMessagesAfterSeconds);
}
bool RetainedMessage::operator==(const RetainedMessage &rhs) const
{
return this->publish.topic == rhs.publish.topic;
}
bool RetainedMessage::empty() const
{
return publish.payload.empty();
}
uint32_t RetainedMessage::getSize() const
{
return publish.topic.length() + publish.payload.length() + 1;
}
/**
* @brief RetainedMessage::hasExpired is more dynamic than a publish's expire info, because the settings may have changed in the mean time.
* @return
*/
bool RetainedMessage::hasExpired() const
{
if (this->publish.hasExpired())
return true;
const Settings *settings = ThreadGlobals::getSettings();
std::chrono::seconds expireAge(settings->expireRetainedMessagesAfterSeconds);
if (this->publish.getAge() > expireAge)
return true;
return false;
}