C++ Distributed Hash Table
node_cache.h
1 /*
2  * Copyright (C) 2014-2017 Savoir-faire Linux Inc.
3  * Author(s) : Adrien BĂ©raud <adrien.beraud@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 "node.h"
22 
23 #include <list>
24 #include <memory>
25 
26 namespace dht {
27 
28 struct NodeCache {
29  Sp<Node> getNode(const InfoHash& id, sa_family_t family);
30  Sp<Node> getNode(const InfoHash& id, const SockAddr&, time_point now, bool confirmed, bool client=false);
31  std::vector<Sp<Node>> getCachedNodes(const InfoHash& id, sa_family_t sa_f, size_t count) const;
32 
38  void clearBadNodes(sa_family_t family = 0);
39 
40  ~NodeCache();
41 
42 private:
43  class NodeMap : private std::map<InfoHash, std::weak_ptr<Node>> {
44  public:
45  Sp<Node> getNode(const InfoHash& id);
46  Sp<Node> getNode(const InfoHash& id, const SockAddr&, time_point now, bool confirmed, bool client);
47  std::vector<Sp<Node>> getCachedNodes(const InfoHash& id, size_t count) const;
48  void clearBadNodes();
49  void setExpired();
50  void cleanup();
51  private:
52  size_t cleanup_counter {0};
53  };
54 
55  const NodeMap& cache(sa_family_t af) const { return af == AF_INET ? cache_4 : cache_6; }
56  NodeMap& cache(sa_family_t af) { return af == AF_INET ? cache_4 : cache_6; }
57  NodeMap cache_4;
58  NodeMap cache_6;
59 };
60 
61 }
void clearBadNodes(sa_family_t family=0)
Definition: callbacks.h:34