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  std::string toString() const;
55 
56 #ifdef OPENDHT_JSONCPP
57 
60  Json::Value toJson() const;
61  NodeStats() {};
62  explicit NodeStats(const Json::Value& v);
63 #endif
64 
65  MSGPACK_DEFINE_MAP(good_nodes, dubious_nodes, cached_nodes, incoming_nodes, table_depth)
66 };
67 
68 struct OPENDHT_PUBLIC NodeInfo {
69  InfoHash id;
70  InfoHash node_id;
71  NodeStats ipv4;
72  NodeStats ipv6;
73 
74 #ifdef OPENDHT_JSONCPP
75 
78  Json::Value toJson() const;
79  NodeInfo() {};
80  explicit NodeInfo(const Json::Value& v);
81 #endif
82 
83  MSGPACK_DEFINE_MAP(id, node_id, ipv4, ipv6)
84 };
85 
89 struct OPENDHT_PUBLIC Config {
92 
98  NetId network;
99 
102 
105 };
106 
110 struct OPENDHT_PUBLIC SecureDhtConfig
111 {
112  Config node_config;
113  crypto::Identity id;
114 };
115 
116 static constexpr size_t DEFAULT_STORAGE_LIMIT {1024 * 1024 * 64};
117 
118 using ValuesExport = std::pair<InfoHash, Blob>;
119 
120 using QueryCallback = std::function<bool(const std::vector<std::shared_ptr<FieldValueIndex>>& fields)>;
121 using GetCallback = std::function<bool(const std::vector<std::shared_ptr<Value>>& values)>;
122 using ValueCallback = std::function<bool(const std::vector<std::shared_ptr<Value>>& values, bool expired)>;
123 using GetCallbackSimple = std::function<bool(std::shared_ptr<Value> value)>;
124 using ShutdownCallback = std::function<void()>;
125 
126 using CertificateStoreQuery = std::function<std::vector<std::shared_ptr<crypto::Certificate>>(const InfoHash& pk_id)>;
127 
128 typedef bool (*GetCallbackRaw)(std::shared_ptr<Value>, void *user_data);
129 
130 OPENDHT_PUBLIC GetCallbackSimple bindGetCb(GetCallbackRaw raw_cb, void* user_data);
131 OPENDHT_PUBLIC GetCallback bindGetCb(GetCallbackSimple cb);
132 
133 using DoneCallback = std::function<void(bool success, const std::vector<std::shared_ptr<Node>>& nodes)>;
134 typedef void (*DoneCallbackRaw)(bool, std::vector<std::shared_ptr<Node>>*, void *user_data);
135 typedef void (*ShutdownCallbackRaw)(void *user_data);
136 typedef void (*DoneCallbackSimpleRaw)(bool, void *user_data);
137 typedef bool (*FilterRaw)(const Value&, void *user_data);
138 
139 using DoneCallbackSimple = std::function<void(bool success)>;
140 
141 OPENDHT_PUBLIC ShutdownCallback bindShutdownCb(ShutdownCallbackRaw shutdown_cb_raw, void* user_data);
142 OPENDHT_PUBLIC DoneCallback bindDoneCb(DoneCallbackSimple donecb);
143 OPENDHT_PUBLIC DoneCallback bindDoneCb(DoneCallbackRaw raw_cb, void* user_data);
144 OPENDHT_PUBLIC DoneCallbackSimple bindDoneCbSimple(DoneCallbackSimpleRaw raw_cb, void* user_data);
145 OPENDHT_PUBLIC Value::Filter bindFilterRaw(FilterRaw raw_filter, void* user_data);
146 
147 }
bool is_bootstrap
Definition: callbacks.h:101
InfoHash node_id
Definition: callbacks.h:91
Definition: node.h:47
NodeStatus
Definition: callbacks.h:41
bool maintain_storage
Definition: callbacks.h:104
NetId network
Definition: callbacks.h:98
Definition: callbacks.h:34