C++ Distributed Hash Table
callbacks.h
1 /*
2  * Copyright (C) 2014-2017 Savoir-faire Linux Inc.
3  * Authors: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
4  * Simon Désaulniers <simon.desaulniers@savoirfairelinux.com>
5  * Sébastien Blin <sebastien.blin@savoirfairelinux.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #pragma once
22 
23 #include "infohash.h"
24 #include "value.h"
25 
26 #include <vector>
27 #include <memory>
28 #include <functional>
29 
30 #ifdef OPENDHT_JSONCPP
31 #include <json/json.h>
32 #endif
33 
34 namespace dht {
35 
36 struct Node;
37 
41 enum class NodeStatus {
42  Disconnected, // 0 nodes
43  Connecting, // 1+ nodes
44  Connected // 1+ good nodes
45 };
46 
47 struct OPENDHT_PUBLIC NodeStats {
48  unsigned good_nodes {0},
49  dubious_nodes {0},
50  cached_nodes {0},
51  incoming_nodes {0};
52  unsigned table_depth {0};
53  unsigned getKnownNodes() const { return good_nodes + dubious_nodes; }
54  unsigned long getNetworkSizeEstimation() const { return 8 * std::exp2(table_depth); }
55  std::string toString() const;
56 
57 #ifdef OPENDHT_JSONCPP
58 
61  Json::Value toJson() const;
62  NodeStats() {};
63  explicit NodeStats(const Json::Value& v);
64 #endif
65 
66  MSGPACK_DEFINE_MAP(good_nodes, dubious_nodes, cached_nodes, incoming_nodes, table_depth)
67 };
68 
69 struct OPENDHT_PUBLIC NodeInfo {
70  InfoHash id;
71  InfoHash node_id;
72  NodeStats ipv4;
73  NodeStats ipv6;
74 
75 #ifdef OPENDHT_JSONCPP
76 
79  Json::Value toJson() const;
80  NodeInfo() {};
81  explicit NodeInfo(const Json::Value& v);
82 #endif
83 
84  MSGPACK_DEFINE_MAP(id, node_id, ipv4, ipv6)
85 };
86 
90 struct OPENDHT_PUBLIC Config {
93 
99  NetId network;
100 
103 
106 };
107 
111 struct OPENDHT_PUBLIC SecureDhtConfig
112 {
113  Config node_config;
114  crypto::Identity id;
115 };
116 
117 static constexpr size_t DEFAULT_STORAGE_LIMIT {1024 * 1024 * 64};
118 
119 using ValuesExport = std::pair<InfoHash, Blob>;
120 
121 using QueryCallback = std::function<bool(const std::vector<std::shared_ptr<FieldValueIndex>>& fields)>;
122 using GetCallback = std::function<bool(const std::vector<std::shared_ptr<Value>>& values)>;
123 using ValueCallback = std::function<bool(const std::vector<std::shared_ptr<Value>>& values, bool expired)>;
124 using GetCallbackSimple = std::function<bool(std::shared_ptr<Value> value)>;
125 using ShutdownCallback = std::function<void()>;
126 
127 using CertificateStoreQuery = std::function<std::vector<std::shared_ptr<crypto::Certificate>>(const InfoHash& pk_id)>;
128 
129 typedef bool (*GetCallbackRaw)(std::shared_ptr<Value>, void *user_data);
130 
131 OPENDHT_PUBLIC GetCallbackSimple bindGetCb(GetCallbackRaw raw_cb, void* user_data);
132 OPENDHT_PUBLIC GetCallback bindGetCb(GetCallbackSimple cb);
133 
134 using DoneCallback = std::function<void(bool success, const std::vector<std::shared_ptr<Node>>& nodes)>;
135 typedef void (*DoneCallbackRaw)(bool, std::vector<std::shared_ptr<Node>>*, void *user_data);
136 typedef void (*ShutdownCallbackRaw)(void *user_data);
137 typedef void (*DoneCallbackSimpleRaw)(bool, void *user_data);
138 typedef bool (*FilterRaw)(const Value&, void *user_data);
139 
140 using DoneCallbackSimple = std::function<void(bool success)>;
141 
142 OPENDHT_PUBLIC ShutdownCallback bindShutdownCb(ShutdownCallbackRaw shutdown_cb_raw, void* user_data);
143 OPENDHT_PUBLIC DoneCallback bindDoneCb(DoneCallbackSimple donecb);
144 OPENDHT_PUBLIC DoneCallback bindDoneCb(DoneCallbackRaw raw_cb, void* user_data);
145 OPENDHT_PUBLIC DoneCallbackSimple bindDoneCbSimple(DoneCallbackSimpleRaw raw_cb, void* user_data);
146 OPENDHT_PUBLIC Value::Filter bindFilterRaw(FilterRaw raw_filter, void* user_data);
147 
148 }
bool is_bootstrap
Definition: callbacks.h:102
InfoHash node_id
Definition: callbacks.h:92
Definition: node.h:47
NodeStatus
Definition: callbacks.h:41
bool maintain_storage
Definition: callbacks.h:105
NetId network
Definition: callbacks.h:99
Definition: callbacks.h:34