-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathshared_subscriptions.hpp
65 lines (47 loc) · 2.11 KB
/
shared_subscriptions.hpp
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
// Copyright Takatoshi Kondo 2020
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#if !defined(MQTT_BROKER_SHARED_SUBSCRIPTIONS_HPP)
#define MQTT_BROKER_SHARED_SUBSCRIPTIONS_HPP
#include <mqtt/config.hpp>
#include <tuple>
#include <mqtt/buffer.hpp>
#include <mqtt/move.hpp>
namespace MQTT_NS {
struct share_name_topic_filter {
share_name_topic_filter(buffer share_name, buffer topic_filter)
: share_name { force_move(share_name) }, topic_filter{ force_move(topic_filter) }
{}
buffer share_name;
buffer topic_filter;
};
inline bool operator<(share_name_topic_filter const& lhs, share_name_topic_filter const& rhs) {
if (lhs.share_name < rhs.share_name) return true;
if (rhs.share_name < lhs.share_name) return false;
return lhs.topic_filter < rhs.topic_filter;
}
inline bool operator==(share_name_topic_filter const& lhs, share_name_topic_filter const& rhs) {
return lhs.share_name == rhs.share_name && lhs.topic_filter == rhs.topic_filter;
}
inline bool operator!=(share_name_topic_filter const& lhs, share_name_topic_filter const& rhs) {
return !(lhs == rhs);
}
inline share_name_topic_filter parse_shared_subscription(buffer whole_topic_filter) {
auto const shared_prefix = string_view("$share/");
if (whole_topic_filter.substr(0, shared_prefix.size()) != shared_prefix) {
return { buffer{}, force_move(whole_topic_filter) };
}
// Remove $share/
whole_topic_filter.remove_prefix(shared_prefix.size());
// This is the '/' seperating the subscription group from the actual topic_filter.
auto const idx = whole_topic_filter.find_first_of('/');
// We return the share and the topic as buffers that point to the same
// storage. So we grab the substr for "share", and then remove it from whole_topic_filter.
auto share = whole_topic_filter.substr(0, idx);
whole_topic_filter.remove_prefix(idx + 1);
return { force_move(share), force_move(whole_topic_filter) };
}
} // namespace MQTT_NS
#endif // MQTT_BROKER_SHARED_SUBSCRIPTIONS_HPP