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 
38 namespace dht {
39 
40 using NetId = uint32_t;
41 using want_t = int_fast8_t;
42 
43 // shortcut for std::shared_ptr
44 template<class T>
45 using Sp = std::shared_ptr<T>;
46 
47 template <typename Key, typename Item, typename Condition>
48 void 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 
60 OPENDHT_PUBLIC std::pair<std::string, std::string>
61 splitPort(const std::string& s);
62 
63 class OPENDHT_PUBLIC DhtException : public std::runtime_error {
64 public:
65  DhtException(const std::string &str = "") :
66  std::runtime_error("DhtException occurred: " + str) {}
67 };
68 
69 class OPENDHT_PUBLIC SocketException : public DhtException {
70 public:
71  SocketException(int err) :
72  DhtException(strerror(err)) {}
73 };
74 
75 // Time related definitions and utility functions
76 
77 using clock = std::chrono::steady_clock;
78 using time_point = clock::time_point;
79 using duration = clock::duration;
80 
81 time_point from_time_t(std::time_t t);
82 std::time_t to_time_t(time_point t);
83 
87 template <class DT>
88 static double
89 print_dt(DT d) {
90  return std::chrono::duration_cast<std::chrono::duration<double>>(d).count();
91 }
92 
93 template <typename Duration = duration>
94 class 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;
97 public:
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 
114 using Blob = std::vector<uint8_t>;
115 
119 OPENDHT_PUBLIC Blob unpackBlob(msgpack::object& o);
120 
121 template <typename Type>
122 Blob
123 packMsg(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 
130 template <typename Type>
131 Type
132 unpackMsg(Blob b) {
133  msgpack::unpacked msg_res = msgpack::unpack((const char*)b.data(), b.size());
134  return msg_res.get().as<Type>();
135 }
136 
137 msgpack::unpacked unpackMsg(Blob b);
138 
139 msgpack::object* findMapValue(msgpack::object& map, const std::string& key);
140 
141 } // namespace dht
static double print_dt(DT d)
Definition: utils.h:89
std::vector< uint8_t > Blob
Definition: utils.h:114
OPENDHT_PUBLIC Blob unpackBlob(msgpack::object &o)
OPENDHT_PUBLIC std::pair< std::string, std::string > splitPort(const std::string &s)
Definition: callbacks.h:34