My Project 3.6.0
C++ Distributed Hash Table
Loading...
Searching...
No Matches
utils.h
1/*
2 * Copyright (C) 2014-2025 Savoir-faire Linux Inc.
3 * Author : Adrien Béraud <adrien.beraud@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include "def.h"
22
23#include <type_traits>
24#include <msgpack.hpp>
25#include <fmt/format.h>
26
27#include <chrono>
28#include <random>
29#include <functional>
30#include <map>
31#include <string_view>
32
33#include <cstdarg>
34
35#define WANT4 1
36#define WANT6 2
37
41namespace dht {
42
43using NetId = uint32_t;
44using want_t = int_fast8_t;
45
46OPENDHT_PUBLIC const char* version();
47
48// shortcut for std::shared_ptr
49template<class T>
50using Sp = std::shared_ptr<T>;
51
52template<typename Key, typename Item, typename Condition>
53void
54erase_if(std::map<Key, Item>& map, const Condition& condition)
55{
56 for (auto it = map.begin(); it != map.end();) {
57 if (condition(*it)) {
58 it = map.erase(it);
59 } else {
60 ++it;
61 }
62 }
63}
64
68OPENDHT_PUBLIC std::pair<std::string, std::string> splitPort(std::string_view s);
69
70class OPENDHT_PUBLIC DhtException : public std::runtime_error
71{
72public:
73 DhtException(const std::string& str = "")
74 : std::runtime_error("DhtException occurred: " + str)
75 {}
76};
77
78class OPENDHT_PUBLIC SocketException : public DhtException
79{
80public:
81 SocketException(int err)
82 : DhtException(strerror(err))
83 {}
84};
85
86// Time related definitions and utility functions
87
88using clock = std::chrono::steady_clock;
89using system_clock = std::chrono::system_clock;
90using time_point = clock::time_point;
91using duration = clock::duration;
92
93time_point from_time_t(std::time_t t);
94std::time_t to_time_t(time_point t);
95
96template<class DT>
97static std::string
98print_duration(DT d)
99{
100 if (d < std::chrono::seconds(0)) {
101 return "-" + print_duration(-d);
102 } else if (d < std::chrono::milliseconds(1)) {
103 return fmt::format("{:.3g} us",
104 std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(d).count());
105 } else if (d < std::chrono::seconds(1)) {
106 return fmt::format("{:.3g} ms",
107 std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(d).count());
108 } else if (d < std::chrono::minutes(1)) {
109 return fmt::format("{:.3g} s", std::chrono::duration_cast<std::chrono::duration<double>>(d).count());
110 } else if (d < std::chrono::hours(1)) {
111 return fmt::format("{:.3g} min",
112 std::chrono::duration_cast<std::chrono::duration<double, std::ratio<60>>>(d).count());
113 } else if (d < std::chrono::hours(72)) {
114 return fmt::format("{:.3g} h",
115 std::chrono::duration_cast<std::chrono::duration<double, std::ratio<3600>>>(d).count());
116 } else {
117 return fmt::format("{:.3g} days",
118 std::chrono::duration_cast<std::chrono::duration<double, std::ratio<86400>>>(d).count());
119 }
120}
121
122template<class TimePoint>
123static std::string
124print_time_relative(TimePoint now, TimePoint d)
125{
126 if (d == TimePoint::min())
127 return "never";
128 if (d == now)
129 return "now";
130 return (d > now) ? std::string("in ") + print_duration(d - now) : print_duration(now - d) + std::string(" ago");
131}
132
133template<typename Duration = duration>
134class uniform_duration_distribution : public std::uniform_int_distribution<typename Duration::rep>
135{
136 using Base = std::uniform_int_distribution<typename Duration::rep>;
137 using param_type = typename Base::param_type;
138
139public:
140 uniform_duration_distribution(Duration min, Duration max)
141 : Base(min.count(), max.count())
142 {}
143 template<class Generator>
144 Duration operator()(Generator&& g)
145 {
146 return Duration(Base::operator()(g));
147 }
148 template<class Generator>
149 Duration operator()(Generator&& g, const param_type& params)
150 {
151 return Duration(Base::operator()(g, params));
152 }
153};
154
155// Serialization related definitions and utility functions
156
160using Blob = std::vector<uint8_t>;
161
165OPENDHT_PUBLIC Blob unpackBlob(const msgpack::object& o);
166
167template<typename Type>
168Blob
169packMsg(const Type& t)
170{
171 msgpack::sbuffer buffer;
172 msgpack::packer<msgpack::sbuffer> pk(&buffer);
173 pk.pack(t);
174 return {buffer.data(), buffer.data() + buffer.size()};
175}
176
177template<typename Type>
178Type
179unpackMsg(Blob b)
180{
181 msgpack::unpacked msg_res = msgpack::unpack((const char*) b.data(), b.size());
182 return msg_res.get().as<Type>();
183}
184
185msgpack::unpacked unpackMsg(Blob b);
186
187msgpack::object* findMapValue(const msgpack::object& map, const char* key, size_t length);
188
189inline msgpack::object*
190findMapValue(const msgpack::object& map, const char* key)
191{
192 return findMapValue(map, key, strlen(key));
193}
194inline msgpack::object*
195findMapValue(const msgpack::object& map, std::string_view key)
196{
197 return findMapValue(map, key.data(), key.size());
198}
199
200} // namespace dht
OPENDHT_PUBLIC Blob unpackBlob(const msgpack::object &o)
std::vector< uint8_t > Blob
Definition utils.h:160
OPENDHT_PUBLIC std::pair< std::string, std::string > splitPort(std::string_view s)