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