My Project 1.7.4
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
26namespace dht {
27
28struct 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
42private:
43 class NodeMap : public 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 void clearBadNodes();
48 void setExpired();
49 };
50
51 const NodeMap& cache(sa_family_t af) const { return af == AF_INET ? cache_4 : cache_6; }
52 NodeMap& cache(sa_family_t af) { return af == AF_INET ? cache_4 : cache_6; }
53 NodeMap cache_4;
54 NodeMap cache_6;
55};
56
57}
Definition: callbacks.h:34
void clearBadNodes(sa_family_t family=0)