My Project 3.2.0
C++ Distributed Hash Table
Loading...
Searching...
No Matches
callbacks.h
1/*
2 * Copyright (C) 2014-2023 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#include <string>
30
31#ifdef OPENDHT_JSONCPP
32#include <json/json.h>
33#endif
34
35namespace dht {
36
37struct Node;
38
42enum class NodeStatus {
43 Disconnected, // 0 nodes
44 Connecting, // 1+ nodes
45 Connected // 1+ good nodes
46};
47
48inline constexpr const char*
49statusToStr(NodeStatus status) {
50 return status == NodeStatus::Connected ? "connected" : (
51 status == NodeStatus::Connecting ? "connecting" :
52 "disconnected");
53}
54
55struct OPENDHT_PUBLIC NodeStats {
56 unsigned good_nodes {0},
57 dubious_nodes {0},
58 cached_nodes {0},
59 incoming_nodes {0};
60 unsigned table_depth {0};
61 unsigned searches {0};
62 unsigned node_cache_size {0};
63 unsigned getKnownNodes() const { return good_nodes + dubious_nodes; }
64 unsigned long getNetworkSizeEstimation() const { return 8 * std::exp2(table_depth); }
65 std::string toString() const;
66
67#ifdef OPENDHT_JSONCPP
71 Json::Value toJson() const;
72 NodeStats() {};
73 explicit NodeStats(const Json::Value& v);
74#endif
75
76 MSGPACK_DEFINE_MAP(good_nodes, dubious_nodes, cached_nodes, incoming_nodes, table_depth, searches, node_cache_size)
77};
78
79struct OPENDHT_PUBLIC NodeInfo {
80 InfoHash id;
81 InfoHash node_id;
82 NodeStats ipv4 {};
83 NodeStats ipv6 {};
84 size_t ongoing_ops {0};
85 size_t storage_values {0};
86 size_t storage_size {0};
87 in_port_t bound4 {0};
88 in_port_t bound6 {0};
89
90#ifdef OPENDHT_JSONCPP
94 Json::Value toJson() const;
95 NodeInfo() {};
96 explicit NodeInfo(const Json::Value& v);
97#endif
98
99 MSGPACK_DEFINE_MAP(id, node_id, ipv4, ipv6)
100};
101
105struct OPENDHT_PUBLIC Config {
107 InfoHash node_id {};
108
114 NetId network {0};
115
117 bool is_bootstrap {false};
118
120 bool maintain_storage {false};
121
123 std::string persist_path {};
124
126 ssize_t max_req_per_sec {0};
127
130
131 /* If non-0, overrides the default maximum number of searches. -1 means no limit. */
132 ssize_t max_searches {0};
133
134 /* If non-0, overrides the default maximum store size. -1 means no limit. */
135 ssize_t max_store_size {0};
136
137 /* If non-0, overrides the default maximum store key count. -1 means no limit. */
138 ssize_t max_store_keys {0};
139
145 bool public_stable {false};
146
147 /* Client mode, node will not be used by other nodes to store data. */
148 bool client_mode {false};
149};
150
154struct OPENDHT_PUBLIC SecureDhtConfig
155{
156 Config node_config {};
157 crypto::Identity id {};
158
163 bool cert_cache_all {false};
164};
165
166static constexpr size_t DEFAULT_STORAGE_LIMIT {1024 * 1024 * 64};
167
168using ValuesExport = std::pair<InfoHash, Blob>;
169
170using QueryCallback = std::function<bool(const std::vector<std::shared_ptr<FieldValueIndex>>& fields)>;
171using GetCallback = std::function<bool(const std::vector<std::shared_ptr<Value>>& values)>;
172using ValueCallback = std::function<bool(const std::vector<std::shared_ptr<Value>>& values, bool expired)>;
173using GetCallbackSimple = std::function<bool(std::shared_ptr<Value> value)>;
174using ShutdownCallback = std::function<void()>;
175using IdentityAnnouncedCb = std::function<void(bool)>;
176using PublicAddressChangedCb = std::function<void(std::vector<SockAddr>)>;
177
178using CertificateStoreQuery = std::function<std::vector<std::shared_ptr<crypto::Certificate>>(const InfoHash& pk_id)>;
179using DoneCallback = std::function<void(bool success, const std::vector<std::shared_ptr<Node>>& nodes)>;
180using DoneCallbackSimple = std::function<void(bool success)>;
181
182typedef bool (*GetCallbackRaw)(std::shared_ptr<Value>, void *user_data);
183typedef bool (*ValueCallbackRaw)(std::shared_ptr<Value>, bool expired, void *user_data);
184typedef void (*DoneCallbackRaw)(bool, std::vector<std::shared_ptr<Node>>*, void *user_data);
185typedef void (*ShutdownCallbackRaw)(void *user_data);
186typedef void (*DoneCallbackSimpleRaw)(bool, void *user_data);
187typedef bool (*FilterRaw)(const Value&, void *user_data);
188
189
190OPENDHT_PUBLIC GetCallbackSimple bindGetCb(GetCallbackRaw raw_cb, void* user_data);
191OPENDHT_PUBLIC GetCallback bindGetCb(GetCallbackSimple cb);
192OPENDHT_PUBLIC ValueCallback bindValueCb(ValueCallbackRaw raw_cb, void* user_data);
193OPENDHT_PUBLIC ShutdownCallback bindShutdownCb(ShutdownCallbackRaw shutdown_cb_raw, void* user_data);
194OPENDHT_PUBLIC DoneCallback bindDoneCb(DoneCallbackSimple donecb);
195OPENDHT_PUBLIC DoneCallback bindDoneCb(DoneCallbackRaw raw_cb, void* user_data);
196OPENDHT_PUBLIC DoneCallbackSimple bindDoneCbSimple(DoneCallbackSimpleRaw raw_cb, void* user_data);
197OPENDHT_PUBLIC Value::Filter bindFilterRaw(FilterRaw raw_filter, void* user_data);
198
199}
NodeStatus
Definition callbacks.h:42
bool is_bootstrap
Definition callbacks.h:117
ssize_t max_peer_req_per_sec
Definition callbacks.h:129
std::string persist_path
Definition callbacks.h:123
bool public_stable
Definition callbacks.h:145
NetId network
Definition callbacks.h:114
InfoHash node_id
Definition callbacks.h:107
ssize_t max_req_per_sec
Definition callbacks.h:126
bool maintain_storage
Definition callbacks.h:120