My Project 1.7.4
C++ Distributed Hash Table
utils.h
1/*
2 * Copyright (C) 2014-2017 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
25#include <chrono>
26#include <random>
27#include <functional>
28#include <map>
29
30#include <cstdarg>
31
32#define WANT4 1
33#define WANT6 2
34
38namespace dht {
39
40using NetId = uint32_t;
41using want_t = int_fast8_t;
42
43// shortcut for std::shared_ptr
44template<class T>
45using Sp = std::shared_ptr<T>;
46
47template <typename Key, typename Item, typename Condition>
48void erase_if(std::map<Key, Item>& map, const Condition& condition)
49{
50 for (auto it = map.begin(); it != map.end(); ) {
51 if (condition(*it)) {
52 it = map.erase(it);
53 } else { ++it; }
54 }
55}
56
60OPENDHT_PUBLIC std::pair<std::string, std::string>
61splitPort(const std::string& s);
62
63class OPENDHT_PUBLIC DhtException : public std::runtime_error {
64public:
65 DhtException(const std::string &str = "") :
66 std::runtime_error("DhtException occurred: " + str) {}
67};
68
69class OPENDHT_PUBLIC SocketException : public DhtException {
70public:
71 SocketException(int err) :
72 DhtException(strerror(err)) {}
73};
74
75// Time related definitions and utility functions
76
77using clock = std::chrono::steady_clock;
78using time_point = clock::time_point;
79using duration = clock::duration;
80
81time_point from_time_t(std::time_t t);
82std::time_t to_time_t(time_point t);
83
87template <class DT>
88static double
89print_dt(DT d) {
90 return std::chrono::duration_cast<std::chrono::duration<double>>(d).count();
91}
92
93template <typename Duration = duration>
94class uniform_duration_distribution : public std::uniform_int_distribution<typename Duration::rep> {
95 using Base = std::uniform_int_distribution<typename Duration::rep>;
96 using param_type = typename Base::param_type;
97public:
98 uniform_duration_distribution(Duration min, Duration max) : Base(min.count(), max.count()) {}
99 template <class Generator>
100 Duration operator()(Generator && g) {
101 return Duration(Base::operator()(g));
102 }
103 template< class Generator >
104 Duration operator()( Generator && g, const param_type& params ) {
105 return Duration(Base::operator()(g, params));
106 }
107};
108
109// Serialization related definitions and utility functions
110
114using Blob = std::vector<uint8_t>;
115
119OPENDHT_PUBLIC Blob unpackBlob(msgpack::object& o);
120
121template <typename Type>
122Blob
123packMsg(const Type& t) {
124 msgpack::sbuffer buffer;
125 msgpack::packer<msgpack::sbuffer> pk(&buffer);
126 pk.pack(t);
127 return {buffer.data(), buffer.data()+buffer.size()};
128}
129
130template <typename Type>
131Type
132unpackMsg(Blob b) {
133 msgpack::unpacked msg_res = msgpack::unpack((const char*)b.data(), b.size());
134 return msg_res.get().as<Type>();
135}
136
137msgpack::unpacked unpackMsg(Blob b);
138
139msgpack::object* findMapValue(msgpack::object& map, const std::string& key);
140
141} // namespace dht
Definition: callbacks.h:34
OPENDHT_PUBLIC Blob unpackBlob(msgpack::object &o)
OPENDHT_PUBLIC std::pair< std::string, std::string > splitPort(const std::string &s)
static double print_dt(DT d)
Definition: utils.h:89
std::vector< uint8_t > Blob
Definition: utils.h:114