36class OPENDHT_PUBLIC DhtRunner
45 std::string proxy_server {};
46 std::string push_node_id {};
47 std::string push_token {};
48 std::string push_topic {};
49 std::string push_platform {};
50 std::string proxy_user_agent {};
51 bool peer_discovery {
false};
52 bool peer_publish {
false};
53 std::shared_ptr<dht::crypto::Certificate> server_ca;
54 dht::crypto::Identity client_identity;
60 std::shared_ptr<Logger> logger {};
61 std::unique_ptr<net::DatagramSocket> sock;
62 std::shared_ptr<PeerDiscovery> peerDiscovery {};
63 StatusCallback statusChangedCallback {};
64 CertificateStoreQuery certificateStore {};
65 IdentityAnnouncedCb identityAnnouncedCb {};
66 PublicAddressChangedCb publicAddressChangedCb {};
67 std::unique_ptr<std::mt19937_64> rng {};
74 void get(InfoHash
id, GetCallbackSimple cb, DoneCallback donecb = {},
Value::Filter f = {}, Where w = {})
76 get(
id, bindGetCb(cb), donecb, f, w);
79 void get(InfoHash
id, GetCallbackSimple cb, DoneCallbackSimple donecb = {}, Value::Filter f = {}, Where w = {})
81 get(
id, bindGetCb(cb), donecb, f, w);
84 void get(InfoHash hash, GetCallback vcb, DoneCallback dcb, Value::Filter f = {}, Where w = {});
86 void get(InfoHash
id, GetCallback cb, DoneCallbackSimple donecb = {}, Value::Filter f = {}, Where w = {})
88 get(
id, cb, bindDoneCb(donecb), f, w);
90 void get(
const std::string& key, GetCallback vcb, DoneCallbackSimple dcb = {}, Value::Filter f = {}, Where w = {});
93 void get(InfoHash hash, std::function<
bool(std::vector<T>&&)> cb, DoneCallbackSimple dcb = {})
97 [cb = std::move(cb)](
const std::vector<std::shared_ptr<Value>>& vals) {
return cb(unpackVector<T>(vals)); },
102 void get(InfoHash hash, std::function<
bool(T&&)> cb, DoneCallbackSimple dcb = {})
106 [cb = std::move(cb)](
const std::vector<std::shared_ptr<Value>>& vals) {
107 for (
const auto& v : vals) {
109 if (not cb(Value::unpack<T>(*v)))
111 }
catch (
const std::exception&) {
121 std::future<std::vector<std::shared_ptr<dht::Value>>> get(InfoHash key, Value::Filter f = {}, Where w = {})
123 auto p = std::make_shared<std::promise<std::vector<std::shared_ptr<dht::Value>>>>();
124 auto values = std::make_shared<std::vector<std::shared_ptr<dht::Value>>>();
127 [=](
const std::vector<std::shared_ptr<dht::Value>>& vlist) {
128 values->insert(values->end(), vlist.begin(), vlist.end());
131 [=](bool) { p->set_value(std::move(*values)); },
134 return p->get_future();
138 std::future<std::vector<T>> get(InfoHash key)
140 auto p = std::make_shared<std::promise<std::vector<T>>>();
141 auto values = std::make_shared<std::vector<T>>();
145 values->emplace_back(std::move(v));
148 [=](bool) { p->set_value(std::move(*values)); });
149 return p->get_future();
152 void query(
const InfoHash& hash, QueryCallback cb, DoneCallback done_cb = {}, Query q = {});
153 void query(
const InfoHash& hash, QueryCallback cb, DoneCallbackSimple done_cb = {}, Query q = {})
155 query(hash, cb, bindDoneCb(done_cb), q);
158 std::future<size_t> listen(InfoHash key, ValueCallback vcb, Value::Filter f = {}, Where w = {});
160 std::future<size_t> listen(InfoHash key, GetCallback cb, Value::Filter f = {}, Where w = {})
164 [cb = std::move(cb)](
const std::vector<Sp<Value>>& vals,
bool expired) {
169 std::forward<Value::Filter>(f),
170 std::forward<Where>(w));
172 std::future<size_t> listen(
const std::string& key, GetCallback vcb, Value::Filter f = {}, Where w = {});
173 std::future<size_t> listen(InfoHash key, GetCallbackSimple cb, Value::Filter f = {}, Where w = {})
175 return listen(key, bindGetCb(cb), f, w);
179 std::future<size_t> listen(InfoHash hash, std::function<
bool(std::vector<T>&&)> cb)
183 [cb = std::move(cb)](
const std::vector<std::shared_ptr<Value>>& vals) {
return cb(unpackVector<T>(vals)); },
187 std::future<size_t> listen(InfoHash hash, std::function<
bool(std::vector<T>&&,
bool)> cb)
191 [cb = std::move(cb)](
const std::vector<std::shared_ptr<Value>>& vals,
bool expired) {
192 return cb(unpackVector<T>(vals), expired);
198 std::future<size_t> listen(InfoHash hash, std::function<
bool(T&&)> cb, Value::Filter f = {}, Where w = {})
202 [cb = std::move(cb)](
const std::vector<std::shared_ptr<Value>>& vals) {
203 for (
const auto& v : vals) {
205 if (not cb(Value::unpack<T>(*v)))
207 }
catch (
const std::exception&) {
217 std::future<size_t> listen(InfoHash hash, std::function<
bool(T&&,
bool)> cb, Value::Filter f = {}, Where w = {})
221 [cb = std::move(cb)](
const std::vector<std::shared_ptr<Value>>& vals,
bool expired) {
222 for (
const auto& v : vals) {
224 if (not cb(Value::unpack<T>(*v), expired))
226 }
catch (
const std::exception&) {
236 void cancelListen(InfoHash h,
size_t token);
237 void cancelListen(InfoHash h, std::shared_future<size_t> token);
239 void put(InfoHash hash,
240 std::shared_ptr<Value> value,
241 DoneCallback cb = {},
242 time_point created = time_point::max(),
243 bool permanent =
false);
244 void put(InfoHash hash,
245 std::shared_ptr<Value> value,
246 DoneCallbackSimple cb,
247 time_point created = time_point::max(),
248 bool permanent =
false)
250 put(hash, value, bindDoneCb(cb), created, permanent);
253 void put(InfoHash hash,
255 DoneCallback cb = {},
256 time_point created = time_point::max(),
257 bool permanent =
false);
258 void put(InfoHash hash,
260 DoneCallbackSimple cb,
261 time_point created = time_point::max(),
262 bool permanent =
false)
264 put(hash, std::forward<Value>(value), bindDoneCb(cb), created, permanent);
266 void put(
const std::string& key,
268 DoneCallbackSimple cb = {},
269 time_point created = time_point::max(),
270 bool permanent =
false);
272 void cancelPut(
const InfoHash& h, Value::Id
id);
273 void cancelPut(
const InfoHash& h,
const std::shared_ptr<Value>& value);
275 void putSigned(InfoHash hash, std::shared_ptr<Value> value, DoneCallback cb = {},
bool permanent =
false);
276 void putSigned(InfoHash hash, std::shared_ptr<Value> value, DoneCallbackSimple cb,
bool permanent =
false)
278 putSigned(hash, value, bindDoneCb(cb), permanent);
281 void putSigned(InfoHash hash, Value&& value, DoneCallback cb = {},
bool permanent =
false);
282 void putSigned(InfoHash hash, Value&& value, DoneCallbackSimple cb,
bool permanent =
false)
284 putSigned(hash, std::forward<Value>(value), bindDoneCb(cb), permanent);
286 void putSigned(
const std::string& key, Value&& value, DoneCallbackSimple cb = {},
bool permanent =
false);
288 void putEncrypted(InfoHash hash,
289 const std::shared_ptr<crypto::PublicKey>& to,
290 std::shared_ptr<Value> value,
291 DoneCallback cb = {},
292 bool permanent =
false);
293 void putEncrypted(InfoHash hash,
294 const std::shared_ptr<crypto::PublicKey>& to,
295 std::shared_ptr<Value> value,
296 DoneCallbackSimple cb,
297 bool permanent =
false)
299 putEncrypted(hash, to, value, bindDoneCb(cb), permanent);
302 void putEncrypted(InfoHash hash,
303 const std::shared_ptr<crypto::PublicKey>& to,
305 DoneCallback cb = {},
306 bool permanent =
false);
307 void putEncrypted(InfoHash hash,
308 const std::shared_ptr<crypto::PublicKey>& to,
310 DoneCallbackSimple cb,
311 bool permanent =
false)
313 putEncrypted(hash, to, std::forward<Value>(value), bindDoneCb(cb), permanent);
317 InfoHash hash,
const PkId& to, std::shared_ptr<Value> value, DoneCallback cb = {},
bool permanent =
false);
319 InfoHash hash,
const PkId& to, std::shared_ptr<Value> value, DoneCallbackSimple cb,
bool permanent =
false)
321 putEncrypted(hash, to, value, bindDoneCb(cb), permanent);
323 void putEncrypted(InfoHash hash,
const PkId& to, Value&& value, DoneCallback cb = {},
bool permanent =
false)
325 putEncrypted(hash, to, std::make_shared<Value>(std::move(value)), std::move(cb), permanent);
327 void putEncrypted(InfoHash hash,
const PkId& to, Value&& value, DoneCallbackSimple cb,
bool permanent =
false)
329 putEncrypted(hash, to, std::make_shared<Value>(std::move(value)), bindDoneCb(cb), permanent);
336 void bootstrap(std::vector<SockAddr> nodes, DoneCallbackSimple cb = {});
337 void bootstrap(
SockAddr addr, DoneCallbackSimple cb = {});
351 void bootstrap(
const std::string& host,
const std::string& service);
352 void bootstrap(
const std::string& hostService);
372 void dumpTables()
const;
377 [[deprecated(
"Use getPublicKey()->getLongId() instead")]] InfoHash
getId()
const;
378 std::shared_ptr<crypto::PublicKey> getPublicKey()
const;
397 std::pair<size_t, size_t> getStoreSize()
const;
399 size_t getStorageLimit()
const;
400 void setStorageLimit(
size_t limit = 0);
402 size_t getLocalStorageLimit()
const;
403 void setLocalStorageLimit(
size_t limit = 0);
405 std::vector<NodeExport> exportNodes()
const;
407 std::vector<ValuesExport> exportValues()
const;
409 void setLogger(
const Sp<Logger>& logger = {});
416 void registerType(
const ValueType& type);
417 void registerInsecureType(
const ValueType& type);
419 void importValues(
const std::vector<ValuesExport>& values);
421 bool isRunning()
const {
return running != State::Idle; }
423 NodeStats getNodesStats(sa_family_t af)
const;
424 unsigned getNodesStats(sa_family_t af,
425 unsigned* good_return,
426 unsigned* dubious_return,
427 unsigned* cached_return,
428 unsigned* incoming_return)
const;
429 NodeInfo getNodeInfo()
const;
430 void getNodeInfo(std::function<
void(std::shared_ptr<NodeInfo>)>);
432 std::vector<unsigned> getNodeMessageStats(
bool in =
false)
const;
433 std::string getStorageLog()
const;
434 std::string getStorageLog(
const InfoHash&)
const;
435 std::string getRoutingTablesLog(sa_family_t af)
const;
436 std::string getSearchesLog(sa_family_t af = AF_UNSPEC)
const;
437 std::string getSearchLog(
const InfoHash&, sa_family_t af = AF_UNSPEC)
const;
438 std::vector<SockAddr> getPublicAddress(sa_family_t af = AF_UNSPEC)
const;
439 std::vector<std::string> getPublicAddressStr(sa_family_t af = AF_UNSPEC)
const;
440 void getPublicAddress(std::function<
void(std::vector<SockAddr>&&)>, sa_family_t af = AF_UNSPEC);
443 void findCertificate(PkId hash, std::function<
void(
const std::shared_ptr<crypto::Certificate>&)>);
445 void registerCertificate(
const std::shared_ptr<crypto::Certificate>& cert);
446 void setLocalCertificateStore(CertificateStoreQuery&& query_method);
454 void run(in_port_t port = dht::net::DHT_DEFAULT_PORT,
455 const crypto::Identity& identity = {},
456 bool threaded =
true,
460 config.dht_config.node_config.
network = network;
461 config.dht_config.id = identity;
462 config.threaded = threaded;
465 void run(in_port_t port, Config& config, Context&& context = {});
471 void run(
const char* ip4,
const char* ip6,
const char* service,
Config& config,
Context&& context = {});
473 void run(
const Config& config, Context&& context);
475 void setOnStatusChanged(StatusCallback&& cb)
478 statusCbs.emplace_back(std::move(cb));
488 std::lock_guard lck(dht_mtx);
495 void shutdown(ShutdownCallback cb = {},
bool stop =
false);
506 std::shared_ptr<PeerDiscovery> getPeerDiscovery()
const {
return peerDiscovery_; };
508 void setProxyServer(
const std::string& proxy,
const std::string& pushNodeId =
"");
539 void forwardAllMessages(
bool forward);
542 enum class State { Idle, Running, Stopping };
546 NodeStatus getStatus()
const {
return std::max(status4, status6); }
548 bool checkShutdown();
550 DoneCallback bindOpDoneCallback(DoneCallback&& cb);
551 DoneCallbackSimple bindOpDoneCallback(DoneCallbackSimple&& cb);
554 std::unique_ptr<SecureDht> dht_;
557 std::atomic_bool use_proxy {
false};
561 IdentityAnnouncedCb identityAnnouncedCb_;
568 mutable std::mutex dht_mtx;
569 std::thread dht_thread {};
570 std::condition_variable cv {};
572 net::PacketList rcv {};
573 decltype(rcv) rcv_free {};
575 std::queue<std::function<void(SecureDht&)>> pending_ops_prio {};
576 std::queue<std::function<void(SecureDht&)>> pending_ops {};
577 std::mutex storage_mtx;
579 std::atomic<State> running {State::Idle};
580 std::atomic_size_t ongoing_ops {0};
581 std::vector<ShutdownCallback> shutdownCallbacks_;
583 NodeStatus status4 {NodeStatus::Disconnected}, status6 {NodeStatus::Disconnected};
585 std::vector<StatusCallback> statusCbs {};
588 std::shared_ptr<PeerDiscovery> peerDiscovery_;
594 std::shared_ptr<dht::Logger> logger_;