C++ Distributed Hash Table
dht_interface.h
1 /*
2  * Copyright (C) 2014-2022 Savoir-faire Linux Inc.
3  * Author: Sébastien Blin <sebastien.blin@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 "infohash.h"
22 #include "log_enable.h"
23 
24 #include <queue>
25 
26 namespace dht {
27 
28 namespace net {
29  class DatagramSocket;
30 }
31 
32 class OPENDHT_PUBLIC DhtInterface {
33 public:
34  DhtInterface() = default;
35  DhtInterface(const Logger& l) : logger_(std::make_shared<Logger>(l)) {};
36  DhtInterface(const std::shared_ptr<Logger>& l) : logger_(l) {};
37  virtual ~DhtInterface() = default;
38 
39  // [[deprecated]]
40  using Status = NodeStatus;
41  // [[deprecated]]
43 
47  virtual NodeStatus updateStatus(sa_family_t af) { return getStatus(af); };
48  virtual NodeStatus getStatus(sa_family_t af) const = 0;
49  virtual NodeStatus getStatus() const = 0;
50 
51  void addOnConnectedCallback(std::function<void()> cb) {
52  onConnectCallbacks_.emplace(std::move(cb));
53  }
54 
55  virtual net::DatagramSocket* getSocket() const { return {}; };
56 
60  virtual const InfoHash& getNodeId() const = 0;
61 
67  virtual void shutdown(ShutdownCallback cb, bool stop = false) = 0;
68 
75  virtual bool isRunning(sa_family_t af = 0) const = 0;
76 
77  virtual void registerType(const ValueType& type) = 0;
78 
79  virtual const ValueType& getType(ValueType::Id type_id) const = 0;
80 
81  virtual void addBootstrap(const std::string& /*host*/, const std::string& /*service*/) {};
82  virtual void clearBootstrap() {};
83 
89  virtual void insertNode(const InfoHash& id, const SockAddr&) = 0;
90  virtual void insertNode(const NodeExport& n) = 0;
91 
92  virtual void pingNode(SockAddr, DoneCallbackSimple&& cb={}) = 0;
93 
94  virtual time_point periodic(const uint8_t *buf, size_t buflen, SockAddr, const time_point& now) = 0;
95  virtual time_point periodic(const uint8_t *buf, size_t buflen, const sockaddr* from, socklen_t fromlen, const time_point& now) = 0;
96 
107  virtual void get(const InfoHash& key, GetCallback cb, DoneCallback donecb={}, Value::Filter&& f={}, Where&& w = {}) = 0;
108  virtual void get(const InfoHash& key, GetCallback cb, DoneCallbackSimple donecb={}, Value::Filter&& f={}, Where&& w = {}) = 0;
109  virtual void get(const InfoHash& key, GetCallbackSimple cb, DoneCallback donecb={}, Value::Filter&& f={}, Where&& w = {}) = 0;
110  virtual void get(const InfoHash& key, GetCallbackSimple cb, DoneCallbackSimple donecb, Value::Filter&& f={}, Where&& w = {}) = 0;
111 
122  virtual void query(const InfoHash& key, QueryCallback cb, DoneCallback done_cb = {}, Query&& q = {}) = 0;
123  virtual void query(const InfoHash& key, QueryCallback cb, DoneCallbackSimple done_cb = {}, Query&& q = {}) = 0;
124 
128  virtual std::vector<Sp<Value>> getLocal(const InfoHash& key, const Value::Filter& f = {}) const = 0;
129 
133  virtual Sp<Value> getLocalById(const InfoHash& key, Value::Id vid) const = 0;
134 
141  virtual void put(const InfoHash& key,
142  Sp<Value>,
143  DoneCallback cb=nullptr,
144  time_point created=time_point::max(),
145  bool permanent = false) = 0;
146  virtual void put(const InfoHash& key,
147  const Sp<Value>& v,
148  DoneCallbackSimple cb,
149  time_point created=time_point::max(),
150  bool permanent = false) = 0;
151  virtual void put(const InfoHash& key,
152  Value&& v,
153  DoneCallback cb=nullptr,
154  time_point created=time_point::max(),
155  bool permanent = false) = 0;
156  virtual void put(const InfoHash& key,
157  Value&& v,
158  DoneCallbackSimple cb,
159  time_point created=time_point::max(),
160  bool permanent = false) = 0;
161 
165  virtual std::vector<Sp<Value>> getPut(const InfoHash&) const = 0;
166 
170  virtual Sp<Value> getPut(const InfoHash&, const Value::Id&) const = 0;
171 
176  virtual bool cancelPut(const InfoHash&, const Value::Id&) = 0;
177 
185  virtual size_t listen(const InfoHash&, GetCallback, Value::Filter={}, Where w = {}) = 0;
186  virtual size_t listen(const InfoHash& key, GetCallbackSimple cb, Value::Filter f={}, Where w = {}) = 0;
187  virtual size_t listen(const InfoHash&, ValueCallback, Value::Filter={}, Where w = {}) = 0;
188 
189  virtual bool cancelListen(const InfoHash&, size_t token) = 0;
190 
196  virtual void connectivityChanged(sa_family_t) = 0;
197  virtual void connectivityChanged() = 0;
198 
203  virtual std::vector<NodeExport> exportNodes() const = 0;
204 
205  virtual std::vector<ValuesExport> exportValues() const = 0;
206  virtual void importValues(const std::vector<ValuesExport>&) = 0;
207 
208  virtual NodeStats getNodesStats(sa_family_t af) const = 0;
209 
210  virtual std::string getStorageLog() const = 0;
211  virtual std::string getStorageLog(const InfoHash&) const = 0;
212 
213  virtual std::string getRoutingTablesLog(sa_family_t) const = 0;
214  virtual std::string getSearchesLog(sa_family_t) const = 0;
215  virtual std::string getSearchLog(const InfoHash&, sa_family_t af = AF_UNSPEC) const = 0;
216 
217  virtual void dumpTables() const = 0;
218  virtual std::vector<unsigned> getNodeMessageStats(bool in = false) = 0;
219 
223  virtual void setStorageLimit(size_t limit = DEFAULT_STORAGE_LIMIT) = 0;
224  virtual size_t getStorageLimit() const = 0;
225 
230  virtual std::pair<size_t, size_t> getStoreSize() const = 0;
231 
232  virtual std::vector<SockAddr> getPublicAddress(sa_family_t family = 0) = 0;
233 
237  virtual void setLoggers(LogMethod error = {}, LogMethod warn = {}, LogMethod debug = {}) {
238  if (logger_) {
239  logger_->DBG = std::move(debug);
240  logger_->WARN = std::move(warn);
241  logger_->ERR = std::move(error);
242  } else
243  logger_= std::make_shared<Logger>(std::move(error), std::move(warn), std::move(debug));
244  }
245 
246  virtual void setLogger(const Logger& l) {
247  if (logger_)
248  *logger_ = l;
249  else
250  logger_= std::make_shared<Logger>(l);
251  }
252 
253  virtual void setLogger(const std::shared_ptr<Logger>& l) {
254  logger_ = l;
255  }
256 
260  virtual void setLogFilter(const InfoHash& f)
261  {
262  if (logger_)
263  logger_->setFilter(f);
264  }
265 
266  virtual void setPushNotificationToken(const std::string&) {};
267 
272  virtual void pushNotificationReceived(const std::map<std::string, std::string>& data) = 0;
273 
274 protected:
275  std::shared_ptr<Logger> logger_ {};
276  std::queue<std::function<void()>> onConnectCallbacks_ {};
277 };
278 
279 } // namespace dht
virtual void setLoggers(LogMethod error={}, LogMethod warn={}, LogMethod debug={})
NodeStatus
Definition: callbacks.h:42
virtual void setLogFilter(const InfoHash &f)
virtual NodeStatus updateStatus(sa_family_t af)
Definition: dht_interface.h:47
Definition: callbacks.h:35