My Project 3.5.5
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 <msgpack.hpp>
24#include <fmt/format.h>
25
26#include <chrono>
27#include <random>
28#include <functional>
29#include <map>
30#include <string_view>
31
32#include <cstdarg>
33
34#define WANT4 1
35#define WANT6 2
36
40namespace dht {
41
42using NetId = uint32_t;
43using want_t = int_fast8_t;
44
45OPENDHT_PUBLIC const char* version();
46
47// shortcut for std::shared_ptr
48template<class T>
49using Sp = std::shared_ptr<T>;
50
51template <typename Key, typename Item, typename Condition>
52void erase_if(std::map<Key, Item>& map, const Condition& condition)
53{
54 for (auto it = map.begin(); it != map.end(); ) {
55 if (condition(*it)) {
56 it = map.erase(it);
57 } else { ++it; }
58 }
59}
60
64OPENDHT_PUBLIC std::pair<std::string, std::string>
65splitPort(std::string_view s);
66
67class OPENDHT_PUBLIC DhtException : public std::runtime_error {
68public:
69 DhtException(const std::string &str = "") :
70 std::runtime_error("DhtException occurred: " + str) {}
71};
72
73class OPENDHT_PUBLIC SocketException : public DhtException {
74public:
75 SocketException(int err) :
76 DhtException(strerror(err)) {}
77};
78
79// Time related definitions and utility functions
80
81using clock = std::chrono::steady_clock;
82using system_clock = std::chrono::system_clock;
83using time_point = clock::time_point;
84using duration = clock::duration;
85
86time_point from_time_t(std::time_t t);
87std::time_t to_time_t(time_point t);
88
89template <class DT>
90static std::string
91print_duration(DT d) {
92 if (d < std::chrono::seconds(0)) {
93 return "-" + print_duration(-d);
94 } else if (d < std::chrono::milliseconds(1)) {
95 return fmt::format("{:.3g} us", std::chrono::duration_cast<std::chrono::duration<double, std::micro>>(d).count());
96 } else if (d < std::chrono::seconds(1)) {
97 return fmt::format("{:.3g} ms", std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(d).count());
98 } else if (d < std::chrono::minutes(1)) {
99 return fmt::format("{:.3g} s", std::chrono::duration_cast<std::chrono::duration<double>>(d).count());
100 } else if (d < std::chrono::hours(1)) {
101 return fmt::format("{:.3g} min", std::chrono::duration_cast<std::chrono::duration<double, std::ratio<60>>>(d).count());
102 } else if (d < std::chrono::hours(72)) {
103 return fmt::format("{:.3g} h", std::chrono::duration_cast<std::chrono::duration<double, std::ratio<3600>>>(d).count());
104 } else {
105 return fmt::format("{:.3g} days", std::chrono::duration_cast<std::chrono::duration<double, std::ratio<86400>>>(d).count());
106 }
107}
108
109template <class TimePoint>
110static std::string
111print_time_relative(TimePoint now, TimePoint d) {
112 if (d == TimePoint::min()) return "never";
113 if (d == now) return "now";
114 return (d > now) ? std::string("in ") + print_duration(d - now)
115 : print_duration(now - d) + std::string(" ago");
116}
117
118template <typename Duration = duration>
119class uniform_duration_distribution : public std::uniform_int_distribution<typename Duration::rep> {
120 using Base = std::uniform_int_distribution<typename Duration::rep>;
121 using param_type = typename Base::param_type;
122public:
123 uniform_duration_distribution(Duration min, Duration max) : Base(min.count(), max.count()) {}
124 template <class Generator>
125 Duration operator()(Generator && g) {
126 return Duration(Base::operator()(g));
127 }
128 template< class Generator >
129 Duration operator()( Generator && g, const param_type& params ) {
130 return Duration(Base::operator()(g, params));
131 }
132};
133
134// Serialization related definitions and utility functions
135
139using Blob = std::vector<uint8_t>;
140
144OPENDHT_PUBLIC Blob unpackBlob(const msgpack::object& o);
145
146template <typename Type>
147Blob
148packMsg(const Type& t) {
149 msgpack::sbuffer buffer;
150 msgpack::packer<msgpack::sbuffer> pk(&buffer);
151 pk.pack(t);
152 return {buffer.data(), buffer.data()+buffer.size()};
153}
154
155template <typename Type>
156Type
157unpackMsg(Blob b) {
158 msgpack::unpacked msg_res = msgpack::unpack((const char*)b.data(), b.size());
159 return msg_res.get().as<Type>();
160}
161
162msgpack::unpacked unpackMsg(Blob b);
163
164msgpack::object* findMapValue(const msgpack::object& map, const char* key, size_t length);
165
166inline msgpack::object* findMapValue(const msgpack::object& map, const char* key) {
167 return findMapValue(map, key, strlen(key));
168}
169inline msgpack::object* findMapValue(const msgpack::object& map, std::string_view key) {
170 return findMapValue(map, key.data(), key.size());
171}
172
173} // namespace dht
OPENDHT_PUBLIC Blob unpackBlob(const msgpack::object &o)
std::vector< uint8_t > Blob
Definition utils.h:139
OPENDHT_PUBLIC std::pair< std::string, std::string > splitPort(std::string_view s)